function Polls( strControllerURL ){
	this.ControllerURL = strControllerURL;
	this.Init();
};

Polls.prototype.objPostProperties = function( objPostProperties ){
	this.PostProperties = objPostProperties;
};

Polls.prototype = new PageControllerClass;
Polls.prototype.Init = function(){
	var objSelf = this;
	var voteForm = $("#voteOptionsForm");
	$('#submit_vote').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitVote( voteForm );
			objEvent.preventDefault();
			return( false );
		}
	);

	$('div#choices ol li input').bind("change keyup",function(objEvent) {
		objSelf.NotifyChoiceChange( $(this) );
		return(false);
	});


	$('input[name="style"]').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitViewPollType( $this );
		}
	);

	this.SetBusy(false);
};


Polls.prototype.SubmitViewPollType = function( jRadio ){
	var objSelf = this;
	$("#pollsettings").css('background','url(/index.cfm/do/api.polls.polltypepreviewimgsrc/type/'+jRadio.val()+'/format/chart.png) bottom right no-repeat');
};

Polls.prototype.NotifyChoiceChange = function(jElem){
	var objSelf = this;
	var value = jElem.val();
	var id = jElem.attr('id');
	var parent = jElem.parent();
	var next = parent.next();

	if(id=='choice1') {
	} else if( id == 'choice2') {
		if(value.length) {
			next.find('input').css('visibility', 'visible');
		};
	} else {
		if(value.length) {
			next.find('input').css('visibility', 'visible');
		} else {
			var children = $('div#choices ol li input');
			for(var i=0;i <= children.length;i++) {
				var e = $(children[i]);
				if(e.attr('id') > id) {
					e.css('visibility', 'hidden'); e.val('');
				};
			};
		};		
	};
};


Polls.prototype.SubmitVote = function( jForm ){
	var objSelf = this;
	if (objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var objPostProperties = {
		"do":"api.polls.vote",
		pollid:jForm.find("input[name='pollid']").val(),
		choiceid:jForm.find("select[name='choiceid']").val(),
		vote_type:jForm.find("input[name='vote_type']:checked").val(),
		vote_contributed:jForm.find("input[name='vote_contributed']").val()
	};
	$.get(this.ControllerURL,objPostProperties,function(objResponse){objSelf.SubmitVoteHandler(objResponse,jForm);},"json");
};

Polls.prototype.SubmitVoteHandler = function(objResponse,jForm){
	if(objResponse.SUCCESS){
		$("#pollDisplay-container").replaceWith( objResponse.DATA );
		this.SetBusy(false);
	} else {this.ShowAPIErrors(objResponse.ERRORS);};
};
