IOM.PWS.Home			= {};
IOM.PWS.Home.SlideShow	= {};
IOM.PWS.Home.ScreenShot	= {};
IOM.PWS.Home.News		= {};
IOM.PWS.LangUC			= document.getElementById('pageLang').value;
IOM.PWS.Home.imgURL		= '../resource/image/Home/';

IOM.PWS.Home.getBrowserInfo = function(){
	var ua = navigator.userAgent.toLowerCase();
	var browser = {};
	browser.isIE		= false;
	browser.isFirefox	= false;
	browser.isChrome	= false;
	browser.isOpera		= false;
	browser.isSafari	= false;
	(browser.info = ua.match(/msie ([\d.]+)/))				? browser.isIE		= true :
	(browser.info = ua.match(/firefox\/([\d.]+)/) )			? browser.isFirefox	= true :
	(browser.info = ua.match(/chrome\/([\d.]+)/) )			? browser.isChrome	= true :
	(browser.info = ua.match(/opera.([\d.]+)/) )			? browser.isOpera	= true :
	(browser.info = ua.match(/version\/([\d.]+).*safari/) )	? browser.isSafari	= true : false;
	return browser;
};

IOM.PWS.Home.gotoTop5 = function() {
	window.top.location.href = '../Top5/top5.jsp#' + this.id;
};

IOM.PWS.Home.gotoProductDes = function() {
	window.top.location.href = '../Features/features.jsp';
};

IOM.PWS.Home.gotoScreenShot = function() {
	window.top.location.href = '../ScreenShot/screenShot.jsp';
};

IOM.PWS.Home.buttonInitialize = function() {
	//top 5 table item effect
	var top5ContentsOut = function() {
		this.className = 'top5ContentsOut';
	};
	var top5ContentsOver = function() {
		this.className = 'top5ContentsOver';
	};
	if(document.getElementById('tblTop5Secure')) {
		var tblTop5Secure = document.getElementById('tblTop5Secure');
		tblTop5Secure.onmouseout = top5ContentsOut;
		tblTop5Secure.onmouseover = top5ContentsOver;
	}
	if(document.getElementById('tblTop5Global')) {
		var tblTop5Global = document.getElementById('tblTop5Global');
		tblTop5Global.onmouseout = top5ContentsOut;
		tblTop5Global.onmouseover = top5ContentsOver;
	}
	if(document.getElementById('tblTop5Instant')) {
		var tblTop5Instant = document.getElementById('tblTop5Instant');
		tblTop5Instant.onmouseout = top5ContentsOut;
		tblTop5Instant.onmouseover = top5ContentsOver;
	}
	if(document.getElementById('tblTop5XMass')) {
		var tblTop5XMass = document.getElementById('tblTop5XMass');
		tblTop5XMass.onmouseout = top5ContentsOut;
		tblTop5XMass.onmouseover = top5ContentsOver;
	}
	if(document.getElementById('tblTop5Affordable')) {
		var tblTop5Affordable = document.getElementById('tblTop5Affordable');
		tblTop5Affordable.onmouseout = top5ContentsOut;
		tblTop5Affordable.onmouseover = top5ContentsOver;
	}
	//top 5 link
	if(document.getElementById('Secure')) {
		var Secure = document.getElementById('Secure');
		Secure.onclick = IOM.PWS.Home.gotoTop5;
	}
	if(document.getElementById('Global')) {
		var Global = document.getElementById('Global');
		Global.onclick = IOM.PWS.Home.gotoTop5;
	}
	if(document.getElementById('Instant')) {
		var Instant = document.getElementById('Instant');
		Instant.onclick = IOM.PWS.Home.gotoTop5;
	}
	if(document.getElementById('Xplatform')) {
		var Xplatform = document.getElementById('Xplatform');
		Xplatform.onclick = IOM.PWS.Home.gotoTop5;
	}
	if(document.getElementById('Affordable')) {
		var Affordable = document.getElementById('Affordable');
		Affordable.onclick = IOM.PWS.Home.gotoTop5;
	}
	//product description link
	if(document.getElementById('aProductDes')) {
		var aProductDes = document.getElementById('aProductDes');
		aProductDes.onclick = IOM.PWS.Home.gotoProductDes;
	}
	//ScreenShot link
	if(document.getElementById('aScreenShotMore')) {
		var aScreenShotMore = document.getElementById('aScreenShotMore');
		aScreenShotMore.onclick = IOM.PWS.Home.gotoScreenShot;
	}
};

IOM.PWS.Home.News.Initialize = function(){
	var ulNewsTitleList = document.getElementById('ulNewsTitleList');
	if (ulNewsTitleList != null ) {
		var liNewsTitleItem = ulNewsTitleList.getElementsByTagName('li');
		for (var i=0; i<liNewsTitleItem.length; i++) {
			liNewsTitleItem[i].className = 'liNewsTitleItem';
			liNewsTitleItem[i].onmouseover = function f(){
				this.className = 'liNewsTitleItemOver';
			};
			liNewsTitleItem[i].onMouseDown	= function f(){
				this.className = 'liNewsTitleItemOver';
			};
			liNewsTitleItem[i].onMouseUp	= function f(){
				this.className = 'liNewsTitleItemOver';
			};
			liNewsTitleItem[i].onmouseout	= function f(){
				this.className = 'liNewsTitleItemOut';
			};
		}
	}
};

IOM.PWS.Home.News.gotoNewsContent = function(fileName){
	window.top.location.href = '../News/' + fileName + '.jsp?language=' + IOM.PWS.LangUC.toLowerCase();
};

IOM.PWS.Home.SlideShow.action = function(){
	return {		
		clarityDegree	: 0,
		opacityDegree	: 100,
		divs			: null,
		tds				: null,
		startIndex		: 1,
		endIndex		: 0,
		selectSwitch	: false,
		persistTime		: 0,
		countLoop		: null,
		opacityLoop		: null,
		select : function( _idx ){
			for ( i = 0; i < this.divs.length; i++ ){
				if ( i == _idx ){
					this.divs[i].style.filter	= 'alpha(opacity=100)';
					this.divs[i].style.opacity	= 1;
					this.tds[i].className		= 'tdSlideShowIndexSelected';
				}else{
					this.divs[i].style.filter	= 'alpha(opacity=0)';
					this.divs[i].style.opacity	= 0;
					this.tds[i].className		= 'tdSlideShowIndex';
				}
			}
			if ( _idx != ( this.divs.length - 1 ) )
				this.startIndex = _idx + 1;
			else
				this.startIndex = 0;
		},
		slide : function( containerID, persistTime ){
			//get divs
			if( containerID ){
				this.divs = document.getElementById( 'divSlideShowContainer' ).getElementsByTagName('img');
				this.tds = document.getElementById( 'tblSlideShowIndex' ).getElementsByTagName('td');
				this.endIndex = this.divs.length - 1;
			}
			//0 div, 1 div, n divs
			if( this.divs.length == 0 ){
				document.getElementById( containerID ).innerHTML = "Can't find any images!";
			}else if( this.divs.length == 1 ){
				this.divs[0].style.filter = 'alpha(opacity=100)';
				this.divs[0].style.opacity = 1;
			}else if( this.divs.length > 1 ){
				//fade in next image
				this.divs[this.startIndex].style.filter = 'alpha(opacity=' + this.clarityDegree + ')';
				this.divs[this.startIndex].style.opacity = this.clarityDegree/100;
				this.tds[this.startIndex].className = 'tdSlideShowIndexOver';
				//fade out pre image
				var tempIndex = ( this.startIndex == 0 ) ? ( this.divs.length-1 ) : ( this.startIndex-1 );
				this.divs[tempIndex].style.filter = 'alpha(opacity=' + this.opacityDegree + ')';
				this.divs[tempIndex].style.opacity = this.opacityDegree / 100;
				this.tds[tempIndex].className = 'tdSlideShowIndex';
				//persist time
				if( persistTime ) this.persistTime = persistTime;
				//loop var, IE's speed is different than other browsers
				this.clarityDegree += document.all? 10 : 2;
				this.opacityDegree -= document.all? 10 : 2;
				//loop run
				if( this.clarityDegree > 100 ){
					this.clarityDegree = 0;
					this.opacityDegree = 100;
					this.startIndex == this.endIndex ? this.startIndex=0 : this.startIndex += 1;							
					setTimeout( 'IOM.PWS.Home.SlideShow.action.slide()', this.persistTime );
				}else
					setTimeout( 'IOM.PWS.Home.SlideShow.action.slide()', 1 );
			}
		}
	};
}();

IOM.PWS.Home.SlideShow.layout = function() {
	//index function
	var selectPicByIdx = function() {
		var idxTemp = parseInt(this.innerHTML) - 1;
		IOM.PWS.Home.SlideShow.action.select(idxTemp);
	};
	var idxOnmouseover = function() {
		this.className = 'tdSlideShowIndexHover';
	};
	var idxOnmouseout = function() {
		this.className = 'tdSlideShowIndex';
	};
	
	//create temp container, and add pics to it
	var picsArray	= ['01', '02', '03', '04', '05', '06', '07', '08'];
	var container	= document.createElement('div');
	var tblIndex	= document.createElement('table');
	var rowIndex	= tblIndex.insertRow(0);
	container.id	= 'divSlideShowContainer';
	container.className	= 'divSlideShowContainer';
	tblIndex.id			= 'tblSlideShowIndex';
	tblIndex.className	= 'tblSlideShowIndex';
	for (i = 0; i < picsArray.length; i++){
		var img			= document.createElement('img');
		var cell		= rowIndex.insertCell(i)
		cell.className	= 'tdSlideShowIndex';
		cell.onmouseover= idxOnmouseover;
		cell.onmouseout	= idxOnmouseout;	
		cell.innerHTML	= i + 1;
		cell.onclick	= selectPicByIdx;
		img.className	= 'divSlideShowPic';
		img.src			= IOM.PWS.Home.imgURL + IOM.PWS.LangUC + '/slideShow_' + picsArray[i]  + '_1256.gif';
		container.appendChild(img);
	}
	container.appendChild(tblIndex);

	//add temp container to webpage, and start to slide show
	if(document.getElementById('divSlideShowWhole')) {
		var divSlideShowWhole = document.getElementById('divSlideShowWhole');
		divSlideShowWhole.appendChild(container);
		IOM.PWS.Home.SlideShow.action.slide('divSlideShowContainer', 3000)
	}
};

IOM.PWS.Home.ScreenShot.layout = function() {
	for(var i = 1; i <= 4; i++) {
		var divScreenShotItem = 'divScreenShotItem_' + i
		divScreenShotItem = document.getElementById('divScreenShotItem_' + i);
		divScreenShotItem.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot00' + i + 'Down_1155.gif)';
	}
	
	if(document.getElementById('divScreenShotItem_1')) {
		var divScreenShotItem_1 = document.getElementById('divScreenShotItem_1');
		divScreenShotItem_1.onmouseout = function f() {
			divScreenShotItem_1.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot001Down_1155.gif)';
		};
		divScreenShotItem_1.onmouseover = function f() {
			divScreenShotItem_1.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot001Up_1155.gif)';
		};
		divScreenShotItem_1.onclick = function() {
			IOM.PWS.Home.ScreenShot.action.showWindowPic('1');
		};
	}
	if(document.getElementById('divScreenShotItem_2')) {
		var divScreenShotItem_2 = document.getElementById('divScreenShotItem_2');
		divScreenShotItem_2.onmouseout = function f() {
			divScreenShotItem_2.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot002Down_1155.gif)';
		};
		divScreenShotItem_2.onmouseover = function f() {
			divScreenShotItem_2.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot002Up_1155.gif)';
		};
		divScreenShotItem_2.onclick = function() {
			IOM.PWS.Home.ScreenShot.action.showWindowPic('2');
		};
	}
	if(document.getElementById('divScreenShotItem_3')) {
		var divScreenShotItem_3 = document.getElementById('divScreenShotItem_3');
		divScreenShotItem_3.onmouseout = function f() {
			divScreenShotItem_3.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot003Down_1155.gif)';
		};
		divScreenShotItem_3.onmouseover = function f() {
			divScreenShotItem_3.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot003Up_1155.gif)';
		};
		divScreenShotItem_3.onclick = function() {
			IOM.PWS.Home.ScreenShot.action.showWindowPic('3');
		};
	}
	if(document.getElementById('divScreenShotItem_4')) {
		var divScreenShotItem_4 = document.getElementById('divScreenShotItem_4');
		divScreenShotItem_4.onmouseout = function f() {
			divScreenShotItem_4.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot004Down_1155.gif)';
		};
		divScreenShotItem_4.onmouseover = function f() {
			divScreenShotItem_4.style.backgroundImage = 'url(' + IOM.PWS.Home.imgURL + 'imgShot004Up_1155.gif)';
		};
		divScreenShotItem_4.onclick = function() {
			IOM.PWS.Home.ScreenShot.action.showWindowPic('4');
		};
	}
};

IOM.PWS.Home.ScreenShot.setWindowPosition = function(_id) {
	if(document.getElementById(_id)) {
		var container = document.getElementById(_id);
		var el = document.getElementById('divScreenShotWhole');
		var obj = new Object();
		obj.x = 0;
		obj.y = 0;
		var browser = IOM.PWS.Home.getBrowserInfo();
		if(browser.isIE) {
			do {
				obj.x += el.offsetLeft || 0;
				obj.y += el.offsetTop || 0;
				el = el.offsetParent;
				if(el && (el.tagName.toUpperCase() == 'BODY')) {
					break;
				}
			} while (el);
			obj.x = obj.x + 'px';
			obj.y = obj.y - 310 + 'px';
		}
		else {
			obj.x = el.offsetLeft + 'px';
			obj.y = el.offsetTop - 310 + 'px';
		}
		container.style.top = obj.y;
		container.style.left = obj.x;
	} 
};

IOM.PWS.Home.ScreenShot.createWindow = function() {
	//insert a table to screenshot window container
	var tbl	= document.createElement('table');
	var rowT	= tbl.insertRow(0);
	var cellTL	= rowT.insertCell(0);
	var cellTC	= rowT.insertCell(1);
	var cellTR	= rowT.insertCell(2);
	var rowM	= tbl.insertRow(1);
	var cellML	= rowM.insertCell(0);
	var cellMC	= rowM.insertCell(1);
	var cellMR	= rowM.insertCell(2);
	var rowB	= tbl.insertRow(2);
	var cellBL	= rowB.insertCell(0);
	var cellBC	= rowB.insertCell(1);
	var cellBR	= rowB.insertCell(2);
	tbl.className		= 'tblIOMScreenShot';
	tbl.id				= 'tblIOMScreenShot';
	cellTL.className	= 'tdScreenShotTL';
	cellTC.className	= 'tdScreenShotTC';
	cellTR.className	= 'tdScreenShotTR';
	cellML.className	= 'tdScreenShotML';
	cellMC.className	= 'tdScreenShotMC';
	cellMR.className	= 'tdScreenShotMR';
	cellBL.className	= 'tdScreenShotBL';
	cellBC.className	= 'tdScreenShotBC';
	cellBR.className	= 'tdScreenShotBR';
	var divScreenShotTitle			= document.createElement('div');
	divScreenShotTitle.id			= 'divScreenShotTitle';
	divScreenShotTitle.className	= 'divScreenShotTitle';
	divScreenShotTitle.innerHTML	= 'Talking with Tom';
	var divScreenShotCloseBtn		= document.createElement('div');
	divScreenShotCloseBtn.id		= 'divScreenShotCloseBtn';
	divScreenShotCloseBtn.className	= 'divScreenShotCloseBtn divScreenShotCloseBtnUp';
	divScreenShotCloseBtn.onclick	= function f() {
		IOM.PWS.Home.ScreenShot.action.fadeOutContainer();
	};
	divScreenShotCloseBtn.onmouseout = function f() {
		this.className = 'divScreenShotCloseBtn divScreenShotCloseBtnUp';
	};
	divScreenShotCloseBtn.onmouseover = function f() {
		this.className = 'divScreenShotCloseBtn divScreenShotCloseBtnOver';
	};
	
	//create screenshot window container
	var divScreenShotWindow = document.createElement('div');
	divScreenShotWindow.className = 'divScreenShotWindow';
	divScreenShotWindow.id = 'divScreenShotWindow';
	window.document.body.appendChild(divScreenShotWindow);
	IOM.PWS.Home.ScreenShot.setWindowPosition('divScreenShotWindow');
	//create window container body
	var divScreenShotWindowBody = document.createElement('div');
	divScreenShotWindowBody.id = 'divScreenShotWindowBody';
	divScreenShotWindowBody.className = 'divScreenShotWindowBody';
	cellMC.appendChild(divScreenShotWindowBody);
	cellTC.appendChild(divScreenShotTitle);
	cellTC.appendChild(divScreenShotCloseBtn);
	divScreenShotWindow.appendChild(tbl);
	//insert images to window body
	for(var i = 1; i <= 4; i++) {
		var imgSmall = document.createElement('img');
		imgSmall.id = 'imgShot00' + i; 
		imgSmall.className = 'imgScreenShotSmall';
		imgSmall.src = IOM.PWS.Home.imgURL + IOM.PWS.LangUC + '/imgShot00' + i  + 'Small_1036.jpg';
		divScreenShotWindowBody.appendChild(imgSmall);
	}
};


IOM.PWS.Home.ScreenShot.action = function() {
	return {
		container	:	null,
		total		:	100,
		step		:	0,
		opac		:	0,
		lastPic		:	null,
		nextPic		:	null,
		showWindowPic : function(_idx) {
			if(!document.getElementById('divScreenShotWindow')) {
				IOM.PWS.Home.ScreenShot.createWindow();
			}
			//get the window's title
			this.container = document.getElementById('divScreenShotWindow');
			var divScreenShotTitle = document.getElementById('divScreenShotTitle');
			var divScreenShotItem = document.getElementById('divScreenShotItem_' + _idx);
			var divScreenShotItemText = divScreenShotItem.getElementsByTagName('div')[0];
			divScreenShotTitle.innerHTML = divScreenShotItemText.innerHTML;
			//if windows is transparent, ready to fade in
			var flag = false;
			var browser = IOM.PWS.Home.getBrowserInfo();
			if(browser.isIE) {
				flag = (this.container.filters.alpha.Opacity <= 0);
			}
			else {
				flag = (this.container.style.opacity <= 0);
			}
			
			if(flag) {
				IOM.PWS.Home.ScreenShot.action.fadeInContainerWithIdx(_idx);
			}
			else {
				this.nextPic = document.getElementById('imgShot00' + _idx);
				IOM.PWS.Home.ScreenShot.action.fadeInPic(_idx);
			}
		},
		fadeOutContainer : function() {
			this.opac = this.total - this.step;
			this.step += 10;
			this.container.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.container.style.opacity = this.opac / 100;
			this.lastPic.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.lastPic.style.opacity = this.opac / 100;
			
			if(this.step < this.total) {
				setTimeout('IOM.PWS.Home.ScreenShot.action.fadeOutContainer()', 40);
			}
			else {
				this.step = 0;
				window.document.body.removeChild(this.container);
			}
		},
		fadeInContainerWithIdx : function(_idx) {	
			this.opac = this.step;
			this.step += 10;
			this.container.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.container.style.opacity = this.opac / 100;
			if(this.step < this.total) {
				setTimeout('IOM.PWS.Home.ScreenShot.action.fadeInContainerWithIdx(' + _idx + ')', 40);
			}
			else {
				this.step = 0;
				this.container.style.filter = 'alpha(opacity=100)';
				this.container.style.opacity = 1;
				this.nextPic = document.getElementById('imgShot00' + _idx);
				IOM.PWS.Home.ScreenShot.action.fadeInPicWithIdx();
			}
		},
		fadeInPicWithIdx : function() {
			this.opac = this.step;
			this.step += 10;
			this.nextPic.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.nextPic.style.opacity = this.opac / 100;
			if(this.step < this.total) {
				setTimeout('IOM.PWS.Home.ScreenShot.action.fadeInPicWithIdx()', 40);
			}
			else {
				this.step = 0;
				this.nextPic.style.filter = 'alpha(opacity=100)';
				this.nextPic.style.opacity = 1;
				this.lastPic = this.nextPic;
			}
		},
		fadeInPic : function() {
			this.opac = this.step;
			this.step += 10;
			this.lastPic.style.filter = 'alpha(opacity=' + (100 - this.opac) + ')';
			this.lastPic.style.opacity = 1 - this.opac / 100;
			this.nextPic.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.nextPic.style.opacity = this.opac / 100;
			if(this.step < this.total) {
				setTimeout('IOM.PWS.Home.ScreenShot.action.fadeInPic()', 40);
			}
			else {
				this.step = 0;
				this.lastPic.style.filter = 'alpha(opacity=0)';
				this.lastPic.style.opacity = 0;
				this.nextPic.style.filter = 'alpha(opacity=100)';
				this.nextPic.style.opacity = 1;
				this.lastPic = this.nextPic;
			}
		} 
	};
}();

IOM.PWS.Home.SlideShow.preloadImgs = function() {
	var picsContainer = document.getElementById('divHomePicsContainer');
	for(var i = 1; i <= 8; i++) {
		var img = document.createElement('img');
		img.src = IOM.PWS.Home.imgURL + IOM.PWS.LangUC + '/slideShow_0' + i  + '_1256.gif';
		picsContainer.appendChild(img);
	}
};

IOM.PWS.Home.ScreenShot.preloadSmallImgs = function() {
	var picsContainer = document.getElementById('divHomePicsContainer');
	for(var i = 1; i <= 4; i++) {
		var imgUp = document.createElement('img');
		var imgDown = document.createElement('img');
		imgUp.src = IOM.PWS.Home.imgURL  + 'imgShot00' + i  + 'Up_1155.gif';
		imgDown.src = IOM.PWS.Home.imgURL  + 'imgShot00' + i  + 'Down_1155.gif';
		picsContainer.appendChild(imgUp);
		picsContainer.appendChild(imgDown);
	}
};

IOM.PWS.Home.ScreenShot.preloadLargeImgs = function() {
	var picsContainer = document.getElementById('divHomePicsContainer');
	for(var i = 1; i <= 4; i++) {
		var img = document.createElement('img');
		img.src = IOM.PWS.Home.imgURL + IOM.PWS.LangUC + '/imgShot00' + i  + 'Small_1036.jpg';
		picsContainer.appendChild(img);
	}
};

IOM.PWS.Home.Initialize = function() {
	IOM.PWS.Home.News.Initialize();
	IOM.PWS.Home.buttonInitialize();
	
	IOM.PWS.Home.SlideShow.preloadImgs();
	IOM.PWS.Home.SlideShow.layout();
	IOM.PWS.Home.ScreenShot.preloadSmallImgs();
	IOM.PWS.Home.ScreenShot.layout();
	
	window.setTimeout('IOM.PWS.Home.ScreenShot.preloadLargeImgs()', 10000);
};

window.onresize = function f() {
	if(document.getElementById('divScreenShotWindow')) {
		IOM.PWS.Home.ScreenShot.setWindowPosition('divScreenShotWindow');
	}
};
	
if(document.all) {
	window.attachEvent('onload', IOM.PWS.Home.Initialize);
}
else {
	window.addEventListener('load', IOM.PWS.Home.Initialize, false);
}
