﻿function PostBackUrl(Url){
    //var _form=jQuery("form");
    //_form.attr("action",Url);
    //_form.submit();
    var _gc=jQuery("#ctl00_search_gameSelect").val();_gc=(undefined==_gc?"0":_gc);
    var _dc=jQuery("#ctl00_search_districtSelect").val();_dc=(undefined==_dc?"0":_dc);
    var _sc=jQuery("#ctl00_serverSelect").val();_sc=(undefined==_sc?"0":_sc);
    var _mc=jQuery("#ctl00_itemsSelect").val();_mc=(undefined==_mc?"0":_mc);
    var _url=Url+"?ctl00$search_gameSelect="+_gc+
        "&ctl00$search_districtSelect="+_dc+
        "&ctl00$search_serverSelect="+_sc+
        "&ctl00$search_itemsSelect="+_mc+
        "&TopSearchType="+jQuery("#TopSearchType").val()+
        "&ctl00$KW="+encodeURIComponent(jQuery("#ctl00_KW").val());
    top.location.href=_url;
}

var topSearch = {
    GameListSelect : null,
    GameItemSelect : null,
    GameDistrictSelect : null,
    GameServerSelect : null,
    GameIDCard : null,
    MerIsCate : "",
    AjaxPath : "/",
    ModelExt : ".aspx",
	//添加关注的游戏，sN:游戏名称,游戏id
	sG_AddCookie : function (sN,catagoryid){
		var oldCookie=cookie.get("mySelectCatagoryList");
		var newCookie=sN+","+catagoryid;
		//先将当前选择的游戏加入Cookie
		cookie.set("mySelectCatagory",newCookie);
		newCookie += ((null!=oldCookie)?(
			(-1==oldCookie.indexOf(newCookie))?"|||"+oldCookie:"|||"+oldCookie.replace(newCookie+"|||","")
			):"|||");
		//更新所有选择的游戏Cookie
		cookie.set("mySelectCatagoryList",newCookie,"7d");
		this.showMySelectGame();
	},
	clearCookie : function(){
	    cookie.del("mySelectCatagoryList");
	    jQuery("#search_selectGameDiv_mySelect").find("span").html("");
	},

	//将iframe放置在游戏列表下面
	ShowSelectGameFrame : function (obj)
	{
		if("none"!=obj.style.display){
			SelectGameFrame.style.display="";
			SelectGameFrame.style.width=obj.offsetWidth;
			SelectGameFrame.style.height=obj.offsetHeight;
			SelectGameFrame.style.left=obj.offsetLeft;
			SelectGameFrame.style.top=obj.offsetTop;
		}
	},
	HideSelectGameFrame : function (){
		SelectGameFrame.style.display="none";
	},
    SearchKW : function(obj){
        if("请输入商品标题或编号"==obj.value){
            obj.value="";
            obj.style.color="#000000";
        }else if(""==obj.value){
            obj.value="请输入商品标题或编号";
            obj.style.color="#999999";
        }
    },
	//显示/隐藏所有游戏列表
	showAndHideSelectGame : function (){
		var hiddenSelectGameDiv=jQuery("#search_selectGameDiv");
		if("none"==hiddenSelectGameDiv.css("display")){
			hiddenSelectGameDiv.show();
			var _tempObj=jQuery("#search_selectGame");
			hiddenSelectGameDiv.css("left",_tempObj.offset().left);
			hiddenSelectGameDiv.css("top",_tempObj.offset().top+_tempObj.height()+1);
			//alert(jQuery("#search_selectGame").offset().left);
			this.ShowSelectGameFrame(hiddenSelectGameDiv[0]);
		}else{
			hiddenSelectGameDiv.hide();
			this.HideSelectGameFrame();
		}
	},
	//延时隐藏选择游戏列表
	hideGameTime : null,
	hideSelectGame : function (){
		var hiddenSelectGameDiv=jQuery("#search_selectGameDiv");
		if("block"==hiddenSelectGameDiv.css("display")){
			this.hideGameTime=setTimeout(function(){topSearch.showAndHideSelectGame();},50);
		}
	},
	clearHideGameTime : function(){
		clearTimeout(this.hideGameTime);
	},
	//显示相应的游戏列表菜单
	lastSearchClickID : null,
	showGameList : function (id,obj){
		var gameMenu=jQuery("div.search_selectGameDiv_body dt li");
		var gameListBody=jQuery("div.search_selectGameDiv_body dd");
		var gameLoading=jQuery("div.search_selectGameDiv_body dd div.loading");
		gameMenu.each(function(){jQuery(this).removeClass();});
		if(null!=obj)jQuery(obj).addClass("selectMenuOver");
		else jQuery(gameMenu[id-1]).addClass("selectMenuOver");
		
		if(this.lastSearchClickID!=id){
		    if(!jQuery("#search-gamelist-"+id).is("div")){
		        gameListBody.append("<div id=\"search-gamelist-"+id+"\"></div>");
		    }
		    
		    if(""!=jQuery("#search-gamelist-"+id).html()){
	            jQuery("#search-gamelist-"+id).show();
	            jQuery("#search-gamelist-"+this.lastSearchClickID).hide();
	            this.lastSearchClickID=id;
		    }else{
		        var l="hotgame";
		        switch(id){
		            case 1:l="hotgame";break;
		            case 2:l="ABCD";break;
		            case 3:l="EFGH";break;
		            case 4:l="JKLM";break;
		            case 5:l="NOPQ";break;
		            case 6:l="RSTW";break;
		            case 7:l="XYZ";break;
		            case 8:l="freegame";break;
		        }
		        jQuery("#search-gamelist-"+this.lastSearchClickID).hide();
		        gameLoading.show();
		        jQuery.get(this.AjaxPath+"tools/AjaxPage.aspx",{Action:"catagory",l:l,html:"search",temp:Math.random()},function(xml){
		            var tempxml="暂无任何游戏";
		            if(""!=xml&&"null"!=xml){
		                tempxml=xml
		            }
		            jQuery("#search-gamelist-"+id).html(tempxml);
		            gameLoading.hide();
		            topSearch.lastSearchClickID=id;
		            topSearch.ShowSelectGameFrame(jQuery("#search_selectGameDiv")[0]);
		        });
		    }
		}
		this.ShowSelectGameFrame(jQuery("#search_selectGameDiv")[0]);
	},
	//选择游戏
	lastSelectGame : null,
	sG : function (name,catagoryid){
	    var _tmpName=name.replace(/<[\/\!]*?[^<>]*?>/ig,"");
		jQuery("#search_selectGame_middle").text(_tmpName);
		this.showAndHideSelectGame();
		if(this.lastSelectGame!=name){//当选择的游戏不等于前一次选择，则执行
			//设置游戏的选择项
			jQuery("#"+this.GameListSelect).val(catagoryid);
			//添加游戏到cookie
			this.sG_AddCookie(_tmpName,catagoryid);
			//宽度自动适应游戏名称的长度
			var strLength=StrLenthByByte(_tmpName)[1];
			jQuery("#search_selectGame").width(40+strLength*7);
			//显示物品列表
			//clearSelect("search_itemsSelect","加载中");
			//clearSelect("search_districtSelect","游戏区");
			clearSelect(this.GameServerSelect,"游戏服务器");
			this.showItems();
			this.showGameDS();
			//this.showDistrict();
		}
		this.lastSelectGame=name;
	},
	//装载物品列表
	ItemsIsAdd : false ,
	lastSelectItems : "" ,
	showItems : function (){
		var theGame=jQuery("#"+this.GameListSelect);
		clearSelect(this.GameItemSelect,"加载中");
		if(!this.ItemsIsAdd){
		    jQuery("#"+this.GameItemSelect).change(function(){
		        var _tempSelect = this.options[this.selectedIndex].value; 
		        //if(topSearch.GameIDCard==_tempSelect||topSearch.GameIDCard==topSearch.lastSelectItems
		        //    ||-1!=topSearch.MerIsCate.indexOf(_tempSelect)){
		            topSearch.showDistrict();
		        //}
		        
		        topSearch.lastSelectItems=_tempSelect;
		    });
		}
		jQuery.get(this.AjaxPath+"tools/AjaxPage.aspx",{Action:"mertype",gc:theGame.val(),selltype:"All",temp:Math.random()},function(txt){
			clearSelect(topSearch.GameItemSelect,"全部物品");
			if(""!=txt&&"null"!=txt){
			    var o=eval("["+txt+"]");
			    topSearch.MerIsCate="";
			    var _tempCard="";
		        for(var i=0;i<o.length;i++){
		            if(4==o[i][6]){//取出点卡编号
		                topSearch.GameIDCard=o[i][0];
		                _tempCard+=","+o[i][0]+",Card";
		            }else if(5==o[i][6]){
		                _tempCard+=","+o[i][0]+",DL";
		            }else if(o[i][7]){
		                //设置有独立分类的商品
		                topSearch.MerIsCate+=","+o[i][0];
		            }
		        }
		        //给点卡，代练标识
		        jQuery("#TopSearchType").val(_tempCard+",");
		        if(""!=topSearch.MerIsCate)topSearch.MerIsCate+=",";
			    
			    inToSelectByArr(topSearch.GameItemSelect,o);
		        if(topSearch.lastSelectItems){
		            jQuery("#"+topSearch.GameItemSelect).val(topSearch.lastSelectItems);
		            //topSearch.showServer();
		        }
			}
			topSearch.showDistrict();
		});
	},
	//装载游戏区列表
	showDistrict : function (){
		var theGame=jQuery("#"+this.GameListSelect);
		var _tempMerCard=jQuery("#"+this.GameItemSelect);
		clearSelect(this.GameDistrictSelect,"加载中");
		//判断是否为点卡，是则读取点卡类别
		var _isCard=false,_temps="游戏区";
		var _isCate=false;
		if(this.GameIDCard==_tempMerCard.val()){
		    _isCard=true;
		    _temps="请选择面值";
		}else if(-1!=this.MerIsCate.indexOf(_tempMerCard.val())){
		    _isCate=true;
		    _temps="请选择";
		}
		jQuery.get(this.AjaxPath+"tools/AjaxPage.aspx",{Action:"district",card:_isCard,gc:theGame.val(),mc:_tempMerCard.val(),iscate:_isCate,temp:Math.random()},function(txt){
			clearSelect(topSearch.GameDistrictSelect,_temps);
			if(""!=txt&&"null"!=txt){
		    	inToSelectByArr(topSearch.GameDistrictSelect,eval("["+txt+"]"));
		    	var _obj=jQuery("#"+topSearch.GameDistrictSelect);
		        if(topSearch.lastSelectDistrict){
		            _obj.val(topSearch.lastSelectDistrict);
		            topSearch.showServer();
		        }
			    jQuery("#"+topSearch.GameDistrictSelect).show();
			    topSearch.showServer();
			    //alert(jQuery("#"+topSearch.GameDistrictSelect).attr("size"));
			}else{
			    jQuery("#"+topSearch.GameDistrictSelect).hide();
			    jQuery("#"+topSearch.GameServerSelect).hide();
			}
			
		});
	},
	//装载游戏服务器列表
	showServer : function (){
	    var _tempMerCard=jQuery("#"+this.GameItemSelect);
	    var theGame=jQuery("#"+this.GameListSelect);
		var theDistrict=jQuery("#"+this.GameDistrictSelect);
		//判断是否为点卡，是则读取点卡类别
		var _isCard=false,_temps="游戏服务器";
		var _isCate=false;
		if(this.GameIDCard==jQuery("#"+this.GameItemSelect).val()){
		    _isCard=true;
		    _temps="充值方式";
		}else if(-1!=this.MerIsCate.indexOf(_tempMerCard.val())){
		    _isCate=true;
		    _temps="请选择";
		}
		if("0"==theDistrict.val()){
		    jQuery("#"+topSearch.GameServerSelect).hide();
		    clearSelect(this.GameServerSelect,_temps);return;
		}
		clearSelect(this.GameServerSelect,"加载中");
		jQuery.get(this.AjaxPath+"tools/AjaxPage.aspx",{Action:"server",card:_isCard,dc:theDistrict.val(),
		    iscate:_isCate,gc:theGame.val(),mc:_tempMerCard.val(),temp:Math.random()},function(txt){
			clearSelect(topSearch.GameServerSelect,_temps);
			if(""!=txt&&"null"!=txt){
		    	inToSelectByArr(topSearch.GameServerSelect,eval("["+txt+"]"));
		    	var _obj=jQuery("#"+topSearch.GameServerSelect);
		        if(topSearch.lastSelectServer){
		            _obj.val(topSearch.lastSelectServer);
		        }
		        jQuery("#"+topSearch.GameServerSelect).show();
			}else{
			    jQuery("#"+topSearch.GameServerSelect).hide();
			}
		});
	},
	//显示游戏区以及游戏服务器
	showGameDS : function (){
		//var theItems=jQuery("#search_itemsSelect");
		//if("0"!=theItems.val()){
			jQuery("#search_districtSelect").show();
			jQuery("#search_serverSelect").show();
		//}else{
		//	jQuery("#search_districtSelect").hide();
		//	jQuery("#search_serverSelect").hide();
		//}
	},

	//显示游戏关注列表
	showMySelectGame : function (){
		var oldSelectGameCookie=cookie.get("mySelectCatagoryList");
		if(null==oldSelectGameCookie)return;
		var _oldSelectGameCookie=oldSelectGameCookie.split("|||");
		var _sGstr="<ul>";
		for(var i=0;i<_oldSelectGameCookie.length;i++){
			if(""!=_oldSelectGameCookie[i]){
				var _oSGc=_oldSelectGameCookie[i].split(",");
				_sGstr+="<li><a href=\"#\" onclick=\"javascript:topSearch.sG('"+_oSGc[0].replace(/'/ig,"\\'")+"','"+_oSGc[1]+"');return false;\">"+_oSGc[0]+"</a>";
				_sGstr+="<a href=\""+tradepath+_oSGc[1]+"/List"+topSearch.ModelExt+"\" target=\"_blank\"><img src=\""+imagepath+"images/quick.jpg\"></a></li>";	
			}
		}
		_sGstr+="<li><a href=\"#\" style=\"color:blue\" onclick=\"topSearch.clearCookie();return false;\">[清空关注游戏]</a></li>";
		_sGstr+="</ul>";
		jQuery("#search_selectGameDiv_mySelect").find("span").html(_sGstr);
	},
	
	lastSelectDistrict : null,//最后一次选择游戏区
	lastSelectServer : null,//最后一次选择游戏服
	lastSelectItems : null,//最后一次选择的游戏物品
	//显示最后一次选择的游戏
	showLastSG : function (){
		this.showGameList(1);
		var lastSGstr=cookie.get("mySelectCatagory");
		var objSelectGame=jQuery("#"+this.GameListSelect);
		if(!objSelectGame.is("select")){return;}
		if(0<objSelectGame[0].selectedIndex){//页面载入后如果游戏有选中项
		    var _name=objSelectGame[0].options[objSelectGame[0].selectedIndex].text;
		    var _value= objSelectGame.val();
			this.sG(_name,_value);
		}else if(null!=lastSGstr){//最后一次保存的选中游戏不为空
			var _lastStr=lastSGstr.split(",");
			this.sG(_lastStr[0],_lastStr[1]);
		}else{
			this.showMySelectGame();
		}
		jQuery("#search_selectGameDiv").hide();
		jQuery(SelectGameFrame).hide();
	}
}

//创建一个iframe以保证游戏列表不被select等遮住(ie浏览器)
var SelectGameFrame = document.createElement("iframe");
jQuery(SelectGameFrame).css({position:"absolute",zIndex:998,visibility:"inherit",display:"none",filter:"progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)"});

var windowloadinner = function(){
	document.body.insertBefore(SelectGameFrame,null);
}
if(jQuery.browser.msie){
	window.attachEvent("onload",windowloadinner);
}

//页面载入后显示关注游戏列表到页面
jQuery(document).ready(function(){
    topSearch.showLastSG();
    jQuery("form").append("<input type=\"hidden\" id=\"TopSearchType\" name=\"TopSearchType\">");
});
