$(document).ready(function(){
	$('body').pngFix();	
	$("#mnav ul li#"+section+" a").addClass("active");//Highlight the users current location
	$('#portfolioWrapper #items div').hide();
	//$('#portfolioWrapper #items div.2009').fadeIn();
	
	//ABOUT
	$('#aboutNav ul li:first-child').addClass('selected');	
	$('#aboutWrapper #items div#workdiv').fadeIn('normal');
	var aboutId = "workdiv";
	//About NAV
	$('#aboutNav ul li').click(function(evt){
		$(this).siblings("li").removeClass("selected");			
		$('#aboutWrapper #items div#'+aboutId).hide();
		aboutId = $(this).attr('id')+'div';				
		$('#aboutWrapper #items div#'+aboutId).fadeIn('normal');
		$(this).addClass("selected"); 
		return false;
	});
	
	$('.article:nth-child(2)').addClass('selected');	
	//$('#portfolioWrapper #items div:first-child ').addClass('selected');
	 
	
	//PORTFOLIO NAV
	var ajax_load = "<img src='/images/ajax-loader.gif' alt='Loading Content...' />";
	$('#portfolioNav ul li').click(function(evt){
		$(this).siblings("li").removeClass("selected");
		$(this).siblings("li").css('opacity', 0.5);
		$(this).css('opacity', 1);	
		var myId = $(this).attr('id');			
		if(myId != 'all'){	
			$('#portfolioWrapper #items div').hide();
			$('#portfolioWrapper #items div.'+myId).fadeIn('normal');
		}
		else{
			
			$('#portfolioWrapper #items div').fadeIn('normal');
		}
		$(this).addClass("selected"); 
		return false;
	});
	
	//Rollovers for Years	
	$('#portfolioNav ul li').each(function () {	
		var $span = $(this).css('opacity', 0.8);																																														
		$(this).hover(function () {		
			// on hover
			if(!$(this).hasClass("selected")){		
				$span.stop().animate({opacity: 1}, 250);
			}
		}, function () {
			// off hover
			if(!$(this).hasClass("selected")){		
				$span.stop().animate({opacity: 0.8}, 250);
			}			
		});
	});
	
	
	//Selected Portfolio Item	
	$('#portfolioWrapper #items div').click(function(evt){															 
		$.scrollTo('#header', 900, { easing:'elasout' });
		 var page = $(this).find('a').attr('rel');
		 var stripChars = page.length;
		 window.location.hash = '/'+page;		 
		$(this).siblings("div").removeClass("selected");
		 $(this).addClass("selected");
		 $("#showcase").css('background', 'none');
		$("#portfolioHolder").animate({opacity: 0.1}, 250,function() {
			$("#portfolioHolder").html(ajax_load).load('/'+page+".html", function(){
				$("#slider").easySlider({
					numeric: true
				});
			});
		});				
	});
	
	
	
	// Ajax Sucess 
	$("#portfolioHolder").ajaxSuccess(function(event,request,settings){											   
		isError = false;
		$(this).animate({opacity: 1}, 450);  		
	});
	
	// Error Handling
	var isError;//Variable to hold if there was an error.
	$("#portfolioHolder").ajaxError(function(event, request, settings){	
		//alert("error");
		$('#portfolioWrapper #items div').each(function(){
			$(this).removeClass("selected");
		});
		isError = true;		
		$('#portfolioWrapper #items div').fadeIn('normal');//fade in current year					
		$('#portfolioNav ul li#all').css('opacity', 1);
		$('#portfolioNav ul li#all').addClass('selected');//Set snav select
		$(this).html(ajax_load).load('/portfolio/error.html');
	 });
	
	
	//PARSE URL ON RELOAD
	function checkAndParseURL() {
		var title = document.title;
   		var url = window.location.hash.slice(2);		
		if(url == ''){
			//url = $('#portfolioWrapper #items div:first').find('a').attr('rel');
			var showYear = $('#portfolioWrapper #items div:first').attr('class');
			$('#portfolioWrapper #items div').each(function(){
					$('#portfolioWrapper #items div.'+showYear).fadeIn('normal');//fade in current year					
					$('#portfolioNav ul li#'+showYear).css('opacity', 1);
					$('#portfolioNav ul li#'+showYear).addClass('selected');//Set snav select
			});
		}
		else{
			$("#showcase").css('background', 'none');
			$("#portfolioHolder").html(ajax_load).load('/'+url+".html", function(){
				$("#slider").easySlider({
					numeric: true
				});
			});			
			$('#portfolioWrapper #items div').each(function(){
				var theLink = $(this).find('a').attr('rel');	
				if(theLink == url){
					var year = $(this).attr('class');//Select curent year
					$('#portfolioNav ul li#'+year).addClass('selected');//Set snav select
					$('#portfolioWrapper #items div.'+year).fadeIn('normal');//fade in current year
						$(this).addClass("selected");					
						$('#portfolioNav ul li#'+year).css('opacity', 1);
				}
			});
		}
	}	
	checkAndParseURL();	
	
	
	
  
	
	
	
	
	//FILTER PORTFOLIO
	var portfolioType = 'none';
	$('.filter ul li').click(function(evt){	
		$(this).removeClass("selected");
		$(this).siblings("li").removeClass("selected");
		var newType=$(this).attr('class');
		if(portfolioType == newType){
			portfolioType='none';
			$('#portfolioWrapper #items div').each(function(){
				$(this).animate({opacity: 1}, 250);
			});	
		}
		else{
		 portfolioType = $(this).attr('class');
		 $(this).addClass('selected');
			 $('#portfolioWrapper #items div').each(function(){
				var thisType = $(this).find('a').attr('class');
				if(thisType == portfolioType){
					$(this).animate({opacity: 1}, 250);
				}
				else{
					$(this).animate({opacity: 0.5}, 250);				
				}
			});	
		}
			 			
	});
	
	
	
	// ABOUT WORK HISTORY
	$('ul#timelineNav li:first-child').addClass("selected");
	$('#history .entry:last-child').css("background","none");

	$('ul#timelineNav li').click(function(evt){	
		$(this).addClass("selected").siblings('li').removeClass("selected");
		var theYear = $(this).attr('title');
		var position = $("#history .entry"+theYear+"").position();
		//alert(position.left);		
		$('#history').animate({'left': -position.left},{ queue:false, duration:500, easing: 'elasout'});			 			
	});
	

	
	
	/*TWITTER*/
	$("#twitter").tweet({
	  count: 3,
	  username: "KaptonKaos",
	  loading_text: "loading list..."
	});
	
	$('.intro').css('opacity','0');
	$('.intro').animate({opacity: 1}, 450);
	
	$('#launch').fadeTo(250, 0.50);
	/*FOOTER*/
	
	//Rollovers for Social	
	$('ul#social li').each(function () {	
		var $span = $(this).css('opacity', 0.6);																																														
		$(this).hover(function () {	
			$span.stop().animate({opacity: 1}, 100);
		}, function () {		
			$span.stop().animate({opacity: 0.6}, 100);				
		});
	});
	
	$('ul#socialFollow li a').each(function () {	
		var $span = $(this).css('opacity', 0.6);																																														
		$(this).hover(function () {	
			$span.stop().animate({opacity: 1}, 100);
		}, function () {		
			$span.stop().animate({opacity: 0.6}, 100);				
		});
	});
	
	
	$('#history a').each(function () {	
		var $span = $(this).css('opacity', 0.6);																																														
		$(this).hover(function () {	
			$span.stop().animate({opacity: 1}, 100);
		}, function () {		
			$span.stop().animate({opacity: 0.6}, 100);				
		});
	});
	
	
	$('#madeMac img').fadeTo(250, 0.50);
	$('#madeMac img').mouseover(function() {
  		$(this).stop(true, true).fadeTo(250, 1);
	});
	$('#madeMac img').mouseout(function() {
  		$(this).stop(true, true).fadeTo(250, 0.50);
	});
	$('p.learnMore').fadeTo(250, 0.50);
	$('p.learnMore').mouseover(function() {
  		$(this).stop(true, true).fadeTo(250, 1);
	});
	$('p.learnMore').mouseout(function() {
  		$(this).stop(true, true).fadeTo(250, 0.50);
	});

	
	$('a.scroll').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 700, { easing:'elasout' });
		return false;
	});
	
	
	//borrowed from jQuery easing plugin
	//http://gsgd.co.uk/sandbox/jquery.easing.php
	$.easing.elasout = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	};
	
	//this one is important, many browsers don't reset scroll on refreshes
	var scrollTime = 800;
	$('div.pane').scrollTo( 0 );//reset all scrollable panes to (0,0)
	$.scrollTo( 0 );//reset the screen to (0,0)
		
});