// ****************************************************************************

// ** 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>';

	}

	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;


http://www.maxthon.cn/ap/checkVersion.js

////////////////////////////////////////////////////////////////////

// Maxthon Version Checker

////////////////////////////////////////////////////////////////////

var max_verCheck = {};



max_verCheck.data = {

	ver1: "1.6.7.34",

	ver2: "2.5.11.3390",

	download1: "/site/download.htm#classic",

	download2: "/site/download.htm",

	txtNotInstalled: "Aucune !",

	package_v1: 'http://download.maxthon.com/cn/mx1/msetup_2008cn.exe',

	package_v2: 'http://dl.maxthon.cn/cn/mx2/mx_2.5.4.159cn.exe',

	updated_v1: '2008-09-28',

	updated_v2: '2009-08-04'

};



max_verCheck.config = {

	useLink: true,

	ver0ID: "maxthonVer0",

	ver1ID: "maxthonVer1",

	ver2ID: "maxthonVer2"

};



max_verCheck.run = function(params){



	if(params){

		if((typeof param["useLink"]) != "undefined") max_verCheck.config["useLink"] = param["useLink"];

		if((typeof param["ver0ID"]) != "undefined") max_verCheck.config["ver0ID"] = param["ver0ID"];

		if((typeof param["ver1ID"]) != "undefined") max_verCheck.config["ver1ID"] = param["ver1ID"];

		if((typeof param["ver2ID"]) != "undefined") max_verCheck.config["ver2ID"] = param["ver2ID"];

	}



	max_verCheck.showLatestVersions();

	max_verCheck.checkVersion();



}





max_verCheck.showLatestVersions = function(){



	var obj = document.getElementById(max_verCheck.config["ver1ID"]);

	if(obj){

		if(max_verCheck.config["useLink"]){

			obj.innerHTML = '<a href="' + max_verCheck.data["download1"] + '" target="_blank">' + max_verCheck.data["ver1"] + '</a>';

		}else{

			obj.innerHTML = max_verCheck.data["ver1"];

		}

	}



	var obj = document.getElementById(max_verCheck.config["ver2ID"]);

	if(obj){

		if(max_verCheck.config["useLink"]){

			obj.innerHTML = '<a href="' + max_verCheck.data["download2"] + '" target="_blank">' + max_verCheck.data["ver2"] + '</a>';

		}else{

			obj.innerHTML = max_verCheck.data["ver2"];

		}

	}



}





max_verCheck.checkVersion = function(){



	var obj = document.getElementById(max_verCheck.config["ver0ID"]);

	if(!obj) return;



	var currentVersion;

	var latestVersion;



	try{

		currentVersion = external.max_version;

	}catch(e){}



	if(!currentVersion)  currentVersion = "0";

	if(currentVersion.indexOf("1.")==0){

		latestVersion = max_verCheck.data["ver1"];

	}else{

		latestVersion = max_verCheck.data["ver2"];

	}



	var result = 0;

	if(currentVersion == "0"){

		result = 0;

		currentVersion =  max_verCheck.data["txtNotInstalled"];

	}else{



		// Compare Version

		var latestVer = latestVersion.split(".");

		var currentVer = currentVersion.split(".");



		for(var i=latestVer.length - 1; i>-1; i--){



			latestVer[i] = parseInt(latestVer[i], 10);

			currentVer[i] = parseInt(currentVer[i], 10);



			if(latestVer[i]>currentVer[i]){

				result = 1;

			}else if(latestVer[i]<currentVer[i]){

				result = -1;

			}



		}



	}



	// Output result

	if(result == -1){

		obj.style.color = "#669900";

	}else if(result == 1){

		obj.style.color = "#CC0000";

	}else{

		obj.style.color = "#999999";

	}



	obj.innerHTML = currentVersion;



}

