$(document).ready(function() {

	//INITIALIZE TIPTIP
	$(".tiptip").tipTip({
		defaultPosition: 'top',
		delay: 200
	});
	
	//SMOOTH SCROLL
	$('#nav li, #footer a').click(function() {
		var navSelection = $(this).attr('class');
		if (navSelection == 'home') {
			window.location.hash = '!welcome';
			$('html, body').stop().animate({scrollTop: 0}, 'slow');
		} else {
			window.location.hash = '!' + navSelection;	
			var offset = $('#' + navSelection).offset();
			$('html, body').stop().animate({scrollTop: offset.top - 85}, 'slow');				
		};	
		return false;		
	});
	
	
	//MENU HIGHLIGHTING
	//function to determine size of box
	function getSectionDimensions(boxName) {
		var sectionDimensions = [];
		sectionDimensions['start'] = $('#' + boxName).offset().top - 85;
		sectionDimensions['end']   = $('#' + boxName).outerHeight() + sectionDimensions['start'];
		return sectionDimensions;
	}
	
	//checks where current position is relative to each boxes' dimensions and highlights menu accordingly
	$(window).scroll(function() {
		$('#nav a').removeClass('over');
		var documentPosition = $(document).scrollTop();
		$('.box').each(function() {
			var boxName = $(this).attr('id');
			var sectionDimensions = getSectionDimensions(boxName);
			if ( documentPosition >= sectionDimensions['start'] && documentPosition <= sectionDimensions['end'] ) {
				window.location.hash = '!' + boxName;
				$('#nav li.' + boxName + ' a').addClass('over');		
			} else if (documentPosition == 0 ) {
				window.location.hash = '!welcome';
			} else {
				$('#nav li.' + boxName + ' a').removeClass('over');
			}
		});
	});
	
	//SLIDESHOW
	//delete portfolioOverlays for each piece as they do not need to fade/in out
	//want them visible in case JS is off
	$('.portfolioPiece').each (function(){
		$('.portfolioOverlay', this).remove();
	});
	
	//add a fixed position overlay after next box
	$('#back').after('<div id=\"portfolioOverlayFixed\"></div>');
	
	//show next/back buttons hidden by default if javascript disabled
	$('#next a, #back a').css({visibility: 'visible'});
		
	//Cycle plugin settings
	$('#slide')
	.after('<div id="slide_nav">')
	.cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 0, 
	    next:   '#next', 
	    prev:   '#back',
		pager:  '#slide_nav',
		height: 325
	});	
	
	//CONTACT FORMS
	//Hide project request form
	$('#general_enquiry').hide();
	$('.project_request a').addClass('selected');
	
	$('.project_request').click (function(){
		if ($('#project_request').is(':visible')) {
			return false;
		} else {
			$('.general_enquiry a').removeClass('selected');
			$('.project_request a').addClass('selected');
			$('#general_enquiry').hide();
			$('#project_request').fadeIn('slow');
			return false;
		}
	});

	$('.general_enquiry').click (function(){
		if ($('#general_enquiry').is(':visible')) {
			return false;
		} else {
			$('.project_request a').removeClass('selected');
			$('.general_enquiry a').addClass('selected');
			$('#project_request').hide();
			$('#general_enquiry').fadeIn('slow');
			return false;
		}
	});
	
	//CALL UNIFORM TO STYLE FORM ELEMENTS
	$("select, input:checkbox, input:radio, input:file").uniform();

	//CALL FANCYBOX
	$("a#single_image").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	300, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor'	: 	'#000',
		'overlayOpacity':	0.8,
		'padding'		: 	3
	});

});

