$(document).ready(function()
{

	Cufon.replace('h2', { hover: true });
	Cufon.replace('h3');

	var deviceAgent = navigator.userAgent.toLowerCase();
	var isIOS = deviceAgent.match(/(iphone|ipod|ipad)/);


   	// set active nav color
    // switch(page) {
    // 
    //  case "products":
    //      $('#header .products').css('color', '#ffcc00');
    //      break;
    // 
    //  case "news":
    //              $('#header .news').css('color', '#ffcc00');
    //      break;
    // }



    /* --------------- MAIN NAV ROLLOVERS ---------------- */

	// cool stuff button
	$('#header .cs_cool_stuff').mouseover(function() {
		$('#header .cool_stuff_cs').css('visibility', 'visible');
		$('#header .cool_stuff_cs').css('opacity', 0);
		$('#header .cool_stuff_cs').stop(true, false).animate({ "opacity":1}, 150);
	});

	$('#header .cs_cool_stuff').mouseout(function() {;
		$('#header .cool_stuff_cs').stop(true, false).animate({ "opacity":0}, 150);
	});

	// my hangar button
	$('#header .cs_my_hangar').mouseover(function(e) {
		$('#header .hangar_cs').css('visibility', 'visible');
		$('#header .hangar_cs').css('opacity', 0);
		$('#header .hangar_cs').stop(true, false).animate({ "opacity":1}, 150);
	});

	$('#header .cs_my_hangar').mouseout(function() {;
		$('#header .hangar_cs').stop(true, false).animate({ "opacity":0}, 150);
	});




	/* --------------- LEAVING SITE INTERSTITIAL ---------------- */

	var target_url, tracking_url;

	function launch_interstitial(e) {
		e.preventDefault();
		$('#leave_site').css('visibility', 'visible');
		$('#leave_site').show();
		$('#leave_site .leave_site_main').css('opacity', 0);
		$('#leave_site .leave_site_main').animate({opacity: 1}, isIOS ? 0 : 400);
		$('#leave_site').css("height", $(document).height());
		target_url = $(this).attr('href');
		tracking_url = $(this).data('tracking_url');
		center_interstitial();

		if(isIOS) {
			$('html, body').animate({scrollTop:0}, 0);
			$('#leave_site').css('width', 1000);
	    }
	}
    
	function center_interstitial() {
		var topCalc = ($(window).height() / 2) - ($('#leave_site .leave_site_main').height()/2) + $(window).scrollTop();
		$('#leave_site .leave_site_main').css('margin-top', isIOS ? 20 : topCalc);
	}

	if(!isIOS)
		$(window).bind('scroll resize', center_interstitial);

	// 'back to airhogs' button
	$('#leave_site .leave_buttons').children(":first").click(function(e) {
		e.preventDefault();
		$('#leave_site .leave_site_main').animate({opacity: 0}, isIOS ? 0 : 300, function() {
			$('#leave_site').hide();
		});
	});

	// 'continue' button
	$('#leave_site .leave_buttons').children(":last").click(function(e) {
		e.preventDefault();
		function resume(){
			$('#leave_site').hide();
			window.open(target_url, '_blank');
		}
		
		if(tracking_url){
			track_click(tracking_url, resume);
		} else {
			resume();
		}
	});
	
    // Bindings
    $('.social .social_icons a').click(launch_interstitial);
    $('a.external-link').click(launch_interstitial);


	/* --------------- CUSTOMER CARE / SUPPORT LIGHTBOX ---------------- */

	$('.cust_care').click(function(e) {
		e.preventDefault();
		$('#customer_care').css('visibility', 'visible');
		$('#customer_care').show();
		$('#customer_care .customer_care_main').css('opacity', 0);
		$('#customer_care .customer_care_main').animate({opacity: 1}, isIOS ? 0 : 400);
		$('#customer_care').css("height", $(document).height());
  		center_cust_care();

		if(isIOS) {
			$('html, body').animate({scrollTop:0}, 0);
			$('#customer_care').css('width', 1000);
	    }
	});

	function center_cust_care() {
		var topCalc = ($(window).height() / 2) - ($('#customer_care .customer_care_main').height()/2) + $(window).scrollTop();
		$('#customer_care .customer_care_main').css('margin-top', isIOS ? 20 : topCalc);
	}

	if(!isIOS)
		$(window).bind('scroll resize', center_cust_care);

    // 'back to airhogs' button
	$('#customer_care .big_button').click(function(e) {
		e.preventDefault();
		$('#customer_care .customer_care_main').animate({opacity: 0}, isIOS ? 0 : 300, function() {
			$('#customer_care').hide();
		});
	});
});


function track_click(url, callback){
	var random = (Math.random() * 10000000000000) + "";
	var frame = $('<iframe/>', { src: url + random }).hide();
	frame.load(function(){
		callback();
		//frame.remove();
	});
	$('body').append(frame);
}



