//Dropdown Functions
var menu_timeout = 100;

function toggle_menu()
{
	subnav = $(this).find('ul:first');
	subnav.stopTime();
	
	//Check if the manu has the "on" class.
	if(subnav.is('.on'))
	{
		//Subnav is currently on, start the timer to close the subnav.
		subnav.oneTime(menu_timeout, function() {
			if($(this).is('.on'))
			{	
		    	$(this).removeClass("on");
			}
	    });
	}
	else if (subnav.is(':not(.on)'))
	{
		//Subnav menu is not on, turn it on.
		subnav.addClass("on");
	}
}

$(document).ready(function() {
						   
	//Clear the search box the first time it is clicked.
	$("#search-box").one("click", function() {
		$("#search-box").val('');
	});
	
	/*if($('.refreshcaptcha').length){
		setTimeout(function(){
			console.log('Refreshing Captcha');
			$('.refreshcaptcha').click();
		},1000);
	}*/

	//Apply Dropdowns
//	$('#main-nav li').bind("mouseenter", toggle_menu);
//	$('#main-nav li').bind("mouseleave", toggle_menu);
	
	// Enable LightBox
	$('a[rel*="lightbox"]').lightBox({
		imageLoading: '/images/lightbox-btn-loading.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif'
	});
	
	//IE CSS Corrections.
	$('#top-nav #left-nav #main-navigation ul li:last-child').css({'padding-right':0, 'margin-right':0, 'background':0});
	$('#top-nav #right-nav #secondary-navigation ul li:last-child').css({'padding-right':0, 'margin-right':0, 'background':0});
	$('div#footer div#foot-nav ul li:last-child').css({'border-right':'none'});
	$('div.dated-item ul.sub-headings li:first-child').css({'margin-left':'0px'});
	$('div.dated-item ul.sub-headings li:last-child').css({'border':'none','margin-bottom':'0px'});
	//Last item in navigation menus should have 10px bottom margin.
	$('#top-nav #left-nav #main-navigation ul li ul li:last-child a').css({'margin-bottom':'10px'});
	$('ul.breadcrumb-nav li:last-child span.separator').css({'display':'none'});
	
	
	$('form[action="/leads/contact-interest"]').validate({
		errorElement: "em",
		errorClass: "invalid",
		rules: {
			first_name	:	{ required:true },
			last_name	:	{ required:true },
			company		:	{ required:true },
			job_title	:	{ required:true },
			email		:	{ required:true, email:true },
			phone		:	{ required:true },
			captcha_code:	{ required:true }
		},
		messages: {
			first_name	:	{ required:'Please enter your First Name.' },
			last_name	:	{ required:'Please enter your Last Name.' },
			company		:	{ required:'Please enter your Company.' },
			job_title	:	{ required:'Please enter your Job Title.' },
			email		:	{ required:'Please enter your Email Address.', email: 'Please enter a valid email address.' },
			phone		:	{ required:'Please enter your Phone Number.' },
			captcha_code:	{ required:'Please enter the letters in the image.' }
		}
	});
	
	
	var page_title_text = $('#main-content-box .box-header span').text();
	if(stristr(page_title_text,'Better Time') !== false){
		$('#main-content-box .box-header span').html(str_replace(' - ',' -<br />',page_title_text));
	} 
	
});

function topSearch()
{
	$('#top-search-form').submit();
}
