// If page is ready

$(document).ready(function(){
						   

	initPollInput();
	$('a#pollvote').click(function(){
		_val = $('input[@name=poll]:checked').val();
		
		if(_val == undefined || _val.length == 0){
			_val = $('input[name=poll]:first').val()
			if(_val == undefined || _val.length == 0)
				loadPollVote();

			return false;
		}
		
		_other = $('input#pollother').val();

		setTimeout(function(){
			$.ajax({
		    	type: "POST",
		        url:  baseUrl+'ajax/votepoll',
				data: 'vote='+_val+'&other='+_other,
		        complete: function(msg){
		        	var _parts = msg.responseText.split('##|##');
		        	
		        	setTimeout(function(){
		        		loadPollView(_parts[1]);
		        	}, 	unloadingPoll(_parts[0])+1000);
		        }
			});
		},loadingPoll());
		return false;
	});
	$('a#pollview').click(function(){
		loadPollView($(this).attr('rel'));
		return false;
	});
						   
});

function initPollInput(){
	$('input[name=poll]').click(function(){
		$('#pollother').css({'color': '#A5AAAF'});
		if($(this).attr('id') === 'pollotherchk'){
			$('#pollother').focus();
		}else{
			$('#pollother').val($('#pollotherlabel').html());
		}
	});
	
	$('#pollother').focus(function(){
		$(this).css({'color': '#000000'});
		$('#pollotherchk').attr('checked', 'checked');
		if($('#pollother').val() == $('#pollotherlabel').html()){
			$('#pollother').val('');
		}
	});
}

function loadingPoll(){
	var speed = 200;
	
	$('#poll-spinner').height($('#poll-content').height());
	$('#poll-spinner').show();
	
	/*$('#poll-content-holder, #poll-btn-holder').animate({'opacity' : 0.4}, speed, 'linear',function(){
		this.style.removeAttribute("filter");
	});*/
	
	return speed;
}

function unloadingPoll(html){
	var speed = 100;
	
	$('#poll-content-holder').html(html);
	$('#poll-spinner').hide();
	/*$('#poll-content-holder, #poll-btn-holder').animate({'opacity' : 1}, speed, 'linear', function(){
		$('#poll-spinner').hide();
		this.style.removeAttribute("filter");
	});*/
	
	return speed;
}

function loadPollVote(){
	setTimeout(function(){
		$.ajax({
	    	type: "POST",
	        url:  baseUrl+'ajax/pollform',
	        complete: function(msg){
	        	unloadingPoll(msg.responseText);
	        	initPollInput();
	        }
		});
	},loadingPoll());
}

function loadPollView(pollId){
	if(pollId != undefined && pollId.length > 0){
		setTimeout(function(){
			$.ajax({
		    	type: "POST",
		        url:  baseUrl+'ajax/pollresult',
				data: 'pollId='+pollId,
		        complete: function(msg){
		        	unloadingPoll(msg.responseText);
		        }
			});
		},loadingPoll());
	}
}

