try {
	if(!__poll_controllers) {
		__poll_controllers = new Object;
	}
}
catch(err) {
	__poll_controllers = new Object;
}

function createPollBox(id) {
	__poll_controllers[id] = new PollController(id);
	__poll_controllers[id].init();
}

function PollController(id) {
	this.id			= id,
	this.rate		= null,
	this.obj		= "__poll_controllers['"+id+"']",
	this.value		= 0,
	this.step		= 0.25,
	this.spead		= 50,
	this.max		= 5,
	this.inited		= false,
	this.anim		= null,

	this.init = function() {
		if(!this.inited) {
			for(var i=1;i<=this.max;i++) {
				var mo = $(this.id+"_pollClick"+i+"Id");
				if(mo) {
					mo.onclick = Function(this.obj+".onVote("+i+");");
					mo.onmouseover = Function(this.obj+".select("+i+");");
					mo.onmouseout = Function(this.obj+".reset();");
				}
			}
			this.inited = true;
		}
		var box = $(this.id+'_pollStarBoxId');
		if(box) {
			this.update();
		}
	},

	this.update = function() {
		loadScript("/poll/ajax/"+id+'.js',"polldata_"+id,this.obj+".process()");
	},

	this.reset = function() {
		$(this.id+"_pollYourVoteId").hide();
		for(var i=1;i<=this.max;i++) {
			$(this.id+"_pollRateText"+i+"Id").hide();
		}
		$(this.id+"_pollRateBoxId").show();
		if(this.rate) {
			this.show(this.rate.average);
		}
		else {
			this.show(0.0);
		}
	},

	this.process = function() {
		try {
			this.rate = __poll_data[this.id];
			var box = $(this.id+'_pollStarBoxId');
			if(box) {
				box.style.cursor = this.rate.isVoted ? 'default' : 'pointer';
			}
			// login error
			if(this.rate.message==1) {
				loginHandler.openLoginWindow(null,null,window.location.href);
			}
			else {
				this.value = 0;
				setTimeout(this.obj+".animate()",0);
			}
		}
		catch(err) {
		}
	},

	this.animate = function() {
		this.show(this.value);
		this.value = this.value + this.step;
		if(this.value<=this.rate.average) {
			this.anim = setTimeout(this.obj+".animate()",this.spead);
		}
		else {
			this.show(this.rate.average);
		}
	},
	
	this.show = function(cv) {
		var fs = Math.floor(cv);
		for(var i=1;i<=this.max;i++) {
			var img = $(this.id+"_pollStar"+i+"Id");
			if(img) {
				if(i<=fs) {
					img.src = "/poll/img/star_blue.png";
				}
				else {
					img.src = "/poll/img/star_gray.png";
				}
			}
		}
		var mi = fs+1;
		if(mi<=this.max) {
			var ms = cv - fs;
			if(ms>0.05) {
				var img = $(this.id+"_pollStar"+mi+"Id");
				if(ms<0.3) {
					img.src = "/poll/img/star_blue_25.png";
				}
				else if (ms<0.6) {
					img.src = "/poll/img/star_blue_50.png";
				}
				else {
					img.src = "/poll/img/star_blue_75.png";
				}
			}
		}
		var rate = $(this.id+"_pollAverageId");
		if(rate) {
			rate.innerHTML = cv;
		}
		var votes = $(this.id+"_pollVotesId");
		if(votes) {
			votes.innerHTML = this.rate.votes;
		}
	},
	
	this.select = function(cv) {
		
		if(this.anim) {
			clearTimeout(this.anim);
		}
		$(this.id+"_pollRateBoxId").hide();
		if(this.rate.isVoted) {
			cv = this.rate.yourVote;
			$(this.id+"_pollYourRateId").innerHTML = cv;
			$(this.id+"_pollYourVoteId").show();
		}
		else {
			$(this.id+"_pollRateText"+cv+"Id").show();
		}

		var fs = Math.floor(cv);
		for(var i=1;i<=this.max;i++) {
			var img = $(this.id+"_pollStar"+i+"Id");
			if(img) {
				if(i<=fs) {
					img.src = "/poll/img/star_yellow.png";
				}
				else {
					img.src = "/poll/img/star_gray.png";
				}
			}
		}
	},
	
	this.onVote = function(value) {
		if(this.rate.isVoted) return;
		if(this.anim) {
			clearTimeout(this.anim);
		}
		loadScript("/poll/ajax/vote/"+id+"/"+value+".js","polldata_"+id,this.obj+".process()");
	}

}
 

