var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value once a selection has been made
var rated;
var pass_id = '';
var pass_mem_id = ''
var avg = '';
var status = '';
var xmlHttp;

//--------------- show image rate
function star_up(img_id,memID){
	avg = '';
	status = '';
	pass_id = img_id;
	pass_mem_id = memID;
// Get Image Votes
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	  var url ="showImgRate.php?rand="+Math.random()+"&photo_id="+img_id;
	  xmlHttp.onreadystatechange=stateChanged;
	  xmlHttp.open("GET",url,true);
	  xmlHttp.send(null);

//	  setTimeout('displayAvg()',1000);
}

function GetXmlHttpObject(){
	var objXMLHttp=null;
	try{
	  // Firefox, Opera 8.0+, Safari
	  objXMLHttp=new XMLHttpRequest();
	}
	catch (e){
	  // Internet Explorer
	  try{
		objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	return objXMLHttp;
}

function stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		xmlDoc = xmlHttp.responseXML;
			avg = xmlDoc.getElementsByTagName("average")[0].firstChild.nodeValue;
			status = xmlDoc.getElementsByTagName("memCount")[0].firstChild.nodeValue;
	}
	 displayAvg();
}

function displayAvg(){
	  if(avg == 0){
		  for(i=1; i<6; i++){
			  document.getElementById("_"+i).className = "off";
			  document.getElementById("memCount").innerHTML = "Out of "+status+" vote/s.";	
		  }		  
	  }
	  else{
	  	  for(i=1; i<=avg; i++){
			  document.getElementById("_"+i).className = "on";
			  document.getElementById("memCount").innerHTML = "Out of "+status+" vote/s.";	
		  }
	  }

}




//------------------------------------------------------------------------------
// 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";
				document.getElementById("rateStatus").innerHTML = num.title;	
				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 = "";
				document.getElementById("rateStatus").innerHTML = me.parentNode.title;
			}
		}
		else{
			rating(preSet);
			document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
		}
	}
}

// When you actually rate something //
function rateIt(me,vote){
	if(!rated){
		document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
		preSet = me;
//		rated=1;
		sendRate(me,vote);
		rating(me);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel,count){
	alert("Your rating was: "+sel.title+" Vote: "+count);
		var xmlHttp;
		  try{    // Firefox, Opera 8.0+, Safari    
			xmlHttp=new XMLHttpRequest();    
		  }
		  catch (e){    // Internet Explorer    
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
			}
			catch (e){
				try{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e){
				  alert("Your browser does not support AJAX!");        
				  return false;
				}      
			}    
		 }
			xmlHttp.onreadystatechange=function(){
				if(xmlHttp.readyState==4){
				}
				else{
				}
			}

			var param = 'rand=' + Math.random() + 
						'&customer_id='+pass_mem_id+
						'&photo_id='+pass_id+
						'&rating='+count;
			xmlHttp.open('POST','rateImg.php', true);    
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
			xmlHttp.setRequestHeader("Content-length", param.length);    
			xmlHttp.setRequestHeader("Connection", "close");    
			xmlHttp.send(param);
			imgGalleryView();
}
