$(document).ready(function(){
	setupajaxhistory();
	overRideNavLinks();
});

function setupajaxhistory() {
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
}

function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		$("#justForAjax").css("visibility","hidden");
		
		$("#justForAjax").load("/pageParser.php?q=" + hash, animateIn);
	} else {
		// start page
	//	$("#justForAjax").load("pageParser.php?q=" + "index.php",animateIn);
	}
	
}


function overRideNavLinks() {
	
	formatajaxlinks();
	
	$(".dropdown a").click(function() {
		
		if($(this).hasClass('ext')) {
		  return;
		}
		
		var myHref = $(this).attr('href');
	
		// hack to get in voids without doing anything
		if(myHref == "javascript:void(0);") {
			return true;
		}
		
		
		animateOut($(this));
		
		if(myHref !== "keepOut/" && myHref !== "../keepOut/") {
			return false;	
		}
		
	});
	
	
	
}


function animateOut(obj) {


	if($("#bCell").size() > 0) {
				
		$("#bCell").fadeOut(250,function() {
			doThePageSwitch(obj);
		})
	}
	
	$("#content").append('<img src="/_img/ajax-loader.gif" height="31" width="31" class="ajaxloader" />');
	
	if($("#lCell").size() > 0) {
		$('#rCell').animate({"height":"0px","opacity":"0"},{duration:250,queue:false});
		$('#lCell').animate({"height":"0px","opacity":"0"},250,function() {
				
			$("#content").fadeOut(250,function() {
					doThePageSwitch(obj);
			});	
			
		});
	}
	
	
}

var animateIn = function() {
	
	$(".ajaxloader").remove();
	
	// set up internal ajax links
	formatajaxlinks();
	
	if($("#bCell").size() > 0) {
		
		$("#bCell").css("opacity","0");
		$("#content").css("background-image","none");
		$("#justForAjax").css("visibility","visible");
		
		$("#bCell").animate({"opacity":"1"},250,function() {
			fixPngs();
		});		
	}
	
	
	if($("#lCell").size() > 0) {

		$("#content").css("opacity","0");		
		$("#lCell").css("opacity","0");
		$("#justForAjax").css("visibility","visible");
		
		$("#content").animate({"opacity":"1"},250,function() {
			$("#lCell").animate({"opacity":"1"},250,function() {
				fixPngs();
			});
		});		
		
	}
	
}

function formatajaxlinks() {
	$(".nextbrand, .front-page-logos a").click(function() {
				
		$(this).css('font-weight','normal');
		
		var myHref = $(this).attr('href');
				
		// hack to get in voids without doing anything
		if(myHref == "javascript:void(0);") {
			return true;
		}		
		
		animateOut($(this));
		
		return false;		
	});
}


function doThePageSwitch(obj) {
	
	
	var hash = obj.attr('href');
	hash = hash.replace(/^.*#/, '');
	// moves to a new page. 
	// pageload is called at once. 
	$.historyLoad(hash);
	return false;
}


// ////////////
// This function trys to preload the bg image for the sub pages to get a nicer transition


$(window).bind('load', function() {
	
	var preload = new Array('/_img/backgrounds/aqualung.jpg','/_img/backgrounds/aquasphere.jpg','/_img/backgrounds/deepsee.jpg','/_img/backgrounds/princetontec.jpg','/_img/backgrounds/pristine.jpg','/_img/backgrounds/sequest.jpg','/_img/backgrounds/stohlquist.jpg','/_img/backgrounds/usdiver.jpg');

	jQuery.each(preload,function() {
		
		var img = new Image();
		img.src = this;
	});       
});	



// PNG FIX FOR LOADED CONTENT




// Loops through all img tags
function fixPngs(){
	
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

	if(IE6 == false) { return; }
	
   for (i = 0; i < document.images.length; i++){
      var s = document.images[i].src;
      if (s.indexOf('.png') > 0)  // Checks for the .png extension
         fixPng(s, document.images[i]);
   }
}

// u = url of the image
// o = image object
function fixPng(u, o){
   o.src = '/dev/_img/x.gif';  // Need to give it an image so we don't get the red x
   o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='crop')";
}




