

$(document).ready(function(){

	var $initialActiveList;
	var $timeOuter;
	var $timeOuter2;
	var firstTime;	 
	var slidenext = false;
	var menuslide = true;
	var outsidemenu = false;
	  
	$(window).resize(function () {
		snWidth = $(window).width()	
		$(".subnavcont").css("width", snWidth);
    });
	
	snWidth = $(window).width()	
	$(".subnavcont").css("width", snWidth);
	
	$('.bigSubNavie > ul > li').each(function (index) {    
		$(this).find(".subnavcont").css({ 'visibility': 'hidden', 'display': 'block' });
		optionHeight = ($(this).find('.subnavcont li a').height())+53;
		
		var twoRows = $(this).find('li').size();
		$(this).find(".subnavcont").css({ 'visibility': 'visible', 'display': 'none' });
		$(this).find(".subnavh").height(optionHeight);
		$(this).find(".subnavcont").height(optionHeight);
		$(this).find(".subnav").height(optionHeight);
		$(this).find(".subnav").css("top", -optionHeight);
		
 		if ($(this).hasClass("open")) {
			$initialActiveList = $(this);
			setInitialActive();
		}
	});   
	
	
	$(".bigSubNav ul:first > li").hoverIntent(function () { 					
		clearTimeout($timeOuter);
		
		// For first time use to activate selected item
		if (firstTime){
			firstTime = false;
			if($(this).hasClass("open") && $(this).find('.subnavcont').is(':visible')) {
			}else {
				
				if(menuslide || outsidemenu) {
					$(".bigSubNav ul:first > li.open").removeClass('active');
					var maxH = 0;
					maxH = ($(".bigSubNav ul:first > li.open").find('.subnavcont li a').height())+35;
					$(".bigSubNav ul:first > li.open").find('.subnav').stop().animate({ 'top': -maxH + 'px' }, 400);
					$(".bigSubNav ul:first > li").find(".subnavh").stop(false, true).slideUp(400, function () {
						$("#cont").toggleClass("test");
					});
					$(".bigSubNav ul:first > li").find(".subnavcont").delay(400).hide(0);	
				} else {
					$(".bigSubNav ul:first > li.open").find(".subnavcont").fadeOut(400, function () {
						$(".bigSubNav ul:first > li.open").find(".subnav").css({ 'top': '-1000px' });
					});
					menuslide = false;					
				}
			}
		}
	 	
		// On hover we first remove any possible active class on another element
		$(".bigSubNav ul:first > li").removeClass('active');
		
		// We add the active class to our hovered element
		if($(this).hasClass('last')){
		}else {
			$(this).addClass('active');
			$(".subnavbackground").css("border-bottom", "1px solid #cdcdcd");
		}
		// We only want to open the submenus if they actually exist
		if ($(this).find(".subnavcont").size() > 0)
		{	
			// Set some CSS properties
			$(".bigSubNav").css("background", "url(http://www.creone.se/images/bigsubnavbg.png) repeat-x");  
			
			// If we came from support, slidenext will be activated.
			if(menuslide || slidenext) {
				//SLIDE DOWN
				slidenext = false;
				openSlider($(this));
			} else {
				//FADE IN
				$(this).find(".subnav").css({ 'top': '0px' });
				$(this).find(".subnavcont").fadeIn(400);
			}
		}
		else
			{
				slidenext = true;
				closeSlider($(this));
			}
			menuslide = false;
	}, 
	function () {	
		if($(this).hasClass("open") && $(this).find('.subnavcont').is(':visible')) {
			firstTime = true;
			$initialActiveList = $(this);
			$timeOuter = setTimeout(function() { setInitialActive; }, 500);
		}else {
			$(this).removeClass('active');
			
			var timeout2function = hoverOutEvents($(this));
			$timeOuter2 = setTimeout(function() { timeout2function; }, 100);
		}
	});	
	
	function hoverOutEvents(jThis) {
		
		if(menuslide && outsidemenu) {
			var maxH = 0;
			maxH = (jThis.find('.subnavcont li a').height())+35;
			jThis.find('.subnav').stop().animate({ 'top': -maxH + 'px' }, 0);
			$(".subnavh").stop(false, true).slideUp(0, function () {
				$("#cont").toggleClass("test");
				menuslide = true;
			});
			$(".bigSubNav ul:first > li").find(".subnavcont").delay(0).hide(0);
			$(".subnavbackground").stop().animate({ 'height': '0px' },0, function() {
				$(".subnavbackground").css("border", "none");
				if ($(this).hasClass("open")) {
				}else {
					$(".bigSubNav").css("background", "none"); 	
				}
			});
		}else if (outsidemenu) {
			if(slidenext){
				var maxH = 0;
				maxH = (jThis.find('.subnavcont li a').height())+35;
				jThis.find('.subnav').stop().animate({ 'top': -maxH + 'px' }, 0);
				$(".subnavh").stop(false, true).slideUp(0, function () {
					$("#cont").toggleClass("test");
					menuslide = true;
				});
				$(".bigSubNav ul:first > li").find(".subnavcont").delay(0).hide(0);
				$(".subnavbackground").stop().animate({ 'height': '0px' }, 0, function() {
					$(".subnavbackground").css("border", "none");
					if ($(this).hasClass("open")) {
					}else {
						$(".bigSubNav").css("background", "none"); 	
					}
				});
			}else {
				if ($('.bigSubNav ul li').hasClass('open')) {
					jThis.find(".subnavcont").fadeOut(400, function() {
						jThis.find(".subnav").css({ 'top': '-1000px' });
					});	
				}else {
					closeSlider(jThis);
				}
			}
		} else {
			jThis.find(".subnavcont").fadeOut(400, function() {
				jThis.find(".subnav").css({ 'top': '-1000px' });
			});
			menuslide = false;
		}
		$timeOuter = setTimeout(setInitialActive, 500);
		
		clearTimeout($timeOuter2);
		
	}
	
	function openSlider(jThis)
	{
		var maxH = 0;
		maxH = (jThis.find(".subnavcont").height())-22;
		if (maxH == null) {
			maxH = 0;
		}
		
		jThis.find(".subnavcont").show(0, function () {	
			$(".subnavbackground").stop().animate({ 'height': maxH + 'px' }, 400);
			$("#cont").toggleClass("test");
			jThis.find(".subnav").stop().animate({ 'top': '0px' }, 400);
			$(".subnavh").stop(true, true).slideDown(400).show(0, function () {
				$("#cont").toggleClass("test");
				menuslide = false;
			});
		});
	}
	
	function closeSlider(jThis)
	{
	
		var maxH = 0;
		maxH = (jThis.find('.subnavcont li a').height())+35;
		jThis.find('.subnav').stop().animate({ 'top': -maxH + 'px' }, 400);
		$(".subnavh").stop(false, true).slideUp(400, function () {
			$("#cont").toggleClass("test");
			menuslide = true;
		});
		
		$(".bigSubNav ul:first > li").find(".subnavcont").delay(400).hide(0);
		$(".subnavbackground").stop().animate({ 'height': '0px' }, 400, function() {
			$(".subnavbackground").css("border", "none");
			if ($(this).hasClass("open")) {
			}else {
				$(".bigSubNav").css("background", "none"); 	
			}
			if(slidenext){
				if(jThis.hasClass('open')){			
				}else {
					$(".bigSubNav").css("background", "#fff"); 
					if (!jThis.hasClass('active'))
						$(".subnavbackground").css("border-bottom", "1px solid #fff");
					else
						$(".subnavbackground").css("border-bottom", "1px solid #cdcdcd");
				}
			}
		});
	}
	function closeSliderWithNoElement()
	{
	
		var maxH = 0;
		$(".subnavh").stop(false, true).slideUp(400, function () {
			$("#cont").toggleClass("test");
			menuslide = true;
		});
		
		$(".bigSubNav ul:first > li").find(".subnavcont").delay(400).hide(0);
		$(".subnavbackground").stop().animate({ 'height': '0px' }, 400, function() {
			$(".subnavbackground").css("border", "none");
			if ($(this).hasClass("open")) {
			}else {
				$(".bigSubNav").css("background", "none"); 	
			}
		});
	}
	
	$(".bigSubNav ul:first").hover(function () {
		outsidemenu = false;
	}, 
	function () {
		outsidemenu = true;
	});	
	
	function setInitialActive()
	{
		if ($initialActiveList != null)
		{
			firstTime = true;
			$(".bigSubNav ul:first > li").removeClass('active');
			$initialActiveList.addClass('active');
			
			if ($initialActiveList.find(".subnavcont").size() > 0)
			{	
				if(menuslide){
					$initialActiveList.find(".subnavcont").show(0, function () {	
						$(".bigSubNav").css("background", "url(http://www.creone.se/images/bigsubnavbg.png) repeat-x"); 
						var maxH = 0;
						maxH = ($(".bigSubNav ul:first > li.open").find(".subnavcont").height())-22;
						if (maxH == null) {
							maxH = 0;
						}
						$(".subnavbackground").css("display", "block");
						$(".subnavbackground").css("border-bottom", "1px solid #cdcdcd");
						$initialActiveList.find(".subnav").stop().animate({ 'top': '0px' }, 400);
						$(".subnavbackground").stop().animate({ 'height': maxH + 'px' }, 400);
						$initialActiveList.parent().find(".subnavh").stop(true, true).slideDown(400).show(0, function () {
							$("#cont").toggleClass("test");
						});
						menuslide=false;
					});
				}else { 
					$(".bigSubNav ul:first > li.open").find(".subnav").css({ 'top': '0px' });
					$(".bigSubNav ul:first > li.open").find(".subnavcont").fadeIn(400);	
					menuslide = false;
				}
			}
			else
			{
				closeSlider($initialActiveList);
				slidenext = true;
			}
		}
		else{
			if (outsidemenu) 
			{
				closeSliderWithNoElement();
			}
		}
	}
});
