KumoVote = {
	ajax_url: 'vote.ajax.php',
	id: 0,

	getSelected: function(id) {
		var o = $('kumovote_'+id).getElements('.kumovote_options');
		var selected = [];
		for (var i=0; i<o.length; i++) {
			if ( o[i].checked ) {
				selected.push(o[i].value);
			}
		}
		return selected;
	},
	
	ajax: function(d) {
		var o = {
			url: this.ajax_url,
			method: 'post',
			data: d,
			onSuccess: function ( json_data ) {
				if ( json_data.success ) {
					$('vote_container_'+json_data.id).set('html', json_data.html);
					if (json_data.task == 'vote' ) {
						KumoVote.hide_login_win();
						setTimeout(function(){
							alert( flt('Thank you for voting!', '多謝投票!', '多謝投票!') );
						}, 300);
					}
				} else {
					if ( json_data.error == 'not_login' )
					{
						KumoVote.show_login_form();
					} else if (json_data.error == 'already_vote') {
						setTimeout(function(){
							alert( flt('Error! Your login name / the tel number you have entered was already voted!', '錯誤! 您的登入名稱/輸入的電話號碼已經投了票!', '错误! 您的登入名称/输入的电话号码已经投了票!') );
						}, 300);
					} else {
						alert( json_data.error );
					}
				}
			}
		};
		new Request.JSON(o).send();	
	},

	vote: function( id, extra_info ) {
		this.id = id;

		var selected = this.getSelected(id);
		var num = $('vote_num_choice_'+id).value.split(",");
		if ( selected.length < num[0] || selected.length > num[1]  ) {
			min_max = ((num[0]!=num[1]) ? ' ' + num[0] + ' - ' + num[1]: num[1]);
			
			alert( flt('Please select the options to vote. You can select ' + min_max + ' options.', '請選擇要投票的項目, 您可以選擇'+ min_max + '個項目。', '請選擇要投票的項目, 您可以選擇'+ min_max + '個項目。' ));
			return;
		}
		extra_info=extra_info||{};

		var d = {};
		d.id = id;
		d.task = 'vote';
		d.selected = selected;

		d = $merge(d, extra_info);

		this.ajax(d);
	},

	non_mem_vote: function() {
		if ( $('vote_non_mem_name').value == '' ) {
			alert($('error_msg_vote_non_mem_name').value );
			return;
		}
		if ( $('vote_non_mem_tel').value == '' ) {
			alert($('error_msg_vote_non_mem_tel').value );
			return;
		}
		if ( !frontendIsValidEmail($('vote_non_mem_email').value) ) {
			alert($('error_msg_vote_non_mem_email').value);
			return;
		}
		this.vote(this.id, {'non_mem': 1, 'non_mem_name': $('vote_non_mem_name').value, 'non_mem_tel': $('vote_non_mem_tel').value, 'non_mem_email': $('vote_non_mem_email').value } );
	},

	hide_login_win: function() {
		if ( !!KumoVote.win ) {
			KumoVote.win.hide();
		}
	},

	show_login_form : function( ) {
		this.hide_login_win();

		var id = 'ajax_vote_mem_login';
		var url = 'vote_login.ajax.php';
		var title = $('ajax_vote_mem_login_win_title').value;

		var other_options = {
			onDisplay: function() {
				KumoVote.resize_login_pop_win();
			}
		};

		KumoVote.win = new Popdetail().open(id, url, title , other_options);
	},
	resize_login_pop_win: function() {
		$('ajax_vote_mem_login').setStyle('display', '');
		$('ajax_vote_mem_login').setStyle('width', '500px');
		$('ajax_vote_mem_login').getElements('.redStickyWin').setStyle('width', '500px');
		$('ajax_vote_mem_login').getElements('.popup_body').setStyle('height', '230px');
		$('ajax_vote_mem_login').position();
	},

	focusMemLogin: function() {
		KumoVote.hide_login_win();
		$('login_mem_login').focus();
	},

	mem_login : function(  ) {
		//ajax mem login -> 
		//this.vote
	},

	getForm: function(id) {
		var d = {};
		d.id = id;
		d.task = 'form';

		this.ajax(d);
	},

	getResult: function(id) {
		var d = {};
		d.id = id;
		d.task = 'result';

		this.ajax(d);
	}
};

