var PreloadImg = function(){
	var preloadImages = new Array();
	for(var i=0;i<arguments.length;i++){
		preloadImages[0] = new Image();
		preloadImages[0].src = arguments[i];
		//alert(arguments[i]);
	}
}

PreloadImg("/images/spaid.gif","/images/spaid2.gif","/images/loadingAnimation.gif");

$(document).ready(function()
{
	$(".block").hover(function(){
	  $(this).addClass("hover");
	},function(){
	  $(this).removeClass("hover");
	});
})
function show_shockwave(src,width,height,ver) {
	if(ver==''){ver=7;}
	document.write('<object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=' + ver + ',0,2,0" width="' + width + '" height="' + height + '">');
	document.write('<param name="swRemote" value="swSaveEnabled=\'true\' swVolume=\'true\' swRestart=\'true\' swPausePlay=\'true\' swFastForward=\'true\' swContextMenu=\'true\'">');
	document.write('<param name="swStretchStyle" value="meet">');
	document.write('<param name="bgColor" value="#ffffff">');
	document.write('<param name="src" value="' + src + '">');
	document.write('<embed src="' + src + '" width="' + width + '" height="' + height + '" bgColor="#000000" swRemote="swSaveEnabled=\'true\' swVolume=\'true\' swRestart=\'true\' swPausePlay=\'true\' swFastForward=\'true\' swContextMenu=\'true\'" swStretchStyle="meet" type="application/x-director" pluginspage="http://www.macromedia.com/shockwave/download/"></embed>');
	document.write('</object>');
}
function pwdcheck(){
	var bol=true;
	var emailStr=$('#email').val(); 
	var emailPat=/^(.+)@(.+)$/; 
	var matchArray=emailStr.match(emailPat); 
	if (matchArray==null){ 
		$(".getpwd_err").show().html("Email is wrong!");
		$("#email")[0].focus();
		bol=false;
	}
	if($("#p_username").val()==''){
		$(".getpwd_err").show().html("Please enter username!");
		$("#p_username")[0].focus();
		bol=false;
	}else{
		$(".getpwd_err").hide().html("");
	}
	if(bol==true){
		$(".pwdfrm").addClass("comloading").find("ul").css("visibility","hidden");
	}
	return bol;
}
function logincheck(){
	if($("#l_username").val()==''){
		$(".login_error").show().html("Please enter username!");
		$("#l_username")[0].focus();
		return false;
	}else{
		$(".login_error").hide().html("");
	}
	if($("#l_password").val()==''){
		$(".login_error").show().html("Please enter password!");
		$("#l_password")[0].focus();
		return false;
	}else{
		$(".login_error").hide().html("");
	}
	return true;
}

function reviewfrm(){
	$('.review_frm dl').html('<dd><textarea name="reviewcom" id="reviewcom"></textarea></dd><dt><input name="btn_post" type="button" class="btn_copy" id="btn_post" value="Post" /></dt>');
}
function addreport(gid,url){
	$("<div id='report'></div>").load("/ajax/report.php?action=addreport&gid="+gid+"&url="+url+"&ii="+Math.random(),function(){$(this).fadeIn()}).insertBefore('.info_bug a');
}
function addfav(gid){
	$.post("/ajax/user.php",{action:'addfav',gid:gid},
	function(avr){
		if(avr=='login'){
			sAlert('login','addfav',gid);
		}else if(avr=='false'){
			sAlert('favfaile');
		}else if(avr=='yi'){
			sAlert('favyi');
		}else{
			$('#games_fav').html(avr);
			//$('.info_add').html("Favorited");
			$('#favarea').removeClass('info_add').addClass('info_added').html("Favorited");
			sAlert('favsecc');
		}
	});
}
function CopyText(value) {
	try{
		var targetText = document.getElementById(value);
		targetText.focus();
		targetText.select();
		var clipeText = targetText.createTextRange();
		clipeText.execCommand("Copy");
	}catch(e){}
}
function displayload(){
	$("#preloaddiv").hide();
	$("#ContentDiv").show();
	timesnap = 1;
}
function refreshProgress() {
	var bar = $("#loadcolumn");
	var movie =  $("#flashgame");
	//stringToEval = "window.document[\'flashgame\']";
	//var movie=eval(stringToEval);
	if (window.navigator.userAgent.indexOf("MSIE")>=1){//ie
		var nPercentLoaded = document.getElementById("flashgame").PercentLoaded();
		bar.css({ width: nPercentLoaded + "%"}); 
		bar.html(nPercentLoaded + "%");
		if(nPercentLoaded == 100){
			clearTimeout(nTimeoutId);
			//if(timesnap == 1){
				//displayload();
			//}
			$("#loadbar").hide();
		} else {
			nTimeoutId = setTimeout('refreshProgress()', 100);
			//$("#preloaddiv").show();
			//$("#ContentDiv").hide();
		}
	}else{
		nTimeoutId = setTimeout('refreshProgress()', 100);
		//$("#preloaddiv").hide();
		//$("#ContentDiv").show();
		$("#loadbar").hide();
	}
	
}
function sAlert(str,other,gid){
	if (typeof delayhide!="undefined"){
		clearTimeout(delayhide);
	}
	$('#play_flash').css({visibility: "hidden"});
	if(str=='reg'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=reg&height=140&width=400&modal=true','');
		delayhide=window.setTimeout("self.location='/login.html';",5000);
	}else if(str=='head_login'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=head_login&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='logout'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=logout&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='delpic'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=delpic&height=140&width=400','');
	}else if(str=='copy'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=copy&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='yi'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=yi&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='rate'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=rate&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='ratingfaile'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=ratingfaile&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
		}else if(str=='favfaile'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=favfaile&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='reportfaile'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=reportfaile&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='favyi'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=favyi&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='favsecc'){
		tb_show('PurpGame Notice','/ajax/errtext.php?type=favsecc&height=140&width=400','');
		delayhide=window.setTimeout('tb_remove()',3000);
	}else if(str=='login'){
		if(other!=''){
			tb_show('PurpGame Notice','/ajax/errtext.php?type=login&height=140&width=400&other='+other+'&gid='+gid,'');
		}else{
			tb_show('PurpGame Notice','/ajax/errtext.php?type=login&height=140&width=400','');
		}
		//delayhide=window.setTimeout('tb_remove()',3000);
	}
}

function rate_text(num){
	if(num==1){
		$('#rate_text').html("Bad")
	}else if(num==2){
		$('#rate_text').html("OK")
	}else if(num==3){
		$('#rate_text').html("Decent")
	}else if(num==4){
		$('#rate_text').html("Good")
	}else if(num==5){
		$('#rate_text').html("Excellent")
	}
}

function rating(id,num){
	$.post("/ajax/user.php",{action:'rating',id:id,num:num},
	function(avr){
		if(avr=='yi'){
			sAlert('yi');
		}else if(avr=='false'){
			sAlert('ratingfaile');
		}else if(avr!=''){
			$('#game_ratings_size').html(avr);
			average_rating(id);
			you_rating(id);
			sAlert('rate');
		}
	});
		
}
function you_rating(id){
	$.post("/ajax/user.php",{action:'you_rating',id:id},
	function(msg){
		if(msg){
		$('.yourate').html(msg);
		}
	})
}
function average_rating(id){
	$.post("/ajax/user.php",{action:'average_rating',"gid":id},
	function(msg){
		$('#average_rating').html(msg);
	})
}
function checklogin(){
	if($("#username").val()==''){
		$("#login_err").show().html("Please enter username!");
		$("#username")[0].focus();
		return false;
	}else{
		$("#login_err").hide().html("");
	}
	if($("#password").val()==''){
		$("#login_err").show().html("Please enter password!");
		$("#password")[0].focus();
		return false;
	}else{
		$("#login_err").hide().html("");
	}
	return true;
}
function getuserinfo(){
	$.post("/ajax/user.php",{"action":'index'},
		function(msg){
			if(msg){
			$(".head_login_area").html(msg);
			}
		})
}

function head_login() {
	if($("#t_username").val()=="" || $("#t_username").val()=='User name'){
		$("#t_username").val('');
		$("#login_err").show().html("Please enter username!");
		$("#t_username")[0].focus();
		return false;
	}else{
		$("#login_err").hide().html("");
	}
	if($("#t_password").val()==""){
		$("#login_err").show().html("Please enter password!");
		$("#t_password")[0].focus();
		return false;
	}else{
		$("#login_err").hide().html("");
	}
	if($("#t_username").val()!="" && $("#t_password").val()!=""){
		var username=$("#t_username").val();
		var password=$("#t_password").val();
		$.post("/ajax/user.php",{action:"head_login",username:username,password:password},
		function(msg){
			if(msg=="true"){
				getuserinfo();
				sAlert('head_login');
			}else if(msg=="false"){
				$("#login_err").show().html("Username or Password is wrong!");
				return false;
			}
		})
	}
}