function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function ImportSwfMenu(section){
	var so = new SWFObject("../_swf/Menu7.swf" , "MainMenu", "550", "80", "7", "");
	so.addParam('wmode', 'transparent');
	so.addParam('quality', 'high');
	so.addParam('Menu', 'false');
	so.addVariable('Section', section);
	so.write('MainMenu');
}

function ImportRotateSwf(section){
	var so = new SWFObject("../_swf/RotateSwf.swf" , "RotateSwf", "415", "695", "7", "");
	so.addParam('wmode', 'transparent');
	so.addParam('quality', 'high');
	so.addParam('Menu', 'false');
	so.addVariable('Section', section);
	so.write('RotateSwf');
}


function ImportIntroMov(){
	var so = new SWFObject("../_swf/IndexVideo2.swf" , "IntroMov", "240", "180", "7", "");
	so.addParam('wmode', 'transparent');
	so.addParam('quality', 'high');
	so.addParam('Menu', 'false');
	so.write('IntroMov');
}


function ImportSlideShow(){
	var AY = new Array('Ruby' , 'Sharon' , 'Judy' , 'Ady');
	for( i=0 ; i<AY.length ; i++){
		var so = new SWFObject("../_swf/"+AY[i]+"_slideshow.swf" , AY[i]+"_slideshow", "240", "350", "7", "");
		so.addParam('wmode', 'transparent');
		so.addParam('quality', 'high');
		so.addParam('Menu', 'false');
		so.write(AY[i]+'_slideshow');
	}
}

function ImportMovPayer(movID , divID){
	var so = new SWFObject("../_swf/480_360_Player.swf" , divID , "480", "405", "7", "");
	so.addParam('wmode', 'transparent');
	so.addParam('quality', 'high');
	so.addParam('Menu', 'false');
	so.addVariable('movID', movID);
	so.write(divID);
}

function goURL(path){
	if(path && path!='#'){
		window.location=path;
	}else{
		return false;
	}
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}

function checkEmail(mail_str) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(mail_str)){
		return false;
	}else{
		return true;
	}
}

function roundNum(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function WriteToday(){
	var today = new Date(); 
	var myDay = new Array("Sun","Mon","Tue","Wed","Thr","Fri","Sat"); 
	var mymonth = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	document.write("Today : "+mymonth[today.getMonth()]+" "+today.getDate()+","+today.getFullYear()+" - "+myDay[today.getDay()]); 
}

function getBodySize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
	//	console.log(self.innerWidth);
	//	console.log(document.documentElement.clientWidth);
	
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
	//	console.log("xScroll " + xScroll)
	//	console.log("windowWidth " + windowWidth)
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
	//	console.log("pageWidth " + pageWidth)
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
}

var SwitchTabID;

function SwitchTab(id){
	if( SwitchTabID != id ){
		
		$('WhisperContent').update('資料傳送中...');
		
		for(i=1;i<=4;i++){
			if(i==id){
				SwitchTabOn(id);	
			}else{
				SwitchTabOff(i);
			}
		}
		var load_url = "../_ajax/WhisperReturn.php";
		var myAjax = new Ajax.Request(load_url, {method:'post' , encoding:'UTF-8' , parameters:"id="+id , onComplete:TabResponse});
		SwitchTabID = id;
	}else{
		return false;
	}
}

function TabResponse(originalRequest){
	var xmldoc = originalRequest.responseXML;
	var Txt = xmldoc.getElementsByTagName("Content")[0].firstChild.nodeValue;
	var Img = xmldoc.getElementsByTagName("Img")[0].firstChild.nodeValue;
	var ImgAlt = xmldoc.getElementsByTagName("Artist")[0].firstChild.nodeValue;
	
	Txt = Txt.replace( "\n" , "<br />" );
	
	var PreviewImg = document.createElement('img');
			PreviewImg.src = "../_images/index/Whisper/Preview"+Img+".jpg";
			PreviewImg.alt = ImgAlt;

	$('WhisperImg').update('');
	$('WhisperImg').appendChild(PreviewImg);
	$('WhisperContent').update(Txt);
}

function SwitchTabOn(id){
	var ImgID = "ImgTab_"+id;
	$(ImgID).src = "../_images/index/Whisper/"+id+"_On.gif";
}

function SwitchTabOff(id){
	var ImgID = "ImgTab_"+id;
	$(ImgID).src = "../_images/index/Whisper/"+id+"_Off.gif";
}

function GoToTopPage(){
	window.location = "#Top";
}

function VoteSubmit(Request){

	var load_url = "../_ajax/VoteReturn.php?Request="+Request;
	var opts = new Object;
	opts.method = 'post';
  opts.parameters = $('VoteForm').serialize();
  
  switch(Request){
  	case 'UpdateResult' : opts.onComplete = UpdateResult;break;
  	case 'ListResult' : opts.onComplete = ListResult;break;
  	case 'ListAns' : opts.onComplete = ListAns;break;
  	default : break;
  }
  
  var ajax = new Ajax.Request(load_url, opts);
  return false;
}

function UpdateResult(originalRequest){
	var xmldoc = originalRequest.responseXML;
	var TotalPoll = xmldoc.getElementsByTagName("Total")[0].firstChild.nodeValue;
	var succ_sign = xmldoc.getElementsByTagName("success")[0].firstChild.nodeValue;
	if(succ_sign==1){
		ListResult(originalRequest);
	}else{
		alert("請選擇投票項目!");
	}
}

function ListResult(originalRequest){
	var xmldoc = originalRequest.responseXML;
	
	var VoteTopic = xmldoc.getElementsByTagName("VoteTopic")[0].firstChild.nodeValue;
	var TopRank = xmldoc.getElementsByTagName("TopRank")[0].firstChild.nodeValue;
	var TotalPoll = xmldoc.getElementsByTagName("Total")[0].firstChild.nodeValue;
	
	var objBody = $('VoteBody');
			objBody.update('');
			$('VoteTopic').update(VoteTopic);
	
	var VoteResult = document.createElement("div");
			VoteResult.id = 'VoteResult';
			objBody.appendChild(VoteResult);
	
	for( i=0; i < xmldoc.getElementsByTagName("result").length ; i++ ){
	
		var poll = xmldoc.getElementsByTagName("poll")[i].firstChild.nodeValue;
		var answer = xmldoc.getElementsByTagName("answer")[i].firstChild.nodeValue;
		
		var AnsContainter = document.createElement("div");
				AnsContainter.className = 'AnsContainter';
				
		var Ans = document.createElement("div");
				Ans.className = 'blue_wd12';
				Ans.innerHTML = answer;
				
		var RecContainter = document.createElement("div");
		
		var BarContainter = document.createElement("div")
				BarContainter.className = 'BarContainter';
		
		var TopRankBar = roundNum(poll/TopRank*100 , 1);
	
		var Bar = document.createElement("div");
				Bar.className = 'Bar';
				Bar.style.width = TopRankBar+'%';
				
		var Percentage = roundNum(poll/TotalPoll*100 , 1);
		
		var VoteStat = document.createElement("div");
				VoteStat.className = 'VoteRec gray_wd10';
				VoteStat.innerHTML = poll+'/'+Percentage+'%';
				
		var ClearDiv = document.createElement("div");
				ClearDiv.className = 'Clear';
				
				
		VoteResult.appendChild(AnsContainter);
		AnsContainter.appendChild(Ans);
		AnsContainter.appendChild(RecContainter);
		RecContainter.appendChild(BarContainter);
		BarContainter.appendChild(Bar);
		RecContainter.appendChild(VoteStat);
		RecContainter.appendChild(ClearDiv);
		
		/*
		<div id="VoteResult">
			<div class="AnsContainter">
				<div class="blue_wd12">電視廣告</div>
				<div>
					<div class="BarContainter"><div class="Bar" style="width:70%;"></div></div>
					<div class="VoteRec gray_wd10">1321/23.1%</div>
					<div class="Clear"></div>
				</div>
			</div>
		</div>
		*/
	}
	
	var HR = document.createElement('div');
			HR.className = 'ac_DotHR';
			
	var VotePPL = document.createElement('p');
			VotePPL.className = 'gray_wd11 ARight';
			VotePPL.innerHTML = '總投票人數 : '+TotalPoll;
	
	objBody.appendChild(HR);
	objBody.appendChild(VotePPL);
}


function ListAns(originalRequest){
	
	var xmldoc = originalRequest.responseXML;
	var VoteTopic = xmldoc.getElementsByTagName("VoteTopic")[0].firstChild.nodeValue;
	var VID = xmldoc.getElementsByTagName("VID")[0].firstChild.nodeValue;
	var BowserVer = navigator.userAgent.toLowerCase();
	
	$('VoteTopic').update(VoteTopic);
	$('VoteID').value = VID;
	Obj = $('VoteAnsList');
	
	var ul = document.createElement("ul");
	Obj.appendChild(ul);
	
	for ( i=0; i < xmldoc.getElementsByTagName("list").length ; i++ ){
		
		var id = xmldoc.getElementsByTagName("id")[i].firstChild.nodeValue;
		var answer = xmldoc.getElementsByTagName("answer")[i].firstChild.nodeValue;
		
		var li = document.createElement("li");
				li.className = 'gray_wd12';

		if(BowserVer.indexOf('msie')!=-1 && document.all){
			var InputField = document.createElement('<input type=\"radio\" name=\"VoteAns\" value=\"'+id+'\" id=\"VoteAns'+id+'\" />');
		}else{
			var InputField = document.createElement('input');
			InputField.name = 'VoteAns';
			InputField.type = 'radio';
			InputField.id = 'VoteAns'+id;
			InputField.value = id;
		}
				
		var Label = document.createElement('label');
				Label.htmlFor  = 'VoteAns'+id;
				Label.innerHTML = '&nbsp;&nbsp;'+answer;
						
		ul.appendChild(li);
		li.appendChild(InputField);
		li.appendChild(Label);
	}
	
	//<li class="gray_wd12">< type="radio" name="VoteAns" value="4" id="VoteAns4" /><label for="VoteAns4">&nbsp;&nbsp;其他網站</label></li>
}


function SwitchPersonalInfo(id){
	for(i=1;i<=4;i++){
		if(i==id){
			$('ArtistData_'+id).removeClassName('DisableArtistData').addClassName('ArtistData');
		}else{
			$('ArtistData_'+i).removeClassName('ArtistData').addClassName('DisableArtistData');
		}
	}
}

function SwitchFeeling(id){
	for(i=1;i<=4;i++){
		if(i==id){
			$('GirlsFeeling_'+id).removeClassName('DisableFeeling').addClassName('EnableFeeling');
		}else{
			$('GirlsFeeling_'+i).removeClassName('EnableFeeling').addClassName('DisableFeeling');
		}
	}
}

function PopListen(AID){
	if( !isNaN(AID) ){
		window.open('../Album/Listen.php?aid='+AID, 'ListenWin', 'height=150, width=300, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no , location=no , status=no');
	}
	return false;
}

function InputSelectAll(Obj){
	Obj.select();
}

function ShowEventImg(id){
	$(id).style.display = 'block';
}

function DisableEventImg(id){
	$(id).style.display = 'none';
}