var ie = ! $.support.opacity;

function reset_project_navigation() {
    // get the number of projects for the selected platform
    var project_count = $("#projects ul.selected li.project").length;
    // if there is only 1, hide the project navigation
    if (project_count == 1) {
        var $nav = $("#project_previous:visible, #project_next:visible, #project_paging:visible");
        if(ie){
            $nav.hide();
        } else {
            $nav.fadeOut(300);
        }
        
    } else {
        // show the hidden project navaigation
        var $nav = $("#project_previous:hidden, #project_next:hidden, #project_paging:hidden");
        if(ie){
            $nav.show();
        } else {
            $nav.fadeIn(300);
        }
        // hide all the dots
        $("#project_paging li").hide().removeClass('selected');
        // show only the dots needed
        $("#project_paging li:lt(" + project_count + ")").show().removeClass('selected');
        
        // get the currently displayed project index and select its corresponding dot
	    var index = $("#projects ul.selected li.project.selected").prevAll().length;
	    $("#project_paging li:eq(" + index + ")").addClass("selected");
	    
	    // make sure the disabled classes on the projects are correctly reset
	    if (index == 0) {
	        $("#project_previous").addClass('disabled');
	        $("#project_next").removeClass('disabled');
	    } else if (index == (project_count - 1)) {
	        $("#project_previous").removeClass('disabled');
	        $("#project_next").addClass('disabled');
	    }
    }
}


$(function() {

    // start by reseting the project navigation, though this should already be the default in the HTML
    reset_project_navigation();
    
    // for each platform, set the appropiate left offset
    $("ul.platform").each(function(){
       $(this).children("li.project").each(function(index){
           var px = index * 60;
          $(this).css({left: px + "px"}); 
       });
    });
	
	// Smooth scroll on nav clicks
	$("li.work_link").click(function(){
		$.scrollTo( $("#portfolio"), 800);
	});
	
	$("li.jobs_link").click(function(){
		$.scrollTo( $("#contact"), 800);
	});
	
	$("li.about_link").click(function(){
		$.scrollTo( $("#about"), 800);
	});
	
	// Platform picker click handler
	$("#platform_picker li").click(function(event) {
	    // prevent the click from occurring
	    event.preventDefault();
	    
	    // cache this
	    var $this = $(this);	    
	    
	    // if they clicked on a platform not already selected...
	    if(! $this.hasClass("selected")) {
	        
	        // clear currently selected
	        $("#platform_picker li.selected").removeClass('selected');

	        // select the platform clicked
	        $this.addClass('selected');
	        
	        // get the target and select it
	        var target = $this.find("a").attr("href");
	        
	        //move current indicator and projects          
            if (target == "#platform_ios") {
                $("#platform_current").stop().animate({left: "803px"}, 300);
                if (ie) {
                    $("ul.platform.selected").hide().removeClass("selected");
                    $("#projects").css({top: "49px"});
                    $(target).show().addClass("selected");
                } else {
                    $("ul.platform.selected").stop().animate({opacity: 0}, 300).removeClass("selected");
                    $("#projects").stop().animate({top: "49px"}, 300);
                    $(target).stop().animate({opacity: 1}, 300).addClass("selected");
                }                
            } else if (target == "#platform_android") {
                $("#platform_current").stop().animate({left: "872px"}, 300);
                if (ie) {
                    $("ul.platform.selected").hide().removeClass("selected");
                    $("#projects").css({top: "-11px"});
                    $(target).show().addClass("selected");
                } else {
                    $("ul.platform.selected").stop().animate({opacity: 0}, 300).removeClass("selected");
                    $("#projects").stop().animate({top: "-11px"}, 300);
                    $(target).stop().animate({opacity: 1}, 300).addClass("selected");
                }
            } else if (target == "#platform_web") {
                $("#platform_current").stop().animate({left: "954px"}, 300);
                if (ie) {
                    $("ul.platform.selected").hide().removeClass("selected");
                    $("#projects").css({top: "-71px"});
                    $(target).show().addClass("selected");
                } else {
                    $("ul.platform.selected").stop().animate({opacity: 0}, 300).removeClass("selected");
                    $("#projects").stop().animate({top: "-71px"}, 300);
                    $(target).stop().animate({opacity: 1}, 300).addClass("selected");
                }
            }			
	        
	        // reset the project navigation for the currently displayed platform
	        reset_project_navigation();
	    }
	});
	
	// previous navigation click handler
	$("#project_previous").click(function(event){
	    // prevent the click from occurring
	    event.preventDefault();
	    
	    // get the currently display project
	    var $current = $("#projects ul.selected li.project.selected");
        
        // if it is not the first one
	    if ($current.prev().length > 0) {
	        // hide and show the next one
	        var $ul = $("ul.platform.selected");	        
	        var left = parseInt($ul.css("left")) + 60;	        
	        if (ie){
	            $current.hide().removeClass("selected").prev().show().addClass("selected");
    	        $ul.css({left: left + "px"});   
	        } else {
	            $current.animate({opacity: 0}, 300).removeClass("selected").prev().animate({opacity: 1}, 300).addClass("selected");
    	        $ul.animate({left: left + "px"}, 300);
	        }
	        
	        // decrement the dots
	        $("#project_paging li.selected").removeClass("selected").prev().addClass("selected");
	        
	        // remove disabled from the previous arrow if it has it
	        var $next_arrow = $("#project_next");
	        if($next_arrow.hasClass('disabled')) {
	            $next_arrow.removeClass('disabled');
	        }
	        
	        // check to see if disabled the next arrow
	        $current = $("#projects ul.selected li.project.selected");
	        if ($current.prev().length == 0) {
	            $("#project_previous").addClass('disabled');
	        }
	    }
	});
	
	// next navigation click handler
	$("#project_next").click(function(event){
	    // prevent the click from occurring
	    event.preventDefault();
	    
	     // get the currently display project
	    var $current = $("#projects ul.selected li.project.selected");
        
        // if it is not the last one
	    if ($current.next().length > 0) {
	        
	        // hide and show the next one
	        var $ul = $("ul.platform.selected");	        
	        var left = parseInt($ul.css("left")) - 60;	        
	        if (ie) {
	            $current.hide().removeClass("selected").next().show().addClass("selected");
	            $ul.css({left: left + "px"});
	        } else {
	            $current.animate({opacity: 0}, 300).removeClass("selected").next().animate({opacity: 1}, 300).addClass("selected");
    	        $ul.animate({left: left + "px"}, 300); 
	        }
	        
	        // incremenet the dots
	        $("#project_paging li.selected").removeClass("selected").next().addClass("selected");
	        
	        // remove disabled from the previous arrow if it has it
	        var $prev_arrow = $("#project_previous");
	        if($prev_arrow.hasClass('disabled')) {
	            $prev_arrow.removeClass('disabled');
	        }
	        
	        // check to see if disabled the next arrow
	        $current = $("#projects ul.selected li.project.selected");
	        if ($current.next().length == 0) {
	            $("#project_next").addClass('disabled');
	        }
	    }
	    
	});
	
	// dot navigation click handler
	$("#project_paging li").click(function(event){
	    // prevent the click from occurring
	    event.preventDefault();
	    
	    // cache this
	    var $this = $(this);
	    
	    // if they click on the dot that is not already selected
	    if(! $this.hasClass("selected")) {	        
	        // get the index of the dot clicked
	        var index = $this.prevAll().length;
	        var current = $("#project_paging li.selected").prevAll().length;
	        var right = index > current;
	        
	        
	        if (ie) {
	            // hide the visible project
	            $("#projects ul.selected li.project.selected").hide().removeClass("selected");
	            // show the selected project
    	        $("#projects ul.selected li.project:eq(" + index + ")").show().addClass("selected");
    	        // move the viewport
    	        var left = index * -60;
    	        $("ul.platform.selected").css({left: left + "px"});
	        } else {
	            // hide the visible project
	            $("#projects ul.selected li.project.selected").animate({opacity: 0}, 300).removeClass("selected");
	            // show the selected project
    	        $("#projects ul.selected li.project:eq(" + index + ")").animate({opacity: 1}, 300).addClass("selected");
    	        // move the viewport
    	        var left = index * -60;
    	        $("ul.platform.selected").animate({left: left + "px"}, 300);
	        }
	        
	        // remove the selected class from the sibling dot and add it to the dot clicked
	        $this.siblings('li.selected').removeClass('selected');
	        $this.addClass('selected');
	        
	        // handle navigation arrows
	        if (index == 0) {
	            $("#project_previous").addClass('disabled');
	        } else {
	            $("#project_previous").removeClass('disabled');
	        }
	        
	        var project_count = $("#projects ul.selected li.project").length - 1;
	        if (index == project_count) {
	            $("#project_next").addClass('disabled');
	        } else {
	            $("#project_next").removeClass('disabled');
	        }
	    }
	});
	
	// hide all of the project image shots, except for the first one in each UL
	$(".project_shots ul li").hide();
	$(".project_shots ul").each(function(index) {
	    $(this).children().first().show();
	});
	
	// make sure each of the first thumbnails has the selected class by default
	$("ul.thumbs").each(function(index) {
	   $(this).children().first().addClass("selected");
	});
	
	// thumbnail click handler
	$(".thumbs li").click(function(event){
	    // prevent the click from occurring
	    event.preventDefault();
	    
	    // cache this
	    var $this = $(this);
	    
	    // if this thumbnail is not already selected
	    if(! $this.hasClass("selected")) {
	        // remove the class from the sibling thumbnail that is selected and add it to this one
	        $this.siblings("li.selected").removeClass("selected");
	        $this.addClass("selected");
	        
	        // find the target image to display
	        var target = $this.find("a").attr("href");
	        var $target = $(target);
	        
	        // fade out the previously selected image and fade in the select image
	        $target.siblings("li.selected").removeClass("selected").fadeOut(600);
	        $target.addClass("selected").fadeIn(600);
	    }
	});
});

function currentYear() {
 	var today = new Date();
	var year = today.getFullYear();
	return year;
 }

