/*
Script: rating.js
	Contains <rating>

Author:
	Alan Roemen

Class: rating
	A 5 star link rating system

Options:
	starImgOn : Star image used on mouseover or successfull vote. Default: false
	starImgOff : Star image used on load and to show empty rated star. Default: false
	baseURL : Page location. Default: false
	className : CSS class name. Default: 'LINKRating'
*/
var LinkRating=new Class({options:{starImgOn:false,starImgOff:false,baseURL:false,className:"LINKRating",scriptName:"rating",autoFillEmail:true},initialize:function(G,C){var B,E,A="";this.setOptions(C);this.field=$(G);if(!this.field){return }this.field.className=this.options.className;if(!this.options.baseURL){var F=document.getElementsByTagName("script");for(var D=0;D<F.length;D++){if(F[D].src&&(F[D].src.indexOf(this.options.scriptName+".js")!=-1)){var H=F[D].src;this.options.baseURL=H.substring(0,H.lastIndexOf("/"));break}}this.documentBasePath=document.location.href;if(this.documentBasePath.indexOf("?")!=-1){this.documentBasePath=this.documentBasePath.substring(0,this.documentBasePath.indexOf("?"))}this.documentBasePath=this.documentBasePath.substring(0,this.documentBasePath.lastIndexOf("/"));if(this.options.baseURL.indexOf("://")==-1&&this.options.baseURL.charAt(0)!="/"){this.options.baseURL=this.documentBasePath+"/"+this.options.baseURL}}new Asset.css(this.options.baseURL+"/"+this.options.scriptName+".css");this.link=this.field.innerHTML;this.field.setHTML("");this.url=this.options.baseURL+"/"+this.options.scriptName+".php";if(!this.options.starImgOn){this.options.starImgOn=this.options.baseURL+"/star.png"}if(!this.options.starImgOff){this.options.starImgOff=this.options.baseURL+"/star_grey.png"}this.voted=false;this.start();if(this.options.autoFillEmail==true){this.getEmail()}},start:function(){new Ajax(this.url,{method:"post",data:"link="+this.link+"&get_vote=true",onComplete:function(A){this.build();if(A!="false"){this.vote(A,false);this.complete("true")}}.bind(this)}).request()},getEmail:function(){new Ajax(this.url,{method:"get",data:"get_email=true",onComplete:function(A){if(A!="false"){this.user_email=A}else{this.user_email=""}}.bind(this)}).request()},build:function(){this.star=new Array();this.star[0]=new Element("img",{"src":this.options.starImgOff,"styles":{"cursor":"pointer"},"events":{"mouseover":(function(){if(this.voted){return }this.rate(1);this.star[0].src=this.options.starImgOn}).bindWithEvent(this),"mouseout":(function(){if(this.voted){return }this.rate(false);this.star[0].src=this.options.starImgOff}).bindWithEvent(this),"click":(function(){if(this.voted){return }this.vote(1);this.show()}).bindWithEvent(this)}}).injectInside(this.field);this.star[1]=new Element("img",{"src":this.options.starImgOff,"styles":{"cursor":"pointer"},"events":{"mouseover":(function(){if(this.voted){return }this.rate(2);this.star[0].src=this.options.starImgOn;this.star[1].src=this.options.starImgOn}).bindWithEvent(this),"mouseout":(function(){if(this.voted){return }this.rate(false);this.star[0].src=this.options.starImgOff;this.star[1].src=this.options.starImgOff}).bindWithEvent(this),"click":(function(){if(this.voted){return }this.vote(2);this.show()}).bindWithEvent(this)}}).injectInside(this.field);this.star[2]=new Element("img",{"src":this.options.starImgOff,"styles":{"cursor":"pointer"},"events":{"mouseover":(function(){if(this.voted){return }this.rate(3);this.star[0].src=this.options.starImgOn;this.star[1].src=this.options.starImgOn;this.star[2].src=this.options.starImgOn}).bindWithEvent(this),"mouseout":(function(){if(this.voted){return }this.rate(false);this.star[0].src=this.options.starImgOff;this.star[1].src=this.options.starImgOff;this.star[2].src=this.options.starImgOff}).bindWithEvent(this),"click":(function(){if(this.voted){return }this.vote(3);this.show()}).bindWithEvent(this)}}).injectInside(this.field);this.star[3]=new Element("img",{"src":this.options.starImgOff,"styles":{"cursor":"pointer"},"events":{"mouseover":(function(){if(this.voted){return }this.rate(4);this.star[0].src=this.options.starImgOn;this.star[1].src=this.options.starImgOn;this.star[2].src=this.options.starImgOn;this.star[3].src=this.options.starImgOn}).bindWithEvent(this),"mouseout":(function(){if(this.voted){return }this.rate(false);this.star[0].src=this.options.starImgOff;this.star[1].src=this.options.starImgOff;this.star[2].src=this.options.starImgOff;this.star[3].src=this.options.starImgOff}).bindWithEvent(this),"click":(function(){if(this.voted){return }this.vote(4);this.show()}).bindWithEvent(this)}}).injectInside(this.field);this.star[4]=new Element("img",{"src":this.options.starImgOff,"styles":{"cursor":"pointer"},"events":{"mouseover":(function(){if(this.voted){return }this.rate(5);this.star[0].src=this.options.starImgOn;this.star[1].src=this.options.starImgOn;this.star[2].src=this.options.starImgOn;this.star[3].src=this.options.starImgOn;this.star[4].src=this.options.starImgOn}).bindWithEvent(this),"mouseout":(function(){if(this.voted){return }this.rate(false);this.star[0].src=this.options.starImgOff;this.star[1].src=this.options.starImgOff;this.star[2].src=this.options.starImgOff;this.star[3].src=this.options.starImgOff;this.star[4].src=this.options.starImgOff}).bindWithEvent(this),"click":(function(){if(this.voted){return }this.vote(5);this.show()}).bindWithEvent(this)}}).injectInside(this.field);this.txt=new Element("span").injectInside(this.field)},rate:function(A){if(A===false){this.txt.setHTML("")}else{this.txt.setHTML(A+" of 5 Stars")}},vote:function(C,B){if(B!==false){B=true}this.voted=true;this.txt.setHTML("You voted "+C+" star"+(C==1?"":"s"));for(var A=0;A<C;A++){this.star[A].src=this.options.starImgOn}if(!B){return }new Ajax(this.url,{method:"post",data:"link="+this.link+"&save_rating="+C,onComplete:function(D){this.complete(D)}.bind(this)}).request()},comment:function(A,B){new Ajax(this.url,{method:"post",data:"email="+A+"&comment="+B+"&link="+this.link+"&save_comment=true",onComplete:function(C){this.complete(C)}.bind(this)}).request()},show:function(){this.background=new Element("div",{"class":"background","styles":{"width":"100%","height":"100%","opacity":"0.7","background":"#000000","position":"fixed","top":"0","left":"0"},"events":{"click":(function(E){E=new Event(E).stop();this.hide()}).bindWithEvent(this)}});this.container=new Element("div",{"class":this.options.className+"popUP"});new Element("p").setHTML("<strong> Thank you for ranking this page! Your opinion is valuable.</strong>").injectInside(this.container);new Element("p").setHTML("If you\u2019d like to send us a comment about this page, write a message below and click submit.").injectInside(this.container);new Element("p").setHTML("If you have a question regarding products or services, please include your e-mail address below for a personal response.").injectInside(this.container);var C=new Element("div").setHTML("<label>E-mail address (Optional):</label>").injectInside(this.container);this.email=new Element("input").setProperties({type:"text",name:"rating_email",style:"width: 218px;",value:this.user_email}).injectInside(C);new Element("label").setStyle("font-style","italic").setHTML("Your e-mail address will be used solely for responses to your comments.").injectInside(this.container);var A=new Element("div").injectInside(this.container);this.user_comment=new Element("textarea").setProperties({name:"rating_comment",cols:"42",rows:"4"}).injectInside(A);new Element("input",{"events":{"click":(function(E){E=new Event(E).stop();this.comment(this.email.getProperty("value"),this.user_comment.getProperty("value"));this.hide()}).bindWithEvent(this)}}).setProperties({type:"image",src:"/images/button_submit.gif",value:"Submit"}).injectInside(A);new Element("input",{"events":{"click":(function(E){E=new Event(E).stop();this.hide()}).bindWithEvent(this)}}).setProperties({type:"image",src:"/images/button_cancel.gif",value:"Cancel"}).injectInside(A);var D=this.container.getElement(".close");if(D){D.addEvent("click",function(E){E=new Event(E).stop();this.hide()}.bind(this))}this.background.inject($$("body")[0]);this.container.inject($$("body")[0]);var B=this.container.getSize().size;this.container.setStyles({"margin-top":(((B.y/2).toInt())*-1)+"px","margin-left":(((B.x/2).toInt())*-1)+"px"});if(window.ie6){new Ajax(this.options.baseURL+"/ie7-fixed.js",{method:"get",evalScripts:true}).request()}this.fireEvent("onLoad")},hide:function(){this.container.remove();this.background.remove();this.active=false},complete:function(A){if(A=="true"){for(var B=0;B<this.star.length;B++){this.star[B].setStyle("cursor","auto")}return }this.voted=false;this.field.empty();this.build();alert(A)}});LinkRating.implement(new Events,new Options)