/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
		  
var tb_pathToImage = "images/xajax-loading.gif";

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_init
$(document).ready(function(){   
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).click(function(){
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		if (t == "装备") {
			equip_show(t,a,g);
		/*} else if (t == "俱乐部详情") {
			equip_show(t,a,g);*/
		} else {
			div_show(t,a,g);
		}
		this.blur();
		return false;
	});
}

function equip_show(caption, url, reltype) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("DIV_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='DIV_HideSelect'></iframe><div id='DIV_mask'></div><div id='DIV_window1'><div id='equip_object' class='div_win_con1'></div></div>");
				$("#DIV_mask").click(tb_remove_equip);
			}
		}else{ //all others
			if(document.getElementById("DIV_mask") === null){
				$("body").append("<div id='DIV_mask'></div><div id='DIV_window1'><div id='equip_object' class='div_win_con1'></div></div>");
				$("#DIV_mask").click(tb_remove_equip);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#DIV_mask").addClass("DIV_maskMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#DIV_mask").addClass("DIV_maskBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='DIV_load'><p><b><b><b><img src='"+imgLoader.src+"'>&nbsp;&nbsp;&nbsp;loading...</b></b></b></p></div>");//add loader to the page
		$('#DIV_load').show();//show loader
		
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery(queryString);
		DIV_WIDTH  = (params['width']*1) || 400; //defaults to 630 if no paramaters were added to URL
		DIV_HEIGHT = (params['height']*1) + 40 || 240; //defaults to 440 if no paramaters were added to URL
		ajaxContentW = DIV_WIDTH - 50;
		ajaxContentH = DIV_HEIGHT - 45;

		urlNoQuery = url.split('DIV_');
		$("#DIV_iframeContent").remove();
		$("<h1><span class='close' id='DIV_closeWindowButton'></span>"+caption+"</h1>").insertBefore('#equip_object');
		$("#equip_object").append("<iframe frameborder='0' hspace='0' src='"+url+"' id='DIV_iframeContent' name='DIV_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe_equip()' style='width:"+(ajaxContentW+20)+"px;height:"+(ajaxContentH+3)+"px;'></iframe>");
		$("<div class='div_win_bot'><input type='button' onclick='tb_remove_equip()' class='div_win_but' value='确定' /></div>").insertAfter("#equip_object");
		$("#DIV_closeWindowButton").click(tb_remove_equip);
		tb_position_equip();
		if($.browser.safari){//safari needs help because it will not fire iframe onload
			$("#DIV_load").remove();
			$("#DIV_window1").css({display:"block"});
		}
		
		if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove_equip();
				}	
			};
		}
	}catch(e){}
}

function div_show(caption, url, reltype) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("DIV_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='DIV_HideSelect'></iframe><div id='DIV_mask'></div><div id='DIV_window'><div id='equip_object' class='div_win_con1'></div></div>");
				$("#DIV_mask").click(tb_remove);
			}
		}else{ //all others
			if(document.getElementById("DIV_mask") === null){
				$("body").append("<div id='DIV_mask'></div><div id='DIV_window'><div id='equip_object' class='div_win_con1'></div></div>");
				$("#DIV_mask").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#DIV_mask").addClass("DIV_maskMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#DIV_mask").addClass("DIV_maskBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='DIV_load'><p><b><b><b><img src='"+imgLoader.src+"'>&nbsp;&nbsp;&nbsp;loading...</b></b></b></p></div>");//add loader to the page
		$('#DIV_load').show();//show loader
	
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = tb_parseQuery(queryString);
		DIV_WIDTH  = (params['width']*1) || 400; //defaults to 630 if no paramaters were added to URL
		DIV_HEIGHT = (params['height']*1) + 40 || 240; //defaults to 440 if no paramaters were added to URL
		ajaxContentW = DIV_WIDTH - 50;
		ajaxContentH = DIV_HEIGHT - 45;
		
		if(url.indexOf('DIV_iframe') != -1){// either iframe or ajax window		
				urlNoQuery = url.split('DIV_');
				$("#DIV_iframeContent").remove();
				if(params['modal'] != "true"){//iframe no modal
					$("<h1><span class='close' id='DIV_closeWindowButton'></span>"+caption+"</h1>").insertBefore('#equip_object');
					$("#equip_object").append("<iframe frameborder='0' hspace='0' src='"+url+"' id='DIV_iframeContent' name='DIV_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW+20)+"px;height:"+(ajaxContentH+3)+"px;'></iframe>");
				}else{//iframe modal
					$("#DIV_window").append("<iframe frameborder='0' hspace='0' src='"+url+"' id='DIV_iframeContent' name='DIV_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 25)+"px;height:"+(ajaxContentH + 4)+"px;'> </iframe>");
				}
		}else{// not an iframe, ajax		
			if($("#DIV_window").css("display") != "block"){
				if(params['modal'] != "true"){//ajax no modal
					$("#DIV_window").append("<h1><span class='close' id='DIV_closeWindowButton'></span>"+caption+"</h1><div id='DIV_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
				}else{//ajax modal
					$("#DIV_mask").unbind();
					$("#DIV_window").append("<div id='DIV_ajaxContent' class='DIV_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
				}
			}else{//this means the window is already up, we are just loading new content via ajax
				$("#DIV_ajaxContent")[0].style.width = ajaxContentW +"px";
				$("#DIV_ajaxContent")[0].style.height = ajaxContentH +"px";
				$("#DIV_ajaxContent")[0].scrollTop = 0;
			}
		}

		$("<div class='div_win_bot'><input type='button' onclick='tb_remove()' class='div_win_but' value='确定' /></div>").insertAfter("#equip_object");
		$("#DIV_closeWindowButton").click(tb_remove);
		
		if(url.indexOf('DIV_iframe') != -1){
			tb_position();
			if($.browser.safari){//safari needs help because it will not fire iframe onload
				$("#DIV_load").remove();
				$("#DIV_window").css({display:"block"});
			}
		} else {
			$("#DIV_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
				tb_position();
				$("#DIV_load").remove();
				tb_init("#DIV_ajaxContent a.thickbox");
				$("#DIV_window").css({display:"block"});
			});
		}
			    
	    if(!params['modal']){
			document.onkeyup = function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					tb_remove();
				}	
			};
		}
		
	} catch(e) {}
}

function tb_showIframe(){
	$("#DIV_load").remove();
	$("#DIV_window").css({display:"block"});
}

function tb_showIframe_equip(){
	$("#DIV_load").remove();
	$("#DIV_window1").css({display:"block"});
}

function tb_remove() {
	$("#DIV_closeWindowButton").unbind("click");
	$("#DIV_window").fadeOut("fast",function(){$('#DIV_window,#DIV_mask,#DIV_HideSelect').trigger("unload").unbind().remove();});
	$("#DIV_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_remove_equip() {
	$("#DIV_closeWindowButton").unbind("click");
	$("#DIV_window1").fadeOut("fast",function(){$('#DIV_window1,#DIV_mask,#DIV_HideSelect').trigger("unload").unbind().remove();});
	$("#DIV_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
	var	obj = document.getElementById ('DIV_window');
	if ( obj )
	{
		obj.style.top = '50%';
		obj.style.left = '50%';
		try
		{
			obj.style.marginLeft = ( 0 - obj.scrollWidth / 2 + document.documentElement.scrollLeft ) + 'px';
			obj.style.marginTop = ( 0 - obj.scrollHeight / 2 + document.documentElement.scrollTop ) + 'px';
		}
		catch (e) {}
		setTimeout ( "tb_position()", 500 );
	}
}

function tb_position_equip() {
	var	obj = document.getElementById ('DIV_window1');
	if ( obj )
	{
		obj.style.top = '50%';
		obj.style.left = '50%';
		try
		{
			obj.style.marginLeft = ( 0 - obj.scrollWidth / 2 + document.documentElement.scrollLeft ) + 'px';
			obj.style.marginTop = ( 0 - obj.scrollHeight / 2 + document.documentElement.scrollTop ) + 'px';
		}
		catch (e) {}
		setTimeout ( "tb_position_equip()", 500 );
	}
}

function tb_parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}