var initThumbSelect = true;

$(document).ready(function () 
{
	// Initialize scroll panel
	initScrollPanel();
	
	// Set up tab listeners
	$('#welcomeLink').click(function(e) 
	{ 
		// Animate top bar
		$('#topbar').animate({backgroundColor: "#990000" }, 500);

		// Hide work menu
		hideWorkMenu();
	});
	
	// Tab listeners
	$('#workLink').click(function(e) 
	{
		// Animate top bar
		$('#topbar').animate({backgroundColor: "#669900" }, 500, function() { showWorkMenu(); });
	});
	
	$('#teachingLink').click(function(e) 
	{
		// Animate top bar
		$('#topbar').animate({backgroundColor: "#ff6600" }, 500);
		
		// Hide work menu
		hideWorkMenu();
	});
	$('#contactLink').click(function(e) 
	{
		// Animate top bar
		$('#topbar').animate({backgroundColor: "#006699" }, 500);
		
		// Hide work menu
		hideWorkMenu();
	});
	
	// Work listeners
	$('.workthumbnail').click(function(e)
	{
		initThumbSelect = false;
		
		// Extract id		
		var idInfo = this.id.split('_');
		
		// Hide all work panels
		$('.workpanel').hide();
		
		// Show selected panel
		$('#work_' + idInfo[0]).show();
		
		// Place downarrow
		placeDownArrow(idInfo[0]);
		
		// Remove selected thumbnail
		$('#worknavigation img').removeClass('current');
		
		// Add selected thumbnail
		$('#' + idInfo[0] + '_thumbnail img').addClass('current');

	});
	
	// Contact listener
	$('#contactform').submit(function(event)
    {
    	// Prevent page reload
        event.preventDefault();
		
		// Send AJAX request
        contactPost();
    });
    
    // Gallery images
    $("#socialsyllabus_gallery").click(function(event) 
    {
    	// Prevent page reload
        event.preventDefault();
        
		$.fancybox([
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_1.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_2.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_3.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_4.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_5.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_6.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_7.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_8.png',
			'http://www.johnkuiphoff.com/p_images/gallery/socialsyllabus_9.png'
		], {
			'padding'			: 0,
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'type'              : 'image',
			'changeFade'        : 0,
			'showNavArrows'		: true
		});
	});
	
	$("#reedbox_gallery").click(function(event) 
    {
    	// Prevent page reload
        event.preventDefault();
        
		$.fancybox([
			'http://www.johnkuiphoff.com/p_images/gallery/reedbox_1.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/reedbox_2.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/reedbox_3.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/reedbox_4.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/reedbox_5.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/reedbox_6.jpg'
		], {
			'padding'			: 0,
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'type'              : 'image',
			'changeFade'        : 0,
			'showNavArrows'		: true
		});
	});
	
	$("#superfund_gallery").click(function(event) 
    {
    	// Prevent page reload
        event.preventDefault();
        
		$.fancybox([
			'http://www.johnkuiphoff.com/p_images/gallery/superfund_1.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/superfund_2.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/superfund_3.jpg'
		], {
			'padding'			: 0,
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'type'              : 'image',
			'changeFade'        : 0,
			'showNavArrows'		: true
		});
	});
	
	$("#crankitup_gallery").click(function(event) 
    {
    	// Prevent page reload
        event.preventDefault();
        
		$.fancybox([
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_1.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_2.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_3.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_4.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_5.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_6.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/crankitup_7.jpg'
		], {
			'padding'			: 0,
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'type'              : 'image',
			'changeFade'        : 0,
			'showNavArrows'		: true
		});
	});
	
	$("#its_gallery").click(function(event) 
    {
    	// Prevent page reload
        event.preventDefault();
        
		$.fancybox([
			'http://www.johnkuiphoff.com/p_images/gallery/its_1.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/its_2.jpg',
			'http://www.johnkuiphoff.com/p_images/gallery/its_3.jpg'
		], {
			'padding'			: 0,
			'transitionIn'		: 'none',
			'transitionOut'		: 'fade',
			'type'              : 'image',
			'changeFade'        : 0
		});
	});

});

// Send Contact Request
function contactPost()
{
    // AJAX call
	$.ajax({
        type: "POST",
        url: "contact.php",
        dataType: "json",
        data: $('#contactform').serialize(),
        success: contactResponse
    });
    
    // Hide Confirmation
    $('SSConfirmation').hide();
    
    // Hide errors
    $('#emailerror').empty();
    $('#messageerror').empty();
	
	// Show indicator
	$('#contact_indicator').show();
	$('#contact_check').hide();
}

// Receive Contact Response
function contactResponse(json)
{

   	// Check if successful
    if(json.status == 'success') 
    {
    	// Display confirmation
    	$('#confirmation').fadeIn();
    	
    	// Hide Error
    	$('#email').val('');
    	$('#message').val('');
    }
    
    // Check if failure
    if(json.status == 'failure')
    {
        // Display errors
        if(json.error.email)
        {
        	$('#emailerror').html(json.error.email);
        }
    }
    
    // Hide indicator
	$('#contact_indicator').hide();
	$('#contact_check').show();
}

// Show work menu
function showWorkMenu()
{
	$('#downarrow').show();
	$('#workthumbnails').show();
	
	if(initThumbSelect == true)
	{
		placeDownArrow('socialsyllabus');
	}
}

// Hide portfolio menu
function hideWorkMenu()
{
	$('#downarrow').hide();
	$('#workthumbnails').hide();
}


// Position down arrow
function placeDownArrow(thumbnail)
{
	// Get position of selected thumbnail
	var posThumb = $('#' + thumbnail + '_li').position();
	
	// Position down arrow
	$('#downarrow').css({top: posThumb.top - 27 + 'px', left: posThumb.left + 14 + 'px'});
}

// Scroll panel - adapted from: http://jqueryfordesigners.com/coda-slider-effect/
function initScrollPanel()
{
	
	// Define panels and container
	var $panels = $('#slider .scrollContainer > div');
	var $container = $('#slider .scrollContainer');

	// Set horizontal flag
	var horizontal = true;

	// Float panels left
	if (horizontal) 
	{
  		$panels.css({
  	  		'float' : 'left',
 	   		'position' : 'relative' // IE fix to ensure overflow is hidden
 		});
  
 		// Calculate width for container
 	 	$container.css('width', $panels[0].offsetWidth * $panels.length);
	}

	// Set scroller overflow to hidden
	var $scroll = $('#slider .scroll').css('overflow', 'hidden');

	// Handle tab selection
	function selectNav() 
	{
		$(this)
			.parents('ul:first')
				.find('a')
					.removeClass('selected')
				.end()
			.end()
	    .addClass('selected');
	}
	
	$('#slider .navigation').find('a').click(selectNav);

	// Get navigation link that has this target and select the nav
	function trigger(data) 
	{
		var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
		selectNav.call(el);
	}

	if (window.location.hash) 
	{
		trigger({ id : window.location.hash.substr(1) });
	} else {
		$('ul.navigation a:first').click();
	}

	// Subtract padding from offset
	var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) || 0) * -1;

	// Define scroll options
	var scrollOptions = 
	{
		target: $scroll,
  		items: $panels,
  		navigation: '.navigation a',
		axis: 'xy',
		onAfter: trigger,  
		offset: offset,
		duration: 500,
		easing: 'swing'
	};

	// Apply serialScroll to the slider
	$('#slider').serialScroll(scrollOptions);

	// Apply localScroll to hook any other arbitrary links to trigger the effect
	$.localScroll(scrollOptions);

	// Move the slider into position 
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);
	
}