﻿(function($){
	var path = $("script").eq(-1).attr("src");
	try { document.execCommand('BackgroundImageCache', false, true) } catch (e) { };
	//地址加亮
	$(function(){
		var index,url = document.URL.replace(/[^/]+\.html$/i,"");
		//移除js标签
		$("script").remove();
		
		//菜单加亮
		$("#naviagtion a").each(function(i){
			return index=i,this.href.replace(/[^/]+\.html$/i,"") != url
		}).eq(index).addClass("current");
		
		//文章侧边栏选中加亮
		$("div.parent dt.icon>a").each(function(i){
			return index = i,this.href != document.URL
		})
		.eq(index).addClass("selected")
		.parent().find("a").removeAttr("href");
		
		//首页选项卡
		/^http:\/\/[^/]+\/$/.test(url) && 
		(function(){
			var tab = $("div.servier") , title = "<li class='border'></li>" , cache , current = 0;
			if(tab.length<2) return tab=title=null;
			
			tab.find("h2").each(function(i){
				$(this).next("ul.ul").appendTo(tab.eq(0)).css("display","none");
				title +="<li id='lintop" + i+"'>" + $(this).remove().text() + "</li>";
			});
			tab.slice(1).remove();
			tab = tab.find("ul.ul");			
			cache = $("<ul/>").addClass("tab-title").html(title);
			
			tab.eq(0).css("display","block").before(cache);
			
			//选项卡优化
			cache = cache.find("li.border").width( $("#lintop0").width()).css("left",
			function(){
				$(this.parentNode).mouseover(function(event){
					var  el = event.target;
					if(el.nodeName != "LI" || el == cache[0]){ return false};
					tab.eq(current).hide();
					current = el.id.replace(/^lintop/,"");
					tab.eq(current).show();
					cache.width( el.clientWidth ).animate({left: $(el).position().left});
				});
				return $("#lintop0").position().left;
			});
		})();
		
		//中文简繁体转换
		document.getElementById("TraditionalSimplified") && (function(){
			var zh_cn = "",type = ["simple","traditional"],
			//读取cookie
			cookie = /(^|; )zh_cn_traditional=true($|;)/.test(document.cookie),
			//字符转换
			toChange = function(string){
				var pos , ch , result = "", n , m;
				cookie ? ( n = "traditional", m = "simple") : ( m = "traditional", n = "simple");
				for(var i=0,len=string.length;i<len;i++){
					ch = string.charAt(i);		
					result += string.charCodeAt(i) >19974  && 
					( pos = zh_cn[m].indexOf(ch) ) != -1 ? zh_cn[n].charAt( pos ) : ch;
				};
				return result;
			},
			
			//遍历节点
			traversal = function( dom ){
				var dom = dom.childNodes;
						
				for(var i=0,len = dom.length;i<len;i++){
					switch(dom[i].nodeType){
						case 3 : 
							dom[i].data = toChange( dom[i].data );break;
						
						case 1 :
							//元素提示文本
							dom[i].title && (dom[i].title = toChange(dom[i].title) );
							
							//图片alt
							dom[i].alt && (dom[i].alt = toChange(dom[i].alt) );
							
							//交互元素值
							dom[i].value && ( dom[i].value = toChange( dom[i].value ) );
							
							//调用所有元素
							arguments.callee( dom[i] ); break; 
					}; //switch
				};//for
				return false;
			};
			//获取字库
			$.get(path.replace(/\w+\.js(\?.*)?/i,"zh-cn.txt"),null,function(json){
				if(!json || !json.simple) return;
				zh_cn = json;
				//cookie && traversal();
				var json = $("#TraditionalSimplified").click(function(){
					cookie = !cookie;
					document.cookie = "zh_cn_traditional=" + cookie +";path=/" ;
					traversal(document.documentElement);
					this.innerHTML = (cookie ? "简体" :"繁體" ) +"中文";
					return false;
				});
				
				cookie &&$("#TraditionalSimplified").html("简体中文")&&traversal(document.documentElement);
			},"json");
		})();
		
		//图片切换效果 
		document.getElementById("flash") && (function(){
			var resize = function(){
				var parent = this.parentNode;
				
				this.width < parent.clientWidth || this.height< parent.clinetHeight ? 
				
				(this.width = parent.clientWidth,this.height = parent.clientHeight) :
				
				(parent.style.backgroundImage = "url(" + this.src + ")",$(parent).attr("longdesc",$("img[longdesc]",$(parent)).attr("longdesc")),parent.removeChild(this));
			},
			
			move = function(id ){
				if(being) return cache = id; being = true;
				
				id = id && isNaN(id) ? parseInt( id.substring( guid ) ) : (index + 1)  % small.length ;
				
				small.eq(index).css("borderColor", "#fff");
				
				small.eq(index = id).css("borderColor", "#9f6");
				
				mover.animate({ "margin-top": -index * 250}, {
					complete: function() {
						being = false;
						isNaN(cache) ? ( move(cache) , cache = 0 ) : (clearTimeout(timer) , timer = setTimeout(move, delay) );
					},
					duration: 1000
				});
			},
			
			guid = "lyq" + (new Date).getTime(),
			
			being = false,
			
			delay = 8000,
			
			index = 0,
			
			timer,
			
			cache = 0,
			
			mover = $("#flash ul.big li:eq(0)"),
			
			small = $("<ul/>").addClass("small").mouseover(function(event){
				if(event.target.nodeName == "LI" ) move(event.target.id);
			});
			
			$("#flash").append(small).append($("<div/>").addClass("opacity").css("opacity", 0.35));
			
			$("#flash img").each(function(i){
				i % 2 && $("<li/>").appendTo(small).append(this).attr("id",guid + (i - 1) / 2);
				
				this.complete ? resize.apply(this) : $(this).one("load",resize);
			});

			//----------单击大图打开新窗口 seven 20111010----------------
			$("#flash .big li[longdesc]").css({"cursor":"pointer"});	
			$("#flash .big li[longdesc]").click(function(){
				var href= $(this).attr("longdesc");
				window.open(href);
			});

			$("#flash .big li:last img[longdesc]").css({"cursor":"pointer"});	
			$("#flash .big li:last img[longdesc]").click(function(){
				var href= $(this).attr("longdesc");
				window.open(href);
			});
                        //----------单击大图打开新窗口 seven 20111010----------------


			small = small.find("li");
			
			small[0].style.borderColor = "#9f6";
			
			guid = guid.length;
			
			timer = window.setTimeout(move, delay);
		})();

	});
	//window.jQuery = window.$ = null;

})(window.jQuery);
