var year = 2010;

$(document).ready(function(){
    // category selector
    $('#categoryTab_10').categoryLoader();
    $('#categoryTab_11').categoryLoader();
    $('#categoryTab_12').categoryLoader();
    $('#categoryTab_13').categoryLoader();
    $('#categoryTab_14').categoryLoader();
    $('#categoryTab_15').categoryLoader();
    $('#categoryTab_16').categoryLoader();
    $('#categoryTab_17').categoryLoader();
    $('#categoryTab_18').categoryLoader();


    // load the first category
    $('#categoryTab_18').trigger('click');

    // bin the slider nav buttons
    $('#voting_bottomNav-prv').voteSliderButton(1);
    $('#voting_bottomNav-nxt').voteSliderButton(-1);
    $('#voting_infoPane-nav-prv').voteSliderButton(1);
    $('#voting_infoPane-nav-nxt').voteSliderButton(-1);

    // checkbox behaviour
    $('#bigCheckbox').vote();

    // form submission handler
    $('#categoryTab_7').submitVote();

    // in case the page has been re-loaded, re-tick the categories
    $('#voteForm input').each(function(){
	if($(this).val() > 0) {
	    $('#categoryTab_'+$(this).attr('id').substr(14,1)).addClass('done');
	}
    });

    // if all categories ticked, enable the submit button
    if($('#categoryTabs .done').length == 6) {
	$('#categoryTab_7').removeClass('disabled');
    }
});

// use window.load rather than document.ready so that we ensure the images are loaded before the animation starts
$(window).load(function(){
	// start the animation after a short delay
	setTimeout("animate()",1000);
});


// animate the logo sign
function animate() {
	var spd = 1500;
	
	// show the coming soon logo
	$('#comingSoon_coverVoting').fadeIn('slow');
	
	// show the main logo and scroll
	$('#theMaggies_logo').animate({ "top": "-5px" }, spd, "swing");
	
	$('#scroll').animate({ "top": "70px" }, spd, "swing", function(){
		// add some glitter to the scroll
		$('#stage-front').addSparkleGroup(501,116,4,220,50);
	});
};


function retick() {
	// remove the tick from the box
	$('#bigCheckbox').removeClass('checked');
				
	// re-enable the tick if this is the cover selected
	var id = (parseInt($('#voting_coverView-holder').css('left'))/-1000) + 1;
	var coverID = $('#voting_coverView_'+id).attr('ident');
	var categoryID = $('#categoryTabs .selected').attr('id').substr(12,1);
	
	if($('#vote_category_'+categoryID).val() == coverID) {
		$('#bigCheckbox').addClass('checked');
	}
}

function rehighlight() {
	// remove the selected
	$('#voting_thumbBrowser li a').removeClass('selected');
				
	// re-enable the tick if this is the cover selected
	var id = (parseInt($('#voting_coverView-holder').css('left'))/-1000) + 1;
	var coverID = $('#voting_coverView_'+id).attr('ident');
	$('#voting_thumb_'+coverID).addClass('selected');
}

function retitle() {
	// re-enable the tick if this is the cover selected
	var id = (parseInt($('#voting_coverView-holder').css('left'))/-1000) + 1;
	var title = $('#voting_coverView_'+id+' img').attr('alt');
	
	$('#voting_infoPane-footer h4').html(title);
}

// slider behaviour for the vote page (queued from the bottom nav, or right pane nav)
// this is bound to each button, with a 1 for previous, and a -1 for next passed in for "direction"
jQuery.fn.extend({
	voteSliderButton: function(direction) {
		
		$(this).bind('click',function(){
			
			// only move if the button is not diabled
			if (!$(this).hasClass('disabled')) {
				
				$('#voting_thumbBrowser li a').removeClass('selected');
				
				var dst1 = 1000 * direction; 	// how far to move the cover images
				var dst2 = 281 * direction;		// how far to move the info pane
				
				$('#voting_infoPane-holder').animate( { "left": '+=' + dst2 + 'px' }, "slow", "swing");
				$('#voting_coverView-holder').animate( { "left": '+=' + dst1 + 'px' }, "slow", "swing", function(){
						
						// re-enable the buttons if applicable
						if(parseInt($('#voting_coverView-holder').css('left')) < 0) {
							$('#voting_bottomNav-prv').removeClass('disabled');
							$('#voting_infoPane-nav-prv').removeClass('disabled');
						}
						
						if(parseInt($('#voting_coverView-holder').css('left')) > -4000) {
							$('#voting_bottomNav-nxt').removeClass('disabled');
							$('#voting_infoPane-nav-nxt').removeClass('disabled');
						}
						
						retick();
						retitle();
						rehighlight();
				});
				
				// disable both the buttons while we scroll
				$('#voting_bottomNav-prv').addClass('disabled');
				$('#voting_bottomNav-nxt').addClass('disabled');
				$('#voting_infoPane-nav-prv').addClass('disabled');
				$('#voting_infoPane-nav-nxt').addClass('disabled');
			}
			
			return false;
		});
	},
	
	voteThumbButton: function() {
		
		$(this).bind('click',function(){
			
			// only move if we are not currently moving
			if ($('#voting_thumbBrowser li a.selected').length == 1) {
				
				$('#voting_thumbBrowser li a').removeClass('selected');
				
				var currentPosition = parseInt($('#voting_coverView-holder').css('left'));
				var targetPosition = parseInt($(this).attr('position'))*-1000;
				var dst1 = (currentPosition - targetPosition) * -1;
				var dst2 = (dst1 / 1000) * 281;
				//var dst1 = 1000 * direction; 	// how far to move the cover images
				//var dst2 = 281 * direction;		// how far to move the info pane
				
				
				$('#voting_infoPane-holder').animate( { "left": '+=' + dst2 + 'px' }, "slow", "swing");
				$('#voting_coverView-holder').animate( { "left": '+=' + dst1 + 'px' }, "slow", "swing", function(){
						
						// re-enable the buttons if applicable
						if(parseInt($('#voting_coverView-holder').css('left')) < 0) {
							$('#voting_bottomNav-prv').removeClass('disabled');
							$('#voting_infoPane-nav-prv').removeClass('disabled');
						}
						
						if(parseInt($('#voting_coverView-holder').css('left')) > -4000) {
							$('#voting_bottomNav-nxt').removeClass('disabled');
							$('#voting_infoPane-nav-nxt').removeClass('disabled');
						}
						
						retick();
						retitle();
						rehighlight();
				});
				
				// disable both the buttons while we scroll
				$('#voting_bottomNav-prv').addClass('disabled');
				$('#voting_bottomNav-nxt').addClass('disabled');
				$('#voting_infoPane-nav-prv').addClass('disabled');
				$('#voting_infoPane-nav-nxt').addClass('disabled');
			}
			
			return false;
		});
	},
	
	vote: function() {
		$(this).bind('click',function() {
			// what is the coverID of the ticked item?
			var id = (parseInt($('#voting_coverView-holder').css('left'))/-1000) + 1;
			var coverID = $('#voting_coverView_'+id).attr('ident');
			
			// what category are we in?
			var categoryID = $('#categoryTabs .selected').attr('id').substr(12,1);
			
			// set the form element
			$('#vote_category_'+categoryID).val(coverID);
			
			// add a tick in the nav
			$('#categoryTab_'+categoryID).addClass('done');
			
			// put the tick in the box
			$('#bigCheckbox').addClass('checked');
			
			// check to see if all categories have been completed
			if($('#categoryTabs .done').length == 6) {
				$('#categoryTab_7').removeClass('disabled');
			}
			
			return false;
		});
	},
	
	categoryLoader: function() {
		$(this).bind('click',function(){
			$('#bigCheckbox').removeClass('checked');
			
			// remove the old selected
			$('#categoryTabs li').removeClass('selected');
			
			// add the new selected
			$(this).addClass('selected');
			
			// change the left hand pane title
			var cid = $(this).attr('id').substr(12,2);

			// hide the nav elements on the overall winner
			if(cid == 18) {
			    $('#voting_bottomNav-prv').hide();
			    $('#voting_bottomNav-nxt').hide();
			    $('#voting_infoPane-nav-prv').hide();
			    $('#voting_infoPane-nav-nxt').hide();
			} else {
			    $('#voting_bottomNav-prv').show();
			    $('#voting_bottomNav-nxt').show();
			    $('#voting_infoPane-nav-prv').show();
			    $('#voting_infoPane-nav-nxt').show();
			}

			$('#voting_infoPane-title').attr('src','/images/title_category_'+cid+'.png');
			
			// load the data from the server
			$.post('/script/votes/winners-2010.php', { categoryID:cid, year:year }, function(data){
				var content = data.split('|||');
				
				// move the sections back to the start
				$('#voting_coverView-holder').animate( { "left": '0px' }, "slow", "swing");
				$('#voting_infoPane-holder').animate( { "left": '0px' }, "slow", "swing", function(){
					retick();
					retitle();
					rehighlight();
					
					$('#voting_bottomNav-nxt').removeClass('disabled');
					$('#voting_infoPane-nav-nxt').removeClass('disabled');
					
					if(cid == 8)
					{
						$('#voting_bottomNav-nxt').addClass('disabled');
						$('#voting_infoPane-nav-nxt').addClass('disabled');
					}
				});
				
				$('#voting_bottomNav-prv').addClass('disabled');
				$('#voting_infoPane-nav-prv').addClass('disabled');
				
				$('#voting_coverView-holder').html(content[0]);
				$('#voting_infoPane-holder').html(content[1]);
				$('#voting_thumbBrowser').html(content[2]);
				
				// thumb browser behaviour
				$('#voting_thumbBrowser li a').voteThumbButton();
			}); 
		});
	},
	
	submitVote: function() {
		$(this).bind('click',function(){
			if(!$('#categoryTab_7').hasClass('disabled')) {
				$('#voteForm').submit();
			} else {
				$('#allCategoryWarning').show();
			}
		});
	}
});

