﻿function processXmlResponse(req, status){
  if(status != 'parsererror')
  {
    xmlDoc = null;
    if(window.DOMParser)
    {
      parser=new DOMParser();
      xmlDoc=parser.parseFromString(req.responseText,"text/xml");
    }
    else // Internet Explorer
    {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = "false" ;
      xmlDoc.loadXML(req.responseText);
    }
	return xmlDoc;
  }
  else{
    alert("Error");	  
  }
}
$.fn.rater = function(options) {
    var opts = $.extend({}, $.fn.rater.defaults, options);
    return this.each(function(){
        var $this = $(this);
        var $on = $this.find('.ui-rater-starsOn');
        var $off = $this.find('.ui-rater-starsOff');
        opts.size = $on.height();
        if (opts.rating == undefined) opts.rating = $on.width() / opts.size;
        if (opts.id == undefined) opts.id = $this.prev().text();
        $off.mousemove(function(e) {
            var left = e.clientX - $off.offset().left;
            var width = $off.width() - ($off.width() - left);
            width = Math.ceil(width / (opts.size / opts.step)) * opts.size / opts.step;
            $on.width(width);
        }).hover(function(e) { $on.addClass('ui-rater-starsHover'); }, function(e) {
            $on.removeClass('ui-rater-starsHover'); $on.width(opts.rating * opts.size);
        }).click(function(e) {
            var r = Math.round($on.width() / $off.width() * (opts.units * opts.step)) / opts.step;
            $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
            $off.css('cursor', 'default'); $on.css('cursor', 'default');
            $.fn.rater.rate($this, opts, r);
        }).css('cursor', 'pointer'); $on.css('cursor', 'pointer');
    });
};

$.fn.rater.defaults = {
    postHref: "",
    units: 5,
    step: 1
};

$.fn.rater.rate = function($this, opts, rating) {
    var root_url = window.root_url;    
	var $on = $this.find('.ui-rater-starsOn');
    var $off = $this.find('.ui-rater-starsOff');
	var item_id = $this.prev().text();
	var rating = $on.width() / opts.size;
	var cookie = checkCookie("rating_item_" + item_id);
	if(cookie){	
		$off.fadeTo(600, 0.4, function() {
			$.ajax({
				url: root_url + "/ar/item/xmlrating/" + item_id + "/" + rating,
				type: "POST",
				complete: function(req,status) {
					if (req.status == 200) { //success
                        xmlDoc = processXmlResponse(req, status);
						opts.rating = parseFloat($(xmlDoc).find("status").text());
						$off.fadeTo(600, 0.1, function() {
							$on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
							var $count = $this.find('.ui-rater-rateCount');
							$count.text(parseInt($count.text()) + 1);
							$this.find('.ui-rater-rating').text(opts.rating.toFixed(1));
							$off.fadeTo(600, 1);
							$this.attr('title', 'تقييمك : ' + rating);  
							setCookie("rating_item_" + item_id,rating,30);                      						
						});
					} else { //failure
						alert("Error");
						$on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
						$this.rater(opts);
						$off.fadeTo(2200, 1);
					}
				}
			});
		});
	}else{
           alert("لقد قمت بتقييم هذا المنتج من قبل وكان تقييمك : " + getCookie("rating_item_" + item_id));
           $on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
           $this.rater(opts);
           $off.fadeTo(2200, 1);
		   $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
		   $off.css('cursor', 'default'); $on.css('cursor', 'default');
           $this.attr('title', 'تقييمك : ' + getCookie("rating_item_" + item_id));		   
		}
};

function setCookie(c_name,value,exdays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function checkCookie(c_name)
{
  var cookie=getCookie(c_name);
  if (cookie!=null && cookie!="")
  {
    return false;
  }
  else
  {
    return true;
  }
}
