/**
 * Mutes firebug console code and errors on browsers where firebug is not installed
 */
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i) {
        window.console[names[i]] = function() {};
	}
}

//Global jQuery DOM Ready
$(function() {
	
	/* 
	=======================
	Google Analytics Functions
	=======================
	*/
	//Activate modal windows
	$(".jqmWindow").jqm();
	
	/* 
	=======================
	Google Analytics Functions
	=======================
	*/
	$("#game_alt_link").click(function(){
		//console.log('product ALT BUTTON clicked');
		if ($.os.name == 'mac') {
			pageTracker._trackPageview('/_convert-alt/mac/'+ $(this).attr('href'));
		} 
		else {
			pageTracker._trackPageview('/_convert-alt/'+ $(this).attr('href'));
		}
	});
	$("#game_buynow_link").click(function(){
		//console.log('product BUY NOW clicked');
		if ($.os.name == 'mac') {
			pageTracker._trackPageview('/_convert-buy/mac/'+ $(this).attr('href'));
		} 
		else {
			pageTracker._trackPageview('/_convert-buy/'+ $(this).attr('href'));
		}
	});
	$("#game_tryfree_link").click(function(){
		//console.log('product TRY FREE clicked');
		if ($.os.name == 'mac') {
			pageTracker._trackPageview('/_convert-try/mac/'+ $(this).attr('href'));
		} 
		else {
			pageTracker._trackPageview('/_convert-try/'+ $(this).attr('href'));
		}
	});
	
	/* 
	=======================
	Mac-Only Interception
	=======================
	*/
		
	//If we're on a game page...
	if ( window.location.pathname == "/game.php") {
		
		//...and if the user is on a mac...
		if ($.os.name == 'mac') {
			
			//Hide the "Try Free" button as it's only only available for Windoze.
			$('#game_tryfree').parent().hide();
			
			//First establish the generic "close" listener within the modal in the modal
			$('#macDownloadModalClose').click( function() { 
				$("#macDownloadModal").jqmHide();
				return false;
			});
			
			
			
			//Change the behavior of the call-to-action buttons
			$('#game_buynow_link').click( function () {
				 
				//Remember the button's original href.
				var originalButtonHref = $(this).closest('a').attr('href');
				
				if ( $('#macModalAlt').attr('href') == "#none" ) {
					$('#macModalAlt').hide();
					$('#macModalContinue').css('margin-left', '135px');
				}
				
				//Set the "continue" link to the button's original href.
				$('#macModalContinue').attr('href', originalButtonHref);
				
				//Show the modal window
				$("#macDownloadModal").jqmShow();
				
				//stay on the page
				return false;
			
			});
			
			//Let's change the content of the modal window if it's a pre-sale
			/*if ( $('#game_alt_link img[src$="purple_presale_btn.gif"]').length == 1 ) {
				$('#macDownloadModal p:first').html('Looks like you\'re on a mac. Pre-ordering is not yet available for the Mac version.');
				$('#macModalContinue').html('Continue pre-ordering a <br />Windows-only version.');
			}*/
			
		}
		
	}

	
	/* 
	=======================
	Game Finder
	=======================
	*/
	$('#os_list option[value=""]:gt(0)').remove();
	
	/* 
	=======================
	404 Page Operations
	=======================
	*/
	
	//If this function firesm jQuery is loading fine. So show the search utlility.
	$('#googleSearchWrap').show();
	
	//Just for kicks...
	$('#googleQuery').focus(function() {
		$(this).css('border','2px solid #FFCC00');
	}).blur( function () {
		$(this).css('border','2px solid #A17F00');
	})
	
	//Before the form submits, take on the site: directive.
	$("#googleSearch").submit(function() {
		var originalQuery = $('#googleQuery').val();
		if ( originalQuery == "") {
			$('#googleQuery').css('border','2px solid red');
			return false;
		}
		$('#googleQuery').val("site:hoylegaming.com " + originalQuery);
		return true;
	});
	
});