var Rating = new Class({
		options:{
			numberOfStars: 5,
			imagePath: '/images/Rating/stars/',
			objectType: 'content',
			objectId: 0,
			rating: 0,
			ajax:false,
			ajaxUrl:'/votos.aspx'
		},
		initialize: function(element, options) {
			//this.setOptions(options);
			// funciones de initialize
			this.rating=options.rating;
			this.ratingId='1';
			this.element=element;
			
			var OajaxUrl = (options.ajaxUrl || this.options.ajaxUrl);
			var OobjectType = (options.objectType || this.options.objectType);
			var OobjectId = (options.objectId || this.options.objectId);
			
			if(OajaxUrl){
			    var STRurl = OajaxUrl + '?ObjectType=' + OobjectType + '&ObjectId=' + OobjectId + '&Content-Type=text/html';
			    //alert(STRurl);
			    var myAjax= new Ajax(STRurl,{
				    method: 'get', 
				    onSuccess: function()
				    {
					    var avgRating = this.response.text;
						
						var redirect;
						var RXredirect = /^redirect:(.*)/;
						if (redirect = RXredirect.exec(avgRating)){
							var href = RXredirect[1];
							location.href = href;
						}
						
					    avgRating = avgRating.replace(/(\d+)[\W\w]+/gm, '$1');
						avgRating = new Number(avgRating);
						this.Rating.rating = avgRating;
						this.Rating.initRating();
				    },
				    onFailure: function()
				    {
						alert("Hubo un problema al votar, por favor intente mas tarde");
				    },
				    evalScripts:false
			    });
				myAjax.STRurl = STRurl;
				myAjax.Rating = this;
				myAjax.request();
			
			}
			
			// getrating()
		}, initRating: function(){
			//if (this.rating > options.numberOfStars || rating < 0) no esta dentro de los valores validos
			options= this.options;
			for (var j = 1; j <= options.numberOfStars; j++){
				var star = new Element('img');
				if (this.rating >= 1)
				{
					star.setAttribute('src', this.options.imagePath + 'rating_on.png');
					star.className = 'on';
					this.rating--;
				}
				else if(this.rating == 0.5)
				{
					star.setAttribute('src', this.options.imagePath + 'rating_half.gif');
					star.className = 'half';
					this.rating = 0;
				}
				else
				{
					star.setAttribute('src', this.options.imagePath + 'rating_off.png');
					star.className = 'off';
				}
				var widgetId = this.ratingId;
				
				star.setAttribute('id', 'star_'+widgetId+'_'+j);
				star.setAttribute ("widgetId", widgetId);
				star.setAttribute ("j", j);
				
				star.caller = this;
				
				star.addEvent("mouseover", function(){
					this.caller.displayHover(this.getAttribute("widgetId"), this.getAttribute("j"))
					}
				)
				
				star.addEvent("mouseout", function(){
					this.caller.displayNormal(this.getAttribute("widgetId"), this.getAttribute("j"))
					}
				)
				star.addEvent('click', this.submitRating);
				
				this.element.appendChild(star);
			} 
		},displayNormal: function(ratingId, star){
			for (var i = 1; i <= star; i++)
			{
				var status = document.getElementById('star_'+ratingId+'_'+i).className;
				var starElement = document.getElementById('star_'+ratingId+'_'+i);
				starElement.setAttribute('src', this.options.imagePath + 'rating_'+status+'.png');
			}
		},
		displayHover: function(ratingId, star){
			for (var i = 1; i <= star; i++)
			{
				var starElement = document.getElementById('star_'+ratingId+'_'+i)
				starElement.setAttribute('src', this.options.imagePath + 'rating_over.png');
			}
		},submitRating: function(evt){
		    //alert(Event(evt).target);
			var tmp = new Event(evt).target.getAttribute('id').substr(5);
			var widgetId = tmp.substr(0, tmp.indexOf('_'));
			var starNbr = tmp.substr(tmp.indexOf('_')+1);
			
			//var RXid = /[?&]id=(\d+)/i;
			var urlrequest = location.href;
            var arrid = urlrequest.split('-');//RXid.exec(location.search);
	        var Qid = arrid[1];
			//si es null el id en la url natural, se pregunta por las dudas por el querystring Id por si es un contenido accedido desde url vieja
	        if (Qid==null)
	        {
	            var RXid = /[?&]id=(\d+)/i;
	            arrid = RXid.exec(location.search);
	            Qid = arrid[1];
	        }
	
			var queryString = Object.toQueryString({	
				'Command': 'Vote', 
				'ObjectType': 'Content', 
				'ObjectId': Qid, 
				'value': starNbr}
			);
			
			var strurl = '/Votos.aspx?'+queryString;
			//alert(strurl);
			var myAjax = new Ajax(strurl, {
				method: 'get',
				onComplete: function(result)
					{
						var avgRating = this.response.text;
						var redirect;
						var RXredirect = /^redirect:(.*)$/m;
						if (redirect = RXredirect.exec(avgRating)){
							var href = redirect[1];
							location.href = href;
						}else{
						    //alert(result);
							if (result == 0)
							   alert("No puede volver a votar la pagina!");
							else
							   alert("Gracias por votar!");
						}
					}
				,onFailure: function(result){
					alert("Hubo un error al votar. Por favor intente de nuevo.");
				}
				}).request();
		}
	}
)
