/*
 * jQuery UI Stars v1.1
 *
 * Copyright (c) 2008 Orkan (orkans@gmail.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Depends:
 *	ui.core.js
 *
 */
;(function($){$.widget("ui.stars",{init:function(){var e=this,o=this.options;o.isSelect=o.inputType=="select";this.$selec=o.isSelect?$("select",this.element):null;this.$rboxs=o.isSelect?$("option",this.$selec):$(":radio",this.element);this.$stars=this.$rboxs.map(function(i){if(i==0){o.split=typeof o.split!="number"?0:o.split;o.val2id=[];o.id2val=[];o.id2title=[];o.name=o.isSelect?e.$selec.get(0).name:this.name;o.disabled=o.disabled||(o.isSelect?$(e.$selec).attr('disabled'):$(this).attr('disabled'));o.items=0}o.items++;o.val2id[this.value]=i;o.id2val[i]=this.value;o.id2title[i]=(o.isSelect?this.text:this.title)||this.value;if(o.selected==i||(o.selected==-1&&(o.isSelect?this.defaultSelected:this.defaultChecked))){o.checked=i;o.value=o.id2val[i];o.title=o.id2title[i]}var a=$("<div/>").addClass(o.starClass);var b=$('<a/>').attr("title",o.showTitles?o.id2title[i]:"").text(this.value);if(o.split){var c=(i%o.split);var d=Math.floor(o.starWidth/o.split);a.width(d);b.css("margin-left","-"+(c*d)+"px")}return a.append(b).get(0)});this.$cancel=$("<div/>").addClass(o.cancelClass).append($("<a/>").attr("title",o.showTitles?o.cancelTitle:"").text(o.cancelValue));this.$value=$('<input type="hidden" name="'+o.name+'" value="'+o.value+'" />');o.cancelShow&=!o.disabled&&!o.oneVoteOnly;if(o.cancelShow)this.element.append(this.$cancel);this.element.append(this.$stars);this.element.append(this.$value);o.isSelect?this.$selec.remove():this.$rboxs.remove();if(o.checked===undefined){o.checked=-1;o.value=o.cancelValue;o.title="";if(o.cancelShow)this._disableCancel()}else{fillTo(o.checked,false)}if(o.disabled)this.disable();$(window).bind("unload",function(){e.$cancel.unbind(".stars");e.$stars.unbind(".stars");e.$selec=e.$rboxs=e.$stars=e.$value=e.$cancel=null});function fillNone(){e.$stars.removeClass([o.starOnClass,o.starHoverClass].join(' '));e._showCap("")}function fillTo(a,b){if(a!=-1){var c=b?o.starHoverClass:o.starOnClass;var d=b?o.starOnClass:o.starHoverClass;e.$stars.eq(a).prevAll("."+o.starClass).andSelf().removeClass(d).addClass(c);e.$stars.eq(a).nextAll("."+o.starClass).removeClass([o.starHoverClass,o.starOnClass].join(' '));e._showCap(o.id2title[a])}else fillNone()}this.$stars.bind("click.stars",function(){if(!o.forceSelect&&o.disabled)return false;var i=e.$stars.index(this);o.checked=i;o.value=o.id2val[i];o.title=o.id2title[i];e.$value.attr({disabled:o.disabled?"disabled":"",value:o.value});fillTo(i,false);e._disableCancel();if(!o.forceSelect){e.callback("star")}}).bind("mouseover.stars",function(){if(o.disabled)return false;var i=e.$stars.index(this);fillTo(i,true)}).bind("mouseout.stars",function(){if(o.disabled)return false;fillTo(e.options.checked,false)});this.$cancel.bind("click.stars",function(){if(!o.forceSelect&&(o.disabled||(o.value==o.cancelValue)))return false;o.checked=-1;o.value=o.cancelValue;o.title="";e.$value.attr({value:o.value,disabled:"disabled"});fillNone();e._disableCancel();if(!o.forceSelect){e.callback("cancel")}}).bind("mouseover.stars",function(){if(e._disableCancel())return false;e.$cancel.addClass(o.cancelHoverClass);fillNone();e._showCap(o.cancelTitle)}).bind("mouseout.stars",function(){if(e._disableCancel())return false;e.$cancel.removeClass(o.cancelHoverClass);e.$stars.triggerHandler("mouseout.stars")})},select:function(a){var o=this.options;o.forceSelect=true;if(a==o.cancelValue)this.$cancel.triggerHandler("click.stars");else this.$stars.eq(o.val2id[a]).triggerHandler("click.stars");o.forceSelect=false},selectID:function(a){var o=this.options;o.forceSelect=true;if(a==-1)this.$cancel.triggerHandler("click.stars");else this.$stars.eq(a).triggerHandler("click.stars");o.forceSelect=false},enable:function(){this.options.disabled=false;this._disableAll()},disable:function(){this.options.disabled=true;this._disableAll()},_disableCancel:function(){var o=this.options,disabled=o.disabled||o.oneVoteOnly||(o.value==o.cancelValue);if(disabled)this.$cancel.removeClass(o.cancelHoverClass).addClass(o.cancelDisabledClass);else this.$cancel.removeClass(o.cancelDisabledClass);this.$cancel.css("opacity",disabled?0.5:1);return disabled},_disableAll:function(){var o=this.options;this._disableCancel();if(o.disabled)this.$stars.filter("div").addClass(o.starDisabledClass);else this.$stars.filter("div").removeClass(o.starDisabledClass)},_showCap:function(s){var o=this.options;if(o.captionEl)o.captionEl.text(s)},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(a){var o=this.options;o.callback(this,a,o.value);if(o.oneVoteOnly&&!o.disabled)this.disable()}});$.ui.stars.defaults={inputType:"radio",split:0,selected:-1,disabled:false,cancelTitle:"Cancel Rating",cancelValue:0,cancelShow:true,oneVoteOnly:false,showTitles:false,captionEl:null,callback:function(a,b,c){},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);