/*
Author: Addam M. Driver
Date: 10/31/2006
*/

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 $(d) {
    return document.getElementById(d);
}


// When you actually rate something //
function rateIt(me, id, site_url){
	if(!rated){
		preSet = me;
		rated=1;
		sendRate(me, id, site_url);
		if (me.title == "1"){rating_class = "onestar";}
		else if (me.title == "2"){rating_class = "twostar";}
		else if (me.title == "3"){rating_class = "threestar";}
		else if (me.title == "4"){rating_class = "fourstar";}
		else if (me.title == "5"){rating_class = "fivestar";}
		else {rating_class = "nostar";}
		document.getElementById("newRate").className = ("rated "+rating_class);
	}
}

// Send the rating information somewhere using Ajax or something like that
function sendRate(sel, id, site_url){
	AjaxPost(site_url+"/includes/view_game/ajax/add_rating.php", "id=" + id + "&rating=" + sel.title, 
			 function () {}
	)
}

//counts gameviews as well as tracks currently played games and collects similar game data
function collectStats(id, userid, site_url){
	AjaxPost(site_url+"/includes/view_game/ajax/collect_stats.php", "id=" + id + "&userid=" + userid, 
			 function () {}
	);
}

// ADD/DELETE FAV GAME
function AddFav(id, typ, site_url, unfav, fav) {
	AjaxPost(site_url+"/includes/view_game/ajax/add_fav.php", "id=" + id, 
			 function () {}
	)
	
	if (typ == 0) {
		$('favbutton').innerHTML = '<a href="#" onclick="AddFav('+id+', 1, \''+site_url+'\', \''+unfav+'\', \''+fav+'\'); return false">'+unfav+'</a>';
	}
	else {
		$('favbutton').innerHTML = '<a href="#" onclick="AddFav('+id+', 0, \''+site_url+'\', \''+unfav+'\', \''+fav+'\'); return false">'+fav+'</a>';
	}
}


// ADD COMMENT

function AddComment(id, site_url) {
	$('comment_submit').disabled=true;
	$('comment_submit').value="Adding comment...";
	
	thecomment = $('the_comment').value; 
	
	AjaxPost(site_url+"/includes/view_game/ajax/add_comment.php", "comment="+thecomment+"&id="+id, 
	function () {
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
 			if (xmlHttp.responseText == '') {
	 			alert("An error occured in sending your message");
 			}
 			else {
  				var container = document.getElementById('comment_list');
  				var new_element = document.createElement('li');
  				new_element.innerHTML = xmlHttp.responseText;
  				container.insertBefore(new_element, container.firstChild);
  				window.location.hash="1"; 
  				$('comment_submit').value="Comment added!";
  				setTimeout("EnableButton()",30000);
  			}
		}
	}
	)
}

// Re-enable add-comment button

function EnableButton () {
	$('comment_submit').value="Add comment";
	$('comment_submit').disabled=false;
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}
    
function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

// Ajax delete comment
function DeleteComment(id, site_url) {
	AjaxPost(site_url+"/admin/includes/delete_comment.php", "id=" + id, 
			 function () {
					$('comment-' + id).style.display = 'none';
    		}
	)
}

//converts a time in miliseconds to the standard Days:Hours:Mins:Secs
function time(ms) {
	var sec = Math.floor(ms/1000)
	ms = ms % 1000
	t = three(ms)

	var min = Math.floor(sec/60)
	sec = sec % 60
	t = two(sec) + ":" + t

	var hr = Math.floor(min/60)
	min = min % 60
	t = two(min) + ":" + t

	var day = Math.floor(hr/60)
	hr = hr % 60
	t = two(hr) + ":" + t
	t = day + ":" + t

	return t
}
