$(document).ready(function() {


// Services sidebar navigation
	$('ul#services-nav li a').click(function() {
		var sidebarHeight = $('#sidebar').outerHeight();
		var listHeight = $('ul#services-nav > li').size() * 116;		
		var subNavHeight = sidebarHeight - listHeight;
				
		//$(this).parent().children('ul').css('height', subNavHeight);
		$(this).parent().children('ul:first').toggle();
		$(this).parent().siblings().find('ul:visible').hide();
	});
	
	/* Show active section's child pages */
	if ( $('ul#services-nav li[id=active]') ) {
		$('ul#services-nav li#active a').trigger('click');
	}

	if($('ul#section-nav li > ul.sub-menu').children('li').size() > 0 && (/(the-novus-core-team|\/staff\/)/i.test(location.pathname))){
		$('ul#section-nav li > ul.sub-menu').show();
	}

// General sidebar navigation
	$('ul#section-nav li a').click(function() {
	
		if ( $(this).parent().children('ul').size() > 0  ) {
			$(this).parent().children('ul:first').toggle();
			$(this).parent().siblings().find('ul:visible').hide();
			$(this).parents('ul').children('li').removeClass('active');
			$(this).parent().addClass('active');
		}	
	});
		

// Contact Panel
	$('#contact-panel').hide();
	$('ul#site-nav li.contact a').click(function() {
		$('#contact-panel').show().animate({ right: '0' });
	});
	
	$('a#close').live('click', function() {
		$('#contact-panel').animate({ right: '-930px' }).fadeOut('fast');
	});



// Homepage Animation
	var home1 = function() {
	
		$('#ajax_loader, #loading_branding').remove();
		$('#left_slides').load('wp-content/themes/novus/inc/block-slides.php #left');
		$('#right_slides').load('wp-content/themes/novus/inc/block-slides.php #right');
		//$('#mid_bar').delay(300).animate({ top: '245px' }, { complete: home2 });
		$('#mid_bar').animate({ top: '245px' }, { complete: home2 });
	};
	
	var home2 = function() {
		//$('#right').delay(500).animate({ right: '0' }, { complete: home3 });
		$('#right').animate({ right: '0' }, { complete: home3 });
		
	};	
	
	var home3 = function() {
		//$('#left ul.natural').delay(500).animate({ opacity: '1' }, { complete: home4 });
		$('#left ul.natural').animate({ opacity: '1' }, { complete: home4 });
	};
	
	var home4 = function() {
		//$('#left ul.built').delay(500).animate({ opacity: '1' }, { complete: home5 });
		$('#left ul.built').animate({ opacity: '1' }, { complete: home5 });
	};
	
	var home5 = function() {
		var midBarPos =	$('#mid_bar').position().top + $('#mid_bar').height();
		//$('#home-navbar').delay(500).animate({ top: 0 });
		$('#slide_wrapper').remove();
		$('#left_slides, #right_slides').show();
		$('#tagline').show().animate({}, { complete: home6 });
	};	
	
	var home6 = function() {
		$('#left ul').cycle({
			fx:	'fade',
			timeout: 3000,
			cleartypeNoBg: 'true'
		});
		
		$('#right ul').cycle({
			fx:	'fade',
			delay: 3000,
			timeout: 3000,
			cleartypeNoBg: 'true'
		});
	};


// Homepage Configuration
	/* Once homepage loads, run animation and make ajax call */
	if ( $('body').attr('id') == 'home' ) {
		$('<div id="loading_branding"></div>').appendTo('#wrapper').hide();
		$('<div id="left_slides"></div>').appendTo('#wrapper').hide();
		$('<div id="right_slides"></div>').appendTo('#wrapper').hide();
		//$('#tagline').hide();
		//$('#tagline, #ajax_loader').hide();
		//$('#ajax_loader, #loading_branding').delay(500).fadeIn();
		//$('#mid_bar').css('top','-300px');
		//$('#left ul').css('opacity','0');
		//$('#right').css('right','-580px');
		//$('#home-navbar').css('top','-47px');
		
		$(window).load(function() {
			$('#ajax_loader, #loading_branding').delay(2000).animate({ opacity: 0 }, { complete: home1 });
		});
	}


// Featured Slideshow

	$('#slideshow ul').cycle({
		fx: 'fade',
		timeout: 8000,
		speed: 700,
		prev: '#controls a.prev',
		next: '#controls a.next'
	});
		
	// Toggle slideshow animation when element is hovered
	$('#slideshow').hover(function() {
		$('#slideshow ul').cycle('pause');
	}, function() {
		$('#slideshow ul').cycle('resume');
	});


// featured filter function ;)
	$('#project_filter_form .visuallyhidden').hide();
	$('#project_filter_form_sector .visuallyhidden').hide();
	
	$('#listing-filter-service').change(function(e){
		window.location = location.protocol + "//" + location.host + location.pathname + "?listing-filter-service=" + $(this).val();
		
		//if($(this).val() == "all") location.search = "";
		
	});
	
	
	$('#listing-filter-sector').change(function(e){
		window.location = location.protocol + "//" + location.host + location.pathname + "?listing-filter-sector=" + $(this).val();
		
		//if($(this).val() == "all") location.search = "";
		
	});
	
// Featured Single Jquery Functions
	
	//hide it 
	
	
	if(!window.location.search.match(/\w/) && window.location.pathname.match(/\/ft_project\//)){
		$('#project_list').hide();
		$('#slider_close').hide();
		$('#project_filter_form fieldset label,#project_filter_form fieldset select').hide();
		$('#project_filter_form_sector fieldset label,#project_filter_form_sector fieldset select').hide();
	}
	
	$('#browse_toggle').click(function(){
		
		$('#project_list').slideToggle('fast', function() {
    		
    		if(this.style.display == "block"){
    			$('#project_filter_form fieldset label,#project_filter_form fieldset select,#slider_close').fadeIn('fast').show();
    			$('#project_filter_form_sector fieldset label,#project_filter_form_sector fieldset select').fadeIn('fast').show();
    		}else{
    			$('#project_filter_form fieldset label,#project_filter_form fieldset select').fadeOut('fast').hide();
    			$('#project_filter_form_sector fieldset label,#project_filter_form_sector fieldset select').fadeOut('fast').hide();
    			$('#slider_close').hide();
    			location.search = "";
    		}
    		
    	});
		
		return false;
	});


	$('#slider_close').click(function(){
		$('#project_list').slideToggle('fast', function() {
    		$('#slider_close').fadeOut('fast').hide();
  			$('#project_filter_form fieldset label,#project_filter_form fieldset select').fadeOut('fast').hide();
  			$('#project_filter_form_sector fieldset label,#project_filter_form_sector fieldset select').fadeOut('fast').hide();
  			
  			location.search = "";
		});
  		
  		return false;
	});

}); // End document ready
