IOM.HWS.Home				= {};
IOM.HWS.Home.SlideShow		= {};
IOM.HWS.Home.News			= {};
IOM.HWS.Home.Top5			= {};
IOM.HWS.Home.imgURL			= '../resource/image/Home/';
IOM.HWS.Home.screenshotURL	= '../resource/image/ScreenShot/allScreenShot/';
IOM.HWS.Home.Capture		= {};

IOM.HWS.Home.buttonInitialize = function() {
	//top 5 table item effect
	var itemTds = document.getElementsByTagName('td');
	for (var i = 0; i < itemTds.length; i++) {
		if(itemTds[i].className == 'tdTop5ItemText') {
			itemTds[i].onmouseout = function() {
				this.className = 'tdTop5ItemText';
			};
			itemTds[i].onmouseover = function() {
				this.className = 'tdTop5ItemTextOver';
			};
		}
	}
	
	if(document.getElementById('divTop5XPlatform')) {
		var divTop5XPlatform = document.getElementById('divTop5XPlatform');
		divTop5XPlatform.onclick = function() {
			IOM.HWS.Home.Top5.gotoPage('Xplatform');
		};
	}
	
	if(document.getElementById('divTop5Global')) {
		var divTop5Global = document.getElementById('divTop5Global');
		divTop5Global.onclick = function() {
			IOM.HWS.Home.Top5.gotoPage('Global');
		};
	}
	
	if(document.getElementById('divTop5NoPopup')) {
		var divTop5NoPopup = document.getElementById('divTop5NoPopup');
		divTop5NoPopup.onclick = function() {
			IOM.HWS.Home.Top5.gotoPage('NoPopup');
		};
	}
	
	if(document.getElementById('divTop5Quick')) {
		var divTop5Quick = document.getElementById('divTop5Quick');
		divTop5Quick.onclick = function() {
			IOM.HWS.Home.Top5.gotoPage('Quick');
		};
	}
	
	if(document.getElementById('divTop5MultMedia')) {
		var divTop5MultMedia = document.getElementById('divTop5MultMedia');
		divTop5MultMedia.onclick = function() {
			IOM.HWS.Home.Top5.gotoPage('Feature');
		};
	}
};

IOM.HWS.Home.Capture.createBtns = function() {
	var divHomeCaptureItems = document.createElement('div');
	divHomeCaptureItems.className = 'divHomeCaptureItems';
	if(document.getElementById('divHomeCaptureInner')) {
		var divHomeCaptureInner = document.getElementById('divHomeCaptureInner');
		divHomeCaptureInner.appendChild(divHomeCaptureItems);
	}
	else {
		return;
	}
	var getHomeCaptureBackground = function(_x, _y) {
		return 'transparent url(' + IOM.HWS.Home.imgURL + 'homeCaptureItemPics_347.gif) no-repeat ' + _x + 'px ' + _y + 'px';
	};

	for(var i = 0; i < 4; i++) {
		var item = document.createElement('input');
		item.type = 'button';
		item.className = 'divHomeCaptureItem';
		item.hideFocus = true;
		item.name = i;
		item.value = IOM.HWS.captureTitles[i];
		item.style.background = getHomeCaptureBackground(-i * 110, 0);
		item.onmouseout = function() {
			this.style.background = getHomeCaptureBackground(-this.name * 110, 0);
		};
		item.onmouseover = function() {
			this.style.background = getHomeCaptureBackground(-this.name * 110, -110);
		};
		item.onclick = function() {
			IOM.HWS.Home.Capture.action.showWindowPic(this.name);
		};
		divHomeCaptureItems.appendChild(item);
	}
	
	var divHomeCaptureLinkMoreWrap			= document.createElement('div');
	divHomeCaptureLinkMoreWrap.className	= 'divHomeCaptureLinkMoreWrap';
	divHomeCaptureInner.appendChild(divHomeCaptureLinkMoreWrap);
	var aHomeCaptureLinkMore		= document.createElement('a');
	aHomeCaptureLinkMore.className	= 'aHomepageLink';
	aHomeCaptureLinkMore.target		= '_self';
	aHomeCaptureLinkMore.innerHTML	= IOM.HWS.homeMore;
	aHomeCaptureLinkMore.href		= '../ScreenShot/screenShot.jsp?language=' + IOM.HWS.LangUC;
	divHomeCaptureLinkMoreWrap.appendChild(aHomeCaptureLinkMore);
};

IOM.HWS.Home.Capture.setWindowPosition = function(_id) {
	if(document.getElementById(_id)) {
		var container = document.getElementById(_id);
		var el = document.getElementById('divHomeCaptureWrap');
		var obj = new Object();
		obj.x = 0;
		obj.y = 0;
		if(document.all) { //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 - 520 + 'px';
		}
		else {
			obj.x = el.offsetLeft + 'px';
			obj.y = el.offsetTop - 520 + 'px';
		}
		container.style.top = obj.y;
		container.style.left = obj.x;
	} 
};

IOM.HWS.Home.Capture.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 divCaptureTitle			= document.createElement('div');
	divCaptureTitle.id			= 'divCaptureTitle';
	divCaptureTitle.className	= 'divScreenShotTitle';
	var divCaptureCloseBtn		= document.createElement('div');
	divCaptureCloseBtn.id		= 'divScreenShotCloseBtn';
	divCaptureCloseBtn.className	= 'divScreenShotCloseBtn divScreenShotCloseBtnUp';
	divCaptureCloseBtn.onclick	= function f() {
		IOM.HWS.Home.Capture.action.fadeOutContainer();
	};
	divCaptureCloseBtn.onmouseout = function f() {
		this.className = 'divScreenShotCloseBtn divScreenShotCloseBtnUp';
	};
	divCaptureCloseBtn.onmouseover = function f() {
		this.className = 'divScreenShotCloseBtn divScreenShotCloseBtnOver';
	};
	
	//create screenshot window container
	var divCaptureBigWindow = document.createElement('div');
	divCaptureBigWindow.className = 'divCaptureBigWindow';
	divCaptureBigWindow.id = 'divCaptureBigWindow';
	window.document.body.appendChild(divCaptureBigWindow);
	IOM.HWS.Home.Capture.setWindowPosition('divCaptureBigWindow');
	//create window container body
	var divCaptureBigWindowBody = document.createElement('div');
	divCaptureBigWindowBody.id = 'divCaptureBigWindowBody';
	divCaptureBigWindowBody.className = 'divCaptureBigWindowBody';
	cellMC.appendChild(divCaptureBigWindowBody);
	cellTC.appendChild(divCaptureTitle);
	cellTC.appendChild(divCaptureCloseBtn);
	divCaptureBigWindow.appendChild(tbl);
	//insert images to window body
	for(var i = 0; i < 4; i++) {
		var imgSmall = document.createElement('img');
		imgSmall.id = 'imgShot00' + i; 
		imgSmall.className = 'imgScreenShotSmall';
		imgSmall.src = IOM.HWS.Home.screenshotURL + IOM.HWS.LangUC + '/imgShot00' + (i + 1) + 'Big_585.jpg';
		divCaptureBigWindowBody.appendChild(imgSmall);
	}
};

IOM.HWS.Home.Capture.action = function() {
	return {
		window	:	null,
		total		:	100,
		step		:	0,
		opac		:	0,
		lastPic		:	null,
		nextPic		:	null,
		showWindowPic : function(_idx) {
			if(!document.getElementById('divCaptureBigWindow')) {
				IOM.HWS.Home.Capture.createWindow();
			}
			//get the window's title
			this.window = document.getElementById('divCaptureBigWindow');
			var divCaptureTitle = document.getElementById('divCaptureTitle');
			divCaptureTitle.innerHTML = IOM.HWS.captureTitles[_idx];
			//if windows is transparent, ready to fade in
			var flag = true;
			if(document.all) {	//isIE
				flag = (this.window.filters.alpha.Opacity <= 0);
			}
			else {
				flag = (this.window.style.opacity <= 0);
			}
			this.nextPic = document.getElementById('imgShot00' + _idx);
			if(flag) {
				IOM.HWS.Home.Capture.action.fadeInContainerWithIdx(_idx);	
			}
			else {
				IOM.HWS.Home.Capture.action.fadeInPic(_idx);
			}
		},
		fadeOutContainer : function() {
			this.opac = this.total - this.step;
			this.step += 10;
			this.window.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.window.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.HWS.Home.Capture.action.fadeOutContainer()', 40);
			}
			else {
				this.step = 0;
				window.document.body.removeChild(this.window);
			}
		},
		fadeInContainerWithIdx : function(_idx) {	
			this.opac = this.step;
			this.step += 10;
			this.window.style.filter = 'alpha(opacity=' + this.opac + ')';
			this.window.style.opacity = this.opac / 100;
			if(this.step < this.total) {
				setTimeout('IOM.HWS.Home.Capture.action.fadeInContainerWithIdx(' + _idx + ')', 40);
			}
			else {
				this.step = 0;
				this.window.style.filter = 'alpha(opacity=100)';
				this.window.style.opacity = 1;
//				this.nextPic = document.getElementById('imgShot00' + _idx);
				IOM.HWS.Home.Capture.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.HWS.Home.Capture.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.HWS.Home.Capture.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.HWS.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.HWS.Home.Top5.gotoPage = function(_a){
	window.top.location.href = '../Top5/top5.jsp#' + _a;
};

IOM.HWS.Home.News.gotoNewsContent = function(fileName){
	window.top.location.href = '../News/' + fileName + '.jsp?language=' + IOM.HWS.LangUC.toLowerCase();
};

IOM.HWS.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 (var 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( windowID, persistTime ){
			//get divs
			if( windowID ){
				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( windowID ).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.HWS.Home.SlideShow.action.slide()', this.persistTime );
				}
				else {
					setTimeout( 'IOM.HWS.Home.SlideShow.action.slide()', 1 );
				}
			}
		}
	};
}();

IOM.HWS.Home.SlideShow.layout = function() {
	//index function
	var selectPicByIdx = function() {
		var idxTemp = parseInt(this.innerHTML) - 1;
		IOM.HWS.Home.SlideShow.action.select(idxTemp);
	};
	var idxOnmouseover = function() {
		this.className = 'tdSlideShowIndexHover';
	};
	var idxOnmouseout = function() {
		this.className = 'tdSlideShowIndex';
	};
	
	//create temp window, and add pics to it
	var picsArray	= ['01', '03', '05', '06', '07'];
	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 (var 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.HWS.Home.imgURL + IOM.HWS.LangUC + '/slideShow_' + picsArray[i]  + '_465.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.HWS.Home.SlideShow.action.slide('divSlideShowContainer', 3000);
	}
};

IOM.HWS.Home.SlideShow.preloadImgs = function() {
	var picsContainer = document.getElementById('divHomePicsContainer');
	for(var i = 1; i <= 8; i++) {
		var img = document.createElement('img');
		img.src = IOM.HWS.Home.imgURL + IOM.HWS.LangUC + '/slideShow_0' + i +  '_465.gif';
		picsContainer.appendChild(img);
	}
};

IOM.HWS.Home.Initialize = function() {
	IOM.HWS.Home.News.Initialize();
	IOM.HWS.Home.buttonInitialize();
	IOM.HWS.Home.SlideShow.preloadImgs();
	IOM.HWS.Home.Capture.createBtns();
	IOM.HWS.Home.SlideShow.layout();
};

window.onresize = function f() {
	if(document.getElementById('divCaptureBigWindow')) {
		IOM.HWS.Home.Capture.setWindowPosition('divCaptureBigWindow');
	}
};
	
if(document.all) {
	window.attachEvent('onload', IOM.HWS.Home.Initialize);
}
else {
	window.addEventListener('load', IOM.HWS.Home.Initialize, false);
}

