var Contest = function($container) {
	// CONSTRUCTOR /////////////////////////////////////////
	this.container = $container;
	var self = this;
	
	jQuery($container + ' li img').click(function () {
		
		if(self.enabled) {
		    self.vote(jQuery(jQuery(this).parent().get(0)).attr('id'));
    		alert(self.message);
		} else {
			if(self.disable_message) {
	    		alert(self.disable_message);
			}
		}
	});
}
Contest.prototype.version = '0.01';
Contest.prototype.container = null;
Contest.prototype.items = Array();
Contest.prototype.enabled = true;
Contest.prototype.vote_count_class = '.contest_vote_count';
Contest.prototype.url = '/contest.php';
Contest.prototype.message = '投票ありがとうございました。';
Contest.prototype.disable_message = '投票は終了しました。';
Contest.prototype.vote = function($id) {
	var self = this;

	if($id == null) {
		$id = '';
	}
	jQuery.post(
		self.url, 
		{ vote: $id }, 
		function (response) {
			jQuery(response).each(function () {
				jQuery('#' + this.id + ' ' + self.vote_count_class).html(this.count);
				jQuery(self.container).append(jQuery('#' + this.id));
			});
			var items = jQuery(self.container).children();
			jQuery(items).each(function () {
				var id = jQuery(this).attr('id');
				var count = jQuery('#' + id + ' ' + self.vote_count_class).html();
				if(count <= 0) {
					jQuery(self.container).append(jQuery('#' + id));
				}
				
			});
		},
		'json'
	);
}
