﻿// ****************************************************************************
// ** Script for www.maxthon.cn v2.5
// ****************************************************************************
// @file    : script.js
// @codex   : Javascript v1.2
// @version : 2.5
// @author  : Jinjiang
// @update  : 2009-07-01 10:30 AM
// @note    : none
// ****************************************************************************


//----------------------------------------------------------------------------
// ** Init and build news list block. If the news list block hasn't been
// ** initialized, the pageOffset will be set to 0 and won't went the next page
// ** or the previous one.
//----------------------------------------------------------------------------
// @param <string>     strId      : the id of the news list block.
// @param <string>     arrData    : the data of the news.
// @param <boolean>    boolGoNext : decide go the next page or the previous one.
//----------------------------------------------------------------------------
function buildNewsList(strId, arrData, pageUnit, boolGoNext) {

	// confirm the list block is existed and init data
	var domRoot = document.getElementById(strId);
	if (!domRoot) return;
	if (!arrData || !arrData.length) arrData = [];
	pageUnit = pageUnit || 10;
	var itemLength = arrData.length;
	var pageCount = Math.floor(itemLength / pageUnit);

	var domList, domNav;
	if (!domRoot.list) {

		// initiation if the list block hasn't been initialized
		domRoot.list = domRoot.firstChild;
		domRoot.nav = domRoot.lastChild;
		domRoot.nav.firstChild.onclick = function() {
			this.blur();
			buildNewsList(strId, arrData, pageUnit, false);
			return false;
		};
		domRoot.nav.lastChild.onclick = function() {
			this.blur();
			buildNewsList(strId, arrData, pageUnit, true);
			return false;
		};
		var itemOffset = 0;
		domRoot.pageOffset = Math.floor(itemOffset / pageUnit);
	}
	else {

		// change pageOffset if the list block has been initialized
		var itemOffset = itemOffset + (boolGoNext ? 1 : -1) * pageUnit;
		domRoot.pageOffset = domRoot.pageOffset + (boolGoNext ? 1 : -1);
		if (domRoot.pageOffset < 0) {
			domRoot.pageOffset = 0;
		}
		if (domRoot.pageOffset >= pageCount) {
			domRoot.pageOffset = pageCount - 1;
		}
	}

	// build the list items
	domRoot.list.innerHTML = '';
	var itemOffset = domRoot.pageOffset * pageUnit;
	for (var i = 0; i < pageUnit; i++) {
		var temp = arrData[itemOffset + i];
		if (!temp) continue;
		var domItem = document.createElement('li');
		domRoot.list.appendChild(domItem);
		domItem.innerHTML = '<a href="' + temp[0] + '" title="' + temp[1] + '" target="_blank">' + temp[1] + '</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	if (domRoot.list.innerHTML == '')
		domRoot.list.innerHTML = '<li>数据载入失败！</li>';

	// update the mode of list navigator
	if (pageCount <= 1) {
		domRoot.nav.className = 'no-link';
	}
	else if (domRoot.pageOffset == 0) {
		domRoot.nav.className = 'next-link-only';
	}
	else if (domRoot.pageOffset == pageCount - 1) {
		domRoot.nav.className = 'prev-link-only';
	}
	else {
		domRoot.nav.className = '';
	}
}


//----------------------------------------------------------------------------
// ** Init and Q-n-A list block.
// ** Make troggle the answer content when click a certain question title.
//----------------------------------------------------------------------------
// @param <string>  strId   : the id of the Q-n-A list block
//----------------------------------------------------------------------------
function initQnaList(strId) {

	var domList = document.getElementById(strId);
	if (!domList) return;

	var length = domList.childNodes.length;
	for (var i = 0; i < length; i++) {

		var domItem = domList.childNodes[i];
		if (domItem && domItem.className == 'content-block') {

			domItem.domQuestion = domItem.firstChild;
			domItem.domAnswer = domItem.lastChild;
			domItem.domQuestion.boolExpanded = false;

			//----------------------------------------------------------------------------
			// ** troggle for the answers in each q-n-a block when click the question title.
			//----------------------------------------------------------------------------
			// @param <event>   evt : the event object.
			//----------------------------------------------------------------------------
			domItem.domQuestion.firstChild.onclick = function() {
				this.blur();
				this.parentNode.boolExpanded = !this.parentNode.boolExpanded;
				this.parentNode.parentNode.domAnswer.style.display = (this.parentNode.boolExpanded) ? 'block' : 'none';
				return false;
			};
		}
	}
}


//----------------------------------------------------------------------------
// ** Set the format of given text box
//----------------------------------------------------------------------------
function initFeatureSlide(strId) {

	// confirm the slide block is existed and init each feature-slide-preview elements
	var domRoot = document.getElementById('feature-slide-block');
	if (!domRoot) return;
	domRoot.list = [];
	var children = domRoot.childNodes;
	var offset = 0;
	for (var i in children) {
		var domItem = children[i];
		if (domItem && domItem.className == 'feature-slide-preview') {
			domRoot.list.push(domItem);
			domItem.offset = offset;
			offset++;
		}
	}

	// build the slide list items
	var domList = document.getElementById('feature-slide-list-items'); // domRoot.lastChild;
	if (!domList) return;
	domList.innerHTML = '';
	domList.items = [];
	for (var i = 0; i < domRoot.list.length; i++) {
		var temp = domRoot.list[i];
		var domItem = document.createElement('a');
		domList.appendChild(domItem);
		domItem.href = '#';
		domItem.onclick = function(){return false;}
		domList.items.push(domItem);
		domItem.offset = i;
	}

	// prev & next button events
	var domPreviousBtn = document.getElementById('feature-slide-list-previous');
	var domNextBtn = document.getElementById('feature-slide-list-next');
	domPreviousBtn.onclick = function(evt) {
		evt = evt || window.event;
		var target = evt.target || evt.srcElement;
		var offset = domList.current.offset;
		offset--;
		if (offset >= domList.items.length || offset < 0)
			offset = domList.items.length - 1;
		target.blur();
		doSlide(offset);
		return false;
	}
	domNextBtn.onclick = function(evt) {
		evt = evt || window.event;
		var target = evt.target || evt.srcElement;
		var offset = domList.current.offset;
		offset++;
		if (offset >= domList.items.length || offset < 0)
			offset = 0;
		target.blur();
		doSlide(offset);
		return false;
	}

	// init current slide
	domRoot.current = domRoot.list[0];
	domList.current = domList.items[0];
	domRoot.current.style.display = 'block';
	domList.current.className = 'current';
	
	
	//----------------------------------------------------------------------------
	// ** change the current feature preview element and do this per 3 seconds when
	// ** mouse not hover on it and no list item clicked during this time.
	// ** see the details of the params in the sub funtions' definition below.
	//----------------------------------------------------------------------------
	// @param <int>    offset   : the offset of the slide list, if this param is
	//                            null, the offset will be a random number.
	// @param <int>    timeStemp: the time stamp when the setTimeout function called.
	//----------------------------------------------------------------------------
	function doSlide(offset, timeStamp) {
		if (
			timeStamp &&
			(
				domRoot.boolHover ||
				timeStamp != domRoot.timeStamp
			)
		) return;

		if (typeof(offset) != 'number') {
			// offset = Math.floor(Math.random() * domList.items.length);
			offset = domList.current.offset - (-1);
			if (offset >= domList.items.length || offset < 0)
				offset = 0;
		}
		domRoot.current.style.display = 'none';
		domList.current.className = '';
		domRoot.current = domRoot.list[offset];
		domList.current = domList.items[offset];
		domRoot.current.style.display = 'block';
		domList.current.className = 'current';

		if (domRoot.boolHover) return;

		var now = new Date();
		domRoot.timeStamp = now.valueOf();

		window.setTimeout(function() {
			doSlide(null, now.valueOf());
		}, 5000);
	}

	//----------------------------------------------------------------------------
	// ** when hover on the list item, change the current feature preview with the
	// ** offset of the event target.
	//----------------------------------------------------------------------------
	// @param <event>   evt : the event object.
	//----------------------------------------------------------------------------
	domList.onmouseover =
	domList.onclick = function (evt) {
		evt = evt || window.event;
		var target = evt.target || evt.srcElement;
		while (target && target != domList) {
			if (target.tagName.toLowerCase() == 'a') {
				target.blur();
				doSlide(target.offset);
				return false;
			}
			target = target.parentNode;
		}
	}
	
	//----------------------------------------------------------------------------
	// ** stop sliding when the mouse move to/on the whole slide block.
	// ** domRoot.boolHover is used in doSlide function
	//----------------------------------------------------------------------------
	domRoot.onmouseover =
	domRoot.onmousemove = function() {
		domRoot.boolHover = true;
	}
	
	//----------------------------------------------------------------------------
	// ** reverse sliding when mouse not hover on the whole slide block
	//----------------------------------------------------------------------------
	// @param <event>   evt : the event object.
	//----------------------------------------------------------------------------
	domRoot.onmouseout = function(evt) {
		domRoot.boolHover = false;
		var now = new Date();
		domRoot.timeStamp = now.valueOf();
		window.setTimeout(function() {
			doSlide(null, now.valueOf());
		}, 5000);
	}
	
	// start sliding
	var now = new Date();
	domRoot.timeStamp = now.valueOf();
	window.setTimeout(function() {
		doSlide(null, now.valueOf());
	}, 5000);
}


//----------------------------------------------------------------------------
// ** confirm and do initiation from initMap for all webs
//----------------------------------------------------------------------------
function init() {
	var initMap = {
		'qna-body': function(){
			initQnaList('qna-list-block');
		},
		'awards-body': function(){
			buildNewsList('media-list-block', window.mediaData, 10);
		},
		'press-body': function(){
			buildNewsList('news-list-block', window.newsData, 10);
			buildNewsList('media-list-block', window.mediaData, 10);
		},
		'index-body': function(){
			initFeatureSlide();
			buildNewsList('news-list-block', window.newsData, 5);
			if (window.maxWebsite && maxWebsite.updateCounter)
				maxWebsite.updateCounter();
		}
	};
	var initKey = document.body.id;
	if (initKey && initMap[initKey])
		initMap[initKey]();
}


// runtime entrance here
window.onload = init;
