(function($)
{
	Shadowbox.init({
    	// let's skip the automatic setup because we don't have any
    	// properly configured link elements on the page
    	skipSetup: true
	});

	// DOM Ready
	$(function()
	{
		$('#content .home-flash-left').html(homepageSlides).cycle({
			fx: 'fade',
			timeout: 5000,
			speed: 2000
		});
				
		$('#news-summary').after('<span id="news-prev">&lsaquo; Previous</span><span id="news-next">Next &rsaquo;</span>')
		$('#news').jCarouselLite({
			vertical: true,
			auto: 5000,
    		speed: 1000,
    		 btnNext: "#news-next",
   			 btnPrev: "#news-prev"

		});
		
		$('#news-summary a[rel="shadowbox"]').bind('click', trigger_news_overlay);
		
		$('#case-study a').bind('click', trigger_brochure_overlay);
		
		$('body').append('<div id="tmp"></div>');		
	});
		
	function trigger_news_overlay(e)
	{
		if( touchDevice ){
			return true;
		}
		
		var $this = $(e.target);
		
		// for images within links the event registers the target as the image when clicked
		// traverse up the tree to find the nearest parent anchor
		if($this.context.nodeName.toLowerCase() != 'a')
		{
			$this = $this.closest('a');
		}
			
		var slug = $this.attr('href');
		
		if( typeof slug != 'undefined' || slug != '' || slug == false )
		{
			slug = slug.split('?story=')[1];
						
			$.ajax({
				url: '/inc/news_story.php',
				type: 'GET',
				data: 'story=' + slug,
				dataType: 'html',
				success: display_overlay
			});
		}
		
		e.preventDefault();
		
		return false;
	}
	
	function trigger_brochure_overlay(e)
	{
		if( touchDevice ){
			return true;
		}
		
		var $this = $(e.target);
		
		// for images within links the event registers the target as the image when clicked
		// traverse up the tree to find the nearest parent anchor
		if($this.context.nodeName.toLowerCase() != 'a')
		{
			$this = $this.closest('a');
		}
			
		var target = $this.attr('href');
		
		if( typeof target != 'undefined' || slug != '' || slug == false )
		{						
			$.ajax({
				url: target,
				type: 'GET',
				data: 'ajax=true',
				dataType: 'html',
				success: display_overlay
			});
		}
		
		$('#order form').live('submit', function(e){
			var data = {
				ajax: true,
				submit: true
			}
			
			$('input, textarea', $(this)).each(function(){
				data[$(this).attr('name')] = $(this).val();
			});
					
			$.ajax({
				url: target,
				type: 'GET',
				data: data,
				dataType: 'html',
				success: update_form
			});
			
			e.preventDefault();
			
			return false;
		});
		
		e.preventDefault();
		
		return false;
	}
	
	function display_overlay(data, status, request)
	{
		var $tmp = $('#tmp');
		
		$tmp.html(data+'<a href="#" id="sb-nav-close2" onclick="Shadowbox.close(); return false;">Close</a>');
						
		Shadowbox.open({
			content: $tmp.html(),
			player: 'html',
			width: 550,
			height: $tmp.outerHeight(),
			options: {
				enableKeys: false,
				onFinish: function()
				{
					$tmp.html('');
				}
			}
		});
	}
	
	function update_form(data, status, request)
	{
		var $tmp = $('#tmp');
		
		$tmp.html(data+'<a href="#" id="sb-nav-close2" onclick="Shadowbox.close(); return false;">Close</a>');
				
		$('#sb-player').html( $tmp.html() );
				
		$tmp.html('');
	}
	
})(jQuery);
