///////////////////////////////////////////////////////////////////////////////////////////////////
//Google Analytics tracking

function trackPage() {
	try {
		var pageTracker = _gat._getTracker("UA-5024096-1");
		pageTracker._trackPageview();
	} catch(err) {}
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function pageload(hash) {
		// hash doesn't contain the first # character.
		
		if(hash) {
			// restore loaded state from the hash
			$('#projects .all:visible').hide();
			$('#projects .'+hash).fadeIn();
			$('#folio-nav li').removeClass('current');
			$('#folio-nav').find("a[href='#" +hash+ "']").parent().addClass('current');
			var sectionName = $('#folio-nav').find("a[href='#" +hash+ "']").html();
			$("h2.sub-head").html(sectionName);
			
		} else {
			// start state
			$('#projects .all').show();
			//$('#projects .featured').show();
			
			//$('#folio-nav li').removeClass('current');
			//$('#folio-nav a:first').parent().addClass('current');
			$("h2.sub-head").html("View All");
		}
	}

///////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function() {
	//track the page
	trackPage();
	
	//fix png's for in IE
	$(document).pngFix();
	
	//////////////////////////////////////////////////
	//home page functions
	if ( $("#home-page").length > 0 ) {
		
		//slideshow
		$('.cycle').cycle({ 
		    fx: 'fade', 
		    speed: 3000,
			timeout: 3000, 
			pause: 4,
			random: 1
		 });
		
		//create the home page tabs
		$("#featured-projects").tabs({ selected: 0});
		
	}
	
	//////////////////////////////////////////////////
	//about page functions
	if ( $("#about-page").length > 0 ) {
		
		//slideshow
		$('.cycle').cycle({ 
		    fx:    'fade', 
		    speed:  3000,
			timeout: 3000, 
			pause: 4,
			random: 1
		 });
		
		//create the home page tabs
		$("#about").tabs({ selected: 0});
		
	}
	
	//////////////////////////////////////////////////
	//portfolio page functions
	if ( $("#folio-page").length > 0 ) {
		
		//remove the anchors for users with javascript
		//$('a.pane-anchor').remove();
		
		$.historyInit(pageload);
		
		$('#folio-nav a').each(function() {
				$(this).click(function() {
					var hash = this.href;
					hash = hash.replace(/^.*#/, '');
					// moves to a new page. 
					// pageload is called at once. 
					$.historyLoad(hash);
					return false;
				});
			});

	}
	
	//////////////////////////////////////////////////
	//project page functions
	if ( $("#project-page").length > 0 ) {
				
		//project gallery
		$('.cycle').cycle({ 
		    fx:    'fade', 
		    speed:  600,
			timeout: 0, 
			prev: '#prev',
			next: '#next',
			pager: '#pager',
			end: function() {  
			        alert('The slideshow has ended.');  
				}
		 });
		
		
		//if there is only one image
		if($("#images").children().size() < 2){
			//hide the controls and show the image
			$("#controls").hide();
			$("#project #viewer img").show();
		}else{
			//otherwise show the prev / next buttons
			$("#project #controls #prev-next").show();	
		}
				
	}
		
	//////////////////////////////////////////////////
	//form validation
	if ( $("#submit-comment").length > 0 && $.browser.msie == false) {
		
		//validate the comment form
		$("#comment_form").validate({
			errorPlacement: function(error, element) {
				error.appendTo( element );
			}
		});
		$("#contact_form").validate({
			errorPlacement: function(error, element) {
				error.appendTo( element );
			}
		});
				
	}
	
});





