var poll_dim = {
	default_poll_result_height:0,
	full_poll_result_height:0
};

/**
 *
 * check for decent browsers
 * ie. not IE 7 or less
 *
 * @param	void
 * @return	boolean
 *
 **/
function goodBrowser()
{
    if ( $.browser.msie )
    {
        if ( $.browser.version >= '8.0' )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
	return true;
}
	
$(function() {
	var poll = {
		// set event handler for "see all polls" link
		set_see_other_poll_event_handler: function(){
			var thispoll = this;
			$("#see-other-results").click(function(event){
				// load other poll results if not loaded yet
				if($('#other-results-container').length == 0){
					$("#sos-results").css({
						height:$('#current-results-container').outerHeight() + $('.other-results').outerHeight() + parseInt($('#current-results-container').css('margin-top')) + 'px',
						overflow:'hidden'
						});

					$.ajax({
						url:      "see-all-polls-ajax.php",
						success:  function(htmlData){
									$('#sos-results').append(htmlData);
								  },
						async:false
					});
					thispoll.set_close_other_poll_event_handler();
				}
				
				// get results container dimensions
				//if(poll_dim.default_poll_result_height == 0) {
					poll_dim.default_poll_result_height = $('#current-results-container').outerHeight() + $('.other-results').outerHeight() + parseInt($('#current-results-container').css('margin-top'));
					poll_dim.full_poll_result_height = $('#current-results-container').outerHeight() + $('#other-results-link-top').outerHeight() + parseInt($('#current-results-container').css('margin-top'));
					if($('#other-results-container').length >0){
						poll_dim.full_poll_result_height += $('#other-results-container').outerHeight() + parseInt($('#other-results-container').css('margin-top')) + $('#other-results-link-bottom').outerHeight();
					}
				//}

				// animate
				if($('#sos-results').height() == poll_dim.default_poll_result_height){
					$(this).text("Hide other results");
					$('#sos-results').animate({ 
						height: poll_dim.full_poll_result_height+'px'
					}, 500 );
				}
				else{
					$(this).text("See other results");
					$('#sos-results').animate({ 
						height: poll_dim.default_poll_result_height+'px'
					}, 500 );
				}
				//alert(poll_dim.default_poll_result_height+' => '+poll_dim.full_poll_result_height);
				//alert($("#sos-results").height);
				return false;
			});
		},
		set_close_other_poll_event_handler: function(){
			$("#hide-other-results").click(function(event){
				//if(poll_dim.default_poll_result_height == 0) {
					poll_dim.default_poll_result_height = $('#current-results-container').outerHeight() + $('.other-results').outerHeight() + parseInt($('#current-results-container').css('margin-top'));
					poll_dim.full_poll_result_height = $('#current-results-container').outerHeight() + $('#other-results-link-top').outerHeight() + parseInt($('#current-results-container').css('margin-top'));
					if($('#other-results-container').length >0)
						poll_dim.full_poll_result_height += $('#other-results-container').outerHeight() + parseInt($('#other-results-container').css('margin-top')) + $('#other-results-link-bottom').outerHeight();
				//}
				$("#see-other-results").text("See other results");
				$('#sos-results').animate({ 
					height: poll_dim.default_poll_result_height+'px'
				}, 500 );
				//alert(poll_dim.full_poll_result_height+' => '+poll_dim.default_poll_result_height);
				//alert($("#sos-results").height);
				return false;
			});
			
		},
		set_tag_event_handler: function(tag){
			return tag.click(function(event){
				// change tag class
				if(tag.attr('id') == 'poll-tag'){
					$('#poll-tag').removeClass('poll-tag-off').addClass('poll-tag-on');
					$('#results-tag').removeClass('results-tag-on').addClass('results-tag-off');
				}
				else if(tag.attr('id') == 'results-tag'){
					$('#poll-tag').removeClass('poll-tag-on').addClass('poll-tag-off');
					$('#results-tag').removeClass('results-tag-off').addClass('results-tag-on');
				}
				
				// change poll's main content class
				if(tag.attr('id') == 'poll-tag'){
					$("#see-other-results").text("See other results");
					$("#sos-results").removeClass("poll-result-visible").addClass("poll-result-hidden");
					$("#sos-poll").removeClass("poll-form-hidden").addClass("poll-form-visible");
				}
				else if(tag.attr('id') == 'results-tag'){
					$("#sos-poll").removeClass("poll-form-visible").addClass("poll-form-hidden");
					$("#sos-results").removeClass("poll-result-hidden").addClass("poll-result-visible");
				};
				
				// reset poll section height.
				//if(poll_dim.default_poll_result_height == 0) {
					poll_dim.default_poll_result_height = $('#current-results-container').outerHeight() + $('.other-results').outerHeight() + parseInt($('#current-results-container').css('margin-top'));
					poll_dim.full_poll_result_height = $('#current-results-container').outerHeight() + $('#other-results-link-top').outerHeight() + parseInt($('#current-results-container').css('margin-top'));
					if($('#other-results-container').length >0)
						poll_dim.full_poll_result_height += $('#other-results-container').outerHeight() + parseInt($('#other-results-container').css('margin-top')) + $('#other-results-link-bottom').outerHeight();
				//}
				$("#sos-poll").css({height:poll_dim.default_poll_result_height, display:''});
				$("#sos-results").css({height:poll_dim.default_poll_result_height, display:''});
				//alert('#current-results-container height: '+$('#current-results-container').outerHeight()+"\n"+'top margin: '+parseInt($('#current-results-container').css('margin-top'))+"\n"+'other results height: '+$('.other-results').outerHeight()+"\n"+'results height = '+poll_dim.full_poll_result_height);
				event.preventDefault();
				return false;
			});
		},
		set_vote_button_event_handler: function(){
			$("#vote-button").click(function(event){
				// check if a vote option has been chosen.
				var no_vote = true;
				$('.vote_opt').each(function(){
					if($(this).attr('checked') == true)
						no_vote = false;
				});
				if(no_vote)
					return false;
					
				// send vote
				$.post("vote-ajax.php",
					{poll_id:$('input[name=poll_id]').val(), vote_opt_id:$('input[name=vote_opt_id]:checked').val()},
					function(xmlData){
					var total = $(xmlData).find('total').text();
					var options = $(xmlData).find('option').each(function(i){
						var rate = parseFloat($(this).text());
						var bar_length = Math.round(rate*150);
						var bar_value = Math.round(rate*100);
						$('.current-poll-bar').eq(i).css('width', bar_length+'px');
						$('.current-poll-bar').eq(i).next('.percentage').text("("+bar_value+"%)");
						$('#results-tag').trigger('click');
					});
				},'xml');
				
				// hide vote button
				$("#vote-button").hide("slow");			
				
				return false;
			});
		}
	};
	
	if (goodBrowser()) {
		//poll.init_poll_height();
		poll.set_see_other_poll_event_handler();
		poll.set_close_other_poll_event_handler();
		poll.set_tag_event_handler($("#poll-tag"));
		poll.set_tag_event_handler($("#results-tag"));
		poll.set_vote_button_event_handler();
	}
	
});