var Poll = new Object();

Poll.handleSuccess = function( xml ) {
	Poll.clearMsgs();
	if( $( xml ).find( 'state' ).text() == 'invalid' ) {
		$( xml ).find( 'msg' ).each(
			function() {
				$( '#msgError ul.msgError' ).append( '<li>'+$(this).text()+'</li>' );
			}
		);
		$( '#msgError' ).show();
		window.scroll(0,0);
		return false;
	} else {
		document.location='/poll/added/';
	}
}

Poll.clearMsgs = function() {
	$( '#msg p' ).html( '' );
	$( '#msgError ul' ).html( '' );
	$( '#msgError' ).hide();
}

Poll.documentReady = function() {
	$(document).ready(
		function() {
			Poll.answerCount = $( 'input.answer' ).length;
			$( '#pollForm' ).ajaxForm(function(i) { Poll.handleSuccess(i); });
			Poll.clearMsgs();
		}
	);
}

Poll.addAnswer = function() {
	var answerRow = '<tr class="answer">'
					+'<td>'+answerLabel+'</td>'
					+'<td><input type="text" name="answer'+Poll.answerCount+'" id="answer'+Poll.answerCount+'" value="" class="answer x" /><img class="remove" src="/images/x.png" onClick="Poll.removeAnswer(this);return false;" alt="RemoveAnswer" /></td>'
				+'</tr>';
	$( 'img.remove' ).hide();
	$( 'img.remove' ).each(
		function() {
			$(this).parent().find( 'input' ).removeClass('x');
		}
	);
	$( '#poll' ).append(answerRow);
	Poll.answerCount=Poll.answerCount+1;
}

Poll.removeAnswer = function( obj ) {
	$( obj ).parent( 'td' ).parent( 'tr' ).prev().find('input').addClass('x');
	$( obj ).parent( 'td' ).parent( 'tr' ).prev().find('img').show();
	$( obj ).parent( 'td' ).parent( 'tr' ).remove();
	Poll.answerCount = Poll.answerCount-1;
}

Poll.edit = function( pollId ) {
	document.location = '/poll/new/id/'+pollId;
}

Poll.addedReady = function() {
	$(document).ready(
		function() {
			$( '#optionsForm' ).ajaxForm(Poll.addedHandleSuccess);
			Poll.clearMsgs();
		}
	);
}

Poll.addedHandleSuccess = function( xml ) {
	Poll.clearMsgs();
	if( $( xml ).find( 'state' ).text() == 'invalid' ) {
		$( xml ).find( 'msg' ).each(
			function() {
				$( '#msgError ul.msgError' ).append( '<li>'+$(this).text()+'</li>' );
			}
		);
		$( '#msgError' ).show();
		return false;
	} else {
		document.location='/poll/end/';
	}
}

Poll.handleSuccessEdition = function( xml ) {
	Poll.clearMsgs();
	if( $( xml ).find( 'state' ).text() == 'invalid' ) {
		$( xml ).find( 'msg' ).each(
			function() {
				$( '#msgError ul.msgError' ).append( '<li>'+$(this).text()+'</li>' );
			}
		);
		$( '#msgError' ).show();
		return false;
	} else {
		document.location='/poll/edit/success/yes';
	}
}

Poll.documentReadyEdit = function() {
	$(document).ready(
		function() {
			Poll.answerCount = $( 'input.answer' ).length;
			$( '#pollForm' ).ajaxForm(function(i) { Poll.handleSuccessEdition(i); });
			Poll.clearMsgs();
		}
	);
}

Poll.selected = 0;
Poll.checkIfCheckedAnswer = function() {
	Poll.selected = 0;
	$('input.answer').each(
		function() {
			if( this.checked ) {
				Poll.selected = 1;
			}
		}
	);
	if( Poll.selected==1 ) {
		$( '#answ' ).removeAttr('disabled')
	} else {
		$( '#answ' ).attr( 'disabled', 'disabled' );
	}
}