var initHash,thisBodyID,
	$browser = {};

$(document).ready(function() {
	
	
	if ($('body').attr('id') == 'about') initAbout();
	if ($('body').attr('id') == 'home') initHome();
	if ($('body').attr('id') == 'contact') initContactForm('#contact-form');
	
	
	/* Temp has path
	-------------------------------------------------------------- */
	
	var browserURL = String(window.location).split('/');
	var thisPage = browserURL[3];
	if (thisPage == '') thisPage = 'home';
	$('#header a[data-bodyid='+thisPage+']').parents('li').addClass('current');
	
	
	/* Browser capabilities via Modernizr
	-------------------------------------------------------------- */
	
	$browser.placeholder = Modernizr.placeholder;
	var thisHTML = $('html');
	var hasTransitions = thisHTML.hasClass('csstransitions');
	var hasHistory = thisHTML.hasClass('history');
	
	
	/* Pushstate
	-------------------------------------------------------------- */
	
	// blah blah
	
	
	function contentIn() {
    
	    /* Margin fix for Portfolio grid
		-------------------------------------------------------------- */
	
		$('#article-grid').find('.article:nth-child(4n)').css('margin-right',"0px");
	
    };

	
	/* Slideshow Cycle
	-------------------------------------------------------------- */
	
	$('#slideshow').cycle({
		fx: 'fade',
		timeout: 0,
		speed: 500,
		easing: 'easeInOutQuad',
		pager:  '#gallerythumbs',
		pagerAnchorBuilder: function(idx, slide) { 
        	return '#gallerythumbs li a:eq(' + idx + ')';
    	}
	});
	
	
	$(document).keydown(function(e){
		if(e.which == 37){ $('#slideshow').cycle('prev'); }
		else if(e.which == 39){ $('#slideshow').cycle('next'); }
    });
	
	
	/* Fancybox
	-------------------------------------------------------------- */
	
	$('#slideshow a[rel=fancybox]').fancybox({
		'autoScale'	:	true,
		'overlayOpacity'	:	'0.6',
		'speedIn'	:	500,
		'speedOut'	:	250,
		'titleShow'	:	true,
		'cyclic'	:	true,
		'centerOnScroll' : true
	});
	
	
	/* Navigation
	-------------------------------------------------------------- */
	
	$('nav#mainmenu ul li a').click(function(e) {
		/*
		e.preventDefault();
		$('nav ul li.current').removeClass('current');
		$(this).parents('li').addClass('current');
		thisBodyID = $(this).attr('data-bodyid');
		
		var thisHref = $(this).attr('href');
		thisHref = thisHref.split('/');
		
		window.location.hash = '/' + thisHref[3];
		*/
	});
	
	
	/* Sub navigation
	-------------------------------------------------------------- */
	
	$('#filter-toggle').click(function(e) {
		e.preventDefault();
		
		var isOpen = $('#submenu').hasClass('openmenu');
		if (isOpen) {
			$('#submenu').animate({height: "-=160px"}, 400, 'swing', function () {
				$('#content').animate({top: "-=160px"}, 400, 'swing');
			}).removeClass('openmenu');
		} else {
			$('#content').animate({top: "+=160px"}, 400, 'swing');
			$('#submenu').animate({height: "+=160px"}, 400, 'swing').addClass('openmenu');
		}
		
	});
	
	
	/* Sub navigation
	-------------------------------------------------------------- */
	
	$('#filters').cycle({
		timeout: 0,
		fx: 'scrollVert',
		speed: 500,
		pager: '#tabs-menu',
		pagerAnchorBuilder: function(idx,slide) {
			return '#tabs-menu a:eq('+idx+')'
		}
	});
	
	$('#view-client-list').fancybox({
		type: 'iframe',
		centerOnScroll: true
	});
	
	$('#about-accordion').accordion({
		header: '.about-form-heading'
	});
	
});

$(window).load(function() {

	/* Resize Background images
	-------------------------------------------------------------- */
	
	// $('section#background img').suprescreen();
	
	
	/* Masonry
	-------------------------------------------------------------- */
	
	$('#article-grid').addClass('notransition').masonry({
		itemSelector: 'div.article',
        columnWidth: 40,
        saveOptions: false,
        animate: false
	}, function() {
		$(this).removeClass('notransition');
	});
	
});


function initHome() {
	
	
	
};


function initAbout() {
	
	initContactForm('#contact-form-1');
	initContactForm('#contact-form-2');
	
};

function initContactForm(self) {

	$self = $(self);
	
	$(document).ready(function() {
		
		$self.submit(function() {
			$self.find('.error').remove();
			var hasError = false;
			$self.find('.requiredField').each(function() {
				if(jQuery.trim($(this).val()) == '') {
					$(this).parent().append('<span class="error">Required field</span>');
					hasError = true;
				} else if($(this).hasClass('email')) {
					var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
					if(!emailReg.test(jQuery.trim($(this).val()))) {
						$(this).parent().append('<span class="error">Invalid field</span>');
						hasError = true;
					}
				}
			});
			if(!hasError) {
				$self.find('button').fadeOut('normal');
				var formInput = $(this).serialize();
				$.post($(this).attr('action'),formInput, function(data){
					$self.slideUp("fast", function() {				   
						$(this).before('<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent.</p>');
					});
				});
			}
			
			return false;
			
		});
		
		if (!$browser.placeholder) {

            $("input").each(

	        function() {
	            if ($(this).val() === "" && $(this).attr("placeholder") !== "") {
	                $(this).val($(this).attr("placeholder"));
	                $(this).focus(function() {
	                    if ($(this).val() === $(this).attr("placeholder")) {
	                        $(this).val("");
	                    }
	                });
	                $(this).blur(function() {
	                    if ($(this).val() === "") {
	                        $(this).val($(this).attr("placeholder"));
	                    }
	                });
	            }
	
	        })
		};
		
	});

};

/*



*/
