/*******动态下拉菜单*******
subMenu v1.0 -- 方便的导航菜单
------没剑(2008-1-17)------
$Date:2008年1月18日16:07:46
********动态下拉菜单******/
(function($){
$.fn.extend({
        submenu:function(opt,callback){
				if(this==null)return false;
                //参数初始化
				
                if(!opt) var opt={};
                var _this=$(this);
				var _This=_this.get(0);
				var submenu=this;
				/*参数定义*/
				this.oneSmOnly=opt.oneSmOnly!=null?opt.oneSmOnly:false				//单独拉开收起，如果是true的话，当前菜单只有一个展开项，默认为false
					,this.speed=opt.speed!=null?opt.speed:'300'						//数字越大，速度越慢,默认为300
					,this.expandNum=opt.expandNum!=null?opt.expandNum:1				//设置初始化时菜单展开的项，默认为第一项
					,this.savestatus=opt.savestatus!=null?opt.savestatus:true			//设置是否保存菜单展开状态，默认为保存
					,this.aHeight=25	//菜单项的默认高度
					,isFirst=true;	//是否为第一次加载(没有cookie的情况下)
				_this.addClass("submenu");//增加CSS
				if($.browser.msie && $.browser.version=="6.0"){
					//假如是ie6.0的浏览器,则为第一个菜单加上first-child类,因为ie6下不认first-child
					_this.find("div").eq(0).addClass("first-child");
				}
				if (submenu.savestatus)//如果选择了保存cookie状态，则去读菜单状态
				{
					var menuCookie=null;
					menuCookie=$.cookie("subMenu_" + encodeURIComponent(_This.id)+"_menuStatus");
					if (menuCookie!=null && menuCookie.length>0) {
							var states = menuCookie.split("");
							isFirst=false;
							_this.find("div").each(function(i){
								if (i>states.length)
									return false;
								if (states[i] == "0")
								{
									$(this).addClass("collapsed");
								}
							});
					}
				}else{
					//清空缓存
					$.cookie("subMenu_" + encodeURIComponent(_This.id)+"_menuStatus",null);
				}
				//取得一个A的高度
				_this.find("div > a").eq(0).each(function(){
					submenu.aHeight=this.offsetHeight;
					return false;
				});//取得一个链接的高度
				_this.find("div").each(function(i){	
					var div=$(this);
					if (isFirst && !(i==submenu.expandNum-1 || i==submenu.expandNum))//如果是第一次打开菜单，则显示默认展开的项
					{
						div.addClass("collapsed");
					}
					//绑定事件:标题点击时
					$(div).find("span").eq(0).mouseup(function(e){
						var ParentDiv=$(this).parent();
						var ParentDivDom=ParentDiv.get(0);
						if (ParentDiv.attr("class")=="collapsed" || ParentDiv.attr("class")=="first-child collapsed"){//状态为隐藏时
							collapseOthers();//隐藏其它已展开的菜单
							var menuCount=0;//子菜单的数量,这个决定父层要展开多大
							menuCount=ParentDiv.find("a").length;
							ParentDiv.animate({height: (Math.round((menuCount * submenu.aHeight)/3)+this.offsetHeight)},submenu.speed,"",function(){ParentDivDom.style.height = "";});
							//this.parentNode.style.height = "";
							ParentDiv.removeClass("collapsed");
						}else{
							collapseOthers(ParentDiv);
							ParentDiv.animate({height: this.offsetHeight+5},submenu.speed);
							ParentDiv.addClass("collapsed");
						}
						writeCookie();
					});
				});
				//把除展开的菜单外的其它展开的菜单合上
				collapseOthers = function(me){
					if(submenu.oneSmOnly==false)return;
					_this.find("div").not($(me)).not(".collapsed,first-child collapsed").each(function(){
							$(this).animate({height: this.offsetHeight},submenu.speed);
							$(this).addClass("collapsed");
					})
				};
				//记录菜单状态
				writeCookie = function(){
					//如果要保存cookie的话
					if (submenu.savestatus=="false"){
						//清空缓存
						if($.cookie("subMenu_" + encodeURIComponent(_This.id)+"_menuStatus")!=null)$.cookie("subMenu_" + encodeURIComponent(_This.id)+"_menuStatus",null);
						return;
					}
					var states = new Array();
					_this.find("div").each(function(i){	
						states.push($(this).attr("class")== "collapsed" ? 0 : 1);
					});
					var d = new Date();
					d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
					$.cookie("subMenu_" + encodeURIComponent(_This.id)+"_menuStatus",states.join(""),{expires: d});
				};
				//展开所有菜单项
				this.expandAll=function(){
					_this.find("div.collapsed").each(function(){
						var menuCount=0;//子菜单的数量,这个决定父层要展开多大
						menuCount=$(this).find("a").length;
						$(this).animate({height: (menuCount*submenu.aHeight)+this.offsetHeight},submenu.speed);	
						$(this).removeClass("collapsed");
					});
				};
				//收起所有菜单项
				this.collapseAll=function(){
					_this.find("div").not(".collapsed").each(function(){
						var menuCount=0;//子菜单的数量,这个决定父层要展开多大
						menuCount=$(this).find("a").length;
						$(this).animate({height: _this.find("div").find("span").eq(0).offsetHeight},submenu.speed);	
						$(this).addClass("collapsed");
					});
				};
				return this;
        }
})
})(jQuery);


menu_show={
    _menu_cookie:{"qa":0,"note":1,"exam":1},
    setCookie: function(Name, cookievalue, time){
		if(!time) time= 1.5 * 60 * 60 * 1000;
		time += 20 * 60 * 1000;
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + time);
        var newcookie = Name + "=" + escape(cookievalue)+";expires="+expDate.toGMTString();
        document.cookie = newcookie;
    },
    getCookie: function(name){
        var result = null;
        var myCookie = document.cookie + ";";
        var searchName = name + "=";
        var startOfCookie = myCookie.indexOf(searchName);
        var endOfCookie;
        if (startOfCookie != -1) {
            startOfCookie += searchName.length;
            endOfCookie = myCookie.indexOf(";", startOfCookie);
            result = unescape(myCookie.substring(startOfCookie, endOfCookie));
        }
        return result;
    },
	showAll:function(){
	  var _menu_cookie=jQuery.decode(this.getCookie("menu_c"));
	  if(_menu_cookie){
		  jQuery.each(_menu_cookie,function(k,v){
			  if(v){
				 menu_show.hide(k);
			  }else{
			     menu_show.show(k);
			  }
		  })
	  }
	},
	set_menu:function(id,stauts){
	 this._menu_cookie=jQuery.decode(this.getCookie("menu_c"));
	  if(!this._menu_cookie)
	  this._menu_cookie={};
	  this._menu_cookie[id]=stauts;
	  this.setCookie("menu_c",jQuery.encode(this._menu_cookie),30 * 24 * 60 * 60 * 1000);
	},
	hide:function(id){
		  jQuery('#menu_'+ id +'_icon').attr('src',"image/icon_add.gif");
		  jQuery('#menu_'+ id +'_ul').hide();
		  menu_show.set_menu( id ,1);
	},
	show:function(id){
		  jQuery('#menu_'+ id +'_icon').attr('src',"image/icon_less.gif");
		  jQuery('#menu_'+ id +'_ul').show();
		  menu_show.set_menu( id ,0);
	}
}

var xkb = {};
/* ajax得到数据 */
xkb.in_ajax = false;
xkb.bodyTop = function() {
	var bodyTop = 0;
	if (typeof window.pageYOffset != 'undefined') {
		bodyTop = window.pageYOffset;
	} else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
		bodyTop = document.documentElement.scrollTop;
	} else if (typeof document.body != 'undefined') {
		bodyTop = document.body.scrollTop;
	}
	return bodyTop;
};
xkb.onajax = function() {
	xkb.in_ajax = true;
	var bodyTop = xkb.bodyTop();
	if(typeof jQuery("#xkbLoading").attr("id") == 'undefined') {
		jQuery("body").append('<div id="xkbLoading" style="position: absolute; display:none;"><img src="/main/Public/shgao/images/loading.gif" alt="处理中……" /></div>');
	}
	jQuery("#xkbLoading").css("display", "block");
	jQuery("#xkbLoading").css("left", (document.body.clientWidth - 140)+'px');
	jQuery("#xkbLoading").css("top", bodyTop);
};
xkb.delajax = function() {
	xkb.in_ajax = false;
	jQuery("#xkbLoading").css("display", "none");
};
xkb.ajax = function(obj) {
	if(xkb.in_ajax) {
		alert("数据处理中..请稍等...") ;
		return false;
	}
	if(typeof obj != "object") return false;
	xkb.onajax();
	jQuery.ajax({
		type: typeof obj.type != 'undefined' ? obj.type : "POST",
		dataType: typeof obj.dataType != 'undefined' ? obj.dataType : "json",
		url: obj.url,
		data: obj.data,
		success: function(s) {
			xkb.delajax();
			obj.success(s);
		},
		error: function(s) {
			alert("通信错误..请刷新页面尝试!");
			xkb.delajax();
		}
	});
};

/* 显示登录框 */
xkb.showlogin = function(info, callback) {
	var bodyTop = 0;
	if (typeof window.pageYOffset != 'undefined') {
		bodyTop = window.pageYOffset;} else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
		bodyTop = document.documentElement.scrollTop;
	}
	else if (typeof document.body != 'undefined') {
		bodyTop = document.body.scrollTop;
	}

    xkb.bgShow();
	jQuery("#tcdiv_login").css("top", bodyTop+100).css("left", (window.screen.availWidth/2 - 180) +"px").fadeIn();
	
	jQuery("#topUserLogin").unbind("click");
	jQuery("#topUserLogin").click(function() {
		/* 顶部用户登录 */
		var top_username = jQuery.trim(jQuery("#top_username", "#tcdiv_login").val());
		var top_password = jQuery.trim(jQuery("#top_password", "#tcdiv_login").val());
		if(top_username == '') {
			alert("请填写用户名!");
			jQuery("#top_username", "#tcdiv_login").focus();
			return false;
		}
		if(top_password == '') {
			alert("请填写密码!");
			jQuery("#top_password", "#tcdiv_login").focus();
			return false;
		}
		xkb.ajax({
			url: "/main/user,dologin.html",
			data: "action=ajax&username="+top_username+"&passwd="+top_password,
			success: function(s) {
				if(!s.info.success) {
					alert(s.info.msg);
					return false;
				}
				if(callback && (typeof callback == 'object' || typeof callback == 'function')){
					xkb.hidelogin();
					callback();
					return true;
				}else {
					window.location.reload();
				}
			}
		});
	});
};
/* 登录 */
xkb.topgologin = function(info, maintype) {

};
/* 隐藏登录框 */
xkb.hidelogin = function() {
	jQuery('#tcdiv_login').hide();
	xkb.bgHide();
};

/* xkb.ajaxlogin */
xkb.ajaxlogin = function(callback) {
	xkb.showlogin('', callback);
};
/* xkb.fogetpasswd */
xkb.ajaxfogetpasswd = function() {
	xkb.showfogetpasswd('');
};

/* 顶部忘记密码 */
xkb.majaxfogetpasswd = function() {
	xkb.hidelogin();
	xkb.ajaxfogetpasswd();
};

//显示背景
xkb.bgShow = function() {
	if(typeof jQuery("#bodybg").attr("id") == 'undefined') {
		jQuery("body").append('<div id="bodybg" style="z-index:3;display:none; position:absolute; background:#000000;"></div>');
	}
	jQuery("#bodybg").css("top", 0).css("left", 0).css("width", document.body.scrollWidth ).css("height", document.body.scrollHeight ).css("opacity", 0.5).fadeIn();
	if(jQuery.browser.msie && jQuery.browser.version=="6.0") {
		if(typeof jQuery("#bodybg_iframe").attr("id") == 'undefined') {
			jQuery("body").append('<iframe id="bodybg_iframe" src="javascript:false;" style="opacity:0.1;display:none; position:absolute; background:#000000; z-index:2;"></iframe>');
		}
		jQuery("#bodybg_iframe").css("top", 0).css("left", 0).css("width", document.body.scrollWidth ).css("height", document.body.scrollHeight ).css("opacity", 0).fadeIn();
	}
};
//隐藏背景
xkb.bgHide=function(){
	if(jQuery("#bodybg")){
        jQuery("#bodybg").fadeOut();
	}
	if(jQuery("#bodybg_iframe")) {
		jQuery("#bodybg_iframe").hide();
	}
};

/* 显示登录框 */
xkb.showfogetpasswd = function(info, maintype) {
	var bodyTop = 0;
	if (typeof window.pageYOffset != 'undefined') {
		bodyTop = window.pageYOffset;} else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
		bodyTop = document.documentElement.scrollTop;
	}
	else if (typeof document.body != 'undefined') {
		bodyTop = document.body.scrollTop;
	}

	jQuery("select").hide();
	jQuery("#fgbgShow").css("top", 0).css("left", 0).css("width", document.body.scrollWidth ).css("height", document.body.scrollHeight ).css("opacity", 0.5).fadeIn();
	jQuery("#tcdiv_fogetpasswd").css("top", bodyTop+150).css("left", (window.screen.availWidth/2 - 240) +"px").fadeIn();
	
	jQuery("#topUserFotgetpasswd").unbind("click");
	jQuery("#topUserFotgetpasswd").click(function() {
		/* 顶部用户 */
		if(jQuery.trim(jQuery("input[name=f_username]", "#tcdiv_fogetpasswd").val()) == '') {
			alert('请填写用户名!');
			return false;
		}
		if(jQuery.trim(jQuery("input[name=f_email]", "#tcdiv_fogetpasswd").val()) == '') {
			alert('请填写Email!');
			return false;
		}
		xkb.ajax({
			url: "../main/user,redopasswd.html",
			data: "username="+jQuery("input[name=f_username]", "#tcdiv_fogetpasswd").val()+"&email="+jQuery("input[name=f_email]", "#tcdiv_fogetpasswd").val(),
			success: function(s) {
				alert(s.info);
				xkb.hidefogetpasswd();
			}
		});
		
		//alert('请致电021－64703333/64701333找回密码!');
	});
};
/* 隐藏登录框 */
xkb.hidefogetpasswd = function() {
	jQuery('#fgbgShow').hide();
	jQuery('#tcdiv_fogetpasswd').hide();
	jQuery("select").show();
};