/*
	Evo.Voting System
	Copyright Jeremy Lauzon 2006
	
	params : options
		stars			: number of star to display
		container		: id of the div container
		starUrl		: Url to the images representing a star
		fullStarUrl	: Url to the images representing a full star
		url				: url of the POST
*/

Evo.Voting = Class.create();
Evo.Voting.prototype = 
{
	initialize: function(options)
	{
		this.options = options;
		this.images = new Array();
		this.active = true;
		this.voteDone = false;
		this.intervalId = null;
		
		//Bind Initialization to window.onload
		YAHOO.util.Event.addListener(window, "load", this.onload, this); 
	},
	
	onload : function(e, obj)
	{		
		obj.container = $(obj.options.container);
		obj.containerMouseover = $(obj.options.container + "-mouseover") ? $(obj.options.container + "-mouseover") : null;
		obj.containerSucess = $(obj.options.container + "-sucess") ? $(obj.options.container + "-sucess") : null;
		obj.txtMouseover = $(obj.options.container + "-mouseover") ? $(obj.options.container + "-mouseover").innerHTML : null;
	
		for(i = 0; i < options.stars;i++){
			//Create IMG nodes
			img = document.createElement("img");
			img.src = obj.options.starUrl;
			img.title = i + 1;
			img.index = i; 
			
			obj.images.push(img);
			
			//Append to Container
			obj.container.appendChild(img);
			
			//add mouseover and mouseout
			YAHOO.util.Event.addListener(img, "mouseover", obj.star_mouseover, obj);
			YAHOO.util.Event.addListener(img, "click", obj.star_click, obj);
		}
			
	},

	sendVote : function(vote)
	{
		callback = 
		{ 
			success	:	this.vote_responseSuccess, 
			failure	:	this.vote_responseFailure, 
			scope		:	this
		}; 
		
		Object.extend(this.options.argument, {vote : vote})
		
		YAHOO.util.Connect.asyncRequest('POST', this.options.url, callback, $H(this.options.argument).toQueryString()); 
			
	},
	
	vote_responseSuccess : function(o)
	{	
		
		if (this.containerSucess){
			this.containerSucess.style.display = "block";
			if (this.txtMouseover && this.containerMouseover)
			{
				 this.containerMouseover.style.display = "none";
			}
		}
		
		this.hideOverlay();
		this.voteDone = true;
	},
	
	vote_responseFailure : function(o)
	{		
	
		if (this.containerSucess){
			this.containerSucess.style.display = "block";
			if (this.txtMouseover && this.containerMouseover)
			{
				 this.containerMouseover.style.display = "none";
			}			
		}
		
		this.hideOverlay();
		this.voteDone = true;
	},
	
	star_click : function(e, obj)
	{
		if (obj.active == true)
		{
			obj.showOverlay(obj);
			
			obj.intervalId = window.setInterval(function() { obj.hideOverlay(); }, 1000);
			
			obj.sendVote(this.index + 1, obj);
			obj.active = false;
		}
	},
	
	star_mouseover : function(e, obj)
	{		
		if (obj.active == true)
		{
			for (i = 0; i <= this.index;i++)
			{
				obj.images[i].src = obj.options.fullStarUrl;
			}

			for (i = this.index + 1; i < obj.options.stars ;i++)
			{
				obj.images[i].src = obj.options.starUrl;
			}
			
			if (obj.txtMouseover && obj.containerMouseover)
			{
				obj.containerMouseover.innerHTML = obj.txtMouseover.replace("[star]", this.index + 1);
				obj.containerMouseover.style.display = "block";
			}
		}			
	},	
		
	hideOverlay : function()
	{
		if (this.voteDone)
		{
			this.overlay.style.display ="none";
			window.clearInterval(this.intervalId);
		}
	},
		
	showOverlay : function(obj){
		div = document.createElement("div");
		
		div.style.backgroundImage = "url(/public/images/evo/ajax-loader.gif)";
		div.style.backgroundRepeat = "no-repeat";
		
		div.style.backgroundPosition = "center center";
		div.style.backgroundColor = "#DDDDDD";
		
		div.style.opacity = "0.8";
		div.style.filter = 'alpha(opacity=80)';
			
		div.style.zIndex = "3000";
		div.style.position = "absolute";
		div.style.top = "0px";
		div.style.left = "0px";
		div.style.width = this.container.offsetWidth + "px";
		div.style.height = this.container.offsetHeight + "px";
		this.overlay = div;
		this.container.appendChild(div);
	}
	
};

