/*
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;

var xmlHttp;
var rootpath;
var multiple;
rootpath = "http://www.everything-jujitsu.co.uk";   // be sure to change this to your URL

function storeRating(videoID, c_type, s) {
  multiple = videoID.substring(0, videoID.length - 4);
  var url = rootpath + "/media/dbEntry.asp?id=" + multiple + "&type=" + c_type + "&stars=" + s;

  xmlHttp = GetXmlHttpObject(stateChanged);
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function stateChanged() 
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    var textString;
    var textString2;
    var pos;
    textString = xmlHttp.responseText;
    //alert(textString);
    pos = textString.indexOf("|");
    textString2 = textString.substring(pos+1, textString.length);
    textString = textString.substring(0, pos);
    document.getElementById(multiple + "r").innerHTML = textString;
    document.getElementById(multiple + "v").innerHTML = textString2;
  }
}

function GetXmlHttpObject(handler) {
  var objXmlHttp = null;

  if (navigator.userAgent.indexOf("Opera") >= 0) {
    //alert("Opera not supported...")
    return;
  }
  if (navigator.userAgent.indexOf("MSIE") >= 0) {
    var strName = "Msxml2.XMLHTTP";
    if (navigator.appVersion.indexOf("MSIE 5.5") >= 0) {
      strName = "Microsoft.XMLHTTP";
    }
    try {
      objXmlHttp = new ActiveXObject(strName);
      objXmlHttp.onreadystatechange = handler;
      return objXmlHttp;
    }
    catch (e) {
      //alert("Error. Scripting for ActiveX might be disabled")
      return;
    }
  }
  if (navigator.userAgent.indexOf("Mozilla") >= 0) {
    objXmlHttp = new XMLHttpRequest();
    objXmlHttp.onload = handler;
    objXmlHttp.onerror = handler;
    return objXmlHttp;
  }
} 

// 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++;	
		}
  }

  s = num.id.substring(num.id.length - 3, num.id.length - 2);
  multiple = num.id.substring(0, num.id.length - 4);
  rated = num.id.substring(num.id.length - 1, num.id.length);

	if(rated == 0)
	{
	  a = 0;
		for(i=1; i<=sMax; i++)
		{		
			if(i<=s) {
				document.getElementById(multiple+"_"+i+"_"+rated).className = "on";
				//document.getElementById("rateStatus").innerHTML = num.title;	
				holder = a+1;
				a++;
			}
			else
			{
			  document.getElementById(multiple + "_" + i + "_" + rated).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me) 
{
  s = me.id.substring(me.id.length - 3, me.id.length - 2);
  multiple = me.id.substring(0, me.id.length - 4);
  rated = me.id.substring(me.id.length - 1, me.id.length);
  
  if (rated == 0)
	{
	  for (i = 1; i <= sMax; i++)
		{
		  document.getElementById(multiple + "_" + i + "_" + rated).className = "";
			//document.getElementById("rateStatus").innerHTML = me.parentNode.title;
		}
	}
}

// When you actually rate something //
function rateIt(me, c_type) 
{
  s = me.id.substring(me.id.length - 3, me.id.length - 2);
  multiple = me.id.substring(0, me.id.length - 4);
  rated = me.id.substring(me.id.length - 1, me.id.length);
  
  if (rated == 0) {
    
	  rated2 = 1;
		//document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
	  for (i = 1; i <= sMax; i++)
		{
		  temp = document.getElementById(multiple + "_" + i + "_" + rated);
		  temp.id = multiple + "_" + i + "_" + rated2;
		  //document.getElementById("rateStatus").innerHTML = me.parentNode.title;
		}
		sendRate(me, c_type, s);
		rating(me);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel, c_type, s)
{
  storeRating(sel.id, c_type, s);
}


