/*---[ Details ]---------------------------------------
Global Javascript
Author: Lee Powell
-------------------------------------------------------*/

/*-----------------------------------------------------
jQuery
-------------------------------------------------------*/
_COOKIENAME = 'dialled';

$('html').addClass('js');

Shadowbox.init({
	// include the html player because we want to display some html content
	players: ['img', 'html']
});

jQuery(function(){
	
	/* 
	 * Make the logo a clickable link back to the homepage
	 */
	$('#logo').click(function(){
		window.location = '/';
	});
	
	/* 
	 * Hide additional images
	 */
	$('div.img-add').addClass('hide');
	
	/*
	 * Handle product navigation in the global nav
	 */
	$('#nav-global .products').each(function(){
		
		// Product list
		var productList = $('ul:eq(0)', $(this));
		
		// Product title
		var productTitle = $('span:first-child', $(this));
		
		// Hide the product list by default
		var cookie = $.cookie(_COOKIENAME);
		
		//var navState = ( cookie == null ) ? 'closed' : cookie;
		//if( navState == 'closed' && productList.is(':visible') && !$('body#product').length )
		
		if( productList.is(':visible') && !$('body#product').length )
		{
			productList.hide();
		}		
		
		// Open / close on click of title
		productTitle.click(function(){
			
			if( productList.is(':visible') )
			{
				productList.slideUp('fast', function(){
					$.cookie('dialled', 'closed');
				});
			}
			else
			{
				productList.slideDown('fast', function(){
					$.cookie('dialled', 'open');
				});
			}
		});

	});
	
	/*
	 * Content block grids, to adjust for equal spacing with floated elements
	 *
	 */
	$('.content-block-grid').each(function(){
		
		var contentBlocks = $('.content-block', $(this));
		
		if( contentBlocks.length > 2 ){
			
			for( var i = 2; i < contentBlocks.length; i++ ){
				
				var aboveBlock = $(contentBlocks[i - 2]);
				var aboveHeight = aboveBlock.outerHeight();
				var abovePosition = aboveBlock.position();
				var aboveMarginTop = parseFloat(aboveBlock.css('margin-top'));
				
				var thisBlock = $(contentBlocks[i]);
				var thisPosition = thisBlock.position();
				
				var theGap = ((abovePosition.top + aboveHeight + aboveMarginTop) - thisPosition.top) * -1;
								
				if( theGap > 10 ){
					var diff = theGap - 10;
				
					thisBlock.css('margin-top', (diff * -1 ));
				}
				
			}
		
		}	
	});
	
	/*
	 * Team page, adjust column padding to line up the right hand column with the bottom of the main content-block
	 *
	 */
	$('body#team').each(
	
		function()
		{
			var con = $('#content');
			var col1 = $('.column.col-1');
			var col2 = $('.column.col-2');
			var cb1 = $('.content-block:first', col1);
			var cb2 = $('.content-block:first', col2);
			
			var cb1Height = cb1.outerHeight();
			var cb2Height = cb2.outerHeight();
			
			var conPadTop = parseFloat(con.css('padding-top'));
			
			var cb1Pos = cb1.position();
			
			var padTop = ((cb1Height - cb2Height) + cb1Pos.top) - conPadTop;
			
			col2.css('padding-top', padTop);
		}
	);
	
	/*
	 *	 Contact link. Hijax the anchor, and grab the include, and display it in a Shadowbox overlay
	 *
	 */
	$('#nav-global .contact a, #footer a[href="/contact.php"]').bind(
		/* Bind both keyboard and keyboard keypress events */
		'click keypress', 
		
		function(){
			
			$.get(
				/* URL to grab */
				'/inc/inc.contact.php',
				
				function(data)
				{
					Shadowbox.open({
				        content:    data,
				        player:     'html',
				        height:     300,
				        width:      420,
				        options:	{
				        	onFinish:	function(){
				        		/* Add hooks and elements we need for styling */
					        	$('#sb-content').addClass('contact').wrapInner('<div></div>');
					        	$('#sb-body-wrapper').append('<div class="splatter"></div>');
					        	$('#sb-body').css({ 'padding-bottom': '30px' });
					        }
				        }					        
				    });
				}
			);
			
			return false;
		}
	);
	
});