/**
 * Cedric Sam <cedric.sam@radio-canada.ca>
 * 
 * Étoiles - client-side processing for single contents page rating
 *
 * Note: Code is from YouTube. XmlRequest was substituted for YUI (Yahoo).
 *
 **/

onLoadFunctionList=new Array();
var nb_stars = 5;
var sUrl = "/util/set_stars.asp"; /* ASP "proxy" qui fait appel au web service, pour contourner le probleme des cross-domaines */

/* Classes */

function hasClass(element,_className){
if(!element){
return;
}
var upperClass=_className.toUpperCase();
if(element.className){
var classes=element.className.split(' ');
for(var i=0;i<classes.length;i++){
if(classes[i].toUpperCase()==upperClass){
return true;
}
}
}
return false;
}

function addClass(element,_class){
if(!hasClass(element,_class)){
element.className+=element.className?(" "+_class):_class;
}
}
function getClassList(element){
if(element.className){
return element.className.split(' ');
}else{
return[];
}
}

function removeClass(element,_class){
var upperClass=_class.toUpperCase();
var remainingClasses=[];
if(element.className){
var classes=element.className.split(' ');
for(var i=0;i<classes.length;i++){
if(classes[i].toUpperCase()!=upperClass){
remainingClasses[remainingClasses.length]=classes[i];
}
}
element.className=remainingClasses.join(' ');
}
}

if((navigator.appVersion.indexOf('MSIE')>-1)&&(typeof window.XMLHttpRequest=='undefined')){
imgExt='gif';
}
else
{
imgExt='png';
}

/* Rating */

var RC_RATING_IMG='icn_star_full_19x20'+imgExt;
var RC_RATING_IMG_HOVER='img/star_hover.gif';
var RC_RATING_IMG_HALF='icn_star_half_19x20'+imgExt;
var RC_RATING_IMG_BG='icn_star_empty_19x20'+imgExt;
var RC_RATING_IMG_REMOVED='img/star_removed.gif';

function RCRating(ratingElementId,maxStars,objectName,formName,ratingMessageId,componentSuffix,size,messages,starCount,callback)
{
this.voted = false;
this.ratingElementId=ratingElementId;
this.maxStars=maxStars;
this.objectName=objectName;
this.formName=formName;
this.ratingMessageId=ratingMessageId
this.componentSuffix=componentSuffix
this.messages=messages;
this.callback=callback;
this.starTimer=null;
this.starCount=0;
if(starCount){
this.starCount=starCount;
that=this;
onLoadFunctionList.push(function(){that.drawStars(that.starCount,true);});
}
if(size=='S'){
RC_RATING_IMG='icn_star_full_11x11gif'
RC_RATING_IMG_HALF='icn_star_half_11x11gif'
RC_RATING_IMG_BG='icn_star_empty_11x11gif'
}

function showStars(starNum,skipMessageUpdate){
	if(!this.voted) {
this.clearStarTimer();
this.greyStars();
this.colorStars(starNum);
/*if(!skipMessageUpdate)
this.setMessage(starNum,messages);*/
	}
}

function setMessage(starNum){
if(parseInt(starNum)>0){
if(!this.savedMessage){
this.savedMessage=document.getElementById(this.ratingMessageId).innerHTML;
}
document.getElementById(this.ratingMessageId).innerHTML=this.messages[starNum-1];
}else if(this.savedMessage){
document.getElementById(this.ratingMessageId).innerHTML=this.savedMessage;
}
}

function colorStars(starNum){
for(var i=0;i<parseInt(starNum);i++){
removeClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG_HALF);
removeClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG_BG);
addClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG);
}
}

function greyStars(){
for(var i=0;i<this.maxStars;i++)
if(i<=this.starCount){
removeClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG);
removeClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG_HALF);
addClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG_BG);
}
else
{
removeClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG);
removeClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG_HALF);
addClass(document.getElementById('star_'+this.componentSuffix+"_"+(i+1)),RC_RATING_IMG_BG);
}
}

function setStars(starNum){
if(!this.voted) {
this.voted = true;
this.starCount=starNum;
this.drawStars(starNum);
document.forms[this.formName]['rating'].value=parseFloat(this.starCount/nb_stars);
var ratingElementId=this.ratingElementId;
that=this;
var formObject = document.getElementById(this.formName);
YAHOO.util.Connect.setForm(formObject);
var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, this.callback, null); 
if(that.callback){
that.callback();
}
}
}

function drawStars(starNum,skipMessageUpdate){
this.starCount=starNum;
this.showStars(starNum,skipMessageUpdate);
}

function clearStars(){
this.starTimer=setTimeout(this.objectName+".resetStars()",300);
}

function resetStars(){
this.clearStarTimer();
if(document.getElementById("RC_current_rating")!=null) {
	this.drawStars(parseInt(document.getElementById("RC_current_rating").innerHTML));
} else if(this.starCount)
	this.drawStars(this.starCount);
else
	this.greyStars();
//this.setMessage(0);
}

function clearStarTimer(){
if(this.starTimer){
clearTimeout(this.starTimer);
this.starTimer=null;
}
}
this.clearStars=clearStars;
this.clearStarTimer=clearStarTimer;
this.greyStars=greyStars;
this.colorStars=colorStars;
this.resetStars=resetStars;
this.setStars=setStars;
this.drawStars=drawStars;
this.showStars=showStars;
//this.setMessage=setMessage;
}
