// ''''''''''''''''''''''''''''''''''''''''''''''''''''''
// ''' Copyleft (c) 2005~2007  Maniadb.com
// ''' Create Date   : 2007. 10. 19.
// ''' Last Modified : 2008. 08. 29.
// ''' First Author  : hyungkyu.ryu
// ''' Last Modifier : hyungkyu.ryu
// ''''''''''''''''''''''''''''''''''''''''''''''''''''''


// 가격 비교 대상이 되는 서비스 사이트 목록 (index 0은 사용하지 않음. 1에서 시작함.)
var g_maniadb_songservice_list; 
var g_song_service_url = "/exSongComparison.asp";

// 파라메터로 넘어온 XML text를 DOM Object로 반환
function get_song_xml_dom( p_xml_text ) 
{
	var my_doc; 

//	alert( p_xml_text );

	if (document.implementation.createDocument){ 
		// Mozilla, create a new DOMParser 
		var parser = new DOMParser(); 
		my_doc = parser.parseFromString( p_xml_text, "text/xml"); 
	} else if (window.ActiveXObject){ 
		// Internet Explorer, create a new XML document using ActiveX 
		// and use loadXML as a DOM parser. 
		my_doc = new ActiveXObject("Microsoft.XMLDOM") 
		my_doc.async="false"; 
		my_doc.loadXML( p_xml_text );   
	} 

	return my_doc;
}

// XML node의 값을 반환
function get_song_node_value( p_element )
{
	return p_element.childNodes[0].nodeValue;
}

// 특정 site에서의 노래정보를 담고 있는 xml 문서를 분석하여 HTML code로 반환
function get_song_serviceinfo( p_xml_text ) 
{

	var siteImage, siteLink, siteLog;
	var songTitle, artistName, albumName, albumArt, songLyric, songLink;
	var linkLyric, linkStream, linkDownload, linkRingtone;
	var i, j, nodeList, resultHTML;
	
	nodeList   = get_song_xml_dom( p_xml_text ).documentElement.childNodes;
	resultHTML = '';
	for( i = 0; i <	nodeList.length; i++ )
	{
		switch( nodeList[i].nodeName ) 
		{
			case "site":
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "url" )
						siteImage = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "link" )
						siteLink = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "log" )
						siteLog = get_song_node_value( nodeList[i].childNodes[j] );
				break;
			case "item":
				songTitle = ''; 
				artistName = '';
				albumName = ''; 
				albumArt = '';
				songLyric = ''; 
				songLink = '';
				linkLyric = ''; 
				linkStream = ''; 
				linkDownload = ''; 
				linkRingtone = '';
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "title" )
						songTitle = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "artist" )
						artistName = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "album" )
						albumName = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "albumart" )
						albumArt = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "lyrics" )
						songLyric = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "link" )
						songLink = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "slink-lyric" )
						linkLyric = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "slink-stream" )
						linkStream = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "slink-download" )
						linkDownload = get_song_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "slink-ringtone" )
						linkRingtone = get_song_node_value( nodeList[i].childNodes[j] );

//        alert( "now i'm here!" );
				//if ( resultHTML == '' )
					resultHTML = resultHTML + get_songcomparion( '<a href="' + siteLink + '" target="_blank"><img src="' + siteImage + '" border="0"/></a>' + siteLog, songTitle, artistName, albumName, albumArt, linkStream, linkDownload, linkLyric );
				//else
				//	resultHTML = resultHTML + get_pricecomparion( '', '', '', productPrice, productReserved, (productPrice-productReserved), '<a href="' + productLink + '" target="_blank"><img src="http://images.maniadb.com/images/_/i_buy.gif" border="0"></a>' );
	
				break;

			default:		
				break;
		}			
	}
	
	if ( resultHTML == '' )
		//resultHTML = get_songcomparion( '<a href="' + siteLink + '" target="_blank"><img src="' + siteImage + '" border="0"></a>', '', '', 0, 0, 0, '' );
		//resultHTML = '<div align="center">검색 결과가 없습니다.</div>';
		resultHTML = get_songcomparion( '<a href="' + siteLink + '" target="_blank"><img src="' + siteImage + '" border="0"></a>' + siteLog, '검색 결과가 없습니다.', '', '',  '', '', '', '' );
	return resultHTML;
}	

// 특정 service site에서의 노래정보를 보여주는 HTML template
function get_songcomparion( p_site, p_title, p_artist, p_album, p_albumart, p_streaming, p_download, p_lyric )
{
	var result_doc;
	if ( p_site == '' ) return '';
		
	result_doc = '<table style="width:100%; table-layout:fixed;">' +
	             '<tr style="background: url(http://images.maniadb.com/images/dottedline.gif) no-repeat bottom;">' +
                  '<td width=""15%"" align="left" class="title_medium_a">' + p_site + '</td>';

  if ( p_album == '' && p_artist == '' ) // yourube case
  	if ( p_albumart == '' ) 
    	result_doc = result_doc +
                    '<td width="79%" align="left" class="title_small_a">' + p_title + '</td>';
    else
    	result_doc = result_doc +
                    '<td width="79%" align="left" class="title_small_a"><a class="highslide" onclick="return hs.expand(this)" href="' + p_albumart + '"><img src="' + p_albumart + '" width="10" height="10"/></a> ' + p_title + '</td>';
  else {                 
  	if ( p_albumart == '' ) 
  		result_doc = result_doc +
                  '<td width="32%" align="left" class="title_small_a">' + p_album + '</td>';
  	else
  		result_doc = result_doc +
                  '<td width="32%" align="left" class="title_small_a"><a class="highslide" onclick="return hs.expand(this)" href="' + p_albumart + '"><img src="' + p_albumart + '" width="10" height="10"/></a> ' + p_album + '</td>';

  	result_doc = result_doc +
                  '<td width="32%" align="left" class="title_small_a">' + p_title + '</td>' +
                  '<td width="15%" align="left" class="title_small_a">' + p_artist + '</td>';
  }
  result_doc = result_doc + '<td width="3%" align="left" class="title_small_a">';
  
	if ( p_streaming != '' )                 
//		result_doc = result_doc + '<img src="http://images.maniadb.com/images/speaker.gif" border="0" OnClick="openwindow( \''+p_streaming+'\', \'\', 200, 200 );"/>';
//		result_doc = result_doc + '<a href="javascript:popupwindow( \'' + p_streaming + '\', \'' + '' + 'PLAYER\', 300, 300 );"><img src="http://images.maniadb.com/images/speaker.gif" border="0"/></a>';
		result_doc = result_doc + '<a href="' + p_streaming + '"><img src="http://images.maniadb.com/images/speaker.gif" border="0"/></a>';
	else
		result_doc = result_doc + p_streaming;

	result_doc = result_doc +
                '</td>' +
                '<td width="3%" align="left" class="title_small_a">';

  // download 제거 : 2008/08/29
  
//	if ( p_download != '' )                 
//		result_doc = result_doc + '<img src="http://images.maniadb.com/images/speaker.gif" border="0" OnClick="openwindow( \''+p_streaming+'\', \'\', 100, 100 );"/>';
//		result_doc = result_doc + '<a href="' + p_download + '"><img src="http://images.maniadb.com/images/download.gif" border="0"/></a>';
//	else
//		result_doc = result_doc + p_download;

	if ( p_lyric != '' )                 
//		result_doc = result_doc + '<img src="http://images.maniadb.com/images/speaker.gif" border="0" OnClick="openwindow( \''+p_streaming+'\', \'\', 100, 100 );"/>';
		result_doc = result_doc + '<a href="' + p_lyric + '"><img src="http://images.maniadb.com/images/song_lyric.gif" border="0"/></a>';
	else
//		result_doc = result_doc + '<img src="http://images.maniadb.com/images/song_lyric_blank.gif" border="0"/>';
		result_doc = result_doc + '';

	result_doc = result_doc + 
                '</td>' +
	             '</tr>' +
	             '</table>';

	//alert( result_doc );
	return result_doc;
}

function getPageX(el){
	return (!el||el==document.body) ? 0 : el.offsetLeft + getPageX(el.offsetParent);
}

function getPageY(el){
	return (!el||el==document.body) ? 0 : el.offsetTop + getPageY(el.offsetParent);
}

function close_window(id){
	var div = document.getElementById(id);
	if(div) div.style.visibility = "hidden";
}

// 노래 비교 시작 : 노래 비교 검색 화면 display
function compare_song( p_album_id, p_song_id ) 
{
//	alert( "S" + p_album_id + "_" + p_song_id );

	for ( var i = 1; i < g_maniadb_songservice_list.length; i++ ) {
		//document.getElementById("songservice_" + p_album_id + "_" + p_song_id + "_" + g_maniadb_songservice_list[i] ).innerHTML = '';
		document.getElementById("songservice_0_0_" + g_maniadb_songservice_list[i] ).innerHTML = '';
	}
	
	var img = document.getElementById( "S" + p_album_id + "_" + p_song_id );
//	var win = document.getElementById( "comparesong" + p_album_id + '_' + p_song_id );
	var win = document.getElementById( "comparesong0_0" );

	win.style.top  = (getPageY(img) + 10) + "px";
	win.style.left = getPageX(img) + "px";
	win.style.visibility = "visible";

	setManiadbLogging();
	search_song( 1, p_album_id, p_song_id );
}

// shop별 가격 비교 시작
function search_song( idx, p_album_id, p_song_id )
{
	
	var objProg;
	
	//objProg = document.getElementById("songprogress_msg" + p_album_id + "_" + p_song_id);
	objProg = document.getElementById("songprogress_msg0_0");
	//alert( objProg );
	
	if ( idx < g_maniadb_songservice_list.length ) 
	{
		objProg.innerHTML = '<blink>' + g_maniadb_songservice_list[idx] + ' 검색중...</blink>';

		songserviceframe.location.href = g_song_service_url + 
		                             '?idx='+idx+'&st=album&sf=' + g_maniadb_songservice_list[idx] + 
		                             '&rt=js&rv=g_maniadb_xml_songservice&a='+p_album_id+'&s='+p_song_id;
		//alert( songserviceframe.location.href );
	}
	else
		objProg.innerHTML = '';

}


// 노래 비교 화면 템플릿
function get_songcomparionbox( p_album_id, p_song_id )
{
	var pcd = '';
	
//	if ( p_album_id > 0 )
//	pcd = '<img src="http://images.maniadb.com/images/_/i_compare.gif" border="0" id="S' + p_album_id + '_' + p_song_id + '" onclick="compare_price( ' + p_album_id + ', ' + p_albumseqno + ');"/>';
	
	pcd = pcd +
	         '<div id="comparesong' + p_album_id + '_' + p_song_id + '" style="position:absolute;visibility:hidden">' +
           '<table width="600" cellpadding="0" cellspacing="2" border="0" bgcolor="#CCDAE7">' +
           '<tr><td>' +
             '<table width="600" cellpadding="0" cellspacing="0" border="0" bgcolor="EFF6F8">' +
             '<tr><td>' +
               '<table width="600" border="0"><tr>' + 
                 '<td align="left"><b>노래찾기</b></td>' + 
                 '<td style="text-align:right"><img src="http://images.maniadb.com/images/_/i_close.gif" onClick="close_window(\'comparesong' + p_album_id + '_' + p_song_id + '\');"/></td></tr>' +
               '</table>' +
             '</td></tr>' +
             '</table>' +
           '</td></tr>' +
           '<tr><td bgcolor="white"><br/>' +
      	     get_songcomparion( '제공사', '노래제목', '아티스트', '앨범', '', '', '', '' );
	
	for ( var i = 1; i < g_maniadb_songservice_list.length; i++ ) {
		pcd = pcd + '<span id="songservice_' + p_album_id + '_' + p_song_id + '_' + g_maniadb_songservice_list[i] + '"><img src="http://images.maniadb.com/images/blank.gif" width="1" height="0" alt=""/><br/></span>';
	}
   pcd = pcd + 
           '<div id="songprogress_msg' + p_album_id + '_' + p_song_id + '" style="width:600px; text-align:center; color:#2C74D4; font-size:12px; font-weight:bold;"></div>' +
           '</td></tr>' +
           '</table>' +
         '</div>' +
         '<div style="visibility:hidden"><iframe name="songserviceframe" width="0" height="0" border="0"></iframe></div>';		

	return pcd;
}


// customizable : for maniadb
var songMerchants;
//songMerchants = '^jukeon^amazon';
//songMerchants = '^melon^jukeon^bugs'; //bugs 에 좀 문제가 있는 듯 함. 2008.02.06.
//songMerchants = '^melon^jukeon^youtube';
songMerchants = '^melon';

g_maniadb_songservice_list = songMerchants.split( '^' );
document.write ( get_songcomparionbox( 0, 0 ) );
	
function show_song( p_album_id, p_song_id ) 
{
//	document.write ( get_songcomparionbox( p_album_id, p_song_id ) );
}
