// Set variables to use as constants
var THEME_COOKIE = 'goodLuckTheme';

// Create a JavaScript object
$.mainObject = {
	// Initialize the main object
	init: function() {
		// Handle a focus on an input with a default text
		$('input.default').bind('focus', function() {
			// Check if the value is the original value, if so clear it
			if ($(this).val() == $(this).attr('title'))
				$(this).val('');
		});

		// Handle a blur on an input with a default text
		$('input.default').bind('blur', function() {
			// Check if the value is empty, if so replace it with the default
			if ($.trim($(this).val()) == '')
				$(this).val($(this).attr('title'));
		});
		
		// Enable drop down menus for the main navigation on mouseover
		$('#mainNavigation li.hasSubNavigation').hover(
			function() {
				$(this).addClass('showSubNavigation');
			},
			function() {
				$(this).removeClass('showSubNavigation');
			}
		);
		
		// Change the site theme if a cookie is set
		var themeCookie = $.cookie(THEME_COOKIE);
	    if (themeCookie) {
			if (themeCookie == 'themeGrass') {
				// Activate the alternative stylesheet
				$("link[title='Gras en lucht']").attr('rel', 'stylesheet').removeAttr('disabled');
			}
			else {
				// Deactivate the alternative stylesheet
				$("link[title='Gras en lucht']").attr('rel', 'alternative stylesheet').attr('disabled', true);
			}
	    }
		
		// Enable the style sheet switcher
		$('a#themeGrass').bind('click', function() {
			// Activate the alternative stylesheet
			$("link[title='Gras en lucht']").attr('rel', 'stylesheet').removeAttr('disabled');
			
			// Set a cookie to remember the settings
			$.cookie(THEME_COOKIE, 'themeGrass', {path: '/', expires: 10});
			
			// Prevent the link from jumping to its target
			return false;
		});
		
		$('a#themeGrey').bind('click', function() {
			// Deactivate the alternative stylesheet
			$("link[title='Gras en lucht']").attr('rel', 'alternative stylesheet').attr('disabled', true);
			
			// Set a cookie to remember the settings
			$.cookie(THEME_COOKIE, 'themeGrey', {path: '/', expires: 10});
			
			// Prevent the link from jumping to its target
			return false;
		});
		
		// Remove TYPO3 anchor tags that cripple the scrollable list
		$('.verticalScroll a:empty').remove();
		
		// Make vertical scrollable content scroll
		$('.verticalScroll').simplyScroll({
			autoMode: 'loop',
			horizontal: false,
			frameRate: 16,
			speed: 1,
			pauseOnHover: true
		});
		
		// Expand truncated text
		$('a.expand').bind('click', function() {
			// Expand the truncated text
			$(this).prevAll('.truncated').toggle();
			
			// Toggle the class
			$(this).toggleClass('readLess');
			
			// Change the text
			if ($(this).hasClass('readLess'))
				$(this).text($(this).attr('rel'));
			else
				$(this).text($(this).attr('title'));
			
			// Prevent the link from jumping to its target
			return false;
		});
		
		// Hide the link to cancelled games if there aren't any
		if ($('#cancelled a').length == 0) {
			$('#cancelled').hide();
		}
		
		// Register Flash content with SWFObject
		var flashvars = {};
		flashvars.url = 'http%3A%2F%2Fsoundcloud.com%2Fvvgoodluck%2Fons-cluppie';
		flashvars.show_comments = 'false';
		flashvars.auto_play = 'false';
		flashvars.color = '333333';
		swfobject.embedSWF('http://player.soundcloud.com/player.swf', 'anthemPlayer', '200', '81', '9.0.0', '', flashvars);
	}
};

// Replace headers with Cufon
// This has to be done before document.ready, to prevent headers from flashing (IE bug)
//Cufon.set('fontWeight', '300');
//Cufon.replace('h2');
//Cufon.set('fontWeight', '500');
//Cufon.replace('h1')('h3');

// Initialize the main object, which handles all website initialization
$(document).ready(function() {
	// Initialize the main object
	$.mainObject.init();
});