/**
 * jQuery-PHP-Mysql Star Rating
 * This jQuery-PHP-Mysql plugin was inspired and based on jQuery Star Rating Plugin (http://www.fyneworks.com/jquery/star-rating/)
 * and adapted to me for use like a plugin from jQuery.
 * @name jQuery-PHP-Mysql Star Rating
 * @author Igor Jovičić - http://www.cashmopolit.hr
 * @version 1.0
 * @date August 14, 2010
 * @category jQuery plugin
 * @copyright (c) 2010 Igor Jovičić (www.cashmopolit.hr)
 * @license CC Attribution-No Derivative Works 2.5 Brazil - http://creativecommons.org/licenses/by-nd/2.5/br/deed.en_US
 * @example Visit http://www.cashmopolit.hr/howto/jquery/how_to_jquery_star_rating.php for more informations about this jQuery/PHP/Mysql plugin
 */
 
function setupRate(){

$('.hover-star').rating({
	focus: function(value, link){
		var id=$(this).attr('id').split('-')[0];
		var tip = $("span[name='hover-test["+id+"]']")
		tip[0].data = tip[0].data || tip.html();
		tip.html(link.title || 'value: '+value);
	},
	blur: function(value, link){
		var id=$(this).attr('id').split('-')[0];
		var tip = $("span[name='hover-test["+id+"]']")
		tip.html(tip[0].data || '');
	}
});

function readCookie(n)
{
 a = document.cookie;
 res = '';
 while(a != '')
 {
  while(a.substr(0,1) == ' '){a = a.substr(1,a.length);}
  cookiename = a.substring(0,a.indexOf('='));
  if(a.indexOf(';') != -1)
  {cookiewert = a.substring(a.indexOf('=')+1,a.indexOf(';'));}
  else{cookiewert = a.substr(a.indexOf('=')+1,a.length);}
  if(n == cookiename){res = cookiewert;}
  i = a.indexOf(';')+1;
  if(i == 0){i = a.length}
  a = a.substring(i,a.length);
 }
 return(res)
}

function getRating(id){
	var url = "http://www.heavyrubber.com/membersv2/includes-new/rating/rate-product.php";
	$.ajax({
		type: "GET",
		url: url,
		dataType : 'json',
		data: "do=getRate&product_id=" + id,
		cache: false,
		async: false,
		success: function(result) {
			avg=result.avg;
			sum=result.count;		
			$("span[name='vote["+id+"]']").html(sum);			
			$("input[name='rate["+id+"]']").rating('select',avg);
			if(readCookie('rw')==""){
				$("input[name='rate["+id+"]']").rating('readOnly',true);
			}
			if(window.location.pathname != "/membersv2/serie.asp"){
				$('.rate').each(function(){
				var id2=$(this).attr('id').split('-')[0];
				if(id==id2){
					$("#"+id2+"-1").unbind();
					$("#"+id2+"-2").unbind();
					$("#"+id2+"-3").unbind();
					$("#"+id2+"-4").unbind();
					$("#"+id2+"-5").unbind();
				}
			});
			$("input[name='rate["+id+"]']").rating('readOnly',true);
			}
		},
		error: function() {
			alert("Error - getRating, please try again!");
			//window.location = "http://www.heavyrubber.com/membersv2/";
		}
	});
}

$('.rate').click(function(){
	var id=$(this).attr('id').split('-')[0];
	var rate=$(this).text();
	var updid = $(this).attr('id');
	var url = "http://www.heavyrubber.com/membersv2/includes-new/rating/rate-product.php";
	if(readCookie('rw')==1){
		$.ajax({
			type: "GET",
			url: url,
			dataType : 'json',
			data: "do=rate&product_id=" + id + "&rate="+rate,
			cache: false,
			async: false,
			success: function(result) {
				avg=result.avg;
				sum=result.count;
				/*$("#votes").html("Average:" + avg);
				$(".vote_count").html(sum + " vote(s)");*/
				$("span[name='vote["+id+"]']").html(sum);
				$("input[name='rate["+id+"]']").rating('select',avg);
				$('.rate').each(function(){
					var id2=$(this).attr('id').split('-')[0];
					if(id==id2){
						$("#"+id2+"-1").unbind();
						$("#"+id2+"-2").unbind();
						$("#"+id2+"-3").unbind();
						$("#"+id2+"-4").unbind();
						$("#"+id2+"-5").unbind();
					}
				});
				$("input[name='rate["+id+"]']").rating('readOnly',true);
			},
			error: function() {
				alert("Error, please try again!");
			}
		});
	}
});

// Sterne aktualisieren
// rw=1 dann ist eingeloggt
var counter=1;
$("input[name^='rate[']").each(function() {
	if(counter==5){
		var id=$(this).attr('id').split('-')[0];
		getRating(id);
		counter=1;
	}
	counter++;
});
}

$(document).ready(setupRate);


