// JavaScript Document
$().ready(function() {
	// Navigation for use with JQuery
	var path = location.pathname;
	if (path != '/gallery/') path += location.search;
	var isBlog = /\/imagine\/*/i;
	if( isBlog.test(path)) {
		path ='/imagine/';
	}
	var isVT = /\/gallery\/gallery_vt*/i;
	if( isVT.test(path)) {
		path ='/gallery/';
	}
	
	$('#nav li a[href="'+path+'"]').addClass('red');
	// Make the current page's nav selected *needs fixing*
	//$('#nav li a[href="'+path+'"]').addClass('red').parents('li.top a:first').addClass('red').parent().addClass('selected');
	
	// Sets/animates the navigation bar
	function setNav(me) {
		$("#nav li ul").queue( [ ] ).stop(true,true);
		// Set the default & initialized nav
		if(me == null) {
			me = $('#nav li a[href="'+path+'"]').parents('li.top');
		}
		// Make sure only top level li's get active
		if($(me).hasClass('top')) {
			//stop animation so queue doesn't 'overload'
			$("#nav li ul").stop().fadeOut(100);
			
			// Center the sub nav ul
			$this_width = $(me).children('ul').width();		
			$div_width  = $(me).children('ul').parents('div').width();
			$margin = ($div_width - $this_width) / 2;
			$(me).children('ul').css('left',$margin);
				
			$(me).children('ul').stop().fadeIn(600);
			
			// Toggle the active hover style
			$('#nav li').removeClass('hover');
			$(me).addClass('hover');
		}
	}	
	
	// Change the nav to show the selected catagory.
	$('#nav li').mouseenter(			   
		function(event) {
			me = this;
			setNav(me);
		}
	);
	
	// Set the nav to show the current selection
	$('#inner_bar').mouseleave(
		function(event) {
			setNav();
		}
	);
	
	// find the width of the top nav ul and center
	$width = 0;
	$('#nav li.top').each( function () {
		$width += $(this).outerWidth();   
	});
		
	$nav_width  = $('#nav').width();
	$margin = ($nav_width - $width) / 2;
	$('#nav').css('padding-left',$margin);
	
	// Initialize the nav
	setNav();
	
	
	var sub_path = location.pathname;
	var sub_path2 = '_';
	query = location.search.split('&');
	if(query[0]) {
		sub_path += query[0] + '&sec=0&img=0';
		sub_path2 = query[0] + '&' + query[1] + '&img=0';
	}
	
	if(sub_path == '/gallery/') {
		sub_path2 = '?pri=0&sec=0&img=0';
		sub_path += sub_path2;
	}
	$('#gallery_top a[href="'+sub_path+'"]').css('color','#7a3607');
	$('#second_nav a[href="'+sub_path2+'"]').css('color','#7a3607');
	
	
	
});