$(function () {
	
	/* Blur Links */
	$('a').click(function(){
		$(this).blur();
	});
	
	/* Primary nav */
	jQuery('ul#primary_nav')
	.supersubs({ 
		minWidth:    9,   // minimum width of sub-menus in em units 
		maxWidth:    15,   // maximum width of sub-menus in em units 
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family 
	})
	.superfish({
		delay:333,
		hoverClass:'sfHover',
		//pathClass:'current',
		//pathLevels:1,
		autoArrows:false,
		animation: {opacity:'show', height:'show'},
		speed:'fast',
		dropShadows:true
	});
	
	// remove 2+ levels from primary nav as fallback
	$('#primary_nav li.current:first ul, #primary_nav li.parent_current:first ul').remove();

	// tweak secondary nav levels
	if($('.secondary_nav li.current').length == 0) {
		$('.secondary_nav li:first').addClass('current');
	}

	/* Login Popup */
	$('a#login_button').click(function(e) {
		e.preventDefault();
		var p = $('a#login_button');
		var pos = p.position();
		var width = $(this).width();
		if ( $('#login_popup').is(':visible') == false) {
			$('a#login_button').addClass('login_button_active');
			$('#login_popup').fadeIn('fast');	 
		} else {
			$('#login_popup').fadeOut('fast', function() {
				$('a#login_button').removeClass('login_button_active');
			});
		};
	});
	
	/* Click outside login popup to close */
	$(document).click(function(e){
		if(!$(e.target).filter('a#login_button').length && !$(e.target).parents().filter('#login_popup').length) {
			if ( $('#login_popup').is(':visible') == true) {
				$('#login_popup').fadeOut('fast', function() {
					$('a#login_button').removeClass('login_button_active');
				});
				
			}
		}
	});
	
	/* Show/hide password field */
	$('#password_clear').show();
	$('#password').hide();
	$('#password_clear').focus(function() {
	    $('#password_clear').hide();
	    $('#password').show();
	    $('#password').focus();
	});
	$('#password').blur(function() {
	    if($('#password').val() == '') {
	        $('#password_clear').show();
	        $('#password').hide();
	    }
	});
	$('#username').focus(function() {
		$('#username').val('');
	});
	$('#username').blur(function() {
	    if($('#username').val() == '') {
	        $('#username').val('Username')
	    }
	});
	
	//clear search on focus
	$('div#quicksearch #keywords').focus(function() {
		if($('div#quicksearch #keywords').val() == 'Search for keyword, name, or event') {
			$('div#quicksearch #keywords').val('');
		}
	});
	$('div#quicksearch #keywords').blur(function() {
	    if($('div#quicksearch #keywords').val() == '') {
	        $('div#quicksearch #keywords').val('Search for keyword, name, or event')
	    }
	});
	
	// home page slide show
	if ( $("div#slide_inner").length > 0 ) {
		$('div#slide_inner').cycle({ 
			fx: 'fade',
			timeout: 5000,
			speed: 1000,
			pause: false,  
			next: '#next_slide', 
			prev: '#prev_slide'
			/*delay: 10000*/
		});
	}
	var slideConfig = {    
		over: makeTall, 
		timeout: 700,   
		out: makeShort 
	};
	$('div.slide a').hoverIntent(slideConfig);
	function makeTall() {
		$('#slide_inner').cycle('pause');
		$(this).find('span.slide_text').slideDown(800, 'easeOutBounce');
	}; 
	function makeShort() {
		$('span.slide_text', this).slideUp(300, function(){
			$('#slide_inner').cycle('resume'); 
		});
	};
	
	// widget slideshows
	if ( $("div.block_gallery").length > 0 ) {
		$('div.block_gallery').cycle({ 
			fx: 'fade',
			timeout: 3000,
			pause: true,
			height: "160px",
			containerResize:0
		});
	}
	
	// insert back to top link into very long content
	var showControl = false;
	if(showControl == true)
	{
		showHideControl();
		$(window).resize(function(){
			showHideControl();
		});
	}        
	function showHideControl() {
		var ch = $("#content").height();
		if (ch < 500) {
			$(".entry a.back_top").remove();
		} else {
			if(!$('.entry a.back_top').length) {
				$('.entry').append('<a href="#header" title="Back to Top" class="back_top">Back to Top</a>');
			}
		}
	}
	
	// adjust footer
	var min = 20
	var fh = $('#footer').outerHeight();
	if(fh < min) fh = min;
	$('.content').css('padding-bottom', '30px');
	$('#footer').css('margin-top', '-'+fh+'px');
	
	// product category menu
	if ( $("#product_category_menu").length > 0 ) {
		if ( $("#product_category_menu li.active_parent").length > 0 ) {
			
			$("#product_category_menu li.active_parent").parents('li').addClass('active_parent');
		}
	}
	


});
