
$(document).ready(function(){

	//check submit
	$("#header_form").submit(function(){
		var str1 = $("#header_user_name").val();
		var str2 = $("#header_pass").val();
		if(str1 == "User Name" || str1 == "" || str2 == "******" || str2 == ""){
			$("#sign_in").hide();
			$("#sign_in1").show();
			return false;
		}
	});


	$("#cate_box").children("a").each(function(){
		$(this).mouseover(function(){
			$(this).css("background","url(/style/default/bg_1_25.gif) repeat-x");
		});
		$(this).mouseout(function(){
			$(this).css("background","");
		});
	});
	
	
	//all pages    //this.style.borderColor='#99cc00'
	$("input").each(function(){
		$(this).mouseover(function(){
			$(this).css("borderColor","#99cc00");
		});
		$(this).mouseout(function(){
			$(this).css("borderColor","#c4c7c8");
		});
	});
	

	
});

//取得绝对位置，返回数组含 top 与 left 两个
function getoffset(e) {  
	var t=e.offsetTop;  
	var l=e.offsetLeft;  
	while(e=e.offsetParent) {  
		t+=e.offsetTop;  
		l+=e.offsetLeft;  
	}  
	var rec = new Array(1); 
	rec[0]  = t; 
	rec[1] = l; 
	return rec 
}  

/**把 vid 定位到 obj 的位置， left and top 是左边与上边的偏差值。
 * obj 一般都是用 this 代替，要用到函数 getoffset() 与 JQuery 库 */
function set_position(obj,vid,left,top){
	var t_l = getoffset(obj);
	$("#"+vid).css("left",t_l[1]+left);
	$("#"+vid).css("top",t_l[0]+top);
	$("#"+vid).show();
}

function hide_this(vid){
	$("#"+vid).hide();
}
function show_this(vid){
	$("#"+vid).show();
}




function myclick(val1,val2){
	if(val1 == "header_sign_in"){//header中点击出现登陆输入框
		$("#sign_in1").hide();
		$("#sign_in").show();
	}

	if(val1 == "header_goto_search"){
		window.location = "Games__90_Popular_1_desc_1_"+$('#header_search').val()+".html";
	}
	if(val1 == "header_cate"){
		if(val2 == "1"){
			$(".header_cate").find("b").html("<a onClick=\"myclick('header_cate','2');\">Less</a>");
			$("#b_more_less").removeClass().addClass("b_less_bg");
			$(".header_cate").find("dl").slideDown();
		}
		if(val2 == "2"){
			$(".header_cate").find("b").html("<a onClick=\"myclick('header_cate','1');\">More</a>");
			$("#b_more_less").removeClass().addClass("b_more_bg");
			$(".header_cate").find("dl").slideUp();
		}
	}
	//play_game.html
	if(val1 == "game_info"){
		$("#game_info_1").removeClass().addClass("game_info_off");
		$("#game_info_2").removeClass().addClass("game_info_off");
		$("#game_info_3").removeClass().addClass("game_info_off");
		$("#game_info_4").removeClass().addClass("game_info_off");
		$("#game_info_"+val2).removeClass().addClass("game_info_on");
		$("#game_other_1").hide();
		$("#game_other_2").hide();
		$("#game_other_3").hide();
		$("#game_other_4").hide();
		$("#game_other_"+val2).show();
	}
	
	if(val1 == "share_with_friends"){
		/*
		$.post("/mail_send.php",{flash_id: val2,your_name: $("#your_name").val(),email_address: $("#send_email_address").val()},function(xml){
			if($("result",xml).text()){
				alert($("result",xml).text());return false;
			}
		});
		/*/
		$.post("/mail_send.php",{flash_id: val2,your_name: $("#your_name").val(),email_address: $("#send_email_address").val()});
		if($("#your_name").val() != "" && $("#send_email_address").val() != "" ){
			alert('Wonderful Games has been send.');
		}
		//*/
	}
	//add times like
	if(val1 == "i_like_this_game"){
		$.post("/save.php",{flash_id: val2},function(xml){
			if($("result",xml).text()){
				alert($("result",xml).text());
			}else{
				$("#play_game_likes").html($("times_like",xml).text());
			}
		});
	}
	//sign in from alert
	if(val1 == "alert_sign_in"){
		$.post("/sign_in.php",{alert_username: $("#alert_username").val(),alert_pass: $("#alert_pass").val()},function(xml){																						
			if($("result",xml).text()){
				if($("result>info",xml).text() != "ok"){
					alert($("result>info",xml).text());
					close_alter('alert_log_in',true);
				}else{
					window.location = $("result>url",xml).text();
				}
			}
		});	
	}
	//add review from play_game.html
	if(val1 == "play_game_review_submit"){
		$.post("/save.php",{play_game_flash_id: val2,review: $("#play_game_review").val()},function(xml){
			if($("result",xml).text()){
				$("#play_game_review_box").hide();
				$("#play_game_review").val("");
				$("#play_game_review_char").html('255');
				$("#play_game_review_ok").html($("result",xml).text());
				$("#play_game_review_ok").show();
				setTimeout('$("#play_game_review_box").show();$("#play_game_review_ok").hide();',5000);
			}
		});
	}
	//add favorites game's id to member's games_fav
	if(val1 == "favorites_this_game"){
		$.post("/save.php",{fav_id: val2, action: val1},function(xml){
			if($("result",xml).text()){
				alert($("result",xml).text());
			}
		});
	}
	
	//后台的set_tags.php页面中的修改、保存、删除
	if(/[0-9]/g.test(val1)){
		if(val2 == ""){
			$("#form1").hide();
			$("#form2").show();
			$('#edit_id').val(val1);
			$('#edit_val1').val($('#f1_'+val1).html());
			$('#edit_val2').val($('#f2_'+val1).html());
		}
	}
	if(val1 == "edit_tag"){
		$('#f1_'+$('#edit_id').val()).html($("#edit_val1").val());
		$('#f2_'+$('#edit_id').val()).html($("#edit_val2").val());
		$.post("/admin/set_tags.php",{edit_id: $('#edit_id').val(),val1: $("#edit_val1").val(),val2: $("#edit_val2").val()});
		$('#edit_val1').val();
		$('#edit_val2').val();
		$("#form2").hide();
		$("#form1").show();
	}
	
	if(val1 == "del_id"){
		$("#del_id_"+val2).hide();
		$.post("/admin/set_tags.php",{del_id: val2});
	}
	//后台的set_tags.php页面中的修改、保存、删除 ====
	
	
}
//拷贝到剪切板
function cb_copy( item ) {
	copied = item.createTextRange();
	item.focus();
	item.select();
	copied.execCommand("Copy");
}

/* 返回指定字符串的长度，一个汉字算两个字符*/
function text_strlen(str){  
	var len;  
	var i;  
	len = 0;  
	for (i=0;i<str.length;i++){  
		if (str.charCodeAt(i)>255){  
			len+=2;   
		}else{  
			len++;  
		}  
	}  
	return len;  
} 


function change_style(style){
	$.post("/save.php?action=change_style",{style: style},function(xml){
		if($("result",xml).text()){
			location.href = $("result",xml).text();
		}
	});
	
}


/* 弹出框 打开 div_id 弹出窗口ID号，div_width 为此DIV的宽度，
   bool_bg 为此DIV下方的大背景开关，值为 true 或 false; bool_set_pos 据此判断是否需要人工设置弹出框显示的位置 */
function open_alter(div_id,div_width,bool_bg,bool_set_pos){
	if(bool_bg == true){
		if(!document.getElementById("my_bg")){
			mybg = document.createElement("div");
			mybg.setAttribute("id","my_bg");
			mybg.style.background = "#000";
			if(navigator.userAgent.indexOf("MSIE")>0) { 
				if(document.body.scrollHeight < window.screen.availHeight){
					mybg.style.width = document.body.scrollWidth+"px";//现在宽度是定死的，所以此处写法无所谓
					mybg.style.height = window.screen.availHeight+"px";
				}else{
					mybg.style.width = document.body.scrollWidth+"px";
					mybg.style.height = document.body.scrollHeight+"px";
				}
			} 
			if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ 
				mybg.style.width =document.documentElement.scrollWidth+"px";
				mybg.style.height = document.documentElement.scrollHeight+"px";
			} 
			mybg.style.position = "absolute";
			mybg.style.top = "0";
			mybg.style.left = "0";
			mybg.style.opacity = "0.3";
			mybg.style.filter = "Alpha(opacity=30)";
			mybg.style.zIndex = "500";
			document.body.appendChild(mybg);
		}
		else{
			document.getElementById("my_bg").style.display = "block";
		}
	}
	
	var broY = GetScrollY(); //取得已滚动过去的高度
	var CreatePF = document.getElementById(div_id);
	CreatePF.style.display = "block";
	CreatePF.style.position = "absolute";
	CreatePF.style.top = "50%";
	CreatePF.style.left = (document.body.scrollWidth - div_width) / 2 + "px";
	if(bool_set_pos == false){//当不需要人工设置位置时，就固定位置显示即可
		CreatePF.style.marginTop = (broY - 250) + "px";
	}
	//CreatePF.style.marginLeft = "-300px";
	CreatePF.style.zIndex = "1000";
}
/*弹出框 关闭*/
function close_alter(div_id,bool_bg){
	document.getElementById(div_id).style.display = "none";
	if(bool_bg == true){
		document.getElementById("my_bg").style.display = "none";
	}
}

/* 获取网页被卷去的高，制作浮动窗体可用 */
function GetScrollY(){        
    if (typeof window.pageYOffset == 'number'){
        return window.pageYOffset;
    }

    var CompatMode = window.document.compatMode;
    var DocumentElement = window.document.documentElement;

    if ((typeof CompatMode == 'string') && (CompatMode.indexOf('CSS') >= 0) && (DocumentElement) && (typeof DocumentElement.getAttribute("scrollTop") == 'number')){
        return DocumentElement.getAttribute("scrollTop");
     } 

    var Body = window.document.body ;

    if ((Body) && (typeof Body.getAttribute("scrollTop") == 'number')){
        return Body.getAttribute("scrollTop");
    }

    return 0;
}














