// JavaScript Document
// 2011-4-1 by Chris
// 2011-4-01 PM05:55 by AMin (domain不加.com.tw)
// 2011-4-11 PM02:30 by AMin (防止resize重疊)
// 2011-4-20 PM12:20 by AMin (link.xml的?拿掉)
// 2011-4-28 AM10:37 by AMin (creatLightbox&creatLightbox2合併)
// 2011-4-28 PM12:10 by AMin (creatLightbox&creatLightbox2 close動作一個在本頁開新視窗一個另外跳新頁)
// 2011-4-29 player 改從 google appengine 下載
// 2011-5-6  player 改從 ad-media.cdn.hinet.net 下載
// 2011-5-13 拿掉背景click close功能(by AMin)
// 2011-6-8 把 .swf 後面的變數全部移至 flashvars 欄位，用於最佳化 CDN (Jimmy)
// 2011-6-22 _redirect 為選填參數，視為社群分享的 URL，若無指定，則取目前的 location 作為分享的 URL (Jimmy)
// 2011-6-23 增加 _redirect 到 creatLightbox2 內 creatLightbox 第七參數 (Kenny)
// 2011-6-23 回復 creatLightbox2 的 Code，增加 creatLightbox3()，可傳入 sociallink (Jimmy)
// 2011-9-14 若 browser 是 google chrome，改用 iframe 去包 player。以避免 player 背後若有其他 iframe 而造成的黑色區塊 (Jimmy)
// 2011-9-15 iframe src 由 CDNHostName 讀取 (Jimmy)

	var OneVision_playerW=640;
	var OneVision_playerH=360;
	var OneVision_imgURL;
	var OneVision_PID;
	var OneVision_type;
	var OneVision_embedcode;
	var OneVision_isPlayer=false;
	var OneVision_domain;
	var OneVision_embedcode_swf="";
	var OneVision_embedcode_xml="";
	var OneVision_iframe_src = "";
	
	var OneVisionPlayerObj;
	var OneVisionCloseObj;
	var OneVisionBgObj;
	
	var OneVisionCloseWidth=17;
	var OneVisionCloseHeight=18;
	
	var OneVision_redirectLocation="";
	var OneVisionPlayerType="1";
	var OneVisiondoubleClose="0";

	var CDNHostName = "advideo.appledaily.com.tw"; // 空字串表示不使用 CDN
	
	/**
	* /w sociallink
	*/
	function creatLightbox3(_PID,_type,_playerW,_playerH,_imgURL,_domain, _redirect, _sociallink){	
		creatLightbox(_PID,_type,_playerW,_playerH,_imgURL,_domain, _redirect, _sociallink);
		OneVision_redirectLocation = _redirect;
		OneVisionPlayerType="2";
	}
	
	
	function creatLightbox2(_PID,_type,_playerW,_playerH,_imgURL,_domain, _redirect){
		var _sociallink = window.location.href;
		creatLightbox(_PID,_type,_playerW,_playerH,_imgURL,_domain, _redirect, _sociallink);
		OneVision_redirectLocation = _redirect;
		OneVisionPlayerType="2";
	}
	
	/** creat Lightbox **/
	function creatLightbox(_PID,_type,_playerW,_playerH,_imgURL,_domain, _redirect, _sociallink){
		///// 設定變數 ///////////////////////////////////////////////////////////////////////////////////////////////
		OneVision_playerH=_playerH;
		OneVision_playerW=_playerW;	
		OneVision_imgURL=_imgURL;	
		OneVision_PID=_PID;
		OneVision_type=_type;
		OneVision_domain='http://'+_domain;
   
		// 若未指定 CDN
		if (CDNHostName == ""){
			CDNHostName = _domain;
		}
   
		// drupal
		OneVision_embedcode_xml='xmlURL=' + OneVision_domain+'/ov_player/xml/link/'+_PID;   
   
		// iframe, src via CDN
		OneVision_iframe_src = 'http://'+ CDNHostName + '/ov_player/iframe/'+_PID+'/1/iframe.html';
   
		// _redirect is optional, especial for facebook, twitter and plurk	
		if (_sociallink == undefined || _sociallink == null || _sociallink == ""){		
			_sociallink = _redirect;
		}
		if(_sociallink == null || _sociallink == ""){
			_sociallink = window.location.href;
		}
   
		OneVision_flashvars = OneVision_embedcode_xml+'&location='+encodeURIComponent(_sociallink);
   
		OneVision_embedcode_swf='http://' + CDNHostName +'/sites/default/files/'+_type+'.swf'; //+OneVision_embedcode_xml;
	    OneVision_embedcode='<object height="100%" width="100%" id="FlashMediaPlayer" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"> <param value="always" name="allowScriptAccess"> <param value="true" name="allowFullScreen"> <param value="high" name="quality"> <param value="#000000" name="bgcolor"> <param value="window" name="wmode"> <param value="'+OneVision_flashvars+'" name="flashvars"> <param value="'+OneVision_embedcode_swf+'" name="movie"> <embed height="100%" wmode="window" width="100%" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" name="FlashMediaPlayer" bgcolor="#000000" quality="high" flashvars="'+OneVision_flashvars+'" src="'+OneVision_embedcode_swf+'"> </object>';
		OneVision_isPlayer=false;
		OneVision_redirectLocation = _redirect;
		OneVisiondoubleClose="0";
		OneVisionPlayerType="1";
		
		////建立一個DIV重新產生一個player div
		OneVisionPlayerObj = document.createElement("div");
		OneVisionPlayerObj.id='OneVisionPlayerObj-ID';
		OneVisionPlayerObj.innerHTML='<div id="OneVisionPlayer-ID" style="width:'+OneVision_playerW+'px;height:'+OneVision_playerH+'px;filter:Alpha(Opacity=100);opacity:1;background-color:#000000;z-index:2147483646;">'+OneVision_embedcode+'</div>';
		OneVisionPlayerObj.style.cssText='width:'+OneVision_playerW+'px;height:'+OneVision_playerH+'px;position:absolute;top:'+getWindowY()+'px;left:'+getWindowX()+'px;z-index: 2147483646;filter:Alpha(Opacity=100);opacity:1;background-color:#000000;';
					
		var is_chrome= navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		if (!is_chrome){
			// IE, firefox, safari
			document.body.appendChild(OneVisionPlayerObj);			
		}else{
			// google chrome
			// create an iframe to wrap the player
			var el = document.createElement("iframe");
			el.setAttribute('id', 'OneVisionPlayerObj-ID');	// instead of div	
			el.setAttribute('width', '640');		
			el.setAttribute('height', '360');		
			el.setAttribute('frameborder', '0');		
			el.setAttribute('border', '0');
			el.setAttribute('cellspacing', '0');
			el.setAttribute('scrolling', 'no');
			el.style.cssText = OneVisionPlayerObj.style.cssText;
			el.setAttribute('src', OneVision_iframe_src);
			document.body.appendChild(el);
		}
			
		/*OneVisionPlayerObj.onclick = function () {			
			if(OneVision_isPlayer==false){
				closeAll();
			}else{
				OneVision_isPlayer=false;
			}
		}

		document.getElementById("OneVisionPlayer-ID").onclick = function () {
			OneVision_isPlayer=true;
		}*/

		///// creat close button ///////////////////////////////////////////////////////////////////////////////////////
		creatClose();
		
		///// creat background ////////////////////////////////////////////////////////////////////////////////////////
		creatBg();
	}
	
	/** creat close button **/
	function creatClose(){
		OneVisionCloseObj = document.createElement("div"); 
		OneVisionCloseObj.id='OneVisionCloseObj-ID';
		OneVisionCloseObj.innerHTML='<a href="javascript:closeAll();"><img src="'+OneVision_imgURL+'" border="0"/></a>';
		//alert(getWindowX()+' '+OneVision_playerW);
		//alert(getWindowX()+' '+getWindowY());
		OneVisionCloseObj.style.cssText = "position:absolute;top:"+(getWindowY()-OneVisionCloseHeight)+"px;left:"+(getWindowX()+OneVision_playerW)+"px;width:"+OneVisionCloseWidth+"px;height:"+OneVisionCloseHeight+"px;z-index:2147483647;"; 
		document.body.appendChild(OneVisionCloseObj); 	
		OneVisionCloseObj.onclick = function () {
			closeAll();
		}
	}
	
	/** creat background **/
	function creatBg(){
		OneVisionBgObj = document.createElement("div"); 
		OneVisionBgObj.id='OneVisionBgObj-ID';
		OneVisionBgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+getPageSize()[0]+"px;height:"+getPageSize()[1]+"px;filter:Alpha(Opacity=80);opacity:0.8;background-color:#000000;z-index:2000000000;";
		document.body.appendChild(OneVisionBgObj); 	
		/*OneVisionBgObj.onclick = function () {
			closeAll();
		}*/
	}
	
	/** close all UI **/
	function closeAll(){
		OneVisionBgObj = document.getElementById("OneVisionBgObj-ID");		
		if(OneVisionBgObj!=null){
			document.body.removeChild(OneVisionBgObj);  
		}
		
		OneVisionCloseObj = document.getElementById("OneVisionCloseObj-ID"); 
		if(OneVisionCloseObj!=null){
			document.body.removeChild(OneVisionCloseObj);
		}		
		
		OneVisionPlayerObj = document.getElementById("OneVisionPlayerObj-ID");		
		if(OneVisionPlayerObj!=null){
			OneVisionPlayerObj.innerHTML = "";
			document.body.removeChild(OneVisionPlayerObj);
		}		
		
		if(OneVision_redirectLocation != "" && OneVision_redirectLocation != null && OneVisiondoubleClose=="0"){
			if(OneVisionPlayerType=="1"){
				window.open(OneVision_redirectLocation);
			}else{
				window.location = OneVision_redirectLocation;
			}
			OneVisiondoubleClose="1";
		}
	}
	
	/** 取得 全文高&全文寬&可見高&可見寬 **/  
	function getPageSize(){	
		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;
			if(yScroll<document.documentElement.scrollHeight){
				yScroll=document.documentElement.scrollHeight;
			}
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
			if(yScroll<document.documentElement.scrollHeight){
				yScroll=document.documentElement.scrollHeight;
			}
		}
		
		var windowWidth, windowHeight;
		
		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;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}
	
	function getPageScroll(){
		var yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}		
		return yScroll;
	}

	/** 取得物件在瀏覽器視窗垂直中間 **/
	function getWindowY() {
		var middleY = ((getPageSize()[3]-OneVision_playerH)/2)+getPageScroll();
		if(middleY<0){
			return	0;
		}else{
			return middleY;
		}
	}
	
	/** 取得物件在瀏覽器視窗水平中間 **/
	function getWindowX() {
		//alert(getPageSize()[2] + " " + OneVision_playerW);
		var middleX = (getPageSize()[2]-OneVision_playerW)/2;
		if(middleX<0){
			return 0;
		}else{
			return middleX;
		}
	}
	
	/** 瀏覽器視窗改變大小時UI位置重新設置 **/
	/*window.onresize = function () {
		OneVisionBgObj = document.getElementById("OneVisionBgObj-ID"); 
		OneVisionPlayerObj = document.getElementById("OneVisionPlayerObj-ID");
		OneVisionCloseObj = document.getElementById("OneVisionCloseObj-ID"); 
		
		if(OneVisionBgObj != null){			
			OneVisionBgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+getPageSize()[0]+"px;height:"+getPageSize()[1]+"px;filter:Alpha(Opacity=80);opacity:0.8;background-color:#000000;z-index:2000000000;";
			OneVisionPlayerObj.style.cssText='width:'+OneVision_playerW+'px;height:'+OneVision_playerH+'px;position:absolute;top:'+getWindowY()+'px;left:'+getWindowX()+'px;z-index: 2147483646;filter:Alpha(Opacity=100);opacity:1;background-color:#00ee00;';
			OneVisionCloseObj.style.cssText = "position:absolute;top:"+(getWindowY()-OneVisionCloseHeight)+"px;left:"+(getWindowX()+OneVision_playerW)+"px;width:"+OneVisionCloseWidth+"px;height:"+OneVisionCloseHeight+"px;z-index:2147483647;"; 
		}		
	}*/
	
	function rollover() {
		OneVisionBgObj = document.getElementById("OneVisionBgObj-ID"); 
		OneVisionPlayerObj = document.getElementById("OneVisionPlayerObj-ID");
		OneVisionCloseObj = document.getElementById("OneVisionCloseObj-ID"); 
		
		if(OneVisionBgObj != null){			
			OneVisionBgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+getPageSize()[0]+"px;height:"+getPageSize()[1]+"px;filter:Alpha(Opacity=80);opacity:0.8;background-color:#000000;z-index:2000000000;";
			OneVisionPlayerObj.style.cssText='width:'+OneVision_playerW+'px;height:'+OneVision_playerH+'px;position:absolute;top:'+getWindowY()+'px;left:'+getWindowX()+'px;z-index: 2147483646;filter:Alpha(Opacity=100);opacity:1;background-color:#000000;';
			OneVisionCloseObj.style.cssText = "position:absolute;top:"+(getWindowY()-OneVisionCloseHeight)+"px;left:"+(getWindowX()+OneVision_playerW)+"px;width:"+OneVisionCloseWidth+"px;height:"+OneVisionCloseHeight+"px;z-index:2147483647;"; 
		}		
	}
		
	//防止resize重疊
	if (typeof window.addEventListener != "undefined") {
		 window.addEventListener("resize",rollover,false);
	} else {
		 window.attachEvent("onresize",rollover)
	}
