//圖的來源,預設
var INimg="http://www.inmusic.com.tw/in/images/";
var INWeb="http://www.inmusic.com.tw/in/";
var img_listen_play="news_cn/icon_play.gif";
var img_listen_stop="news_cn/icon_stop.gif";
var img_song_down="news_cn/icon_download.gif";
var img_song_down_ok="";
//行銷代碼
var _promote_ID="0";
//是否有下載按鈕
var is_download=true;
//是否有整張專輯下載
var send_album_id=0;
//資料欄位放置
var headtitle=new Array("songname","singer","listen","download");
//資料欄的css
var cssStyle=new Array("","","","");
//資料欄的寬度
var cssWidth=new Array("","","","");
//資料分層
var musicDataTable=new Array("songTable");
var musicDataAllSong=new Array("","","","","");
//歌單序號
var is_SN=true;
//DIV有抬頭的
var strHeadTitle="";
//全部下載
	var allMusicID="";
	var saveType="1";
	var allSinger=""
	
var temp;

	var	ie=document.all;
	var	ns4=document.layers;
	var	moz=document.getElementById;
//var strPromoteSongs = '{"Song": [ {"id":"52240","content_id":"107730","name":"眼色","singer":"林宥嘉"},{"id":"4","content_id":"107730","name":"神秘嘉賓","singer":"林宥嘉"},{"id":"5","content_id":"107730","name":"愛情是圓的","singer":"林宥嘉"},{"id":"6","content_id":"107730","name":"伯樂"},{"id":"52241","content_id":"107924","name":"請說","singer":"林宥嘉"},{"id":"7","content_id":"107730","name":"病態","singer":"林宥嘉"},{"id":"52242","content_id":"107709","name":"殘酷月光","singer":"林宥嘉"},{"id":"52243","content_id":"107717","name":"慢一點","singer":"林宥嘉"},{"id":"29","content_id":"107730","name":"心有林夕","singer":"林宥嘉"},{"id":"52244","content_id":"107635","name":"再別康橋","singer":"林宥嘉"},{"id":"31","content_id":"107730","name":"傳說","singer":"林宥嘉"}],"Css":[ {"id":"3","name":"眼色","singer":"林宥嘉"}]}';
if(strPromoteSongs){
	if(strPromoteSongs.length>10){
		//alert(strPromoteSongs)
	var PromoteSongs = JSON.parse(strPromoteSongs);	
	}
}
//
function addSongByDiv(){
	if(songTable){
		var oneRow=songTable.innerHTML;
		var allRow="";
		var tmpRow=oneRow;
		var tmpHead="";
		var r=0;
		var sep=""
		for(r=0;r<PromoteSongs.Song.length;r++){	  	
	  	var oneSong=PromoteSongs.Song[r];
	  	tmpRow=tmpRow.replace(/_no_/g,addZero(r+1,2));
	  	tmpRow=tmpRow.replace(/_songname_/g,oneSong.music_name);
	  	tmpRow=tmpRow.replace(/_singer_/g,oneSong.singer);	  		  	
	  	if(oneSong.content_id!="0"){
				strImg ="<img value=\"Play\" src=\""+INimg+img_listen_play+"\" style=\"cursor:hand\" onclick=\" re_img(this);playme(this,"+oneSong.content_id+",'inmusic');img_swap(this);\">";  
		  }else strImg=""; 	
		  tmpRow=tmpRow.replace(/_listen_/g,strImg);	
			
			if(is_download){				
				if(oneSong.music_id!="0"){
				strDownLoad ="<img src=\""+INimg+img_song_down+"\" style=\"cursor:hand\" onclick=\"sendMusicData(this,"+oneSong.music_id+",'1');\">";  
				}else strDownLoad ="";
				tmpRow=tmpRow.replace(/_download_/g,strDownLoad);	  	
			}
	  	allRow+=tmpRow
			tmpRow=oneRow;
			//全部的歌曲先存
	  	allMusicID+=sep+(oneSong.music_id)+"";
	  	//allSinger+=sep+oneSong.singer;
	  	sep="_";
		}
		

		songTable.innerHTML=strHeadTitle+allRow;	
	}
}
//新增專輯  
function addAlbumSong(){		
	sendMusicData("",send_album_id,"","2");
}
//以分層新增歌曲
function addSong(){
	var i=0;	
	for(i=0;i<musicDataTable.length;i++){
		addSongList(document.all(musicDataTable[i]),i);
	}
}
//新增歌曲
function addSongList(inTable,inLayerID){
	//確認是哪一個 table
	if(inTable){
		_theSongTable=inTable;		
	}	else {
		_theSongTable=songTable;	
		inLayerID=0;
	}
	if(_theSongTable){
		var tbl =_theSongTable;	
	 	var lastRow = tbl.rows.length;
		var r=0;
		var c=0;
		var strCss="";
		var sep="";
		var cell_no=0;//一行多個
		var row_no=0;
		for(r=0;r<PromoteSongs.Song.length;r++,row_no++){
	  	var row = tbl.insertRow(lastRow+row_no);
			var oneSong=PromoteSongs.Song[r];
			if((inLayerID+1)==parseInt(oneSong.layer)){
				var cell ; 
				cell_no=0;
				for(c=0;c<headtitle.length;c++){
					cell = row.insertCell(c); 
					strCss="";
					if(cssStyle[c]!="") {
						strCss="<span class='"+cssStyle[c]+"'>";
					}
					if(cssWidth[c]!="") {
						cell.setAttribute("width", cssWidth[c]);
					}
					if(headtitle[c]=="songname"){
						cell_no++;						 
						cell.innerHTML =strCss+(is_SN?(addZero(r+1,2)+" ."):"")+oneSong.music_name;								
					}else if(headtitle[c]=="singer"){
							cell.innerHTML =strCss+oneSong.singer;			
					}else if(headtitle[c]=="listen"){
							if(oneSong.content_id!="0"){
								cell.innerHTML =strCss+"<img value=\"Play\" src=\""+INimg+img_listen_play+"\" style=\"cursor:hand\" onclick=\" re_img(this);playme(this,"+oneSong.content_id+",'inmusic');img_swap(this);\">";  
							}else cell.innerHTML ="";
					}else if(headtitle[c]=="download"){
						if(oneSong.music_id!="0"){
							cell.innerHTML =strCss+"<img src=\""+INimg+img_song_down+"\" style=\"cursor:hand\" onclick=\"sendMusicData(this,"+oneSong.music_id+",'1');\">";  
						}else{ cell.innerHTML ="";}
					}else{
							cell.innerHTML="";
					}
					if(cell_no>0 && (headtitle[c+1]=="songname")){
						r=r+1;
						if(r<PromoteSongs.Song.length){
							if(oneSong.music_id!="0"){
								musicDataAllSong[inLayerID]+=sep+(oneSong.music_id)+"";
								//allSinger+=sep+oneSong.singer;
								sep="_";
							}	
							oneSong=PromoteSongs.Song[r];
						}
						else cell_no=cell_no-1;
						if(oneSong.layer!=(inLayerID+1))return;
					}//if(cell_no>0 && (headtitle[c]=="songname")){
				}//for(c=0;c<headtitle.length;c++){
				
					//全部的歌曲先存										
					if(oneSong.music_id!="0"){						
							musicDataAllSong[inLayerID]+=sep+(oneSong.music_id)+"";
							sep="_";
					}

			}//是否為同一個layer
		}//for(r=0;r<PromoteSongs.Song.length;r++,row_no++){	  	
	}
}
//補0
function addZero(inStr,len){
	var i=0;
	var retStr=inStr+"";
	if(retStr.length>=len)return inStr
	for(i=0;i<len;i++){
		retStr=0+""+retStr;
		if(retStr.length>=len)return retStr;
	}
	return retStr;
}
function Clear($obj) {
    $obj.value = '';
    $obj.focus();
}
function re_img(obj) {
	 if(temp) {
      if(temp.value=="Stop") {
         if(temp!=obj) {
             temp.value="Play";
             temp.src=INimg+img_listen_play;
         }
      }
  }
}

function playme($obj, $id, $product) {

if ($obj.value == "undefined")
{
  alert("test ok");
}

  if(($obj.value == "Play") || ($obj.value == "undefined")) {
       inPage.src=INWeb+"pd/listen.ashx?content_id="+$id;  
  } else {  
      inPage.src="0.html"; 
  }
}

function img_swap(obj) {
 if ((obj.value=="Play") || (obj.value=="undefined")) {
    obj.src=INimg+img_listen_stop;
    obj.value="Stop";
    temp=obj;
 } else {
   obj.src=INimg+img_listen_play;
   obj.value="Play";
 }
}
//Ajax
function getHttpRequest(){
    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
    }

    if (!http_request) {
      alert('哦哦..無法使用!!');
      return false;
    }
    return http_request
}
//送歌
  function sendMusicData(img1,musicid,albumORsong) {
    var theRequest = getHttpRequest();
    // document.all['AddKart'].src='AddKart.aspx?addkart=" & Music_id & "_" & Server.UrlEncode(Singer) & "_1'
    theRequest.onreadystatechange = function() { 
                                      sendMusicContents(theRequest,musicid,_promote_ID,img1); };
    //theRequest.open('GET',INWeb+ "pd/AddKart.aspx?addkart="+musicid+"__"+albumORsong, true);

    theRequest.open("POST",INWeb+ "pd/AddKart.aspx", true);
    theRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    theRequest.send("music_id="+musicid);
   
    //theRequest.send(null);
  }
//大量歌  
  function sendMusicDataAll(musicid) {
    var theRequest = getHttpRequest();
    // document.all['AddKart'].src='AddKart.aspx?addkart=" & Music_id & "_" & Server.UrlEncode(Singer) & "_1'
    theRequest.onreadystatechange = function() { 
                                      sendMusicContents(theRequest,"0",_promote_ID); };
    theRequest.open('POST',INWeb+ "pd/AddKart.aspx", true);
    theRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    theRequest.send("music_id="+musicid);
  }
  
  function sendMusicContents(http_request,musicid,promote_ID,img1) {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        /*var xmldoc = http_request.responseXML;
        var nodes = xmldoc.getElementsByTagName('area');
        var mesg = "";
        for(var i=0; i<nodes.length; i++) {
          mesg += nodes[i].firstChild.nodeValue + "\n";
        }
        alert(mesg);*/
	      window.showModalDialog(INWeb+"Promote/Login.html?promote_id="+promote_ID+"&music_id="+musicid,document.all("ok"),"dialogWidth=258px;dialogHeight=202px");
        goHomeURL();
        //if(img1)  img1.src=INimg+img_song_down_ok;
        //if(album_id>0)window.location=INWeb;
      } else {
        alert('該網頁有問題唷...');
      }
    }
  }
//回傳本頁導的URL
function goHomeURL(){
   if(document.all("ok")){
   	if(document.all("ok").value!=""){
   		if(parent.parent.parent)parent.parent.parent.window.location=document.all("ok").value;
   		else if(parent.parent)parent.parent.window.location=document.all("ok").value;
   		else if(parent)parent.window.location=document.all("ok").value;
   	}
   }	
}  
//輸出試聽
document.write ("<BGSOUND SRC='' id='inPage' LOOP='1'>");
//輸出AddKart

document.write ("<input type='hidden' id='ok' name='ok'><iframe id='isearchs' src='"+INWeb+ "pd/AddKart.aspx' width='0px' height='0px'></iframe>");

//document.write ("<input type='hidden' id='ok' name='ok'><iframe id='isearchs' src='"+INWeb+ "pd/AddKart.aspx' width='0px' height='0px' style='position:absolute;visibility:hidden;'></iframe>");

function sendAllSongData(inLayer){
	/*
	var objisearchs= isearchs;
	sf=objisearchs.document;
	sf.all.tags("input")[1].value=allMusicID;//歌曲 id
	sf.all.tags("input")[2].value="1";//專輯 ID
	sf.all.tags("input")[3].value=allSinger;//歌手
	sf.forms[0].submit();
	window.showModalDialog(INWeb+"Promote/Login.html?promote_id="+_promote_ID+"&music_id=0",document.all("ok"),"dialogWidth=258px;dialogHeight=202px");	
  goHomeURL();
  */
  if(inLayer){
	  sendMusicDataAll(musicDataAllSong[inLayer]);
	}else{
	  sendMusicDataAll(musicDataAllSong[0]);
	}
}
