$(document).ready(function() {

	/* [1] Hide the main wrapper */
	$('#wrapper').css({'visibility' : 'hidden'});

	/* [2] Remove all scrollbars and replace with jQuery scrollpanes */
	$('.scrollPane').jScrollPane({
		animateTo: true,
		animateInterval: 1
	});

	/* [3] Create the main sliding viewport */
	jQuery("div#slider1").codaSlider();

	/* [4] Setup behavior for the main navigation
	 * and the tiny footer links */
	setUpNavigationBehavior();

	/* [5] Setup NewsLetter Box behavior */
	 setUpNewsLetterBehavior();

	/* [6] Setup portfolio behavior */
	setUpPortfolioBehavior();

	/* [7] Dummy alter on RSS btn. Link it to something */
	$('#rss').click(function(){
		alert('Link this to your feed.')
	});

	/* [8] Setup the main slideshow */
	$('.removeOnLoad').remove();
	$("#slider").easySlider({
		auto: 			true,
		continuous:		true,
		numeric:		true,
		numericId:		'showNavigation'
	});

	/* [9] Setup the feature list for the services section */
	$.featureList(
		$("#tabs li a"),
		$("#output li"), {
			start_item : 0
		}
	);

	/* [10] Setup the lightbox behavior for the portfolio page */
	$("a[rel='folio']").colorbox({
		transition: 'elastic',
		speed: 400
	});

	/* [11] Setup the basic contact form behavior */
	setUpContactFormBehavior();

	/* [12] Setup navigation state according to startup location */
	setUpStartUpLocation();

	/* all processing done, time to revel everything */
	$('#wrapper').css({'visibility' : 'visible'});
	$('#sliderPics').css({'visibility' : 'visible'});
});
// end: onReady
//-----------------------------------------------------------------

function delNavActive() {
	$('#mainNavigation li a').each(function() {
		$(this).removeClass('navCurrent');
	});
}

function setUpNavigationBehavior() {
	//process the main navigation
	$('#mainNavigation li a').bind('click', function() {
		delNavActive();
		$(this).addClass('navCurrent');
		if(0 != $('#mainNavigation li a').index(this)) {
            slideUpShowNav();
        }
		else {
            slideDownShowNav();
        }
	});

	//process the footer links
	$('#footerLinks a.cross-link').bind('click', function() {
		delNavActive();
		var index = $('#footerLinks a.cross-link').index(this);
		$('#mainNavigation li a:nth(' + index + ')').addClass('navCurrent');
		if(index != 0) {
            slideUpShowNav();
		}
		else {
            slideDownShowNav();
		}
	});
}

function setUpNewsLetterBehavior()
{
	if($.browser.msie && parseInt($.browser.version) <= 6)
	{
		$('#newsletter').click(function(){
			$('#newsLetterSlider').animate({
				height: 770
			}, function(){
				$('#nlEmail').focus();
			});
		});
		$('.closeNLB').click(function(){
			$('#newsLetterSlider').animate({
				height: 0
			});
		});
	}
	else
	{
		$('#newsletter').click(function(){
			$('#newsLetterSlider').animate({
				height: 770
			}, function(){
				$('#newsLetterBox').fadeIn();
				$('#nlEmail').focus();
			});
		});

		$('.closeNLB').click(function(){
			$('#newsLetterBox').fadeOut(
				function() {
					$('#newsLetterSlider').animate({
						height: 0
					});
				}
			);
		});
	}
}


function focusYear(year) {

	var count = $('li.' + year).length;

	/*update the counter at the top right*/
	$('li#folioCount').fadeOut(
		function(){
			if (year == 'noFilter')
			{
				$('li#folioCount').html($('li.folioLi').length + ' project(s) found');
			}
			else
			{
				$('li#folioCount').html(count + ' project(s) found');
			}
			$('li#folioCount').fadeTo(200, 1);
		}
	);

	/*add more into this if you need more years*/
	var years = ['2010', '2009', '2008', '2007',
				 '2006', '2005', '2004', '2003',
				 '2002', '2001', '2000', '1999'];

	/* if the user selected 'show all', we show everything and scroll up */
	if(year == 'noFilter')
	{
		$('li.folioLi').fadeTo(200, 1, function(){
			$("#portFolioContainer")[0].scrollTo(0);
		});
		return;
	}

	/* if no items were found, fade all items */
	if(count == 0)
	{
		$('li.folioLi').fadeTo(200, 0.06);
		return;
	}

	/* fade out all items that are not from the current year */
	$('li.folioLi:not([class=folioLi ' + year + '])').fadeTo(200, 0.06);

	/* fade in the items from the current year */
	$('li.'+year).fadeTo(200, 1);

	/* scroll to the first such item (the -1 is necessary) */
	$("#portFolioContainer")[0].scrollTo($('li.'+year + ':nth(0)')[0].offsetTop - 1);
}

function setUpPortfolioBehavior() {
	//first, update the count.
	$('li#folioCount').html($('li.folioLi').length + ' project(s) found');

	//bind filter years to the focusYear function
	$('ul#portFolioFilter li a').click(function(e){
		if (!$(this).hasClass('activeFilter'))
		{
			var year = this.title;
			$('ul#portFolioFilter li a').removeClass('activeFilter');
			$(this).addClass('activeFilter');
			focusYear(year);
		}
	});
}


function setUpContactFormBehavior() {
	$('form#contactForm a#clearBtn').bind('click',			clearButtonHandler);
	$('form#contactForm a#sendBtn').bind('click',			sendButtonHandler);
	$('form#contactForm input[type=text]').bind('focus',	inputFocusHandler);
	$('form#contactForm input[type=text]').bind('blur',		inputBlurHandler);
	$('form#contactForm textarea').bind('focus',			inputFocusHandler);
	$('form#contactForm textarea').bind('blur',				inputBlurHandler);
}

function inputFocusHandler() {
	$(this).addClass('focusInput');
}

function inputBlurHandler() {
	$(this).removeClass('focusInput');
}

function clearButtonHandler() {
	$('form#contactForm input[type=text]').each(function(index){
		this.value = '';
	});
	$('form#contactForm input[type=checkbox]').each(function(index){
		this.checked = false;
	});
	$('form#contactForm textarea').each(function(index){
		this.value = '';
	});
	//document.getElementById('contactForm').reset();
}


function sendButtonHandler() {
	//form submission or validation, et al.
}

function slideUpShowNav() {
	$.timer(500, function (timer) {
        $('#showNavigation li').slideUp(350);
        timer.stop();
    });
}


function slideDownShowNav() {
	$.timer(500, function (timer) {
        $('#showNavigation li').slideDown(350);
        timer.stop();
    });
}

function setUpStartUpLocation()
{
	try {
		var url = window.location.href;
		if(url.split(/#/).length > 1) {
			var index = url.split(/#/)[1] - 1;
			delNavActive();
			$('#mainNavigation li a:nth(' + index + ')').addClass('navCurrent');
			if (index > 0) {
				slideUpShowNav();
			}
			else {
				$('#mainNavigation li a:nth(0)').addClass('navCurrent');
				slideDownShowNav();
			}
		}
		else {
			delNavActive();
			$('#mainNavigation li a:nth(0)').addClass('navCurrent');
			slideDownShowNav();
		}
	}
	catch (e) {
		if(null != typeof console) {
			console.warn('Exception in setting navigation state. Problems extracting from the URL.');
		}
		$('#mainNavigation li a:nth(0)').addClass('navCurrent');
	}
}
