//////////////////////////////////////////
//  Delay Plugin
/////////////////////////////////////////
(function($){
	$.fn.delay = function(time, callback){
		jQuery.fx.step.delay = function(){};
		return this.animate({delay:1}, time, callback);
	}
})(jQuery);
/*
	Live Load* by POWELL (R) MAY (powell.may@gmail.com || powell@iamhovercraft.com)
	Copyright (c) 2009-2010 P.R.May
	Version: .666 (06.06.09)
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.php
	Requires: jQuery v1.2.6 or later
*/
(function($){
	$.fn.liveLoad = function(options) {

		// Options
		var defaults = {  
			self : false,
			openClass : 'open',  
			contentToFetch : '#primeContent',  
			contentToReplace : '#page',
			contentToHide : 'div',
			onBefore : function(){},
			onAfter : function(){}
		};  
		var options = $.extend(defaults, options);


		this.live('click', function() {

			var $this = $(this);

			// URL Location
			var $url = $this.find('a').attr('href');

			if (options.self === true) {
				if ($this.hasClass('open')) {
					return false;
				} else {
					$this.addClass('loading');
					fetchContent();
				}
			} else {
				$(options.contentToReplace).addClass('loading');
				fetchContent();
			} 

			// Content Fetcher
			function fetchContent() {
				if (options.self === true) {
					$this.find(options.contentToHide).hide();
				} else {
					$(options.contentToFetch).remove();
				}
				// Ajax Setup
				$.ajax({
					type: 'get',
					url: $url,
					dataType: 'html',
					success: displayContent
				});
			} 

			// Content Displayer
			function displayContent(html){

				var $html = jQuery('<div>'+html+'</div>').find(options.contentToFetch);
				
				options.onBefore.call();
				
				if (options.self === true) {
					// Remove Injected Content from DOM
					$this.siblings().find(options.contentToFetch).remove().end()
					// Show Description
					.find(options.contentToHide).show().end()
					// Remove open class
					.removeClass(options.openClass).end()
					//  Add open class append content strip loader
					.addClass(options.openClass).append($html).removeClass('loading');
				} else {
					$(options.contentToReplace).append($html).removeClass('loading');
					$html.hide().fadeIn(800);
				}
				
				options.onAfter.call();
			}
			return false;
		});
		return this;
	}
})(jQuery);
/*
	Page Slice* by POWELL (R) MAY (powell.may@gmail.com || powell@iamhovercraft.com)
	Copyright (c) 2009-2010 P.R.May
	Version: .666 (06.06.09)
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.php
	Requires: jQuery v1.2.6 or later
*/
(function($){

	$.fn.pageSlice = function(options) {
		
		var overlay = '<div id="sliceOverlay" class="loading"></div>';
		var sliceContent = '<div class="sliceContent"></div>'; 
       

		var defaults = {  
			contentToFetch : '.content',
			onAfter : function(){
				$('.sliceContent').delay(500).fadeIn();
			} ,
			onBefore : function(){}  
		};  
		var options = $.extend(defaults, options);

		this.live('click', function() {
			var $this = $(this);
			
			var winY = $(window).height();
			var winX = $(window).width();
			
			options.onBefore.call();
			
			// Append Overlay to body
			$('body').prepend(overlay);
			// Create Content holder
			$('#sliceOverlay').after(sliceContent);

			// Set up Dimensions and Hide
			$('#sliceOverlay').css({
				'height' : winY, 
				'width' : winX,  
				'opacity' : 0,  
				'top' : $(window).scrollTop()
			}).animate({'opacity' : 0.5}, 600);

			// Slice Content Poition
			$('.sliceContent').hide().css({
				'left' : (($('#sliceOverlay').width() - $(this).width()) / 2) - ($(this).width() * 2),
				'marginTop' :  $(window).scrollTop() - 100
			});

			// Begin Ajax Request
			$.ajax({
				type: 'get',
				url: $(this).attr('href'),
				dataType: 'html',
				success: contentLoaded
			});
			// Ajax Success
			function contentLoaded(html) {
				var $html = jQuery('<div>'+html+'</div>').find(options.contentToFetch);
				// Remove loader and Attach fetched content to tool tip
				$('#sliceOverlay').removeClass('loading');
				$('.sliceContent').append($html, '<a class="close">Close</a>');
				// Slide Down Overlay
				options.onAfter.call();
			}
			
			// remove default link functionality
			return false;
			
		});
		return this;
	}
	// overlay window events
	$(window).scroll(function(){
		// Simulate Position : Fixed for overlay
		$('#sliceOverlay').css('top', $(window).scrollTop() + "px");

	});
	$(window).resize(function(){
		$('#sliceOverlay').css({
			'width' : $(window).width() + "px",
			'height' : ($(window).height() + 20) + "px"
		});
	});
	// Close Button
	$('.sliceContent a.close, #sliceOverlay').live('click', function(){
		$('body').find('.sliceContent').fadeOut()
		.delay(500, function(){
			$(this).remove();
		})
		// Slide Overlay up
		$('#sliceOverlay').fadeOut()   
		// wait half a second and remove it from DOM
		.delay(500, function(){
			$(this).remove();
		});
	});
})(jQuery);
$(document).ready(function(){

	// Home Page Drop Down
	$('#home .intro a.more').click(function(){
		var $url = $(this).attr('href');
		$.ajax({
			type: 'get',
			url: $url,
			dataType: 'html',
			success: expandHome
		});
		function expandHome(html) {
			var $html = jQuery('<div>'+html+'</div>').find('div.desc');
			$('#pageBody').addClass('open').find('.intro').after($html);
			$('.intro a.more').hide();
			$('.desc').append('<a class="close">Close</a>');
		}
		
		return false;
	});
	$('#home a.close').live('click', function(){
		$(this).parents('#pageBody').removeClass('open')
		.find('.desc').remove().end()
		.find(':hidden').show();
	});    
	
	// Footer Navigation
	$('#tertNav li a').after('<span>&nbsp;|&nbsp;</span>');
	$('#tertNav li:last span').remove();

	// What We Do Box
	$('#wwd .content').liveLoad({
		self : true,
		contentToHide : 'h3, p, span, .open',
		contentToFetch : '#primeContent',
		onAfter : function() {
			$('#primeContent').append('<a class="close">Close</a>');
		}
	});
	$('#wwd a.close').live('click', function(){
		$(this).parents('.content').removeClass('open')
		.find('#primeContent').remove().end()
		.find(':hidden').show();
	});
	
	// Contact Form
	$('#all').click(function(){
		$('input:checkbox').not(this).not(':checked').each( function() {
			this.checked = !this.checked;
		});
	});
	if(jQuery.fn.validate){
		$('#brioContactForm').validate({ submitHandler : ajaxSubmit });
	}
	function ajaxSubmit(form){

			var name = $('#fName').val(); 
			var data = $(form).serialize();

			$('#contactForm button').addClass('working');

			$.ajax({
				type: "POST",
				url: "/components/mailForm.php",
				data: data,
				success: function() {
					$('#contactForm form, #contactForm p.finePrint').hide();
					$('#contactForm').html('<h2 class="success">Thanks</h2><p class="success">We will be in touch shortly '+name+'.</p>');
				}
			});
			return false;
	}
	
	// Overlay Boxes
	$('#bpm a.more, #success a.more').pageSlice({
		contentToFetch : '#primeContent'
	});



});
