$(document).ready(function() {
	
	$('[class^=tip]').each(function() {
		
		// Check which colour we should pick
		var cat = $(this).parents('[class^=cat]').attr('class').split('cat')[1];
		
		var uiclass = 'ui-tooltip-light';
		if (cat == 1) {
			uiclass = 'ui-tooltip-red';
		}
		else if (cat == 2) {
			uiclass = 'ui-tooltip-green';
		}
		else if (cat == 3) {
			uiclass = 'ui-tooltip-blue';
		}
		
		// Hang the qtip under it
		$(this).qtip({
			
			content: {
				text: 'Loading...',
				ajax: {
					url: $(this).attr('rel')
				}
			},
			position: {
				my: 'right center',
				at: 'left center',
				effect: false
			},
			show: {
				event: 'mouseenter',
				solo: true
			},
			hide: {
				when: 'mouseleave'
			},
			style: {
				classes: uiclass + ' ui-tooltip-shadow'
			}
			
		});
		
	});
	
	jQuery.fn.countDown = function(settings,to) {
	  settings = jQuery.extend({
		duration: 1000,
		fontSize: '36px',
		startNumber: 10,
		endNumber: 0,
		callBack: function() { }
	  }, settings);
	  return this.each(function() {
		
		//where do we start?
		if(!to && to != settings.endNumber) { to = settings.startNumber; }
		
		//set the countdown to the starting number
		$(this).text(to).css('fontSize',settings.fontSize);
		
		//loopage
		$(this).animate({
		  'fontSize': settings.fontSize
		},settings.duration,'',function() {
		  if(to > settings.endNumber + 1) {
			$(this).text(to - 1).countDown(settings,to - 1);
		  }
		  else
		  {
			settings.callBack(this);
		  }
		});
			
	  });
	};
	
	$('[id=votebutton]').live('click', function() {
		
		// Get the id
		var gameid = $(this).attr('class');
		
		// Get the to-vote value
		var voting = $('[name=vote]').val();
		
		// Verify vote
		if (voting > 0 && voting < 6) {
			
			// Disable the vote button
			$(this).attr('disabled', 'disabled');
			$(this).unbind('click');
			
			// Create a post call
			$.post(url + '/ajax/addrating.php', {rate: gameid, rating: voting}, function() {
				
				// Let the user know the vote is completed
				$('.votemessage').html('Bedankt voor uw stem!');
				
			}, 'html')
			
		}
		
	});
	
	// Alarm button
	$('#alarm').click(function() {
		
		// First, clear the wrapper and disable background image.
		$('#wrapper').css('display', 'none');
		$('body').css('background-image', 'none');
		$('html').css('background-image', 'none');
		$('body').css('background-color', '#FFFFFF');
		
		// Now load the fake google.
		$('#alarmarea').css('display', 'block');
		
	});
	
	$('#loadsite').click(function() {
		
		// Unload the alarmarea
		$('#alarmarea').css('display', 'none');
		
		// Load the rest
		$('#wrapper').css('display', 'block');
		$('body').css('background-image', '');
		$('html').css('background-image', '');
		$('body').css('background-color', '');
		
	});
	
});
