/** define the variables used **/
var timeout         = 500;
var closetimer 		= 0;
var ddmenuitem      = 0;

/**** open a submenu ****/
function jsddm_open() {

	/** if an a timer was running stop it **/
	jsddm_canceltimer();

	/** hide the current page's submenu **/
	$('#jsddm li.current_page_item').find('ul').eq(0).css('visibility', 'hidden');

	/** if an item was being moused over **/
	if (ddmenuitem) {

		/** and that moused over item is different from the one that is currently being moused over **/
		if (ddmenuitem != $(this)) {

			/** hide the previous submenu before showing the new one **/
			ddmenuitem.css('visibility', 'hidden');
		}

	}

	/** show the currently moused over submenu, and save the menu as a variable **/
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
	
}

/**** close a submenu ****/
function jsddm_close() {

	/** if an element in the submenu is being moused over **/
	if(!($('#jsddm li.page-item a').hover())) {

		/** keep the menu open **/
		$('#jsddm li.current_page_item').trigger('mouseover');

	/** otherwise **/
	} else {

		/** close the submenu **/
		if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');

		/** if the original submenu is for an inner page, not one of the top-level pages, make it visible here **/
		ddmenuitem = $('#jsddm li.current_page_parent').find('ul').eq(0).css('visibility', 'visible');
		
	}
}

/**** run a timer to close the submenu ****/
function jsddm_timer() {

	/** when the submenu is moused off, run a function that ... **/
	closetimer = window.setTimeout(function() {

		/** closes the current submenu **/
		jsddm_close();

		/** opens the original submenu **/
		$('#jsddm li.current_page_item').find('ul').eq(0).css('visibility', 'visible');

		/** and resets the menu variables **/
		closetimer = 0;
		ddemenuitem = 0;

	}, timeout);
}

/**** cancel the timer created by the above function **/
function jsddm_canceltimer() { 	/** if the current times needs to stop **/
	
	/** check if a timer exists **/
	if(closetimer) {
		
		/** stop it and reset the timer variable **/
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(document).ready(function() {

	/** opens the submenu for the moused over item **/
	$('#jsddm > li').bind('mouseover', jsddm_open);

	/** closes the submenu for the moused over item **/
	$('#jsddm > li').bind('mouseout',  jsddm_timer);

	/** prevent the original submenu from hiding on its own **/
	$('#jsddm li.current_page_item').unbind('mouseout');

	/** show the current page's submenu **/
	$('#jsddm li.current_page_item, #jsddm li.current_parent_item').trigger('mouseover');


	/****** mouseovers included here for one less http request ******/
	$('#homeblock_1 a img, #homeblock_2 a img, #homeblock_3 a img, #homeblock_4 a img').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('rel'));
		$(this).attr('rel', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('rel'));
		$(this).attr('rel', currentImg);
	});
	$('#homeblock_5 a img, #homeblock_6 a img').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('rel'));
		$(this).attr('rel', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('rel'));
		$(this).attr('rel', currentImg);
	});
	$('#searchform input#searchsubmit, #searchform2 input#searchsubmit').hover(function() {
		$(this).css({ background: $(this).css("background-image").replace("home_page","rollover_images") + ' no-repeat' });
	}, function() {
		$(this).css({ background: $(this).css("background-image").replace("rollover_images","home_page") + ' no-repeat' });
	});
	$('.gform_footer input').hover(function() {
		$(this).attr('src', $(this).attr("src").replace("home_page","rollover_images"));
	}, function() {
		$(this).attr('src', $(this).attr("src").replace("rollover_images","home_page"));
	});
	$('#acupuncture_readmore1, #acupuncture_readmore2').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('rel'));
		$(this).attr('rel', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('rel'));
		$(this).attr('rel', currentImg);
	});


	/****** preloader for mouseovers ******/
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/chiro_treatments-hover.jpg')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/wellness-hover.jpg')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/new_to_chiro-hover.jpg')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/acupuncture-hover.jpg')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/pt_sports-hover.jpg')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/meet_doc-hover.jpg')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/read-more2.png')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/search_btn.png')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });
	$('<img />')
		.attr('src', js_site_url+'/wp-content/themes/ActiveHealth/images/rollover_images/submit_btn.png')
		.css({'width': '1', 'height': '1'})
		.load(function(){
		$('#jq_preload').append( $(this) );
    });

}); 
