//taken from youtube
var UT_RATING_IMG = '/images/vote1.png';
var UT_RATING_IMG_HOVER = '/images/vote1.png';
var UT_RATING_IMG_HALF = '/images/vote_y.png';
var UT_RATING_IMG_BG = '/images/vote0.png';
var UT_RATING_IMG_REMOVED = '/images/vote0.png';

function UTRating(ID,votewhat,ratingElementId, maxStars, objectName, formName, ratingMessageId, componentSuffix, size,tp)
{

	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ID = ID;
	this.votewhat = votewhat;
	this.tp = tp;
	this.ratingMessageId = ratingMessageId
	this.componentSuffix = componentSuffix

	this.starTimer = null;
	this.starCount = 0;

	(new Image()).src = UT_RATING_IMG;
	(new Image()).src = UT_RATING_IMG_HALF;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);
		if(!skipMessageUpdate)
			this.setMessage(starNum);
	}

	function setMessage(starNum) {
		document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
	}
	function colorStars(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById(tp+'star_'  + this.componentSuffix + "_" + (i+1)).src = UT_RATING_IMG;
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.starCount)
				document.getElementById(tp+'star_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG; // UT_RATING_IMG_REMOVED;
			else
				document.getElementById(tp+'star_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG;
	}


	function S(starNum) {
var bvote=this.starCount;
		this.starCount = starNum;
		this.drawStars(starNum);
		//document.forms[this.formName]['rating'].value = this.starCount;
		//document.forms[this.formName]['ID'].value = votewhat + '_' + ID;
		//var ratingElementId = this.ratingElementId;
		//postForm(this.formName, true, function (req) { replaceDivContents(req, ratingElementId); });

Ajax('/vote/?rating='+this.starCount+'&ID='+votewhat + '_' + ID, tp,bvote)
	}


	function drawStars(starNum, skipMessageUpdate) {
		this.starCount=starNum;
		this.showStars(starNum, skipMessageUpdate);
	}

	function cS() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 500);
	}

	function resetStars() {
		this.clearStarTimer();
		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.cS = cS;
	this.clearStarTimer = clearStarTimer;
	this.greyStars = greyStars;
	this.colorStars = colorStars;
	this.resetStars = resetStars;
	this.S = S;
	this.drawStars = drawStars;
	this.showStars = showStars;
	this.setMessage = setMessage;

}


function Ajax(url,ty,bvote){

var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var dakika=date.getMinutes();
var saat=date.getHours();
var saniye=date.getSeconds();
var zaman=day+'.'+month+'.'+yy+'.'+saat+'.'+dakika+'.'+saniye;

url=url + '&' + zaman

var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request,ty,bvote)
}
page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request,ty,bvote){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))

if (tp=""){

}else{
if (page_request.responseText=='cannot')
{alert(vote_error);
if (ty=='ar'){arrC.starCount=bvote;}
if (ty=='al'){alrC.starCount=bvote;}
if (ty=='so'){sorC.starCount=bvote;}
}
else{
	bvote=page_request.responseText
	if (ty=='ar'){arrC.starCount=bvote;document.getElementById('arrM').innerHTML=vote_success;}
	if (ty=='al'){alrC.starCount=bvote;document.getElementById('alrM').innerHTML=vote_success;}
	if (ty=='so'){sorC.starCount=bvote;document.getElementById('sorM').innerHTML=vote_success;}
}}

}



