$(document).ready(function(){	
	
	//$('body').css('opacity','0');
	
	//$('body').animate({opacity: 1}, { queue:false, duration:1000});
	
	$("#mnav ul li#"+section+" a").addClass("active");//Highlight the users current location
	
	//$("img").lazyload({effect : "fadeIn"});
	
	//Set up INIT variables
	$('#portfolioWrapper #items-2011 div').hide();		
	var ajax_load = "<img src='images/ajax-loader.gif' alt='Loading Content...' />";
	$("#showcase").css({opacity: 0});	
	
	 
	
	//***********************************************
	//PORTFOLIO 
	
	//YEAR FILTERING
	$('#portfolioIntro ul li').click(function(evt){
		$(this).siblings("li").removeClass("selected");	
		var myId = $(this).attr('id');	
		if(myId != 'all'){	
			$('#portfolioWrapper #items-2011 div').hide();
			$('#portfolioWrapper #items-2011 div.'+myId).fadeIn('slow');
		}
		else{			
			$('#portfolioWrapper #items-2011 div').fadeIn('slow');
		}
		
		//$.scrollTo('#folio', 600, { easing:'easeInOutQuart' });	
		$(this).addClass("selected"); 
		return false;
	});
		
	//Test for iPad
	var ua = navigator.userAgent,
    event = (ua.match(/iPad/i)) ? "touchstart" : "click";
	$('#portfolioWrapper #items-2011 div').bind(event, function(e) {});
	//Test for iPhone
	var uaIphone = navigator.userAgent,
    event = (uaIphone.match(/iPhone/i)) ? "touchstart" : "click";
	$('#portfolioWrapper #items-2011 div').bind(event, function(e) {});
	
		
	//Page Title
	var pagetitle = "Darren Cooney - Web Design & Development";		
		
	//PORTFOLIO SELECT
	$('#portfolioWrapper #items-2011 div').click(function(){															  
		//alert("clicked");		
		var $portItem = $(this);																									  
		$('#container').animate({height: 500+'px'}, { queue:false, duration:800, easing: 'easeInOutQuart'});			
		var page = $portItem.find('a').attr('rel');
		window.location.hash = '/'+page;		 
		$portItem.siblings("div").removeClass("selected");
		$portItem.addClass("selected");	
		$.scrollTo('#header', 600, { easing:'easeInOutQuart' });	
		
		document.title = $portItem.find('h3 a').html() + " | " +pagetitle; 
		
		$("#showcase").animate({opacity: 0}, 800, function (){															
			$("#showcase").css('background', 'none');
				$("#portfolioHolder").animate({opacity: 0}, 100,function() {
				$("#portfolioHolder").html(ajax_load).load(page+".html", function(){				
					$("#slider").fade({pause: 5000,	speed:800, loop:true});	
				});
			});
		});					
	});	
		
	
	//PARSE URL ON RELOAD
	function checkAndParseURL() {
		var title = document.title;
   		var url = window.location.hash.slice(2);		
		if(url == ''){
			//INIT CONTAINER LOAD
			$('#container').animate({height: 260+'px'},{duration:800, easing: 'easeInOutQuart', complete:fadeInShowcase});
			var showYear = 'all';			
			$('#portfolioWrapper #items-2011 div').each(function(){				
				$(this).fadeIn('normal');//fade in current year			
				$('#portfolioIntro ul li#'+showYear).addClass('selected');//Set snav select
			});
		}
		
		else{
			$('#container').animate({height: 500+'px'},{duration:800, easing: 'easeInOutQuart', complete:fadeInShowcase});				
			$("#showcase").css('background', 'none');
			$("#portfolioHolder").html(ajax_load).load(url+".html", function(){				
					$("#slider").fade({pause: 5000,	speed:800, loop:true});	
			});
			$('#portfolioWrapper #items-2011 div').each(function(){
				var theLink = $(this).find('a').attr('rel');	
				if(theLink == url){
					var year = $(this).attr('class');//Select curent year					
					document.title = $(this).find('h3 a').html() + " | " +pagetitle; 
					$('#portfolioIntro ul li#'+year).addClass('selected');//Set snav select
					$('#portfolioWrapper #items-2011 div.'+year).fadeIn('normal');//fade in current year
					$('#portfolioWrapper #items-2011 div.'+year+' img').show();//fade in current year
					$(this).addClass("selected");					
				}
			});
		}
	}	
	checkAndParseURL();
	
	function fadeInShowcase(){		
		$("#showcase").animate({opacity: 1}, 800);
	}	
	
	//AJAX FUNCTIONS	
	//Start
	$("#portfolioHolder").ajaxStart(function(){
	    //$.scrollTo('#header', 700, { easing:'easeInOutQuart' });
	});
	
	// Success
	$("#portfolioHolder").ajaxSuccess(function(){											   
		isError = false;
		//alert("complete");
		//alert("done");
	});
	
	//When Ajax is done start teh slideshow
	$("#portfolioHolder").ajaxStop(function(){			
		$("#showcase").animate({opacity: 1}, 800);
		$(this).animate({opacity: 1}, 800);
	});
	
	// Error
	var isError;//Variable to hold if there was an error.
	$("#portfolioHolder").ajaxError(function(){	
		//alert("An error occurred!");
		$('#container').animate({height: 480+'px'},{duration:800, easing: 'easeInOutQuart'});
		$('#portfolioWrapper #items-2011 div').removeClass("selected");
		$('#portfolioIntro ul li').removeClass('selected');//Set snav select
		
		isError = true;		
		$('#portfolioWrapper #items-2011 div').fadeIn('normal');//fade in current year					
		$('#portfolioIntro ul li#all').css('opacity', 1);
		$('#portfolioIntro ul li#all').addClass('selected');//Set snav select
		$(this).html(ajax_load).load('portfolio/error.html');
	 });
	
	
  	
	//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-2011 div').each(function(){
				$(this).animate({opacity: 1}, 250);
			});	
		}
		else{
		 portfolioType = $(this).attr('class');
		 
		 $(this).addClass('selected');
			 $('#portfolioWrapper #items-2011 div').each(function(){
				//if the portfolio item has a class of the portfolio filter type												  
				if($(this).find('a').hasClass(portfolioType)){
					$(this).animate({opacity: 1}, 250);
				}
				else{
					$(this).animate({opacity: 0.2}, 250);				
				}
			});	
		}
			 			
	});
	
	
	//***********************************************
	// ABOUT
	
	//RECENT WORK SELECT
	$('#recent-work #items-2011 div').click(function(){															  
		//alert("clicked");		
		var $portItem = $(this);																						
		var page = $portItem.find('a').attr('rel');
		window.location = '/#/'+page;					
	});
	
	//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: 'easeInOutQuart'});			 			
	});
	

	
	//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');	
	
	
	
	//***********************************************
	/*TWITTER*/
	
	$("#twitter").tweet({
	  count: 3,
	  username: "KaptonKaos",
	  loading_text: "loading list...",
	  outro_text: "&raquo; <a href='http://twitter.com/KaptonKaos' class='learnmore'>Follow Me on Twitter</a>"
	});
	
	
	
	//***********************************************
	/*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 .related ul li').each(function () {	
		var $span = $(this).css('opacity', 0.8);																																														
		$(this).hover(function () {	
			$span.stop().animate({opacity: 1}, 100);
		}, function () {		
			$span.stop().animate({opacity: 0.7}, 100);				
		});
	});
	
	
	//***********************************************
	
	$('a.scroll').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 700, { easing:'easeInOutQuart' });
		return false;
	});	
		
	//this one is important, many browsers don't reset scroll on refreshes
	var scrollTime = 800;
	$.scrollTo( 0 );//reset the screen to (0,0)
		
});
