// Get a query variable
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

$(document).ready(function() {
	
	/**********
	Toggle image resizing
	**********/
	
	var toggle = getQueryVariable('toggle');
	
	if (toggle != 'off') {
		
		// What we are going to wrap our images in
		var wrap = $('<div/>').addClass('img');
		
		// For each image that has the class 'img'
		$('.img').each(function() {
			
			// Our link element
			var toggleImage = $('<a/>');	
			
			// Before each image, insert our link element
			$(this).before(function() {
				$(toggleImage).addClass('toggleImage').html('show image').attr('href', 'javascript:void(0);');
				return $(toggleImage);
			});
			
		}).wrap(wrap);
		
		// Hide all divs with the class 'div'
		$('div.img').hide();
		
		// Toggle the images
		$('.toggleImage').click(function() {
			
			var link = $(this);
			var image = $(this).next('div.img');
			
			$(image).slideToggle('slow', function() {
				if ($(image).is(':visible')) {
					$(link).html('hide image');
				}
				else {
					$(link).html('show image');
				}
			});
			
		});
	
	}
	/**********
	Toggle text resizing
	**********/
	
	var size;
	var fontSize;
	var lineHeight;
	
	// Resize the text
	function resizeText(size) {
		
		$('.resizeToggle a').removeClass('current');
		
		switch (size) {
			case '12px':
				fontSize = '.75em';
				lineHeight = '1.5em';
				$('.resizeToggle a.regular').addClass('current');
				break;
			case '13px':
				fontSize = '1.25em';
				lineHeight = '1.75em';
				$('.resizeToggle a.large').addClass('current');
				break;
			case '14px':
				fontSize = '1.5em';
				lineHeight = '2em';
				$('.resizeToggle a.jumbo').addClass('current');
				break;
			default:
				fontSize = '.75em';
				lineHeight = '1.5em';
				$('.resizeToggle a.regular').addClass('current');
		}
		
		// Set a cookie for this browser session so the text size stays the same for this user's visit
		$.cookie('textSize', size, { expires: 365 });
		
		// Change the text size and line height
		$('body').css('font-size', fontSize);
		$('body').css('line-height', lineHeight);
	}
	
	// Resize the text on page load to match what is found in the cookie
	resizeText($.cookie('textSize'));
	
	// When the user wants to change the text size
	$('.resizeToggle a, a.resize').click(function() {
		size = $(this).css('font-size');
		resizeText(size);
		return false;
	});
	
	/**********
	Toggle new windows on/off
	**********/
	
	var openNewWindow; 
	
	function toggleNewWindow(value) {
		$('div.linkToggle a').removeClass('current');
		
		if (value == false) {
			openNewWindow = false;
			$.cookie('openNewWindow', false, { expires: 365 });
			$('div.linkToggle a.off').addClass('current');
		}
		else {
			openNewWindow = true;
			$.cookie('openNewWindow', true, { expires: 365 });
			$('div.linkToggle a.on').addClass('current');
		}
	}
	
	// Get cookie on page load
	if ($.cookie('openNewWindow') == 'false') {
		openNewWindow = false;
		toggleNewWindow(false);
	}
	else {
		openNewWindow = true;
		toggleNewWindow(true);
	}
	
	// When user clicks the "on" or "off" button
	$('div.linkToggle a.linkToggle').click(function() {
		if ($(this).hasClass('on')) {
			toggleNewWindow(true);
			return false;
		}
		else {
			toggleNewWindow(false);
			return false;
		}
	});
	
	// Make rel=external links open in a new window (alternative to target=_blank)
	$('a[rel*=external]').click( function() {
		if (openNewWindow == true) {
			window.open(this.href);
			return false;
		}
	});
	
	/**********
	Switch CSS style sheet
	**********/

	$.styleswitcher = function(name) {
		// Hide body
		$('body').toggle();
		$('link[rel*=stylesheet][title]').each(function(i) {
			this.setAttribute('rel', 'alternate stylesheet');
			this.disabled = true;
			
			// If the style sheet title attribute is the same as the style sheet we are trying to activate
			if (this.title == name) {
				this.disabled = false;
				this.setAttribute('rel', 'stylesheet');
			}
			
			$('.styleToggle a').removeClass('current');
			$('.styleToggle a[rel="' + name + '"]').addClass('current');
			
		});
		// Show body
		$('body').toggle();
		// Set cookie
		$.cookie('stylesheet', name, {expires: 365});
	};
	
	// When a styleswitcher link is clicked
	$('.styleswitcher').click(function() {
		$.styleswitcher(this.getAttribute('rel'));
		return false;
	});
	if ($.cookie('stylesheet')) {
		$.styleswitcher($.cookie('stylesheet'));
	}
	else {
		$('.styleToggle a.normal').addClass('current');
	}
	
	/**********
	Toggle bottom nav on & off
	**********/
	
	var bottomNav = $('#bottomNav');
	
	function toggleNav(value) {
		$('div.navToggle a').removeClass('current');
		
		if (value == false) {
			$(bottomNav).hide();
			$.cookie('navToggle', false, { expires: 365 });
			$('div.navToggle a.off').addClass('current');
			$('body:not(.mobile) #contentContainer').css('padding-bottom', '175px');
			$('#footerContainer').css('height', '175px');
			$('#footer').css('height', '115px');
		}
		else {
			$(bottomNav).show();
			$.cookie('navToggle', true, { expires: 365 });
			$('div.navToggle a.on').addClass('current');
			$('body:not(.mobile) #contentContainer').css('padding-bottom', '300px');
			$('#footerContainer').css('height', '300px');
			$('#footer').css('height', '240px');
		}
	}
	
	// Get cookie on page load
	if ($.cookie('navToggle') == 'false') {
		toggleNav(false);
	}
	else if ($.cookie('navToggle') == 'true') {
		toggleNav(true);
	}
	else {
		toggleNav(false);
	}
	
	// When user clicks the "on" or "off" button
	$('div.navToggle a').click(function() {
		if ($(this).hasClass('on')) {
			toggleNav(true);
			//return false;
		}
		else {
			toggleNav(false);
			//return false;
		}
	});
	
	/**********
	Add a 'js' class to the body so we can be certain that JavaScript is enabled
	**********/
	
	$('body').addClass('js');
	
	/**********
	Fix dropdowns in IE6
	**********/
	
	// Make sure this does not effect our mobile CSS
	var dropDowns;
	if ($.cookie('view') == 'regular') {
		dropDowns = $('#header .nav li.main');
	}
	
	$(dropDowns).mouseover(function() {
		$(this).children('ul.subMenu').show();
	});
	$(dropDowns).focusin(function() {
		$('.subMenu').hide();
		$(this).children('ul.subMenu').show();
	});
	$(dropDowns).mouseout(function() {
		$(this).children('ul.subMenu').hide();
	});
	
	/**********
	Toggle accessibility
	**********/
	
	$('#accessibilityToggle').click(function() {
		$('#accessibility').slideToggle('slow');
	});
	
	/**********
	Zeba stripe table rows
	**********/
	
	$('tbody tr:odd').addClass('even');
	$('tbody tr:last:not(tr.even) td').css('border-bottom', 'none');
	
	/**********
	Make programs list fall to bottom of page
	**********/
	
	var leftHeight = $('#content div.left').innerHeight();
	var rightHeight = $('#content div.right').innerHeight();
	
	// If the right content is taller than the left content
	if (leftHeight < rightHeight) {
		// Put 'programs' at bottom of page
		var newPadding = rightHeight - leftHeight + 'px';
		$('div.programs').css('margin-top', newPadding);
	}
	
	/**********
	Toggle the Flash statistics
	**********/
	
	$('h3 span a').click(function() {
		var stats = $(this).parents('.downloadGroup').children('.downloadStatsContainer');

		if ($(stats).css('display') == 'none') {
			$(stats).show();
			$(this).html('Hide Download Statistics');
		}
		else {
			$(stats).hide();
			$(this).html('Show Download Statistics');
		}
	});
	
	/**********
	Konami code (up, up, down, down, left, right, left, right, b, a)
	**********/
	
	if ( window.addEventListener ) {
		var kkeys = [], konami = '38,38,40,40,37,39,37,39,66,65';
		window.addEventListener('keydown', function(e){
		kkeys.push( e.keyCode );
		if ( kkeys.toString().indexOf( konami ) >= 0 ) {
				var message;
				
				if ($.browser.webkit) {
					message = 'Ahh.. Webkit. A good choice.';
				}
				else if ($.browser.opera) {
					message = 'Opera, eh? Interesting...';
				}
				else if ($.browser.msie) {
					message = 'Come on. Seriously? Internet Explorer? Please, go download Firefox.';
				}
				else if ($.browser.mozilla) {
					message = 'I like your style. Firefox FTW.';
				}
				else {
					message = 'So your not using Webkit, Opera, IE, or Firefox... You are quite the adventurous one.';
				}

		       alert(message); 
		}
		}, true);
	}
	
	/**********
	Comptia coolness
	**********/
	
	var comptiaLogo = $('img.comptia');
	var comptiaDesc = $('div.comptia');
	
	$(comptiaLogo).bind({
		mouseenter: function(){
			$(comptiaDesc).filter(':not(:animated)').delay(25).fadeIn('fast', function() {
				if (!($.support.opacity)) { 
					this.style.removeAttribute('filter');
				}
			});
		},
		mouseleave: function(){
			$(comptiaDesc).fadeOut('fast');
		}
	});
	
});