// JavaScript Document
var hoveringOn = '';

function isiDevice(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPad") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

jQuery(document).ready(function($) {
	/*$('.menutop').parent().click(function(){
	 window.location = $(this).find('.menutop').attr('data-url');
	 });*/

	if(!isiDevice()) {
	$('.menutop').live('mouseenter',
			function() {
				$(this).addClass('menu1Open');
				$('.submenu').hide();
				$("#" + this.id + "-Submenu").show();
			}).live('mouseleave',
			function() {
				setTimeout("hideSubmenu('" + this.id + "')", 250);
			}).mousemove(function() {
				$(this).addClass('menu1Open');
				$('.submenu').hide();
				$("#" + this.id + "-Submenu").show();
			});
	}
	else {
		$('#menu-products').live('mouseenter',
			function() {
				$(this).addClass('menu1Open');
				$('.submenu').hide();
				$("#" + this.id + "-Submenu").show();
			}).live('mouseleave',
			function() {
				setTimeout("hideSubmenu('" + this.id + "')", 250);
			}).mousemove(function() {
				$(this).addClass('menu1Open');
				$('.submenu').hide();
				$("#" + this.id + "-Submenu").show();
			});
	}
	/*$('.link-container').live('touchmove touchstart touchend', function(e) {
		window.location = $(this).attr('data-url');
	});*/
	$('a').live('touchmove touchstart touchend', function(e) {
		window.location = $(this).attr('href');
	});

	/*$("a").each(function() { // have to use an `each` here - either a jQuery `each` or a `for(...)` loop
	 var onClick; // this will be a function
	 var firstClick = function() {
	 onClick = secondClick;
	 return false;
	 };
	 var secondClick = function() {
	 onClick = firstClick;
	 return true;
	 };
	 onClick = firstClick;
	 $(this).click(function() {
	 return onClick();
	 });
	 });*/


	/*
	 $('.menutop').mouseout(function(){
	 setTimeout("hideSubmenu('"+this.id+"')",250);
	 }).live('mouseover mousemove',function(){
	 $(this).addClass('menu1Open');
	 $('.submenu').hide();
	 $("#"+this.id+"-Submenu").show();
	 });

	 */

	$submenuTop = 0;
	$('.submenu').hover(function() {
		hoveringOn = "#" + this.id;
		debug('submenuHover::hoveringOn = ' + hoveringOn);
		$('.submenu').hide();
		$(this).show();
		$(this).prev().addClass('subhover');
	}, function() {
		setTimeout("unhoverSubmenu('#" + this.id + "')", 1);
	})
		/*.each(function(i){
		 $(this).css('top',$submenuTop+'px');
		 $submenuTop += $(this).prev().height();
		 })
		 */;

	//$(".content-section:containts(&bull;)").
	if ($.browser.msie && $.browser.version < 8) {
		$('.submenu').each(function() {
			if ($(this).children().length == 0)
				$(this).remove();
		});
	}

	if ($.browser.mozilla) {
		setTimeout("mozzMenuFix()", 1);
		setTimeout("mozzMenuFix()", 50);
		setTimeout("mozzMenuFix()", 250);
		setTimeout("mozzMenuFix()", 500);
	}
});

function mozzMenuFix() {
	debug("mozzMenuFix()::running");
	jQuery(".menu1").each(function(i) {
		jQuery(this).find('a').each(function(i) {
			if (jQuery(this).html() == '')
				jQuery(this).remove();
		});
	});
}

function hideSubmenu(menuId) {
	submenuId = "#" + menuId + "-Submenu";
	debug('hideSubmenu::hoveringOn = ' + hoveringOn + ', submenuId = ' + submenuId + ', $(submenuId).css("display") = ' + jQuery(submenuId).css('display'));
	if (hoveringOn == '' && hoveringOn != submenuId && jQuery(submenuId).css('display') == 'block') {
		jQuery("#" + menuId).removeClass('menu1Open');
		jQuery(submenuId).hide();

	}
}

function unhoverSubmenu(submenuId) {
	/*

	 if(hoveringOn == '' && hoveringOn != submenuId && jQuery(submenuId).css('display') == 'block') {
	 */
	debug('unhoverSubmenu::hoveringOn = ' + hoveringOn + ', submenuId = ' + submenuId + ', $(submenuId).css("display") = ' + jQuery(submenuId).css('display'));
	//	if(!jQuery(submenuId).prev().hasClass('subhover')) {
	hoveringOn = '';
	jQuery(submenuId).hide();
	jQuery(submenuId).prev().removeClass('subhover');
	//	}
	/*
	 }
	 else
	 setTimeout("unhoverSubmenu('"+submenuId+"')",50);
	 */
}
