var rate_fadein_opacity = 0;
var rate_fadeout_opacity = 100;
var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);

function showIt(item,i,j,max){
   var id;
   actualItem = item;   
   for (var x=1;x<=i;x++){
      id = item + "_" + x;
      document.getElementById(id).src = "/images/b.gif";
   }
   for (var x=i+1;x<=max;x++){
      id = item + "_" + x;
      if (x<=j) document.getElementById(id).src = "/images/y.gif";
      else document.getElementById(id).src = "/images/w.gif";
   }
}

function showOriginal(i,max){
   for (var x=1;x<=max;x++){
      id = actualItem + "_" + x;
      if (x<=i) document.getElementById(id).src = "/images/y.gif";
      else document.getElementById(id).src = "/images/w.gif";
   }
}

// Get the HTTP Object
function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Ваш браузер не поддерживает технологию AJAX.");
      return null;
   }
}   

// Change the value of the outputText field
function setOutput(){
   var result;
   var data;
   var rating;
   var totalRates;
   var max;
   var msg;
   if(httpObject.readyState == 4) {
      result = httpObject.responseText;
      data = result.split(':::');
      rating     = data[0];
      totalRates = data[1];
      max        = data[2];
      msg			= data[3];      
      removeActions(Math.round(rating),max);
      updateTextRating(actualItem, rating, totalRates, msg)		
   }
	rade_fadein_text();
}

function removeActions(rating,max){
      showOriginal(rating,max);
      var t;
      for (var x=1;x<=max;x++){
         id = actualItem + "_" + x;
         t = document.getElementById(id);
         t.onmouseover = null;
         t.onmouseout  = null;
         t.onclick     = null;
         t.style.cursor = "default";
      }  
}

function updateTextRating(item, rating, total, msg){
   var itemRating;
   var totalRating;
   var itemMsg;
   var id; 
   
   id = item + "_ir";
   itemRating = document.getElementById(id);
   id = item + "_tr";
   totalRating = document.getElementById(id);
   id = item + "_im";
   itemMsg = document.getElementById(id);
   
   if (itemRating != null) itemRating.innerHTML = rating;
   if (totalRating != null) totalRating.innerHTML = total;
   if (itemMsg != null) itemMsg.innerHTML = msg;
   
}

function updateRatingStatus(item, msg){
   var itemMsg;
   var id;    
   id = item + "_im";
   itemMsg = document.getElementById(id);   
   if (itemMsg != null) itemMsg.innerHTML = msg;
   
}

function submitRating(item,rate,max){
   actualItem = item;
	rate_process(item,rate,max);
	updateRatingStatus(actualItem, "<img src=\"/images/ajax-loader.gif\" style=\"vertical-align:middle;\" />&nbsp;Обновление рейтинга...");
}

function rate_process(item,rate,max) {
	if(rate_fadeout_opacity > 0) {
		rate_fadeout_opacity -= 10;
		if(is_opera) {
			rate_fadein_opacity = 0;
		} else if(is_ie) {
			document.getElementById('rating').style.filter = 'alpha(opacity=' + rate_fadeout_opacity + ')';
		} else if(is_moz) {
			document.getElementById('rating').style.MozOpacity = (rate_fadeout_opacity/100);
		}
		//setTimeout("rate_process()", 100);
		setTimeout(function(){rate_process(item,rate,max)},100);
	} else {
		rate_fadeout_opacity = 0;
		httpObject = getHTTPObject();
		httpObject.open("GET", "/submitRating.php?item="+item+"&rating="+rate+"&max="+max, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
		rate_fadein_opacity = 0;
		rate_fadeout_opacity = 100;
	}
}

function rade_fadein_text() {
	if(rate_fadein_opacity < 100) {
		rate_fadein_opacity += 10;
		if(is_opera)  {
			rate_fadein_opacity = 100;
		} else	 if(is_ie) {
			document.getElementById('rating').style.filter = 'alpha(opacity=' + rate_fadein_opacity + ')';
		} else	 if(is_moz) {
			document.getElementById('rating').style.MozOpacity = (rate_fadein_opacity/100);
		}
		setTimeout("rade_fadein_text()", 100); 
	} else {
		rate_fadein_opacity = 100;
	}
}
 
var httpObject = null;
var actualItem = null;
