/*
AV Arcade Pro front-end javascript functions

Author: Andy Venus
Rating stars rollover by Addam M. Driver

*/

var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// AJAX for all browsers
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch(e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

// AJAX POST FUNCTION
function AjaxPost(url, param, success_function) {
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser doesn't support AJAX. You should upgrade it!")
        return
    }
    xmlHttp.onreadystatechange = success_function;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(param);
}

function div(d) {
    return document.getElementById(d);
}

// Rollover for image Stars //
function rating(num){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				holder = a+1;
				a++;
			}else{
				document.getElementById("_"+i).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
			}
		}else{
			rating(preSet);
		}
	}
}

// When you actually rate something //
function rateIt(me, id, site_url){
	if(!rated){
		preSet = me;
		rated=1;
		sendRate(me, id, site_url);
		rating(me);
	}
}

// Send the rating information somewhere using Ajax or something like that
function sendRate(sel, id, site_url){
	AjaxPost(site_url+"/content/forms/add_rating.php", "id=" + id + "&rating=" + sel.title, 
			 function () {}
	)
}

// Preloader for game
function in_fl()
{
       var load=document.getElementById('l_pr');
       var s_prcnt = document.getElementById('s_prcnt');
       var pix=parseInt(s_prcnt.innerHTML);
       var prcnt = Math.round(Math.random()*10);
       var time = Math.round(Math.random()*1000);
       var o_prcnt = parseInt(s_prcnt.innerHTML);
       s_prcnt.innerHTML = String(o_prcnt+prcnt);
       load.style.width=eval((pix+prcnt)*3)+'px';
       if (pix+prcnt<100)
               setTimeout("in_fl()",time);
       else
   {
       document.getElementById('g_c').style.display='none';
       document.getElementById('fl_obj').style.display='block';
   }
}

