$(function() {
	function Retrieve_Background() {
		$.ajax({
		  type: "POST",
		  url: "/",
		  dataType: 'json',
		  data: "focus=Retrieve_Background",
		  success: function(j) {
			  $.backstretch("/images/bg/" + j.file);
		  }
	  });
	}
	function Retrieve_Content(id) {
		if($('#content').css('display') == 'none') {
			$('#content').fadeIn('fast');
		}
		$.ajax({
		  type: "POST",
		  url: "/",
		  dataType: 'json',
		  data: "focus=Retrieve_Content&id=" + id,
		  success: function(j) {
			 $('#db').html(j.c1);
			 $('form.validate').validationEngine();
		  }
	  });
	}
	function Retrieve_Position(id) {
		if($('#content').css('display') == 'none') {
			$('#content').fadeIn('fast');
		}
		$.ajax({
		  type: "POST",
		  url: "/",
		  dataType: 'json',
		  data: "focus=Retrieve_Position&id=" + id,
		  success: function(j) {
			 $('#db').html(j.c1);
			 $('form.validate').validationEngine();
		  }
	  });
	}
	function Current_Hash() {
		if(window.location.hash.substring(1) !== '') {
			return window.location.hash.substring(1);
		}
	}
	var hash = Current_Hash();
	// Pull initial content
	if(hash == null) {
		if(window.location.pathname.indexOf('compass') !== -1) {
			// Portal
			//Retrieve_Content(3);
		}
		else {
			// Homepage
			Retrieve_Content(1);
		}
	}
	else if(hash == 'contact_success') {
		Retrieve_Content(20);
	}
	else if(hash == 'login') {
		Retrieve_Content(21);
	}
	
	Retrieve_Background();
	$('#nav ul li').click(function() {
		Retrieve_Content(this.id);
	});
	
	$('ul#positions li').live('click', function() {
		Retrieve_Position(this.id);
	});
	
	$('button.close').click(function() {
		$(this).parent('div').fadeOut('fast');
	});
	
	$('#logo span').click(function() { Retrieve_Content(1) });
	
	$('#nav ul li a.slide').click(function() {
		$('#nav ul ul').css('display', 'none');
		$(this).siblings('ul').slideDown('fast');
	});
	
	//Modal - Compass
    $('a[name=modal]').click(function(e) {
        e.preventDefault();
        var id = $(this).attr('href');
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     	$('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect     
        $('#mask').fadeIn(100);    
        $('#mask').fadeTo("slow",0.8);  
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
        $(id).fadeIn(100); 
     
    });
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });
	
	$('form.validate').validationEngine();
	
	
});
