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

function createPollBox(id, dir, loginWarning, captchaWarning, rule) {
	__poll_controllers[id] = new PollController(id, dir, loginWarning, captchaWarning, rule);
	__poll_controllers[id].init();
}

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

	this.init = function() {

		if(!this.inited) {

			this.design = $$('.tools').length > 0 ? 'button' : 'star';
			this.max = this.design == 'star' ? 5 : 1;

			if (this.design == 'star') {
				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();");
					}
				}
			} else if (this.design == 'button') {
				for(var i=1;i<=this.max;i++) {
					var mos = $$('.' + this.id+"_pollClick"+i+"Id");
					for (var j=0; j < mos.length; j++) {
						var mo = $(mos[j]);
						if(mo) {
							mo.onclick = Function(this.obj+".onVote("+i+");");
						}
					}
				}
			}
			this.inited = true;
		}
		var box = $(this.design == 'star' ? this.id+'_pollStarBoxId' : this.id+'_pollButtonBoxId');
		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];
			if (this.design == 'button') {
				var boxes = $$('.' + this.id+'_pollButtonBoxId');
				for (var i=0; i < boxes.length; i++) {
					var box = $(boxes[i]);
					if(box) {
						box.style.cursor = this.rate.isVoted ? 'default' : 'pointer';
						if (this.rule == 0) var buttons = [box.down('input')];
						else var buttons = $$('.tools input');
						for (var j=0; j < buttons.length; j++) {
							var button = $(buttons[j]);
							if (button) {
								button.style.cursor = this.rate.isVoted ? 'default' : 'pointer';
								if (this.rate.isVoted) button.disable();
							}
						}
					}
				}
			} else {
				var box = $(this.id+'_pollStarBoxId');
				if(box) {
					box.style.cursor = this.rate.isVoted ? 'default' : 'pointer';
				}
			}
			var capctha = $('captcha_div_' + this.id);
			if (capctha) {
				if (this.rate.isVoted) capctha.hide();
				else capctha.show();
			}
			// login error
			if(this.rate.message==1) {
				if (typeof(loginHandler) != 'undefined') loginHandler.openLoginWindow(null,null,window.location.href);
				else if (this.loginWarning) {
					alert(this.loginWarning);
				}
			}
			else if(this.rate.message==4) { // captcha error
				alert(this.captchaWarning);
			}
			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 = this.dir + "star_blue.png";
				}
				else {
					img.src = this.dir + "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 (img) {
					if(ms<0.3) {
						img.src = this.dir + "star_blue_25.png";
					}
					else if (ms<0.6) {
						img.src = this.dir + "star_blue_50.png";
					}
					else {
						img.src = this.dir + "star_blue_75.png";
					}
				}
			}
		}
		var rate = $(this.id+"_pollAverageId");
		if(rate) {
			rate.innerHTML = cv;
		}

		if (this.design == 'star') {
			var votes = $(this.id+"_pollVotesId");
			if(votes) {
				votes.innerHTML = this.rate.votes;
			}
		} else if (this.design == 'button') {
			var votes = $$('.' + this.id+"_pollVotesId");
			for (var i=0; i < votes.length; i++) {
				vote = $(votes[i]);
				if(vote) {
					vote.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 = this.dir + "star_yellow.png";
				}
				else {
					img.src = this.dir + "star_gray.png";
				}
			}
		}
	},

	this.onVote = function(value) {
		if(this.rate.isVoted) return;
		if(this.anim) {
			clearTimeout(this.anim);
		}

		var capctha = $('captcha_text_' + this.id);
		if (capctha) {
			var cpart = '/captcha/' + escape(capctha.value);
		} else {
			var cpart = '';
		}
		loadScript("./poll/ajax/vote"+cpart+"/"+this.id+"/"+value+".js","polldata_"+this.id,this.obj+".process()");
	}

}


