/**
 * @author myself
 */



date = new Date();
timestamp = date.getTime();
count=0;
MovieConut=0;
playFlag=0;
shuffleFlag=0;
nowMusicFlag=0;

timeWait=Array();
TimerID=Array();
gettag=Array();
waitTime= Array();
artistName = Array();
songName= Array();
cover160= Array();
amazonURL= Array();

TimerIDtmp="";

showVideo = $('showVideo');
showTitle = $('showTitle');
showSong = $('showSong');
playingSign = $('playingSign');
cdJacket = $('cdJacket');
var ExceptionNum=Array();



function loadObj(readXml){
	var GetUrl=readXml+'?'+timestamp;
    var http = new JKL.ParseXML(GetUrl);
    var resXml= http.parse();

				var video= resXml.channel.item;
//alert(video);
					MovieConut=video.length;
					for(i=0; i<MovieConut; i++){
					var timer= video[i].length_seconds;
					timeWait[i] =parseInt(timer)*1010;
					var embed_src= video[i].embed_src + "&autoplay=1";
					waitTime[i]= video[i].length_seconds;
					artistName[i] = video[i].artistName;
//alert(video[i].artistName)
					artistName[i] = video[i].artistName;
					songName[i] = video[i].songName;
					cover160[i] = video[i].cover160;
					amazonURL[i] = video[i].amazonURL;
gettag[i]='<embed src="'+embed_src+'" type="application/x-shockwave-flash" wmode="transparent" width="455" height="380"></embed>';
}

}




function checkDeleteList(count){
var flag=0;
var songList=$$(".songList");
	while(count==MovieConut||flag==0){
		if (songList[count].style.display == "none"){
			count++;
			if(count==MovieConut){count=0}
		}
		else{
			flag=1;
		}
	}

return count;
}



function changeObj(){

//if(bi.ieMVersion==6){
//blowserAlert();
//	return false;
//}
playFlag=1;
if(ExceptionNum.length>0){
count=	checkDeleteList(count);
}

setNowPlaying(count);
nowMusicCount=count;
TimerIDtmp=setTimeout("changeObj()",timeWait[count]);
//次の準備
if(shuffleFlag ==1){
count=Math.floor( Math.random() * MovieConut);
}
else{
count=count+1;
}
if(count==MovieConut){
	count=0;
	}
}


function setNowPlaying(count){
	var nowPlaying =$("musicTitle");
	var showVideo = $('showVideo');
	var showTitle = $('showTitle');
	var showSong = $('showSong');
	//var playingSign = $('playingSign');
	var cdJacket = $('cdJacket');


showVideo.innerHTML=gettag[count];
showTitle.innerHTML=artistName[count];
showSong.innerHTML=songName[count];
//playingSign.innerHTML='<img src="/img/loader.gif" /><span> Now Playing</span>';
if (cover160[count]) {
	cdJacket.innerHTML = '<a href="'+amazonURL[count]+'" title="amazonで購入"　target="_blank" ><img src="' + cover160[count] + '" height="100" width="100" /></a>';
}
	var songDetail =$$(".songDetail");
	var coverImg =$$(".coverImg");
	for (var i = 0; i < songDetail.length; i++) {
		songDetail[i].parentNode.id="";
		//coverImg[i].style.height="32px";
		//coverImg[i].style.width="32px";
	}
	songDetail[count].parentNode.id="nowPlaying";
	//coverImg[count].style.height="50px";
	//coverImg[count].style.width="50px";
}


function nextMovie(){
//if(bi.ieMVersion==6){
//stopMovie();
//blowserAlert();
//return false;
//}
	
	if(count!=MovieConut){
		if (playFlag==1) {
			clearTimeout(TimerIDtmp);
		}
		changeObj();
	}
playFlag==1
}

function beforeMovie(){

	if (playFlag==1) {
		clearTimeout(TimerIDtmp);
		}
	if(count==1){
		count=MovieConut-1;
		changeObj();
	}
	else if (count == 0&&checkObj[0]) {
		count = MovieConut-2;
		changeObj();
	}
	else if (count == 0&&!checkObj[0]) {
		count = MovieConut-1;
		changeObj();
	}
	else if (count != 1) {
		count = count - 2;
		changeObj();
	}
	playFlag=1;
}

function startMovie(){
	if (playFlag==0) {
	changeObj();
	}
playFlag=1;
}

function stopMovie(){
	
	if(count==1){
		count=0;
	}
	else if (count == 0) {
		count = MovieConut-1;
	}
	else if (count != 1) {
		count = count - 1;
	}
	var checkObj=$("showVideo").getElementsByTagName("object")
	if (playFlag==1) {
		clearTimeout(TimerIDtmp);
	}
	$("showVideo").innerHTML="";
	playFlag=0;
	adRotation();
}

function skipMovie(num){
	if (playFlag==1) {
			clearTimeout(TimerIDtmp);
	}
	count=num;
	playFlag=1;
	changeObj();

}


function shuffleMovie(el){
	if (shuffleFlag == 0) {
		el.className="random_on";
		shuffleFlag = 1;
		count=Math.floor( Math.random() * MovieConut);
	}
	else{
		el.className="random";
		shuffleFlag = 0;
		count=nowMusicFlag+1;
	}
}

function deleteSong(el){


	if(restCount==1){
		alert("can`t remove anymore!!");
		return false;
	}
restCount=restCount-1;

var deletObj=el.parentNode.parentNode.parentNode;
var deteltSongNum=deletObj.dir;
deletObj.style.display="none";
ExceptionNum.push(deteltSongNum);

}


function adRotation(){
var movieAds = new Array(
'<a href="http://click.linksynergy.com/fs-bin/click?id=UDNuzqKGkHE&offerid=141501.10000016&type=4&subid=0"><IMG alt="ナップスター" border="0" src="http://www.napster.jp/affiliate/linkshare/bnr_images/bnr_180x150_collage.gif"></a><IMG border="0" width="1" height="1" src="http://ad.linksynergy.com/fs-bin/show?id=UDNuzqKGkHE&bids=141501.10000016&type=4&subid=0">',
'<a href="http://click.linksynergy.com/fs-bin/click?id=UDNuzqKGkHE&offerid=138700.10000025&type=4&subid=0"><IMG alt="ユナイテッド・シネマ映画チケット＆会員登録アフィリエイト" border="0" src="http://www.unitedcinemas.jp/affiliate/image/banner/250-250banner_uc.gif"></a><IMG border="0" width="1" height="1" src="http://ad.linksynergy.com/fs-bin/show?id=UDNuzqKGkHE&bids=138700.10000025&type=4&subid=0">',
'<a href="http://click.linksynergy.com/fs-bin/click?id=UDNuzqKGkHE&offerid=138700.10000023&type=4&subid=0"><IMG alt="ユナイテッド・シネマ映画チケット＆会員登録アフィリエイト" border="0" src="http://www.unitedcinemas.jp/affiliate/image/banner/300-80.jpg"></a><IMG border="0" width="1" height="1" src="http://ad.linksynergy.com/fs-bin/show?id=UDNuzqKGkHE&bids=138700.10000023&type=4&subid=0">'
);
var rand = Math.floor( Math.random() * movieAds.length);

var showVideo=$("showVideo");
var movieAdBnr=document.createElement("div");
movieAdBnr.id="movieAdBnr";
movieAdBnr.innerHTML=movieAds[rand];
showVideo.appendChild(movieAdBnr);
}

function jumpArtistPage(){
	var pageURL=$("pickUp").value;
	if(pageURL!="NG"){
	location.href = pageURL;
	}
}
 
 
function moveArtistPage(){
	var artistSearch=$("artistSearch");
	if(artistSearch.value!=""){
		var jumpURL="/music/artist.php?artist="+encodeURI(artistSearch.value);
		location.href = jumpURL;
	}
	
}


function loadingImg(){
	var loadingTxt =$("loadingTxt");
	loadingTxt.style.display="block";
}