/*
背景轉換
版本 1
*/
var rotationObjs={};

function createRotationObj(objName)
{
	rotationObjs[objName]=
	{
		mainId:				'',				/*轉換物件ID*/
		imgPath:			new Array(),	/*圖檔路徑陣列*/
		isPage:				true,			/*使用頁籤*/
		pageFrameId:		'',				/*page frame id*/
		pageStyle:			'number',		/*number, symbol, img*/
		pageOnClassName:	'',				/*number適用*/
		pageOffClassName:	'',				/*number適用*/
		pageDigit:			1,				/*number適用,頁數的位數*/
		pageImgPath:		new Array(),	/*img適用,頁籤圖檔路徑陣列*/
		pageImgWidth:		0,				/*img適用,圖寬*/
		pageImgHeight:		0,				/*img適用,圖高*/
		transformSec:		1,				/*轉換時間(淡入+淡出)秒*/
		staySec:			5,				/*停留時間(秒)*/
		useTable:			0,				/*0:不使用, 1:使用table絕對置中*/
		left:				0,
		top:				0,
		/*以下不可設定*/
		mainObj:		0,
		pageObj:		0,
		pageObjs:		new Object(),
		imgObjs:		new Object(),
		rotationStayTimerID: 	0,
		rotationShowTimerID: 	0,
		rotationHideTimerID: 	0,
		rotationShowOrder: 		0,
		rotationHideOrder: 		0,
		opacityValue:			0,
		rotationRate:			100
	};
}
function setRotationObjValue(objName,name,value)
{
	rotationObjs[objName][name]=value;
}
function initRotation(objName)
{
	rotationObjs[objName]['mainObj']=document.getElementById(rotationObjs[objName]['mainId']);
	
	rotationObjs[objName]['opacityValue']=100/(rotationObjs[objName]['transformSec']*1000/rotationObjs[objName]['rotationRate']);
	
	if(rotationObjs[objName]['isPage'] == true)
	{
		rotationObjs[objName]['pageObj']=document.getElementById(rotationObjs[objName]['pageFrameId']);
		
		for(var i=0;i<rotationObjs[objName]['imgPath'].length;i++)
		{
			rotationObjs[objName]['pageObjs'][i]=new Object();
			rotationObjs[objName]['pageObjs'][i]['page'] = document.createElement('div');
			rotationObjs[objName]['pageObjs'][i]['page'].className=rotationObjs[objName]['pageOffClassName'];
			if(rotationObjs[objName]['pageStyle']=='number')
			{
				rotationObjs[objName]['pageObjs'][i]['page'].innerHTML=countRotationDigit(i+1,objName);
			}
			else if(rotationObjs[objName]['pageStyle']=='img')
			{
				var tmp=document.createElement('div')
					tmp.style.width=rotationObjs[objName]['pageImgWidth']+'px';
					tmp.style.height=rotationObjs[objName]['pageImgHeight']+'px';
					tmp.style.backgroundImage='url('+rotationObjs[objName]['pageImgPath'][i]+')';
					tmp.style.backgroundRepeat="no-repeat";
				
				rotationObjs[objName]['pageObjs'][i]['page'].appendChild(tmp);
			}
			rotationObjs[objName]['pageObjs'][i]['page'].order=i;
			rotationObjs[objName]['pageObjs'][i]['page'].parentObjName=objName;
			rotationObjs[objName]['pageObjs'][i]['page'].onmouseover=function(){
				clearRotationTimer(this.parentObjName);
				rotationTurnOffAllPage(this.parentObjName);
				rotationHideAllImg(this.parentObjName);
				displayRotation(this.parentObjName,this.order);
				this.className=rotationObjs[this.parentObjName]['pageOnClassName'];
			};
			rotationObjs[objName]['pageObjs'][i]['page'].onmouseout=function(){
				rotationObjs[this.parentObjName]['rotationShowOrder']=this.order;
				
				if(rotationObjs[this.parentObjName]['imgPath'].length>1)
				{
					rotationObjs[this.parentObjName]['rotationStayTimerID']=setTimeout("rotation('"+this.parentObjName+"')",rotationObjs[this.parentObjName]['staySec']*1000);
				}
			};	
			rotationObjs[objName]['pageObj'].appendChild(rotationObjs[objName]['pageObjs'][i]['page']);
		}
	}
	
	for(var i=0;i<rotationObjs[objName]['imgPath'].length;i++)
	{
		rotationObjs[objName]['imgObjs'][i]=new Object();
		
		if(rotationObjs[objName]['useTable']==1)
		{
			rotationObjs[objName]['imgObjs'][i]['img'] = document.createElement("table");
			var tbody=document.createElement("tbody");
			var tr=document.createElement("tr");
			var td=document.createElement("td");
			tbody.appendChild(tr);
			tr.appendChild(td);
			rotationObjs[objName]['imgObjs'][i]['img'].appendChild(tbody);
			//rotationObjs[objName]['imgObjs'][i]['img'].appendChild(tr);
		}
		else
		{
			rotationObjs[objName]['imgObjs'][i]['img'] = new Image();
			rotationObjs[objName]['imgObjs'][i]['img'].src = rotationObjs[objName]['imgPath'][i];
		}
		
		rotationObjs[objName]['imgObjs'][i]['img'].opacity=0;
		rotationObjs[objName]['imgObjs'][i]['img'].style.opacity = rotationObjs[objName]['imgObjs'][i]['img'].opacity/100;
		rotationObjs[objName]['imgObjs'][i]['img'].style.filter = 'alpha(opacity=' + rotationObjs[objName]['imgObjs'][i]['img'].opacity + ')';
		rotationObjs[objName]['imgObjs'][i]['img'].style.display='none';
		rotationObjs[objName]['imgObjs'][i]['img'].style.position="absolute";
		rotationObjs[objName]['imgObjs'][i]['img'].style.top=rotationObjs[objName]['top']+'px';
		rotationObjs[objName]['imgObjs'][i]['img'].style.left=rotationObjs[objName]['left']+'px';	
		/*
		rotationObjs[objName]['imgObjs'][i]['img'].order=i;
		rotationObjs[objName]['imgObjs'][i]['img'].objName=objName;
		rotationObjs[objName]['imgObjs'][i]['img'].onmouseover=function(){
			clearRotationTimer(this.objName);
			rotationTurnOffAllPage(this.objName);
			rotationHideAllImg(this.objName);
			displayRotation(this.objName,this.order);
		};		
		rotationObjs[objName]['imgObjs'][i]['img'].onmouseout=function(){
			rotationObjs[this.objName]['rotationShowOrder']=this.order;
			
			if(rotationObjs[this.objName]['imgPath'].length>1)
			{
				rotationObjs[this.objName]['rotationStayTimerID']=setTimeout("rotation('"+this.objName+"')",rotationObjs[this.objName]['staySec']*1000);
			}
		};	
		*/
		/*2011-08-25*/
		if( rotationObjs[objName]['url'][i] != "" )
		{
			//alert( rotationObjs[objName]['url'][i] );
			
			rotationObjs[objName]['imgObjs'][i]['img'].style.cursor = "pointer";
			
			rotationObjs[objName]['imgObjs'][i]['img'].url = rotationObjs[objName]['url'][i];
			//var url = rotationObjs[objName]['url'][i];
			
			if( rotationObjs[objName]['target'][i] == 0 )
			{
				rotationObjs[objName]['imgObjs'][i]['img'].onclick = function()
				{
					location.href = this.url;
					//alert( this.url );
				}
			}
			else if( rotationObjs[objName]['target'][i] == 1 )
			{
				rotationObjs[objName]['imgObjs'][i]['img'].onclick = function()
				{
					window.open( this.url );
					//alert( this.url );
				}
			}
		}
		
		
		if(rotationObjs[objName]['useTable']==1)
		{
			var img=new Image();
				img.src = rotationObjs[objName]['imgPath'][i];
			
			td.appendChild(img);
		}
		rotationObjs[objName]['mainObj'].appendChild(rotationObjs[objName]['imgObjs'][i]['img']);
	}
	
	if(rotationObjs[objName]['imgPath'].length>0)
	{
		displayRotation(objName,0);

		if(rotationObjs[objName]['imgPath'].length>1)
		{
			rotationObjs[objName]['rotationStayTimerID']=setTimeout("rotation('"+objName+"')",rotationObjs[objName]['staySec']*1000);
		}
	}
}
function countRotationDigit(num,objName)
{
	var digit=rotationObjs[objName]['pageDigit'];
	
	if(num.toString().length < digit)
	{
		for(var i=num.toString().length;i<digit;i++)
		{
			num="0"+num;
		}
	}

	return num;
}
function rotationHideAllImg(objName)
{
	for(var i=0;i<rotationObjs[objName]['imgPath'].length;i++)
	{
		rotationObjs[objName]['imgObjs'][i]['img'].style.display='none';
		rotationObjs[objName]['imgObjs'][i]['img'].opacity=0;
		rotationObjs[objName]['imgObjs'][i]['img'].style.opacity = rotationObjs[objName]['imgObjs'][i]['img'].opacity/100;
		rotationObjs[objName]['imgObjs'][i]['img'].style.filter = 'alpha(opacity=' + rotationObjs[objName]['imgObjs'][i]['img'].opacity + ')';
	}
}
function rotationTurnOffAllPage(objName)
{
	if( rotationObjs[objName]['isPage'] )
	{
		for(var i=0;i<rotationObjs[objName]['imgPath'].length;i++)
		{
			rotationObjs[objName]['pageObjs'][i]['page'].className=rotationObjs[objName]['pageOffClassName'];
		}
	}
}
function displayRotation(objName,order)
{
	rotationObjs[objName]['rotationHideOrder'] = order;
	rotationObjs[objName]['imgObjs'][order]['img'].opacity=100;
	rotationObjs[objName]['imgObjs'][order]['img'].style.opacity = rotationObjs[objName]['imgObjs'][order]['img'].opacity/100;
	rotationObjs[objName]['imgObjs'][order]['img'].style.filter = 'alpha(opacity=' + rotationObjs[objName]['imgObjs'][order]['img'].opacity + ')';
	rotationObjs[objName]['imgObjs'][order]['img'].style.display='';
	
	if( rotationObjs[objName]['isPage'] )
	{
		rotationObjs[objName]['pageObjs'][order]['page'].className=rotationObjs[objName]['pageOnClassName'];
	}
	/*
	rotationObjs[objName]['imgObjs'][order]['img'].onmouseover=function(){
		clearRotationTimer(this.objName);
		rotationTurnOffAllPage(this.objName);
		rotationHideAllImg(this.objName);
		displayRotation(this.objName,this.order);
	};
	*/
}
function rotation(objName)
{
	clearRotationTimer(objName);
	rotationObjs[objName]['rotationHideOrder']=rotationObjs[objName]['rotationShowOrder'];
	//document.getElementById( "testtest" ).innerHTML += rotationObjs[objName]['rotationHideOrder'] + "&nbsp;";
	rotationObjs[objName]['rotationShowOrder']++;
	//document.getElementById( "testtest" ).innerHTML += rotationObjs[objName]['rotationShowOrder'] + "&nbsp;";
	
	if(rotationObjs[objName]['rotationShowOrder']==rotationObjs[objName]['imgPath'].length)
	{
		rotationObjs[objName]['rotationShowOrder']=0;
	}
	
	rotationObjs[objName]['imgObjs'][ rotationObjs[objName]['rotationShowOrder'] ]['img'].style.display="";
	
	rotationObjs[objName]['rotationShowTimerID']=setTimeout("rotationShow('"+objName+"')",rotationObjs[objName]['rotationRate']);	
	rotationObjs[objName]['rotationHideTimerID']=setTimeout("rotationHide('"+objName+"')",rotationObjs[objName]['rotationRate']);	
}
function rotationShow(objName)
{
	var order=rotationObjs[objName]['rotationShowOrder'];
	
	if(rotationObjs[objName]['imgObjs'][order]['img'].opacity<100)
	{
		rotationObjs[objName]['rotationShowTimerID']=setTimeout("rotationShow('"+objName+"')",rotationObjs[objName]['rotationRate']);	
	}
	else
	{
		rotationObjs[objName]['imgObjs'][order]['img'].opacity=100;
		clearTimeout(rotationObjs[objName]['rotationShowTimerID']);
		
		if( rotationObjs[objName]['isPage'] )
		{
			rotationTurnOffAllPage(objName);
			rotationObjs[objName]['pageObjs'][order]['page'].className=rotationObjs[objName]['pageOnClassName'];		
		}
		
		//document.getElementById( "testtest" ).innerHTML += "[" + rotationObjs[objName]['rotationHideOrder'] + "," + rotationObjs[objName]['rotationShowOrder'] + "]";
		
		rotationObjs[objName]['rotationStayTimerID']=setTimeout("rotation('"+objName+"')",rotationObjs[objName]['staySec']*1000);
		
		//alert( rotationObjs[objName]['rotationShowOrder'] );
	}
	rotationObjs[objName]['imgObjs'][order]['img'].lastOrder=rotationObjs[objName]['rotationHideOrder'];
	rotationObjs[objName]['imgObjs'][order]['img'].opacity+=rotationObjs[objName]['opacityValue'];
	rotationObjs[objName]['imgObjs'][order]['img'].style.opacity = rotationObjs[objName]['imgObjs'][order]['img'].opacity/100;
	rotationObjs[objName]['imgObjs'][order]['img'].style.filter = 'alpha(opacity=' + rotationObjs[objName]['imgObjs'][order]['img'].opacity + ')';
	/*
	if(rotationObjs[objName]['imgObjs'][order]['img'].opacity>=100)
	{
			rotationObjs[objName]['imgObjs'][order]['img'].onmouseover=function(){
			clearRotationTimer(this.objName);
			rotationTurnOffAllPage(this.objName);
			rotationHideAllImg(this.objName);
			displayRotation(this.objName,this.order);
		};
	}
	else
	{
		rotationObjs[objName]['imgObjs'][order]['img'].onmouseover=function(){
			clearRotationTimer(this.objName);
			rotationTurnOffAllPage(this.objName);
			rotationHideAllImg(this.objName);
			displayRotation(this.objName,this.lastOrder);
		};
	}
	*/
}
function rotationHide(objName)
{
	var order=rotationObjs[objName]['rotationHideOrder'];
	
	if(rotationObjs[objName]['imgObjs'][order]['img'].opacity>0)
	{
		rotationObjs[objName]['rotationHideTimerID']=setTimeout("rotationHide('"+objName+"')",rotationObjs[objName]['rotationRate']);	
	}
	else
	{
		rotationObjs[objName]['imgObjs'][order]['img'].opacity=0;
		rotationObjs[objName]['imgObjs'][order]['img'].style.display="none";
		clearTimeout(rotationObjs[objName]['rotationHideTimerID']);
		
		//alert( rotationObjs[objName]['rotationHideOrder'] );
	}
	
	rotationObjs[objName]['imgObjs'][order]['img'].opacity-=rotationObjs[objName]['opacityValue'];
	rotationObjs[objName]['imgObjs'][order]['img'].style.opacity = rotationObjs[objName]['imgObjs'][order]['img'].opacity/100;
	rotationObjs[objName]['imgObjs'][order]['img'].style.filter = 'alpha(opacity=' + rotationObjs[objName]['imgObjs'][order]['img'].opacity + ')';		
}
function clearRotationTimer(objName)
{
	clearTimeout(rotationObjs[objName]['rotationStayTimerID']);
	clearTimeout(rotationObjs[objName]['rotationShowTimerID']);
	clearTimeout(rotationObjs[objName]['rotationHideTimerID']);
}
