/* AJAX Star Rating : v1.0.3 : 2008/05/06 */
/* http://www.nofunc.com/AJAX_Star_Rating/ */





function starGet(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function starGetS(o) { return((typeof(o)=='object'?o:starGet(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.documentElement.scrollLeft,'Y':event.clientY+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};

star.num=0;

var ratingContentId = new Array();
var canVote = new Array();

star.mouse=function(e,o,rb) { 
	
		//if(star.stop || isNaN(star.stop)) { 
			
			//star.stop=0;
			
	    	if(canVote[rb]) {
		        var p=abPos(starGet('star' + rb)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; 
		        //star.num=o.id.substr(4);
		        
		        if(oX<0 || oX>84 || oY<0 || oY>19) {
		        	//star.stop=1;
		        	star.revert(rb);
		        } else {
		        	var width;
		        	if(oX>=0 && oX<=17) {
		        		width = 17;
		        	} else if(oX>17 && oX<=34) {
		        		width = 34;
		        	} else if(oX>34 && oX<=51) {
		        		width = 51;
		        	} else if(oX>51 && oX<=68) {
		        		width = 68;
		        	} else if(oX>68 && oX<=84) {
		        		width = 84;
		        	} 
		        	starGetS('starCur' + rb).width=width+'px';
		        	starGetS('starUser' + rb).color='#111';
		            //starGet('starUser').innerHTML=Math.round(oX/84*100);
		        	starGet('starUser' + rb).innerHTML=Math.round(width/17);
		        }
	    	}
		//} 
};

star.out=function(e,o,rb) {
	if(canVote[rb]) {
		star.revert(rb);
	}
}

star.update=function(e,o,rb) {
    if(ratingContentId[rb] != null && canVote[rb]) {
    	var v=parseInt(starGet('starUser' + rb).innerHTML);
    	starGet('starCur' + rb).title=v;

        _updateVotesValueHandlerInternal(ratingContentId[rb], v, function(result) {
            afterUpdate(result, rb);
        });
    }
};

star.articleUpdate=function(e,o,rb,userId) {
    if(ratingContentId[rb] != null && canVote[rb]) {
    	var v=parseInt(starGet('starUser' + rb).innerHTML);
    	starGet('starCur' + rb).title=v;

        _updateVotesValueHandlerInternal(ratingContentId[rb], v, function(result) {
            afterUpdate(result, rb);
        });
        
        articleVoteService.saveRatingStatistics(ratingContentId[rb], v, userId, function(result) {
	    });
    }
};

star.revert=function(rb) { 
	var v=starGet('starCur' + rb).title;
	starGetS('starCur' + rb).width=Math.round(v/5*84)+'px';
    if(v == 0) {
    	v = 'Not rated'
    }
    starGet('starUser' + rb).innerHTML=v;
    starGet('starUser' + rb).style.color='#888';
    
    //document.onmousemove = function() {};
};

function setValue(val, rb){	
	displayval = val/5*84;
    document.getElementById("starCur" + rb).style.width = displayval + "px";
    document.getElementById("starCur" + rb).title = val;
    
    if(!canVote[rb]) {
    	document.getElementById('star' + rb).onmousedown = function() {};
		document.getElementById('star' + rb).onmousemove = function() {};
    }
}

function afterUpdate(result, rb) {
	setCanVote(false, rb);
	
	setValue(result, rb);
	star.revert(rb);
	if(parseInt(document.getElementById('starUser' + rb + 'count').innerHTML) > 0) {
		document.getElementById('starUser' + rb + 'count').innerHTML=parseInt(document.getElementById('starUser' + rb + 'count').innerHTML) + 1 + " ";
	} else {
		document.getElementById('starUser' + rb + 'count').innerHTML=1;
	}
	document.getElementById('star' + rb).onmousedown = function() {};
	document.getElementById('star' + rb).onmousemove = function() {};
}

function setContentId(value, rb) {
	ratingContentId[rb] = value;
}

function setCanVote(state, rb) {
	canVote[rb] = state;
}

