// Google Analytics
if (window.location.toString().indexOf('empirestaging') < 0){
	try {
		var pageTracker = _gat._getTracker("UA-0000000-0");
		pageTracker._trackPageview();
	} catch (err) {
	}
}

// Tests to see if a selector exists
$.exists = function(selector){
	return ($(selector).length > 0);
}


// Advances the jscroll pane to the selected thumb/project
function advanceJscroll() {
	
	// Test for a selected project
	if($.exists('#thumbs_wrapper ul li.selected')) {
		
		var scrollOffset = $($('#thumbs_wrapper ul li.selected').offset())[0].top - 235
		var element = $('.scroll-pane').jScrollPane();
		var api = element.data('jsp');
		api.scrollToY(scrollOffset);
		
	}

}
	
// Custom Scroll Bar  
function callJscroll(){
	
	// Initial call for jScroll Pane 
	$('.scroll-pane').jScrollPane();
	
	// Sets the background width of the pane leaving room for a 1px transparent margin between the pane and the scroll bar  
	if ($.exists('.jspVerticalBar')) {
		$('.jspPane').css({'width':'220px'});
	}

}

// Portfolio/thumblist toggle show
function thumbListToggleOn() {
	$('#projects_thumb_list').css({'display' : 'inline-block'}).fadeIn(0);
	$('#projects_toggle_wrapper').css({'display' : 'block'}).fadeIn(0);
	$('#projects_toggle').css({'display' : 'block'}).fadeIn(0);
} 

//Portfolio/thumblist toggle show
function thumbListToggleOff() {
	$('#projects_thumb_list').delay(500).fadeOut(0);
	$('#projects_toggle_wrapper').delay(500).fadeOut(0);
	$('#projects_toggle').delay(500).fadeOut(0);
} 

// Project thumb balancing script  
function thumbListBalance(){

	bodyHeight = $('.page_margins').height()
	navHeight = ($('#nav_wrapper').height() + $('#projects_toggle_wrapper').height() + 2)
	footerHeight = $('#footer').height() 
	
	//console.log(navHeight)
	
	thumbGridHeight = (bodyHeight - (navHeight + footerHeight))
	
	$('#projects_thumb_list').css({
		'height' : thumbGridHeight
	})

	
}

// Thumbnail hide/show
function thumbToggle(){

	$('#list_wrapper').hide();
	$('#list_wrapper ul').hide();
	$('#list_wrapper').removeClass('selected');
	$('#thumbs_wrapper').show();
	$('#thumbs_wrapper ul').show();
	$('#thumbs_wrapper').addClass('selected');
	$('#show_thumbs').addClass('selected');
	$('#show_list').removeClass('selected');
	thumbListBalance();
 
}

// List hide/show
function listToggle(){

	$('#thumbs_wrapper').hide();
	$('#list_wrapper ul').hide();
	$('#thumbs_wrapper').removeClass('selected');
	$('#list_wrapper').show();
	$('#list_wrapper ul').show();
	$('#list_wrapper').addClass('selected');
	$('#show_thumbs').removeClass('selected');
	$('#show_list').addClass('selected');
	thumbListBalance();

}

// Vertically centers the page margins div in the middle of the window
function vertAlign(){
	
	var innerWindowHeight = document.documentElement.clientHeight;
	var pageHeight = $('.page_margins').height();
	
	if( innerWindowHeight >= pageHeight ){
		heightDiff = ((innerWindowHeight - pageHeight) / 2)
		$('body').css({'padding-top':heightDiff})
	}
	
	$('.page_margins').css({'display':'block'})
	
}

$(document).ready(function(){
	
	// Vertically centers the page margins
	vertAlign()
	
	
	
	// If this was a portfolio list view show the list, otherwise show the thumb nails
	if(window.location.hash.replace(/#/, '') == 'list'){
		
		advanceJscroll()
		listToggle();
		
	}else{
		
		advanceJscroll()
		thumbToggle();
		
	}
	
	// Thumbnail hide/show
	$('#show_thumbs').click(function(){
		
		// Thumbnail show
		thumbToggle()
		
		// Custom scrollbar
		thumbListBalance()
		callJscroll();

	})
	
	// List hide/show
	$('#show_list').click(function(){
		
		// List show
		listToggle()
		
		// Custom scrollbar
		thumbListBalance()
		callJscroll();
	})

	if ($('body').hasClass('projects')){
		
		// Thumbnail tooltips
		//$('#project_thumb_strip ul li a[title]').tooltip({speed: 500});
		
		// Call lightbox for all project images
		$("#project_thumb_strip ul li.image a").colorbox({maxWidth:'75%', maxHeight:'75%'});

		// Call lightbox for all project embedded swfs
		$("#project_thumb_strip ul li.embed a").each(function(i,e){
			$(this).colorbox({maxWidth:'75%', maxHeight:'75%', inline:true, href: '#' + this.name})
		})
		
	}
	
	// Thumb hide and show for pages that are not projects
	if (!$('body').hasClass('projects')) {
		
		// Thumbnail & list hide/show
		$('.portfolio_toggle').hover(function() {
			
			// Thumb on
			thumbListToggleOn()
			
			// Custom Scrollbar
			thumbListBalance()
			callJscroll();
		}, function () {
			
			// Thumb off
			thumbListToggleOff()
			
		})
		
		$('#projects_toggle').hover(function() {
			
			// Thumb on
			thumbListToggleOn()
			
			// Custom Scrollbar
			thumbListBalance()
			callJscroll();
			
		}, function () {
			
			// Thumb off
			thumbListToggleOff()
			
		})
		
		$('#projects_thumb_list').hover(function() {
			
			// Thumb on
			thumbListToggleOn()
			
			// Custom Scrollbar
			thumbListBalance()
			callJscroll();
			
		}, function () {
			
			// Thumb off
			thumbListToggleOff()
			
		})
	}
	
/*
	// Super size full height width background
	$(function(){
		$.fn.supersized.options = {
			startwidth : 1024,
			startheight : 768,
			vertical_center : 1,
			slideshow : 0,
			navigation : 0,
			transition : 0, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover : 0,
			slide_counter : 0,
			slide_captions : 0,
			slide_interval : 0
		};
		$('#supersize').supersized();
	});
*/

});//End - $(document).ready

//Functions called upon window loaded
$(window).load(function(){

/*
	// Shadow behind main menu
	$('.shadow').dropshadow({
	    shadowColor: '#333333',
	    shadowLayer: 3,
	    distance:'-8px',
	    blur:'26px'
	  });
*/
		
	
	if ($('body').hasClass('home') || $('body').hasClass('projects') || $('body').hasClass('static')){
		
		$("#supersize").fadeTransition({pauseTime: 8000, transitionTime: 1000, delayStart: 100, pauseNavigation: false});
		$("#project_titles").fadeTransition({pauseTime: 8000, transitionTime: 1000, delayStart: 100, pauseNavigation: false});
		$('#home_logo img').delay(5000).fadeTo(1000, 0);
		
	}	
	// Custom Scrollbar
	thumbListBalance()
	callJscroll();
	

});// End - $(window).load()

// Functions called upon window resize
$(window).resize(function(){
	
	// Vertically centers the page margins
	vertAlign()
		
/*
	$('.shadow').dropshadow({
		shadowColor: '#333333',
		shadowLayer: 3,
		distance:'-8px',
		blur:'26px'
	});
*/

	// Custom Scrollbar
	thumbListBalance()
	callJscroll();

});// End $(window).resize
