/*
 * jQuery UI Stars v2.0.1
 *
 * Copyright (c) 2009 Orkan (orkans@gmail.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Rev: 50 $
 * $Date:: 2009-05-05 #$
 * $Build: 6 (2009-05-17)
 *
 * Depends:
 *  ui.core.js
 *
 */

/* RAM.Tube Rating INFO 
 *
 * You can use your own rating script if you want. 
 * The following are important:
 *  - url to submit to: http://www.yourdomain.com/ram/public/ram.rating.php
 *  - Parameters: rate & rid (rate is the vote, rid is the Video RID {%name:rid%} where name is your custom template name
 *  - ram.rating.php returns the result in the format: average|votes.
 *  - average (#avg) must be in the template, as this will set the stars on initiation. If you don't want to display the average, then use style display:none.
 */

(function(A){A.widget("ui.stars",{_init:function(){var B=this,E=this.options;E.isSelect=E.inputType=="select";this.$selec=E.isSelect?A("select",this.element):null;this.$rboxs=E.isSelect?A("option",this.$selec):A(":radio",this.element);this.$stars=this.$rboxs.map(function(H){if(H==0){E.split=typeof E.split!="number"?0:E.split;E.val2id=[];E.id2val=[];E.id2title=[];E.name=E.isSelect?B.$selec.get(0).name:this.name;E.disabled=E.disabled||(E.isSelect?A(B.$selec).attr("disabled"):A(this).attr("disabled"));E.items=0}E.items++;E.val2id[this.value]=H;E.id2val[H]=this.value;E.id2title[H]=(E.isSelect?this.text:this.title)||this.value;if(E.selected==H||(E.selected==-1&&(E.isSelect?this.defaultSelected:this.defaultChecked))){E.checked=H;E.value=E.id2val[H];E.title=E.id2title[H]}var G=A("<div/>").addClass(E.starClass);var I=A("<a/>").attr("title",E.showTitles?E.id2title[H]:"").text(this.value);if(E.split){var F=(H%E.split);var J=Math.floor(E.starWidth/E.split);G.width(J);I.css("margin-left","-"+(F*J)+"px")}return G.append(I).get(0)});this.$cancel=A("<div/>").addClass(E.cancelClass).append(A("<a/>").attr("title",E.showTitles?E.cancelTitle:"").text(E.cancelValue));this.$value=A('<input type="hidden" name="'+E.name+'" value="'+E.value+'" />');E.cancelShow&=!E.disabled&&!E.oneVoteOnly;if(E.cancelShow){this.element.append(this.$cancel)}this.element.append(this.$stars);this.element.append(this.$value);E.isSelect?this.$selec.remove():this.$rboxs.remove();if(E.checked===undefined){E.checked=-1;E.value=E.cancelValue;E.title="";if(E.cancelShow){this._disableCancel()}}else{C(E.checked,false)}E.disabled&&this.disable();A(window).bind("unload",function(){B.$cancel.unbind(".stars");B.$stars.unbind(".stars");B.$selec=B.$rboxs=B.$stars=B.$value=B.$cancel=null});function D(){B.$stars.removeClass(E.starOnClass+" "+E.starHoverClass);B._showCap("")}function C(F,H){if(F!=-1){var I=H?E.starHoverClass:E.starOnClass;var G=H?E.starOnClass:E.starHoverClass;B.$stars.eq(F).prevAll("."+E.starClass).andSelf().removeClass(G).addClass(I);B.$stars.eq(F).nextAll("."+E.starClass).removeClass(E.starHoverClass+" "+E.starOnClass);B._showCap(E.id2title[F])}else{D()}}this.$stars.bind("click.stars",function(){if(!E.forceSelect&&E.disabled){return false}var F=B.$stars.index(this);E.checked=F;E.value=E.id2val[F];E.title=E.id2title[F];B.$value.attr({disabled:E.disabled?"disabled":"",value:E.value});C(F,false);B._disableCancel();if(!E.forceSelect){B.callback("star")}}).bind("mouseover.stars",function(){if(E.disabled){return false}var F=B.$stars.index(this);C(F,true)}).bind("mouseout.stars",function(){if(E.disabled){return false}C(B.options.checked,false)});this.$cancel.bind("click.stars",function(){if(!E.forceSelect&&(E.disabled||(E.value==E.cancelValue))){return false}E.checked=-1;E.value=E.cancelValue;E.title="";B.$value.attr({disabled:"disabled",value:E.value});D();B._disableCancel();(!E.forceSelect)&&B.callback("cancel")}).bind("mouseover.stars",function(){if(B._disableCancel()){return false}B.$cancel.addClass(E.cancelHoverClass);D();B._showCap(E.cancelTitle)}).bind("mouseout.stars",function(){if(B._disableCancel()){return false}B.$cancel.removeClass(E.cancelHoverClass);B.$stars.triggerHandler("mouseout.stars")})},_disableCancel:function(){var C=this.options,B=C.disabled||C.oneVoteOnly||(C.value==C.cancelValue);if(B){this.$cancel.removeClass(C.cancelHoverClass).addClass(C.cancelDisabledClass)}else{this.$cancel.removeClass(C.cancelDisabledClass)}this.$cancel.css("opacity",B?0.5:1);return B},_disableAll:function(){var B=this.options;this._disableCancel();if(B.disabled){this.$stars.filter("div").addClass(B.starDisabledClass)}else{this.$stars.filter("div").removeClass(B.starDisabledClass)}},_showCap:function(B){var C=this.options;if(C.captionEl){C.captionEl.text(B)}},value:function(){return this.options.value},select:function(D){var C=this.options,B=(D==C.cancelValue)?this.$cancel:this.$stars.eq(C.val2id[D]);C.forceSelect=true;B.triggerHandler("click.stars");C.forceSelect=false},selectID:function(D){var C=this.options,B=(D==-1)?this.$cancel:this.$stars.eq(D);C.forceSelect=true;B.triggerHandler("click.stars");C.forceSelect=false},enable:function(){this.options.disabled=false;this._disableAll()},disable:function(){this.options.disabled=true;this._disableAll()},destroy:function(){this.options.isSelect?this.$selec.appendTo(this.element):this.$rboxs.appendTo(this.element);this.$cancel.unbind(".stars").remove();this.$stars.unbind(".stars").remove();this.$value.remove();this.element.unbind(".stars").removeData("stars")},callback:function(B){var C=this.options;C.callback(this,B,C.value);C.oneVoteOnly&&!C.disabled&&this.disable()}});A.extend(A.ui.stars,{version:"2.0.1",getter:"value",defaults:{inputType:"radio",split:0,selected:-1,disabled:false,cancelTitle:"Cancel Rating",cancelValue:0,cancelShow:true,oneVoteOnly:false,showTitles:false,captionEl:null,callback:function(C,B,D){},starWidth:16,cancelClass:"ui-stars-cancel",starClass:"ui-stars-star",starOnClass:"ui-stars-star-on",starHoverClass:"ui-stars-star-hover",starDisabledClass:"ui-stars-star-disabled",cancelHoverClass:"ui-stars-cancel-hover",cancelDisabledClass:"ui-stars-cancel-disabled"}})})(jQuery);

$(document).ready(function() {
	
	/* get domain name */
	var myDomain = location.protocol+'//'+location.hostname;

	/* get video id name */
	var id = $("#rid").val();

	if(id){
		/* check if visitor has voted */
		$.post(myDomain+"/ram/public/ram.rating.php", {rid: id}, function(data){
			initiate_rating((data==1?true:false));
		});

		/* initiate rating stars */
		function initiate_rating(voted){
			$("#ram_rating").children().not(":radio").hide();
			$("#ram_rating").stars({
				oneVoteOnly: true,
				disabled: voted,
				captionEl: $("#stars-cap"),
				callback: function(ui, type, value)
				{
					$("#ram_rating").hide();
					$("#loader").show();
					
					/* post visitors vote */
					$.post(myDomain+"/ram/public/ram.rating.php", {rate: value, rid: id}, function(data)
					{
						/* update video ratings from returned data */
						if(data.indexOf("|")!=-1){
							var results = data.split("|");
							var average = results[0];
							var votes = results[1];
							ui.select(Math.round(average));
							$("#avg").text(average);
							$("#votes").text(votes);
						}
						$("#loader").hide();
						$("#ram_rating").show();
					});
				}
			});
			/* on init, select radio option showing amount of stars */
			$("#ram_rating").stars("select", Math.round($("#avg").html()));
		};
	};
});
