// ''''''''''''''''''''''''''''''''''''''''''''''''''''''
// ''' Copyleft (c) 2005~2007  Maniadb.com
// ''' Create Date   : 2006. 04. 27.
// ''' Last Modified : 2007. 03. 04.
// ''' First Author  : hyungkyu.ryu
// ''' Last Modifier : hyungkyu.ryu
// ''''''''''''''''''''''''''''''''''''''''''''''''''''''

/* This 'move box' script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: JTricks.com :: http://www.jtricks.com/ */

function move_box(an, box) {
  var cleft = 0;
  var ctop = 0;
  var obj = an;
  while (obj.offsetParent) {
    cleft += obj.offsetLeft;
    ctop += obj.offsetTop;
    obj = obj.offsetParent;
  }
  box.style.left = cleft + 'px';
  ctop += an.offsetHeight + 8;
  if (document.body.currentStyle &&
    document.body.currentStyle['marginTop']) {
    ctop += parseInt(
      document.body.currentStyle['marginTop']);
  }
  box.style.top = ctop + 'px';
}


// 가격 비교 대상이 되는 상점 목록 (index 0은 사용하지 않음. 1에서 시작함.)
var g_maniadb_merchant_list; 

// 파라메터로 넘어온 XML text를 DOM Object로 반환
function get_xml_dom( p_xml_text ) 
{
	var my_doc; 

	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_node_value( p_element )
{
	return p_element.childNodes[0].nodeValue;
}

// 특정 shop에서의 가격정보를 담고 있는 xml 문서를 분석하여 HTML code로 반환
function get_product_priceinfo( p_xml_text ) 
{
	var shopImage, shopLink, productTitle, productImage, productPrice, productReserved, productLink;
	var i, j, nodeList, resultHTML;
	
	nodeList   = get_xml_dom( p_xml_text ).documentElement.childNodes;
	resultHTML = '';
	for( i = 0; i <	nodeList.length; i++ )
	{
		switch( nodeList[i].nodeName ) 
		{
			case "shop":
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "url" )
						shopImage = get_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "link" )
						shopLink = get_node_value( nodeList[i].childNodes[j] );
				break;
			case "product":
				productTitle = '';
				productImage = '';
				productPrice = '';
				productReserved = '';
				productLink = '';
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "price" )
						productPrice = get_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "reserved" )
						productReserved = get_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "link" )
						productLink = get_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "cover" )
						productImage = get_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "title" )
						productTitle = get_node_value( nodeList[i].childNodes[j] );

				//if ( resultHTML == '' )
					resultHTML = resultHTML + get_pricecomparion( '<a href="' + shopLink + '" target="_blank"><img src="' + shopImage + '" border="0"></a>', productImage, productTitle, productPrice, productReserved, (productPrice-productReserved), '<a href="' + productLink + '" target="_blank"><img src="http://www.maniadb.com/images/_/i_buy.gif" border="0"></a>' );
				//else
				//	resultHTML = resultHTML + get_pricecomparion( '', '', '', productPrice, productReserved, (productPrice-productReserved), '<a href="' + productLink + '" target="_blank"><img src="http://www.maniadb.com/images/_/i_buy.gif" border="0"></a>' );
	
				break;

			default:		
				break;
		}			
	}
	
	if ( resultHTML == '' )
		resultHTML = get_pricecomparion( '<a href="' + shopLink + '" target="_blank"><img src="' + shopImage + '" border="0"></a>', '', '', 0, 0, 0, '' );
	return resultHTML;
}	

// 특정 shop에서의 가격정보를 보여주는 HTML template
function get_pricecomparion( p_shop, p_image, p_title, p_price, p_reserved, p_net_price, p_link )
{
	var result_doc;
	
	if ( p_price == 0 ) return '';
	
	result_doc = '<table border="0" width="600" height="25" cellpadding="0" cellspacing="0" style="table-layout:fixed">' + 
               '<tr>' +
                  '<td width=""80"" align="center">' + p_shop + '</td>';
	
	if ( p_image.length > 10 ) {
		p_title = '<img src="' + p_image + '" width="15" height="15"/>&nbsp;' + p_title;
	}

	if ( p_price == 0 ) 
		result_doc = result_doc +
                  '<td align="center" width="520" colspan="5">검색결과가 없습니다.</td>';
	else		              
		result_doc = result_doc +
                  '<td width="280" align="left" nowrap="nowrap" style="overflow:hidden; text-overflow:ellipsis;"><nobr>' + p_title + '</td>' +
                  '<td width="60" align="right">' + priceformat(p_price) + '</td>' +
                  '<td width="50" align="right">' + priceformat(p_reserved) + '</td>' +
                  '<td width="60" align="right">' + priceformat(p_net_price) + '</td>' +
                  '<td width="70" align="center">' + p_link + '</td>';

	result_doc = result_doc +             '</tr>' +
	             '<tr><td style="background:url(http://www.maniadb.com/images/dottedline.gif);" colspan="6" ><img src="http://www.maniadb.com/images/blank.gif" width="1" height="1" alt=""/></td></tr>' +
               '</table>';

	return result_doc;
}

function priceformat( p_str )
{
	if ( isNaN(p_str) ) return p_str;

	var str = String(p_str);

  len = str.length;
  var str1 = "";

  for(i=1; i<=len; i++) {
   str1 = str.charAt(len-i)+str1;
   if((i%3 == 0)&&(len-i != 0)) str1 = ","+str1;
  }

	return str1 + '원';
}

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.display = "none";
}

// 가격 비교 시작 : 가격 비교 검색 화면 display
function compare_price( obj, p_album_id, p_seq_no ) 
{
  var boxdiv = document.getElementById('productframev');

  if (boxdiv != null) {
	  boxdiv = document.createElement('div');
	  boxdiv.setAttribute('id', 'productframev' );
	  boxdiv.style.display = 'none';
		boxdiv.innerHTML = '<iframe name="productframe" width="0" height="0" border="0"></iframe>';			
  }

  var boxdiv = document.getElementById('compareprice_'+p_album_id);

  if (boxdiv != null) {
    if (boxdiv.style.display=='none') {
      move_box(obj, boxdiv);
      boxdiv.style.display='block';
    } else
      boxdiv.style.display='none';
  }
	else
	{  
	  boxdiv = document.createElement('div');
	  boxdiv.setAttribute('id', 'compareprice_'+p_album_id );
	  boxdiv.style.display = 'block';
	  boxdiv.style.position = 'absolute';
	  boxdiv.style.width = '600px';
	  boxdiv.style.height = '50px';
	  boxdiv.style.backgroundColor = '#fff';
	
	  boxdiv.innerHTML = get_pricecomparionbox( p_album_id, p_seq_no );

	  document.body.appendChild(boxdiv);
	  move_box( obj, boxdiv );

		search_price( 1, p_album_id, p_seq_no );
	}
	return false;
}

// shop별 가격 비교 시작
function search_price( idx, p_album_id, p_seq_no )
{
	if ( idx < g_maniadb_merchant_list.length ) 
	{
		document.getElementById('progress_msg_' + p_album_id).innerHTML = '<blink>' + g_maniadb_merchant_list[idx] + ' 검색중...</blink>';
		productframe.location.href = g_album_product_url + 
		                             '?idx='+idx+'&st=album&sf=' + g_maniadb_merchant_list[idx] + 
		                             '&rt=js&rv=g_maniadb_xml_albumproduct&a='+p_album_id+'&asn='+p_seq_no;
	}
	else
		document.getElementById('progress_msg_' + p_album_id).innerHTML = '';
}


// 가격 비교 화면 템플릿
function get_pricecomparionbox( p_albumid, p_albumseqno )
{
	var pcd = '';
	
	pcd = pcd +
           '<table 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 align="right"><img src="http://www.maniadb.com/images/_/i_close.gif" onClick="close_window(\'compareprice_' + p_albumid + '\');"/></td></tr>' +
               '</table>' +
             '</td></tr>' +
             '</table>' +
           '</td></tr>' +
           '<tr><td bgcolor="white"><br/>' +
      	     get_pricecomparion( '판매처', '', '타이틀', '판매가', '적립금', '실구입가', '&nbsp;' );

	for ( var i = 1; i < g_maniadb_merchant_list.length; i++ ) {
		pcd = pcd + '<span id="shop_' + g_maniadb_merchant_list[i] + '_' + p_albumid + '"><br/></span>';
	}

   pcd = pcd + 
           '<div id="progress_msg_' + p_albumid + '" style="width:600px; text-align:center; color:#2C74D4; font-size:12px; font-weight:bold;"></div>' +
           '</td></tr>' +
           '</table>';

	return pcd;
}

// 가격 비교 버튼
function get_pricecomparisonbutton( p_albumid, p_albumseqno )
{
	return '<a href="#" onClick="return compare_price(this,'+p_albumid+','+p_albumseqno+');"><img src="http://www.maniadb.com/images/_/i_compare.gif" border="0"/></a>';
}

// album 정보를 유형별로 나누어 보여줌
function show_album( p_type, p_width, p_pricecomparison, p_align ) 
{
	var albumId, albumSeqNo, albumTitle, albumLink, albumCoverart, albumTracks, albumMerchants;
	var productRelease, productReleaseDate
	var i, j, k, t, diskno;
	var rootElement, nodeList, nodeValue;
	
	rootElement = get_xml_dom( maniadbXmlText ).documentElement;

  for( i = 0 ; i < rootElement.attributes.length; i++ )
  {
		switch( rootElement.attributes[i].nodeName ) {
		case "a":
			albumId = rootElement.attributes[i].nodeValue;
			break;
		case "asn":
			albumSeqNo = rootElement.attributes[i].nodeValue;
			break;
		default:
			break;
		}
  }	
	  
	t = 0; diskno = 0;
	albumTracks = '';
	albumMerchants = '';
	nodeList = rootElement.childNodes;
	for( i = 0; i <	nodeList.length; i++ )
	{
		switch( nodeList[i].nodeName ) 
		{
			case "title":
				albumTitle = get_node_value( nodeList[i] );
				break;
			case "link":
				albumLink  = get_node_value( nodeList[i] );
				break;
			case "coverart":
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "url" )
						albumCoverart = get_node_value( nodeList[i].childNodes[j] );
				break;
			case "release":
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "title" )
						productRelease = get_node_value( nodeList[i].childNodes[j] );
					else if ( nodeList[i].childNodes[j].nodeName == "pubDate" )
						productReleaseDate = get_node_value( nodeList[i].childNodes[j] );
				break;
			case "merchants":
				for ( j = 0; j < nodeList[i].childNodes.length; j++ )
					if ( nodeList[i].childNodes[j].nodeName == "shop" )
						albumMerchants = albumMerchants + '^' + get_node_value( nodeList[i].childNodes[j] );
				break;
			case "disc":
				diskno = diskno + 1;
				albumTracks = albumTracks + '<div class="ma_diskno">Disc ' + diskno + '</div>';
				if ( p_type == 4 ) {
					albumTracks = albumTracks + '<ul>';
					for ( j = 0; j < nodeList[i].childNodes.length; j++ )
						if ( nodeList[i].childNodes[j].nodeName == "song" )
							for ( k = 0; k < nodeList[i].childNodes[j].childNodes.length; k++ )
								if ( nodeList[i].childNodes[j].childNodes[k].nodeName == "title" )
								{
									t = t + 1;
									if ( t > 1 ) albumTracks = albumTracks + ',&nbsp;&nbsp;';
										albumTracks = albumTracks + '<li>' + t + ". " + get_node_value( nodeList[i].childNodes[j].childNodes[k] );
								}
								else if ( nodeList[i].childNodes[j].childNodes[k].nodeName == "performer" ) {
									nodeValue = get_node_value( nodeList[i].childNodes[j].childNodes[k] );
									if ( nodeValue != '' ) albumTracks = albumTracks + ' - ' + nodeValue;
									albumTracks = albumTracks + '</li>';
								}
									
					albumTracks = albumTracks + '</ul>';

				} else if ( p_type == 3 || p_type == 5 ) {
					t = 0;
					albumTracks = albumTracks + '<ol>';
					for ( j = 0; j < nodeList[i].childNodes.length; j++ )
						if ( nodeList[i].childNodes[j].nodeName == "song" )
							for ( k = 0; k < nodeList[i].childNodes[j].childNodes.length; k++ )
								if ( nodeList[i].childNodes[j].childNodes[k].nodeName == "title" )
								{
									t = t + 1;
									albumTracks = albumTracks + '<li>' + t + ". " + get_node_value( nodeList[i].childNodes[j].childNodes[k] );
								}
								else if ( nodeList[i].childNodes[j].childNodes[k].nodeName == "performer" ) {
									nodeValue = get_node_value( nodeList[i].childNodes[j].childNodes[k] );
									if ( nodeValue != '' ) albumTracks = albumTracks + ' - ' + nodeValue;
									albumTracks = albumTracks + '</li>';
								}
									
					albumTracks = albumTracks + '</ol>';
				}
				break;
			default:		
		}			
	}

	g_maniadb_merchant_list = albumMerchants.split( '^' );
	
	var resultHTML, resultPriceComparison;
	if ( p_pricecomparison == 1 )
		resultPriceComparison = get_pricecomparisonbutton( albumId, albumSeqNo );
	else
		resultPriceComparison = '';

//	if ( p_align != '' )
//		p_align = ' align="' + p_align + '"';
		
	
	switch( p_type ) 
	{
		case 1:
			resultHTML = '<style type="text/css"><!--\n' +
			               '#ma' + albumId + ' .ma_cover     { padding:2px;margin:2px; }\n' +
			               '#ma' + albumId + ' .ma_pricecomp { padding:2px;margin:2px; }\n' +
			             '--></style>\n' + 
			             '<div id="ma' + albumId + '">' + 
				             '<table align="' + p_align + '"><tr><td>' +
				               '<div class="ma_cover"><a href="' + albumLink + '" target="_blank"><img src="' + albumCoverart + '" alt="' + albumTitle + '" width="' + p_width + '" border="0"/></a></div>\n' +
										   '<div class="ma_pricecomp">' + resultPriceComparison + '</div>' +
				             '</td></tr></table>' + 
				           '</div>';
			break;
			
		case 2:
			if ( p_align == 'center' ) p_align = ' align="center" ';
			else                       p_align = ' width="' + p_width + '" align="' + p_align + '" ';
			resultHTML = '<style type="text/css"><!--\n' +
			               '#ma' + albumId + ' .ma_cover     { padding:2px;margin:2px; }\n' +
			               '#ma' + albumId + ' .ma_title     { padding:2px;margin:2px; }\n' +
			               '#ma' + albumId + ' .ma_pricecomp { padding:2px;margin:2px; }\n' +
			             '--></style>\n' + 
			             '<div id="ma' + albumId + '">' +
				             '<table ' + p_align + '><tr><td>' +
				               '<div class="ma_cover"><a href="' + albumLink + '" target="_blank"><img src="' + albumCoverart + '" alt="' + albumTitle + '" width="' + p_width + '" border="0"/></a></div>\n' +
				               '<div class="ma_title"><a href="' + albumLink + '" target="_blank">' + albumTitle + '</a></div>\n' +
										   '<div class="ma_pricecomp">' + resultPriceComparison + '</div>' +
				             '</td></tr></table>' + 
				           '</div>';
			break;
			
		case 3:
			if ( p_align == 'center' ) p_align = ' align="center" ';
			else                       p_align = ' width="' + p_width + '" align="' + p_align + '" ';
			resultHTML = '<style type="text/css"><!--\n' +
			               '#ma' + albumId + ' .ma_cover     { padding:2px;margin:2px; }\n' +
			               '#ma' + albumId + ' .ma_title     { margin-bottom:7px; display:block; }\n' +
			               '#ma' + albumId + ' .ma_product   { margin-bottom:7px; display:block; text-align:left }\n';

			if ( diskno == 1 )
				resultHTML = resultHTML +
			               '#ma' + albumId + ' .ma_diskno    { display:none; }\n';
			else
				resultHTML = resultHTML +
			               '#ma' + albumId + ' .ma_diskno    { margin-top:5px; font-weight:bold; }\n';
				
			resultHTML = resultHTML +
			               '#ma' + albumId + ' .ma_track     { margin-bottom:7px; }\n' +
			               '#ma' + albumId + ' .ma_track ol  { list-style:none;margin:0;padding:0 }\n' +
			               '#ma' + albumId + ' .ma_pricecomp { padding:2px;margin:2px; }\n' +
			             '--></style>\n' + 
			             '<div id="ma' + albumId + '">' +
				             '<table ' + p_align + '><tr><td>' +
				               '<div class="ma_cover"><a href="' + albumLink + '" target="_blank"><img src="' + albumCoverart + '" alt="' + albumTitle + '" width="' + p_width + '" border="0"/></a></div>\n' +
				               '<div class="ma_title"><a href="' + albumLink + '" target="_blank">' + albumTitle + '</a></div>\n' +
				               '<div class="ma_track">' + albumTracks + '</div>' +
										   '<div class="ma_pricecomp">' + resultPriceComparison + '</div>' +
				             '</td></tr></table>' + 
				           '</div>';
			break;

		case 4:
			resultHTML = '<style type="text/css"><!--\n' +
			               '#ma' + albumId + ' .ma_cover     { }\n' +
			               '#ma' + albumId + ' .ma_title h4  { margin-bottom:5px; }\n' +
			               '#ma' + albumId + ' .ma_product   { margin-bottom:5px;text-align:right;width:100% }\n' +
			               '#ma' + albumId + ' .ma_diskno    { display:none; }\n' +
			               '#ma' + albumId + ' .ma_track     { margin-bottom:5px; scrollbar-3dlight-color: #999999; scrollbar-arrow-color: #999999; scrollbar-track-color: #999999; scrollbar-darkshadow-color: #999999; scrollbar-face-color: #999999; scrollbar-highlight-color: #000000; scrollbar-shadow-color: #000000; }\n' +
			               '#ma' + albumId + ' .ma_track ul  { list-style:none;margin:0;padding:0;font-size:0.9em;height:78px;overflow:hidden;text-overflow:ellipsis; }\n' +
			               '#ma' + albumId + ' .ma_track ul li { display:inline; }\n' +
			               '#ma' + albumId + ' .ma_pricecomp { margin:0px; }\n' +
			             '--></style>\n' + 
			             '<div id="ma' + albumId + '">' + 
				             '<table ' + p_align + ' cellpadding="3"><tr><td valign="top">' +
				               '<div class="ma_cover"><a href="' + albumLink + '" target="_blank"><img src="' + albumCoverart + '" alt="' + albumTitle + '" width="' + p_width + '" border="0"/></a></div>\n' +
				             '</td><td valign="top" align="left">' +
				               '<div class="ma_title"><h4><a href="' + albumLink + '" target="_blank">' + albumTitle + '</a></h4></div>' +
				               '<div class="ma_product">' + productRelease + ' | ' + productReleaseDate + '</div>' +
				               '<div class="ma_track">' + albumTracks + '</div>' +
											 '<div class="ma_pricecomp">' + resultPriceComparison + '</div>' +
				             '</td></tr></table>' +
			             '</div>';
			break;

		case 5:
			resultHTML = '<style type="text/css"><!--\n' +
			               '#ma' + albumId + ' .ma_title h4  { margin-bottom:7}\n' +
			               '#ma' + albumId + ' .ma_product   { margin-bottom:0px; }\n';

			if ( diskno == 1 )
				resultHTML = resultHTML +
			               '#ma' + albumId + ' .ma_diskno    { display:none; }\n';
			else
				resultHTML = resultHTML +
			               '#ma' + albumId + ' .ma_diskno    { margin-top:5px; font-weight:bold; }\n';
				
			resultHTML = resultHTML +
			               '#ma' + albumId + ' .ma_track     { margin-top:7px; scrollbar-3dlight-color: #999999; scrollbar-arrow-color: #999999; scrollbar-track-color: #999999; scrollbar-darkshadow-color: #999999; scrollbar-face-color: #999999; scrollbar-highlight-color: #000000; scrollbar-shadow-color: #000000; }\n' +
			               '#ma' + albumId + ' .ma_track ol  { list-style:none;margin:0;padding:0; }\n' +
			               '#ma' + albumId + ' .ma_track ol li { }\n' +
			               '#ma' + albumId + ' .ma_pricecomp { margin-top:7px; }\n' +
			             '--></style>\n' + 
			             '<div id="ma' + albumId + '">' + 
			               '<div class="ma_title"><h3><a href="' + albumLink + '" target="_blank">' + albumTitle + '</a></h3></div>\n' +
			               '<div class="ma_cover"><a href="' + albumLink + '" target="_blank"><img src="' + albumCoverart + '" alt="' + albumTitle + '" width="' + p_width + '" border="0"/></a></div>\n' +
			               '<br/>\n' +
				             '<div class="ma_product">앨범번호 : ' + productRelease + '</div>\n' +
				             '<div class="ma_title">앨범제목 : <a href="' + albumLink + '" target="_blank">' + albumTitle + '</a></div>\n' +
				             '<div class="ma_product">발매일자 : ' + productReleaseDate + '</div>\n' +
				             '<div class="ma_track">' + albumTracks + '</div>' +
									   '<div class="ma_pricecomp">' + resultPriceComparison + '</div>' +
				             '</td></tr></table>' +
			             '</div>';
			break;
		default:
			resultHTML = 'wrong paramenter';
			break;
					
	}	
	resultHTML = resultHTML + '<div style="visibility:hidden"><iframe name="productframe" width="0" height="0" border="0"></iframe></div>';
	document.write( resultHTML );
}

