// JavaScript Document

//Onload初始化
$(document).ready(function() {

	
	initDisplayBox();

	initLanguageMenu();
	
	//initFontSize();
	initTopBorder ();
	initPageContentHeight();
	//heightStopTimer = window.setInterval(initPageContentHeight, 100);
	//nextDisplayImg();
	
	addShortcutIcon();
	
	//removeListSpans();
	
});

$(window).resize(function() {
	initLanguageMenu();
});

// 全局参数

/**
 * 设定当前程序是否处于调试状态 
 * 作者: 唐林华
 */
var isDebug = false;

var heightStopTimer = null;
var heightStopCount = 0;

var shortcutIcon = "images/common/favicon.ico";

/**
 * 配置首页产品与服务展示的图片与链接 
 * 作者:唐林华
 */
var homeDisplayImages = [ {
	"link" : "develop.html", // 点击图片后的链接目标
	"linkImg" : "display_button_1.jpg",// 按钮没有选中的图片
	"linkHoverImg" : "display_button_1_hover.jpg",// 按钮选中之后的图片
	"displayImg" : "Imgshow1.jpg"// 展示图片
}, {
	"link" : "maintain.html",
	"linkImg" : "display_button_3.jpg",
	"linkHoverImg" : "display_button_3_hover.jpg",
	"displayImg" : "Imgshow3.jpg"
},  {
	"link" : "internal_service.html",
	"linkImg" : "display_button_2.jpg",
	"linkHoverImg" : "display_button_2_hover.jpg",
	"displayImg" : "Imgshow2.jpg"
} ];

var homeDisplayImgTimer = null;
var homeDisplayImgIndex = 0;
var homeDisplayImgInterval = 2000;
var homeDisplayImgDone = false;

/**
 * 功能: 显示下一个展示图片 
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function nextDisplayImg() {

	homeDisplayImgIndex++;

	if (homeDisplayImgDone) {
		clearInterval(homeDisplayImgTimer);
		return;
	}

	if (homeDisplayImgIndex >= homeDisplayImages.length) {
		homeDisplayImgIndex = 0;
		homeDisplayImgDone = true;
	}

	$("#displayNavList > li > a").each(function(j) {
		if (j == homeDisplayImgIndex) {
			// $(this).trigger("mouseover");
			updateDisplayImg(j);
		}
	});
}

var langEventInited = false;

/**
 * 功能: 初始化语言选择按钮 
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function initLanguageMenu() {
	var ls = $("#languageSelectList");

	if (ls) {
		
		var button = $("#languageSelect");
		var lanlist = $("#languageList");
		var offset = button.offset();
		var p = button.position();
		
		if (!langEventInited) {
			
			if (window.location.href.indexOf("cn") != -1) {
				button.attr("href", window.location.href.replace("/cn/", "/jp/"));
				button.attr("title", "日本語");
				button.text("日本語");
			}
			
			if (window.location.href.indexOf("jp") != -1) {
				button.attr("href", window.location.href.replace("/jp/", "/cn/"));
				button.text("中国語");
			}
			
			
			lanlist.find("a").each(function (){
				
				if ($(this).text() == "日本語") {
						if (window.location.href.indexOf("cn") != -1) {
							$(this).attr("href", window.location.href.replace("/cn/", "/jp/"));
						}
				} else {
						if (window.location.href.indexOf("jp") != -1) {
							$(this).attr("href", window.location.href.replace("/jp/", "/cn/"));
						}
				}
				
			});
			
			langEventInited = true;
		}
		

		ls.hover(function() {

			lanlist.css( {
				"left" : p.left + "px",
				"top" : (p.top + button.outerHeight()) + "px"
			});
			//lanlist.slideDown("slow");
			lanlist.show();

		}, function(ev) {
			if (isMouseOut(ev.relatedTarget)) {
				//lanlist.slideUp("slow");
				//lanlist.css("display","none");
				lanlist.hide();
			} else {

				lanlist.mouseout(function(ev) {
					// debug("Mouse out #languageList");
						if (isMouseOut(ev.relatedTarget)) {
							//lanlist.slideUp("slow");
							//lanlist.css("display","none");
							lanlist.hide();
						}
					});

			}
		});

		var isMouseOut = function _isMouseOut(el) {

			// debug("Type of el: " + (typeof el));

			if (!el)
				return true;

			if (el.tagName == "body")
				return true;

			if (el.id
					&& (el.id == "languageSelectList" || el.id == "languageList"))
				return false;

			// debug("Type of _isMouseOut: " + (typeof _isMouseOut));

			return _isMouseOut(el.parentNode);
		};

	}
}

/**
 * 功能: 初始化首页产品与服务展示 
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function initDisplayBox() {

	if (!$("#displayBox"))
		return;
	
	//Init static data
	var baseDir = "images/cn/";
	$("#displayNavList > li > a > img").each(function(j) {
		if (j==0) {
			$(this).attr("src", baseDir + homeDisplayImages[j].linkHoverImg);
		} else {
			$(this).attr("src", baseDir + homeDisplayImages[j].linkImg);
		}
	});
	
	$("#displayLeft > a").attr("href", homeDisplayImages[0].link);
	var disImg = $("#displayLeft > a > img");
	disImg.attr("src", baseDir + homeDisplayImages[0].displayImg);
	//#Init static data
	
	$("#displayNavList > li > a").each(function(i) {

		$(this).hover(function() {
			// update(i);
				updateDisplayImg(i);
				// clearInterval(homeDisplayImgTimer);
			}, function() {
				// homeDisplayImgTimer = setInterval(nextDisplayImg,
				// homeDisplayImgInterval);
			});

	});

	$("#displayBox").hover(
			function() {
				clearInterval(homeDisplayImgTimer);
			},
			function() {
				homeDisplayImgTimer = setInterval(nextDisplayImg,
						homeDisplayImgInterval);
			});

	//nextDisplayImg();
	// Autorun Display
	homeDisplayImgTimer = setInterval(nextDisplayImg, homeDisplayImgInterval);

}

/**
 * 功能: 当前展示项目索引
 * 作者: 唐林华 
 */
var currentIndex = 0;

/**
 * 功能: 初始化首页产品与服务展示 
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function updateDisplayImg(i) {

	var baseDir = "images/cn/";

	var data = homeDisplayImages;

	var displayBox = $("#displayBox");

	// var currentIndex = updateCurrentIndex;

	var update = function(i) {

		if (currentIndex == i)
			return;

		// debug(" Img: " + $("#displayLeft > a > img").attr("src"));

		$("#displayLeft > a").attr("href", data[i].link);

		var disImg = $("#displayLeft > a > img");
		disImg.attr("src", baseDir + data[i].displayImg);
		animate(disImg);

		$("#displayNavList > li > a").each(function(j) {

			// debug("CurentIndex: " + currentIndex);

				if (j == currentIndex) {
					var img = $(this).find("img");
					img.attr("src", baseDir + data[j].linkImg);
					// img.css("display", "none");
				}

				if (j == i) {
					var img = $(this).find("img");
					img.attr("src", baseDir + data[j].linkHoverImg);
					animate(img, 4);
				}

			});

		currentIndex = i;
		// debug("AAA: " + $("#displayNavList > li > a")[i]);

	}// update

	var animate = function(el) {

		if (!el)
			return;

		el.css( {
			"display" : "none"
		});
		

		var type = Math.floor(Math.random() * 3) + 1;
		
		//add#2009-09-27#tanglinhua#f3line
		if (arguments[1] && arguments[1] > 0) {
			type = arguments[1];
		}
		
		// debug("Animate Type: " + type);

		switch (type) {
		case 2: {
			el.fadeIn("slow");
			break;
		}

		case 1: {
			/* el.css( {
				"width" : "1000px",
				"opacity" : 0.5
			});

			el.animate( {
				"width" : displayBox.width(),
				"opacity" : 1
			}, {
				duration : "slow"
			}); */
			el.fadeIn("slow");
			break;
		}
		
		//For display navigation only.
		case 4: {
			el.css( {
				"width" : "200px",
				"opacity" : 0.5
			});

			el.animate( {
				"width" : 200,
				"opacity" : 1
			}, {
				duration : "slow"
			});
			break;
		}

		default: {
			/* el.css( {
				"margin-left" : Math.floor(displayBox.width() / 2) + "px",
				"margin-top" : "100px",
				"height" : "1px",
				"width" : "1px",
				"opacity" : "0.5"
			});

			el.animate( {
				"marginLeft" : "0px",
				"marginTop" : "0px",
				"height" : displayBox.height(),
				"width" : displayBox.width(),
				"opacity" : 1
			}, {
				duration : "slow"
			}); */
			el.fadeIn("slow");
			break;
		}
		}// switch
	}// animate

	update(i);
}

/**
 * 功能: 用于输出调试信息，在装有Firebug的Firefox上， 信息将输出到firebug 控制台，否则以alert方式弹出
 * 
 * 作者: 唐林华 
 * 参数：msg, 将要输出的信息 
 * 返回值: 无
 */
function debug(msg) {

	if (!isDebug)
		return;

	if (window.console) {
		window.console.debug(msg);
	} else {
		alert(msg);
	}
}


var bigFont = null;
var bigFontCookie = "bigFont";
var fontSwitchLoaded = false;
var fontImg = ["images/cn/bigfont.gif","images/cn/smallfont.gif"];

/**
 * 功能: 切换字体大小 
 * 作者: 唐林华 
 * 参数：fValue - 字体大小状态，1表示小字体，2表示大字体
 * 返回值: 无
 */
function chgfSize(fValue) {

	/*if ($("#pageContent") && $("#pageContent").height() == 400) {
		
		document.getElementById("pageContent").style.height = "";
		
		debug(" > Height: " + $("#pageContent").height());
		
		//initPageContentHeight();
	}*/
	
	
	
	if (fValue == 2) {

		if (!bigFont) {
			bigFont = document.createElement("link");

			bigFont.setAttribute("type", "text/css");
			bigFont.setAttribute("rel", "stylesheet");
			bigFont.setAttribute("href", "css/bigFont.css");

			document.getElementsByTagName("head")[0].appendChild(bigFont);
			_setCookie(true);

		} else {

			bigFont.disabled = false;
			_setCookie(true);

		}

		
		
	} else {

		if (bigFont) {
			_setCookie(false);
			bigFont.disabled = true;
		}
		
	}
	
	//initPageContentHeight();
	
	function _setCookie(value) {
		setCookie(bigFontCookie, value);
	}
}

/**
 * 功能: 获得指定名称的Cookie值 
 * 作者: 唐林华 
 * 参数：name - Cookie名称
 * 返回值: Cookie值
 */
function getCookieValue(name) {

	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;

	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}

	if (start == -1)
		return null;

	var end = document.cookie.indexOf(";", len);

	if (end == -1)
		end = document.cookie.length;

	return unescape(document.cookie.substring(len, end));
}

/**
 * 功能: 设置Cookie值
 * 作者: 唐林华 
 * 参数：cName - Cookie 名称； cValue - Cookie值
 * 返回值: 无
 */
function setCookie(cName, cValue) {
	document.cookie = cName + "=" + cValue;
}

/**
 * 功能: 初始化页面字体大小
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function initFontSize() {
	var fCookie = getCookieValue(bigFontCookie);
	
	(new Image()).src = fontImg[0];
	(new Image()).src = fontImg[1];
	
	if (!fontSwitchLoaded) {
		//debug("Loading Font Switch: " + $("#logoLink").parent().get(0).tagName);
		
		var a = document.createElement("a");
		var img = document.createElement("img");
		
		a.setAttribute("href", "JavaScript:void(0);");
		a.setAttribute("id", "fontChange");
		a.setAttribute("class", "blockSpan");
		
		if (fCookie && fCookie == "true") {
			img.setAttribute("src", fontImg[1]);
		} else {
			img.setAttribute("src", fontImg[0]);
		}
		
		img.setAttribute("alt", "");
		
		a.appendChild(img);
		
		$(a).click(function () {
			var fc = getCookieValue(bigFontCookie);
			//debug("OK: " + fc);
			if (fc && fc == "true") {
				chgfSize (1);
				img.setAttribute("src", fontImg[0]);
			} else {
				chgfSize (2);
				img.setAttribute("src", fontImg[1]);
			}
			
		});
		
		$("#logoLink").parent().get(0).appendChild(a);
		
	}
	
	if (fCookie && fCookie == "true") {
		chgfSize(2);
	}
	
	//initLanguageMenu();
}

/**
 * 功能: 更改网页语言。
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function languageChange() {
	//var directionPage; // 将要跳转的页面
	var currentPage = document.location.href; // 获得当前页面的URL
	var parameter = arguments[0];

	//debug(parameter);

	if (currentPage.indexOf("/" + parameter + "/") != -1) return;
	
	//debug(currentPage.replace((parameter=="cn" ? "jp" : "cn"),parameter));
	
	//window.location = currentPage.replace((parameter=="cn" ? "jp" : "cn"),parameter);
}

var heightLock = false;
/**
 * 功能: 初始化内容页面内容框的高度
 * 作者: 唐林华 
 * 参数：无 
 * 返回值: 无
 */
function initPageContentHeight () {
	
	//var heightStopTimer = null;
	//var heightStopCount = 0;
	if ((++heightStopCount) == 1) {
		clearInterval(heightStopTimer);
		debug("Height Stoped.");
	}
	
	if ((!heightLock) && $("#pageContent") 
			&& $("#pageContent").height() == 400) {
		heightLock = true;
		document.getElementById("pageContent").style.height = "";
		return;
	}
	
	if ($("#pageContent") && $("#pageContent").height() < 400) {
		heightLock = false;
		$("#pageContent").css("height", "400px");
		
	}
}


/*
 * ########################################################### # # 
 * 功能: 打开视频窗口 #
 * 参数: none # 
 * 返回值: none # 
 * 作者: 胡剑飞 # 
 * 创建日期: 2009/07/31 #
 * ###########################################################
 */

function openVideoWindow() {
	videoWindow = window.open('video.html', 'video', 'height=330,width=320');
	videoWindow.moveTo(screen.width / 2 - 216, screen.Height / 2 - 200);
}

/*
 * ########################################################### # # 功能:
 * 加载Flash播放器 # 
 * 参数: none # 
 * 返回值: none # 
 * 作者: 胡剑飞 # 
 * 创建日期: 2009/07/31 #
 * ###########################################################
 */

function loadPlayer() {

	// -----------------------------------------------------------------------------
	// Globals
	// Major version of Flash required
	var requiredMajorVersion = 8;
	// Minor version of Flash required
	var requiredMinorVersion = 0;
	// Revision of Flash required
	var requiredRevision = 0;
	// -----------------------------------------------------------------------------

	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		var hasRightVersion = DetectFlashVer(requiredMajorVersion,
				requiredMinorVersion, requiredRevision);
		if (hasRightVersion) { // if we've detected an acceptable version
			// embed the flash movie
			AC_FL_RunContent(
					'codebase',
					'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
					'width', '320', 'height', '284', 'src',
					'../cn/video/videoPlayer?videoPath=video.flv', 'quality',
					'high', 'pluginspage',
					'http://www.macromedia.com/go/getflashplayer', 'align',
					'middle', 'play', 'true', 'loop', 'false', 'scale',
					'noscale', 'wmode', 'window', 'devicefont', 'false', 'id',
					'compnentVersion', 'bgcolor', '#000000', 'name',
					'videoPlayer', 'menu', 'true', 'allowScriptAccess',
					'sameDomain', 'allowFullScreen', 'false', 'movie',
					'../cn/video/videoPlayer?videoPath=video.flv', 'salign',
					'lt'); // end AC code
		} else { // flash is too old or we can't detect the plugin
			var alternateContent = '<div id="getPlayer">'
					+ '<p>您的浏览器需要安装必备插件Flashplayer才能够正常显示本页面内容</p>'
					+ '<p>This content requires the Adobe Flash Player.</p><p>&nbsp;</p>'
					+ '<p><a href=http://www.macromedia.com/go/getflash/>获得插件</a></p>'
					+ '<a href=http://www.macromedia.com/go/getflash/><img src="../image/common/getFlashPlayer_bt.gif" width="88" height="31" /></a>'
					+ '</div>';
			document.write(alternateContent); // insert non-flash content
		}
	}
}

function addShortcutIcon () {
	
	//<link rel="shortcut icon" href="favicon.ico" />
	//shortcutIcon
	
	
	
	var sIcon = document.createElement("link");

	//sIcon.setAttribute("type", "image/gif");
	sIcon.setAttribute("rel", "shortcut icon");
	sIcon.setAttribute("href", shortcutIcon);

	document.getElementsByTagName("head")[0].appendChild(sIcon);
	debug("Add Icon");
}


/**
 * Get the main version number of IE.
 * 
 * @author James Tang.
 * @return version number of IE, -1 if the browser is not IE
 */
function getIEVersion () {
	var nav = navigator.appVersion;
	
	if (nav.indexOf("MSIE") != -1) {
		
		return parseInt($.trim(nav.substring(nav.indexOf("MSIE") + 4, nav.length)).charAt(0));
		
	}
	
	return -1;
}

function removeListSpans () {
	var list = "";
		
	$(".homeDataList").find("span").each(function (i) {
		
		list += $(this).html();
		
	});
	
	$("#homeDataList").html(list);
}

function initTopBorder () {
	
	if (getIEVersion () == 8) {
		//alert("IE 8");
		//$("#topPanel").css({"marginTop":"0px"});
		
	}
	
}

