//Copyright (c) 2009 ZBC corporation.
//Author: oyatsunyck.

var request;

function jVote(parent, settings, locked, isLogged, scriptPath)
{
	this.scriptPath = scriptPath;
	this.isLogged = isLogged;
	this.locked = locked;
	this.jvObject = null;
	this.images = [];
	this.settings = settings;
	this.parent = parent;
	this.init();	
}

jVote.prototype.init = function()
{
	var that = this;
	for(var i = 0, e = this.settings.max; i < e; i++)
	{
		var image = document.createElement('img');		
		this.images[i] = image;
		image.value = this.settings.labels[i];
		image.alt = this.settings.labels[i];
		image.style.cursor = 'pointer';
		image.onmouseover = function()
		{
			if(that.locked)
			{
				return;
			}
			that.set(this);
		};
		image.onclick = function(evnt)
		{
			if(!that.isLogged)
			{
				showNotLoggedVoteAlert();
				return;
			}
			if(that.locked)
			{
				return;
			}
			var eEvent = evnt || window.event;
			
			var servlet = 'dosvidos/voting?res=' + this.value + '&ftId=' + that.settings.ftId;
			var arg = "";                
			var reqStr = servlet;				
			that.addrequest(reqStr, that); 
			
		};
		document.getElementById(this.parent).appendChild(image);
	}
	this.set(this.images[this.settings.min-1]);
};
jVote.prototype.set = function(domImage)
{
	domImage.src = this.scriptPath + 'img/dosvidos/pinkStar.png';	
	var next = domImage.nextSibling;
	while(next)
	{
		next.off = true;
		next.src = this.scriptPath + 'img/dosvidos/whiteStar.png';
		next = next.nextSibling;		
	}
	var prev = domImage.previousSibling;
	while(prev)
	{
		prev.off = false;
		prev.src = this.scriptPath + 'img/dosvidos/pinkStar.png';
		prev = prev.previousSibling;		
	}
};

jVote.prototype.reset = function(num)
{
	if(this.locked)
	{
		return;
	}
	var index = (num) ? num : this.settings.min;
	this.set(this.images[index-1]);
};

jVote.prototype.lock = function(val)
{
	this.set(this.images[val-1]);
	this.locked = true;
};


jVote.prototype.unLock = function()
{
	this.locked = false;
};

jVote.prototype.addrequest = function(req, jvOb)
{	
	try {               
       request = new XMLHttpRequest();                
       request.onreadystatechange = function(){                 
    	   if (request.readyState==4){             
              if (request.status == 200) {
                var val = String(request.responseText);    
                if(val == -1){
                	showAlreadyVotedAlert();
                }else{
                	showVoteAlert();
                	jvOb.lock(val);
                }
               } else {
                   //showAlert('error');
               }                       
              }
       }
    }catch (e) {
        try {                               
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e) {                          
            alert("XMLHttpRequest error: " + e);
        }
    }
    request.open("GET", req, true);      
    request.send(null);
    return request;                          
};


function createVoteDiv(divName, divRating, locked, isLogged, scriptPath) {
    var jv;
    jv = new jVote(divName, {max:5,min:divRating,labels:['1','2','3','4','5'],ftId:divName}, locked, isLogged, scriptPath);    
} 



function showNotLoggedVoteAlert() {	
	showShadeDiv();
	document.getElementById("alert_nlv_div").style.display = 'block';			
}
function hideNotLoggedVoteAlert() {	
	hideShadeDiv();
	document.getElementById("alert_nlv_div").style.display = 'none';		
}

function showAlreadyVotedAlert() {	
	showShadeDiv();
	document.getElementById("alert_av_div").style.display = 'block';			
}
function hideAlreadyVotedAlert() {	
	hideShadeDiv();
	document.getElementById("alert_av_div").style.display = 'none';		
}
function showVoteAlert() {	
	showShadeDiv();
	document.getElementById("alert_vote_div").style.display = 'block';			
}
function hideVoteAlert() {	
	hideShadeDiv();
	document.getElementById("alert_vote_div").style.display = 'none';	
}
