function cRate(srYear, BandId, Pic, PicId){
	this.rate = 2;
	this.nbrOfRates = 5;
	this.setRatingElements();
	this.srYear = srYear;
	this.BandId = BandId;
	this.Pic = Pic;
	this.PicId = PicId;
	this.ratingSpanText = '';
	this.ratingSpan = null;

//	alert(this.PicId);
	
	this.server = new cServer();
	this.cookie = new cCookie(this.server);

	arr = getElementsByClass('rating', 'div');
	this.ratingDiv = arr[0];
	
	if (this.ratingDiv){
		arr = this.ratingDiv.getElementsByTagName('span');
		this.ratingSpan = arr[0];

		this.hasVoted = this.hasUserVoted();
		this.server.get('rate.asp?BandId='+this.BandId+'&Pic='+this.Pic, this.getRate, this);
	}
//	this.setRate();

}

cRate.prototype.hasUserVoted = function(){
	if (this.cookie.enabled == false)
		return true;
	
	votes = this.cookie.getJsCookie('votes|'+this.srYear);
	if (votes){
		votesArr = votes.split('_');
		for (i=0;i<votesArr.length-1;i++){
//			alert(votesArr[i] +'|'+ this.PicId);
			if (votesArr[i] == this.PicId && this.PicId != ''){
//				alert(this.srYear + '|' + this.PicId + '|' + votesArr[i]);
				return true;
			}
		}
	}
	return false;
}

cRate.prototype.getRate = function(error, text, obj){
	if (error == false){
//		alert(text);

		arr = text.split('|');
		value = MyReplace(arr[0],',','.')//arr[0].replace(/\,/ga, '.');
		nbrVotes = arr[1];

//		arr = obj.ratingDiv.getElementsByTagName('span');
		if (!obj.cookie.enabled)
			obj.ratingSpan.innerHTML = '<small>(Du måste tillåta Cookies för att rösta!)</small>&nbsp;Antal röster: ' + nbrVotes;
		else if (obj.hasVoted == true)
			obj.ratingSpan.innerHTML = '<small>(Du har röstat på bilden!)</small>&nbsp;Antal röster: ' + nbrVotes;
		else
			obj.ratingSpan.innerHTML = 'Antal röster: ' + nbrVotes;
		obj.ratingSpanText = obj.ratingSpan.innerHTML;

		arr = obj.ratingDiv.getElementsByTagName('li');
//		alert(value + ' ' +Math.round(parseFloat(value)));
		for (i=0;i<Math.round(parseFloat(value));i++)
			arr[i].className = 'rated';


		if (obj.hasVoted == true){
//			alert('voted');
			obj.ratingDiv.className = 'rated';
//			for (i=0;i<arr.length;i++)
//				arr[i].className = 'voted';
		}
	}
}

cRate.prototype.setRatingElements = function(){
	var divs = document.getElementsByTagName('div');
	for (i=0;i<divs.length;i++)
		if (divs[i].className == 'rating'){
			var lis = divs[i].getElementsByTagName('li');
			for (t=0;t<lis.length;t++){
				lis[t]._owner = this;
				lis[t].innerHTML = '&nbsp;';
				lis[t].onmouseover = function(){this._owner.rateHover(this)};
				lis[t].onmouseout = function(){this._owner.rateOut(this)}
				lis[t].onclick = function(){this._owner.rateClick(this);return false;}
			}
		}
	
}
cRate.prototype.rateHover = function(obj){
	after = false;
	rateCnt = 0;
	if (obj.parentNode.parentNode.className != 'rated')
		for (i=0;i<obj.parentNode.childNodes.length;i++)
			if (obj.parentNode.childNodes[i].tagName == 'LI'){
//			cssChanger.setClassElement('div.rating ul li', 'background', 'none');
				if (!obj.parentNode.childNodes[i].oldClassName)
					obj.parentNode.childNodes[i].oldClassName = obj.parentNode.childNodes[i].className;
				if (after)
					obj.parentNode.childNodes[i].className = '';
				else{
					obj.parentNode.childNodes[i].className = 'ratingHover';
					rateCnt++;
				}
				if (obj.parentNode.childNodes[i] == obj)
					after = true;
			}
//	document.getElementById('info').innerHTML = 'Rate: '+rateCnt;
}
cRate.prototype.rateOut = function(obj){
	rateCnt = 0;
	if (obj.parentNode.parentNode.className != 'rated')
		for (i=0;i<obj.parentNode.childNodes.length;i++)
			if (obj.parentNode.childNodes[i].tagName == 'LI'){
				obj.parentNode.childNodes[i].className = obj.parentNode.childNodes[i].oldClassName;
				if (obj.parentNode.childNodes[i].oldClassName != '')
					rateCnt++;
			}
//	document.getElementById('info').innerHTML = 'Rate is: ' + rateCnt;
}
cRate.prototype.rateClick = function(obj){
	var t = 0;
	if (obj.parentNode.parentNode.className != 'rated')
		for (i=0;i<obj.parentNode.childNodes.length;i++){
			if (obj.parentNode.childNodes[i].tagName == 'LI')
				t++;
			if (obj.parentNode.childNodes[i] == obj){
				this.server.post('rate.asp?BandId='+this.BandId+'&Pic='+this.Pic, new Array('Rate'), new Array(t.toString()), this.rateCallBack, this);
//			alert('BandId='+this.BandId+'&Pic='+this.Pic+' Rate='+t);
				break;
			}
		}
}
cRate.prototype.rateCallBack = function(error, text, obj){
	if (error == true)
		alert('Kunde inte spara rösten! ('+text+')');
	else{
		obj.hasVoted = true;
		obj.ratingDiv.className = 'rated';
		obj.PicId = text.split('|')[0];
		alert(text);
		if (obj.cookie.getJsCookie('votes|'+obj.srYear) != null)
			obj.cookie.setJsCookie('votes|'+obj.srYear, obj.cookie.getJsCookie('votes|'+obj.srYear)+obj.PicId.toString()+'_');
		else
			obj.cookie.setJsCookie('votes|'+obj.srYear, obj.PicId.toString()+'_');
		obj.server.get('rate.asp?BandId='+obj.BandId+'&Pic='+obj.Pic, obj.getRate, obj);
		showInfo(text.split('|')[1]);
		//alert(text);
	}
}
/*
cRate.prototype.setInfo = function(text){
	this.ratingSpanText = this.ratingSpan.innerHTML;
	this.ratingSpan.innerHTML = text;
	setTimeout('alert(rating.ratingSpan.tagName + \' \' + rating.ratingSpanText)', 3000);
}
*/
