// JavaScript Document
var thegid;var tw_game;var tw_gid=0;

function setBState(id,state) {
	document.getElementById('buttons'+id).style.backgroundImage='url(/img/g_'+state+'.png)';
	document.getElementById('buttons'+id).style.visibility='visible';
}
function unsetBState(id) {
	document.getElementById('buttons'+id).style.backgroundImage='none';
	document.getElementById('buttons'+id).style.visibility='hidden';
}
function showVoting(gid,uid,thegame) {
  //if (uid>0) {
	thegid = gid;
	tw_game = thegame;
	fixVotePos();
	if (document.getElementById('twitter')!=null) opacity('twitter', 0, 90, 500);
	document.getElementById('dim').style.display='block';
	opacity('dim', 0, 90, 500);
	//document.getElementById('dim').style.display='block';
	//ajax request
	votebox = new sack();
	votebox.requestFile = '/pieces/voting.asp';
	votebox.method = "POST";
	votebox.element = 'voting';
	votebox.setVar("gid", gid);
	votebox.setVar("curstate", document.getElementById('buttons'+gid).style.backgroundImage);
	votebox.onCompletion = function(){opacity('voting', 0, 100, 800);	tw_gid = gid;show_tw();};
	votebox.runAJAX();
	/*
  }
  else {
	  alert('Please login to vote');
  }*/
}
function fixVotePos() {
	//alert(thegid);
  if (thegid>0) {
	var mxy = findPos(document.getElementById('like'+thegid));
	//alert(xy[0]+" "+xy[1]);
	document.getElementById('voting').style.left = (mxy[0]-79) + "px" ;
	document.getElementById('voting').style.top = (mxy[1]-176) + "px";
	if (document.getElementById('twitter')!=null) {
		document.getElementById('twitter').style.left = (mxy[0]-79+320) + "px" ;
		document.getElementById('twitter').style.top = (mxy[1]-176+100) + "px";
	}
  }
}
function endVoting() {
	opacity('dim', 90, 0, 800);
	opacity('voting', 100, 0, 500);
	if (document.getElementById('twitter')!=null) opacity('twitter', 100, 0, 500);
	thegid = 0;
}
function dovote(gid,uid) {
	loading('g_pic');
	dovote = new sack();
	dovote.requestFile = '/pieces/dovote.asp';
	dovote.method = "POST";
	dovote.element = 'signin-form';
	dovote.setVar("gid", gid);
	dovote.setVar("uid", uid);
	dovote.setVar("votestate", document.getElementById('saved-buttons'+gid+'b').style.backgroundImage);
	dovote.setVar("p_gameplay", document.getElementById('starCur0').title);
	dovote.setVar("p_graphics", document.getElementById('starCur1').title);
	dovote.setVar("p_sound", document.getElementById('starCur2').title);
	dovote.setVar("p_plot", document.getElementById('starCur3').title);
	dovote.setVar("comment", document.getElementById('comment').value);
	if (document.getElementById('twitter')!=null) {
  		if (document.getElementById('tw_yesno').checked) {
			//post to twitter
			dovote.setVar("tw_update","1");
			dovote.setVar("tw_n",document.getElementById('tw_user').value);
			dovote.setVar("tw_p",document.getElementById('tw_pass').value);
			dovote.setVar("tw_s",document.getElementById('tw_status').innerHTML);
		}
		else {
			//no post
			dovote.setVar("tw_update","0");
		}
	}
	dovote.onCompletion = function() {if (uid>0) document.location.href="/game/"+gid};
	dovote.runAJAX();
}
function doLogin() {
	dologin = new sack();
	dologin.requestFile = '/discuss/people.php';
	dologin.method = "POST";
	dologin.element = 'user-area'
	dologin.setVar("Username", document.getElementById('frmSignIn').Username.value);
	dologin.setVar("Password", document.getElementById('frmSignIn').Password.value);
	dologin.setVar("RememberMe","1");
	dologin.setVar("PostBackAction", "SignIn");
	dologin.onCompletion = function() {if(success(dologin.response)){loading2('MB_content');location.reload(true)} else {Modalbox.hide();}};
	dologin.runAJAX();
}
function user_popup(uid) {
	//fix popup position
	var mxy = findPos(document.getElementById('gb_'+uid));
	//alert(xy[0]+" "+xy[1]);
	document.getElementById('user_popup').style.left = (mxy[0]-77) + "px" ;
	document.getElementById('user_popup').style.top = (mxy[1]+35) + "px";
	loading('user_popup');
	opacity('user_popup',0,100,300);
	doupop = new sack();
	doupop.requestFile = '/pieces/user_popup.asp';
	doupop.method = "POST";
	doupop.element = 'user_popup'
	doupop.setVar("uid", uid.substr(1));
	//dologin.onCompletion = function() {if(success(dologin.response)){loading2('MB_content');location.reload(true)} else {Modalbox.hide();}};
	doupop.runAJAX();
}
function user_popup_cl(){
	opacity('user_popup',100,0,200);
}
function doRequestPass() {
	dopass = new sack();
	dopass.requestFile = '/discuss/people.php';
	dopass.method = "POST";
	dopass.element = 'modal-reqpass'
	dopass.setVar("Username", document.getElementById('frmRequestPass').Username.value);
	dopass.setVar("PostBackAction", "RequestPasswordReset");
	dopass.onCompletion = function() {};
	dopass.runAJAX();
}
function success(stringa) {
	if (stringa.indexOf('Error')>0) {
			return false;
	}
	else {
			return true;
	}
}
function doLogout() {
	dologout = new sack();
	dologout.requestFile = '/discuss/people.php';
	dologout.method = "POST";
	dologout.element = 'user-area'
	dologout.setVar("PostBackAction", "SignOutNow");
	dologout.onCompletion = function() {location.reload();};
	dologout.runAJAX();
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
function opacity(id, opacStart, opacEnd, millisec) {
  var inc = 1;
  if ((navigator.userAgent.indexOf("MSIE")!=-1)) {inc = 10;}
  //apply opacity only on firefox with PNGs
  if ((navigator.userAgent.indexOf("Firefox")!=-1) || (id!='voting')) {
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i=i-inc) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i=i+inc) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }
  }
  else {
  //every other browser only hides/shows PNGs	
	  if(opacStart > opacEnd) {
		 document.getElementById(id).style.display = 'none'; 
		 //alert("h: "+opacStart+" "+opacEnd);
	  }
	  else {
		 document.getElementById(id).style.display = 'block';  	
	  }
  }
}
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style;
	if (opacity > 0) {
			object.display = 'block';
		}
		else {
			object.display = 'none';
		}
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}
function setVote(gid,state) {
	document.getElementById('saved-buttons'+gid+'b').style.backgroundImage = "url(/img/g_"+state+".png)";
	show_tw();
}
function genBoxes(elemento,tipo,num,gid,tag) {
	if (tag===undefined) {tag='';}
	boxes = new sack();
	boxes.requestFile = '/pieces/gen_boxes.asp';
	boxes.method = "POST";
	boxes.element = elemento;
	boxes.setVar("tipo", tipo);
	boxes.setVar("num", num);
	boxes.setVar("gid", gid);
	boxes.setVar("tag", tag);
	boxes.onCompletion = function() {}//document.location.href=document.URL};
	boxes.runAJAX();
}
function onContent(f){//(C)webreflection.blogspot.com
var a=onContent,b=navigator.userAgent,d=document,w=window,c="onContent",e="addEventListener",o="opera",r="readyState",
s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,".",c,"()}'></scr","ipt>");
a[c]=(function(o){return function(){a[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(a[c]);
if(d[e])d[e]("DOMContentLoaded",a[c],false);
if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))(function(){/loaded|complete/.test(d[r])?a[c]():setTimeout(arguments.callee,1)})();
else if(/MSIE/i.test(b))d.write(s);
}

function man_fr(act,fid) {
	manfr = new sack();
	manfr.requestFile = '/pieces/manageFR.asp';
	manfr.method = "POST";
	manfr.element = 'user_option';
	manfr.setVar("act", act);
	manfr.setVar("fid", fid);
	manfr.onCompletion = function() {}//document.location.href=document.URL};
	manfr.runAJAX();
}

function signup() {
	doreg = new sack();
	doreg.requestFile = '/discuss/people.php';
	doreg.method = "POST";
	doreg.element = 'evi_box';
	doreg.setVar("PostBackAction", "Apply");
	doreg.setVar("ReadTerms", "1");
	doreg.setVar("AgreeToTerms", "1");
	doreg.setVar("Email", document.getElementById('ApplicationForm').Email.value);
	doreg.setVar("Name", document.getElementById('ApplicationForm').Name.value);
	doreg.setVar("NewPassword", document.getElementById('ApplicationForm').NewPassword.value);
	doreg.setVar("ConfirmPassword", document.getElementById('ApplicationForm').ConfirmPassword.value);
	doreg.setVar("PredefinedAttributes[Xbox Live Gamertag]", document.getElementById('ApplicationForm').elements['PredefinedAttributes[Xbox Live Gamertag]'].value);
	doreg.onCompletion = function() {}//document.location.href=document.URL};
	doreg.runAJAX();
}

function loading(elemento) {
	document.getElementById(elemento).innerHTML='<center><img src="/img/loading.gif" /></center>';
}

function loading2(elemento) {
	document.getElementById(elemento).innerHTML='<center><img src="/img/spinner.gif" /></center>';
}

function search_init() {
	document.getElementById('gsearch').value = '';
	document.getElementById('gsearch').style.fontStyle = "normal";
}
function quickSearch(num) {
	doSearch = new sack();
	doSearch.requestFile = '/pieces/quickSearch.asp';
	doSearch.method = "POST";
	doSearch.element = 'qs_results';
	doSearch.setVar("q", document.getElementById('gsearch').value);
	doSearch.setVar("num", num);
	doSearch.onCompletion = function() {document.getElementById('qs_results').style.display='block'};
	doSearch.runAJAX();
}
function quickSearchRef() {
	if (document.getElementById('gsearch').value!='' && document.getElementById('gsearch').value!='find a game') {
		document.getElementById('qs_results').style.display='block';	
	}
}
function show_tw() {
if (document.getElementById('twitter')!=null) {
  if (document.getElementById('tw_yesno').checked) {
   if (document.getElementById('tw_user').value!='' && document.getElementById('tw_pass').value!='') {
	therate = calcRate(); 
	document.getElementById('tw_status').innerHTML='I just rated '+tw_game+'. ';
	if (document.getElementById('saved-buttons'+tw_gid+'b').style.backgroundImage=='url(/img/g_dislike.png)') {
		document.getElementById('tw_status').innerHTML+= "I don't like it. "
		} else {
			document.getElementById('tw_status').innerHTML+= "I like it! "
		}
	//document.getElementById('tw_status').innerHTML+=document.getElementById('saved-buttons'+tw_gid+'b').style.backgroundImage;
	if (therate > 0) document.getElementById('tw_status').innerHTML+='I give it '+therate+' overall. ';
	document.getElementById('tw_status').innerHTML+='http://xilike.net/game/'+tw_gid;
   } else {
	   //no name/pass
	  document.getElementById('tw_status').innerHTML = 'Please type your twitter name and password to post your update.'; 
   }
  } else {
	  //no updated wanted
	  document.getElementById('tw_status').innerHTML = 'Please check the thickbox above to post your update.';
  }
}
}

function calcRate() {
	therate = 0;
	for (var i=0;i<4;i++) {
	if (document.getElementById('starUser'+i).innerHTML!='') {
		therate += parseInt(document.getElementById('starUser'+i).innerHTML.substr(0,document.getElementById('starUser'+i).innerHTML.length-1));
		} else {
		therate +=0;
	}
	}
	return Math.round(therate/4)/10;
}

function act(f,num) {
	if (f=='f') {
		$I('act_lif').className = 'active';
		$I('act_lie').className = '';
		$I('act_lim').className = '';
	}
	if (f=='e') {
		$I('act_lif').className = '';
		$I('act_lie').className = 'active';
		$I('act_lim').className = '';
	}
	if (f=='m') {
		$I('act_lif').className = '';
		$I('act_lie').className = '';
		$I('act_lim').className = 'active';
	}
	doact = new sack();
	doact.requestFile = '/pieces/dogetact.asp';
	doact.method = "POST";
	doact.element = 'gb_act';
	doact.setVar("tipo", f);
	doact.setVar("num", num);
	doact.onCompletion = function() {};
	doact.runAJAX();
}
function news(f,num,gid) {
	if (gid===undefined) {gid=0;}
	if (f=='m') {
		$I('news_lim').className = 'active';
		$I('news_lie').className = '';
	}
	if (f=='e') {
		$I('news_lim').className = '';
		$I('news_lie').className = 'active';
	}
	donews = new sack();
	donews.requestFile = '/pieces/dogetnews.asp';
	donews.method = "POST";
	donews.element = 'gb_news';
	donews.setVar("tipo", f);
	donews.setVar("num", num);
	donews.setVar("gid", gid);
	donews.onCompletion = function() {};
	donews.runAJAX();
}
function popitup(tipo,thetitle,id) {
	switch (tipo) {
			case 'login':
				Modalbox.show('/pieces/dologin.asp',{title:'login with your account',width:400});
				break;
			case 'news':
				Modalbox.show('/pieces/singlenews.asp?id='+id,{title:thetitle,width:940});
	}
}
function $I(elemento) {
	return document.getElementById(elemento);
}