// JavaScript Document
    $(document).ready(function() {
		
		// stops the page being white when back button is pressed
		$(window).bind("unload", function() {
		});
		
		// add a solid div that covers the page, it has a zindex so items are below or above it during fades, css file has more properties.
		var fader = $('<div>',{
			id	: 'fader',
			css:{
				height : $(document).height()
			}
		});
		// add div to the page only if javascript is enabled
		$('body').append(fader);

		
   
		// any link that isn't a hash will cause page to fade out
		$("a").not("[href*='#']").not("[href*='mailto:']").not("#intro-grid a").click(function(event){
			event.preventDefault();
			linkLocation = this.href;
			fadePageOut();
		});
		
		// The protfolio grid on the front page
		$("#intro-grid a").click(function(event){
	 		event.preventDefault();
        	linkLocation = this.href;
	 		$(this).addClass('current');
			//$(this).addClass('current');
	 		var thumb = this;
			$.scrollTo( 0, 500);
	 		$("#fader").css("display", "none");
     		$("#fader").fadeIn(500,function(event){
	 			$("img", thumb).fadeOut(500, redirectPage);
	 		});

	 	});



		function fadeInNextLI() {
  			v.eq(cur++).css('visibility','visible').hide().fadeIn();
			 if(cur != v.length) setTimeout(fadeInNextLI, 50);
		}
		// if the protfolio grid exists do something
		if ($("#homepage #intro-grid").length > 0){
			// Randomly fade in Protfolio grid on front page
			var v = $("#intro-grid > li").css('visibility', 'hidden'), cur = 0;
			//$("#intro-grid li").css('visibility', 'visible');
			$('#intro-grid a').preload({
				threshold:25,
				onFinish: function(){
					for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
					fadeInNextLI();
				}
			});
		}
		if ($("#portfolio #intro-grid").length > 0){
			//$("#intro-grid").css('visibility', 'visible');
		}
		if ($("#intro-grid").length > 0){
			$('#intro-grid li a').hover(function() { 
				$('img', this).animate({"opacity": 0.5}); 
			},function() { 
				$('img', this).animate({"opacity": 1}); 
			});
		}
		function redirectPage() {
			// forwards the page to the link
			window.location = linkLocation;
		}
		function fadePageIn(){
			// fades page from 100 to 0
			$("#fader").css("display", "block");
			$("#fader").fadeOut(750);
		}
		function fadePageOut(){
			// fades page from 0 to 100 then calls redirect function
			// if it's not the front page, fade out the title
			if ($("#intro-grid").length == 0){
				$("#sidebar h1.title").addClass('fade');
			}
			$.scrollTo( 0, 500);
			$("#fader").css("display", "none");
			$("#fader").fadeIn(500, redirectPage);
		}
		
		// fade in first slide
		$('#gallery').cycle({fx:'fade',speedIn: 500, speedOut: 500,sync: 0,timeout: 0, next: '#gallery', pager:'#gallery-nav'});
		$('#gallery img:first').css({'display' : 'none' })
		$('#gallery img:first').preload({
			onComplete: function(){
		    	$('#gallery img:first').fadeIn(500);
			}
			
		});
		
		//
		
		//
		//$("#description").addClass('visible');
		//$("#content").addClass('visible');
		$("#description").css('visibility','visible');
		$("#content").css('visibility', 'visible');
	
		// fade in the page on load...
		fadePageIn();
		
		//var leftInit = $("#sidebar").offset().left;

		/*
		$(window).scroll(function(event) {
			var x = $(this).scrollLeft();
			//alert(x);
		  //var xx = $(this).scrollLeft();
		
		
		
			$("#sidebar").css("left", 0-x);
		    //$("#text").text('hi' + x );
		});*/
	
	// "tabs" on the principal/company pages
	$(".profile-tab").hide(); //Hide all content
	$(".profile-tab:first").show();
	$(".profile-tab:first").addClass("active");
	//var tabContainers = $('#profile #content > div');
    
    $("#nav-sec a").click(function () {
        //tabContainers.fadeOut().filter(this.hash).fadeIn();
		//var activeTab = $(this.hash);
		var activeTab = $(this).attr("href");
		//tabContainers.fadeIn().filter(this.hash);
        $("#content .active").fadeOut(500, function() {
			$("#content .active").removeClass("active");
			$(activeTab).addClass("active");
			$(activeTab).fadeIn(500);    		
			//$(this.hash).fadeIn();
		});
  		//}).filter(this.hash);
	//$(activeTab).fadeIn(); //Fade in the active ID content
		
        //$('#nav-sec a').removeClass('selected');
        //$(this).addClass('selected');
        
        return false;
    });

		// the profile/principal fades
		//$("#nav-sec a").click(function(event){
	 //		$("#nav-sec a").addClass('current');
	 //	});
    });
