$(document).ready(function() {

/*
	var myTimer				= 1000;
	var timerInc				= 100;
	$('.item-container').hide();
	$('.item-container').each(function(index) {
    		$(this).delay(myTimer).fadeIn();
		myTimer += timerInc;
	});
*/

$(".largeFacebook a span").fadeOut();
$(".largeTwitter a span").fadeOut();

$(".largeFacebook a").hover(function() {  $(this).find("span").fadeIn(); } , function() {  $(this).find("span").fadeOut(); })
$(".largeTwitter a").hover(function() { $(this).find("span").fadeIn(); } , function() { $(this).find("span").fadeOut(); })


function validateEmail(email) 
{ 
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
 return email.match(re) 
}


$("#contactForm").submit(function() { 
	
		errorCount = 0;		

		$("#about").find("input").each(function() { 
			if($(this).val() == "" && $(this).hasClass("required")) {
				$(this).addClass("error");
				
				errorCount++;
			} else {
				$(this).removeClass("error");
			}
		});

		if($("#message").val() != "") { 
				$("#message").removeClass("error");
		} else {

				$("#message").addClass("error");
				errorCount++;
		} 

		
			if(!validateEmail($("input.email").val())) {
				errorCount++;
				$("input.email").addClass("error");
			} else {
				$("input.email").removeClass("error");
			}

	

		if(errorCount == 0) {

  var dataString = 'name='+ $("input.name").val() + '&email=' + $("input.email").val() + '&link=' + $("input.link").val() + '&message=' + $(".message").val();
  //alert (dataString);return false;
  $.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    success: function() {
     console.log("BOOM");
    }
  });

  



			$(".contactFormWrapper").animate({ "margin-top": "-50px" } , function() { $(this).animate({ "margin-top": "400px" }); $(".largeSocialShare").animate({ "margin-top": "0px" })  }) ;
		} else {
			//----
		} 
		

		//cancel the submit button default behaviours
		return false;

})




	$('#return a').click(function(){
		$('html, body').stop().animate({scrollTop:0}, 600);
		$("h1").removeClass("green");
		$("body").removeClass("green");
		$("#nav li").removeClass("active");
		$("#nav li.port").addClass("active");

		// Track Event
		_gaq.push(['_trackEvent', 'Navigation', 'Clicked', 'Return To Top']);

		return false;
	});
 
	$('li.about a').click(function(){

		$('html, body').stop().animate({scrollTop: $("#about").position().top }, 600);

		// Track Event
		_gaq.push(['_trackEvent', 'Navigation', 'Clicked', 'About']);
		

		return false;
	});
 
	$('li.port a').click(function(){
		//$("h1").removeClass("green");
		//$("body").removeClass("green");
		$('html, body').stop().animate({scrollTop: 0 }, 600);

		// Track Event
		_gaq.push(['_trackEvent', 'Navigation', 'Clicked', 'Portfolio']);

		return false;
	});
 
	$("#nav li a").click(function() {
		
			$("#nav li").removeClass("active");
			$(this).parent().addClass("active");
		
		
		return false;
 
	})
 
	 
	$(".item-pagination li").click(function() {
 
		if($(this).parent().parent().hasClass("locked")) {
 
		} else {
 
		whichEntry = $(this).parent().parent();
		whichImage = $(this).index();	
 
		// Find Current Active Image Before Transition
		currentSlideIndex = whichEntry.find("li.active").index() + 1;	
		
		// Go To Next Image
		if($(this).hasClass("next")) {
 
			startPosition = "408px";
			endPosition = "-408px";

			projectName = whichEntry.find("h1").text();

			// Track Event
			_gaq.push(['_trackEvent', 'Navigation', 'Next', projectName ]);
		
 
			if($(this).parent().find("li.active").index() == ($(this).parent().children().length - 3)) {
				
 
				nextSlideIndex = 1;
 
				whichEntry.addClass("locked");
 
				whichEntry.find(".item-viewport li:nth-child("+nextSlideIndex+")").css({ "top": startPosition }).animate({ "top": "4px" } , 300, function() { whichEntry.removeClass("locked") });
				whichEntry.find(".item-viewport li:nth-child("+currentSlideIndex+")").animate({ "top": endPosition } , 300);
 
			} else {
				
				nextSlideIndex = whichEntry.find("li.active").index() + 2;
 
		whichEntry.addClass("locked");
 
		whichEntry.find(".item-viewport li:nth-child("+nextSlideIndex+")").css({ "top": startPosition }).animate({ "top": "4px" } , 300, function() { whichEntry.removeClass("locked") });
				whichEntry.find(".item-viewport li:nth-child("+currentSlideIndex+")").animate({ "top": endPosition } , 300);
 
			}
				whichEntry.find("li").removeClass("active");
				whichEntry.find("li:nth-child("+nextSlideIndex+")").addClass("active");
 
		}
 
		
		// Go To Previous Image
		if($(this).hasClass("previous")) {
 
			projectName = whichEntry.find("h1").text();

			// Track Event
			_gaq.push(['_trackEvent', 'Navigation', 'Previous', projectName ]);
 
			if(whichEntry.find("li.active").index() == 0) {
				nextSlideIndex = $(this).parent().children().length - 2;
				
			} else {
				nextSlideIndex = whichEntry.find("li.active").index() ;
				
			}
				startPosition = "-408px";
				endPosition = "408px";
 
 
 
				whichEntry.find("li").removeClass("active");
				whichEntry.find("li:nth-child("+nextSlideIndex+")").addClass("active");
				
		whichEntry.addClass("locked");
 
		whichEntry.find(".item-viewport li:nth-child("+nextSlideIndex+")").css({ "top": startPosition }).animate({ "top": "4px" } , 300, function() { whichEntry.removeClass("locked") });
				whichEntry.find(".item-viewport li:nth-child("+currentSlideIndex+")").animate({ "top": endPosition } , 300);
 
		}
 
 
 
 
		if($(this).hasClass("next") || $(this).hasClass("previous")) {
			
 
 
		} else {
 

			

			if(!$(this).hasClass("active")) {


			/*---- Tracking to go here ----*/

			projectName = whichEntry.find("h1").text();

			// Track Event
			_gaq.push(['_trackEvent', 'Navigation', 'Tab-' + $(this).text() , projectName ]);
			
 
			// Find Current Active Image Before Transition
			currentSlideIndex = whichEntry.find("li.active").index() + 1;			
			nextSlideIndex = $(this).index() + 1;
 
			// Make Start Point Decision
			if(currentSlideIndex > nextSlideIndex) {
			
 
				// Scroll Up
				startPosition = "-408px";
				endPosition = "408px";

 
			} else {

				// Scroll Down
				startPosition = "408px";
				endPosition = "-408px";
 

 
			}
			
 
	 
 
			whichEntry.find("li").removeClass("active");
			$(this).addClass("active");	
			
			whichEntry.addClass("locked");
 
			whichEntry.find(".item-viewport li:nth-child("+nextSlideIndex+")").css({ "top": startPosition }).animate({ "top": "4px" } , 300, function() { whichEntry.removeClass("locked") });
 
			whichEntry.find(".item-viewport li:nth-child("+currentSlideIndex+")").animate({ "top": endPosition } , 300);
			
			}
 
		}
		
 
		}
 
		return false;
		
	});
 
 
$(window).scroll(function () { 
 
	if($(document).scrollTop() > 65) {
		 if($.browser.msie) {
			$("#shadow").show();
		} else {
			$("#shadow").fadeIn();
		} 
	} else {
		 if($.browser.msie) {
			$("#shadow").hide();
		} else {
			$("#shadow").fadeOut();
		} 
	}
 
    
	var docViewTop = $(window).scrollTop();
    	var docViewBottom = docViewTop + $(window).height();
 
    	var elemTop = $("#about").offset().top;
    	var elemBottom = elemTop + $("#about").height();
 
 
	if((elemBottom >= docViewTop) && (elemTop <= docViewBottom)) {
		$("body").addClass("green");
		$("#nav li").removeClass("active");
		$("#nav li.about").addClass("active");
		//hash = "!/about";
		//document.location.hash = hash;

	} else {
		$("body").removeClass("green");
		$("#nav li").removeClass("active");
		$("#nav li.port").addClass("active");

		//hash = "!/portfolio";
		//document.location.hash = hash;

	}
 
 
});
 

$('.hover').hover(function(){
		$(this).addClass('flip');
	},function(){
		$(this).removeClass('flip');
	});
 
})


$(window).bind("load",function(){
    
    // Load in Flickr
	$("#loadFlickr").css("display","none");
    $("#loadFlickr").load("flickr.php", function(){
        $(this).css("display","block");
    });
    
});

