/* ** Bootstrap ** */

   $(function() {
	$('#menu_content > ul').superfish({
						speed: 'normal',
						animation: {opacity:'show', height:'show'},
						autoArrows:  false,
			            dropShadows: false });

	carLayerMenu();
   });

	function carLayerMenu() {
		var $menuoptions = $("#line_menu > div");
		var $car_layers = $("div.layer-cars");
        $menuoptions.css('cursor', 'pointer');
		// options
		var distance = 0;
		var time = 450;
		var hideDelay = 500;

		$car_layers.each(function() {
			var $div = $(this);
			var $lista = $div.find('ul');
			var $lis = $lista.find('li');
			var lon = $lis.length;
			var tmplon, prev;
			var mar;
			var $newlis;
			if (lon > 7) {
				tmplon = 0;
				prev = 0;
				do {
					tmplon +=  6;
					tmplon = (tmplon<=lon)?tmplon:lon;

					$newlis = $('<ul title="cont-' + tmplon + '"></ul>');
					for (var i=prev; i<tmplon; i++) {
						$newlis.append('<li>' + $lis.eq(i).html() + '</li>');
						$lis.eq(i).remove();
					}
					prev = tmplon;
					$div.find('div').append($newlis);
				} while (tmplon < lon);

				lon = 6;
				
			}
			mar = lon * 149 + 24;
			mar = (960 - mar) / 2;
			$div.find('div').css('margin-left', mar);
		});

		var hideDelayTimer = null;
		$menuoptions.each(function() {

			var trigger = $(this);
			var popup;
			var hrefDATA = trigger.attr('href');
			if (!trigger.hasClass('no_layer'))
				popup = $(hrefDATA);
			else
				popup = $('#emptylist');

			var objs = [trigger.get(0), popup.get(0)];
			trigger.data('beingShown', false);
			trigger.data('shown', false);

			$(objs).mouseover(function () {
				// tracker
				var beingShown = trigger.data('beingShown');
				var shown = trigger.data('shown');
				// stops the hide event if we move from the trigger to the popup element
				if (hideDelayTimer) {
					clearTimeout(hideDelayTimer);
				}
				// Reset styles
				$car_layers.not(popup).css({
					display: 'none',
					opacity: 0
				});
				// Reset parameters, reset buttons state
				$menuoptions.not(trigger).data('beingShown', false).data('shown', false).removeClass('selected');
				// don't trigger the animation again if we're being shown, or already visible
				if (beingShown || shown) {
					return;
				} else {
					beingShown = true;
					trigger.data('beingShown', beingShown).addClass('selected');

					if (trigger.hasClass('no_layer')) {
						beingShown = false;
						shown = true;
						trigger.data('shown', true);
						trigger.data('beingShown', false);
					}
					else {
						// reset position of popup box
						popup.css({
							display: 'block' // brings the popup back in to view
						}).animate({
//							top: '-=' + distance + 'px',
							opacity: 1
						}, time, 'swing', function() {
							// once the animation is complete, set the tracker variables
							beingShown = false;
							shown = true;
							trigger.data('shown', true);
							trigger.data('beingShown', false);
						});
					}
				}
			}).mouseout(function () {
				var x, texto;
				// reset the timer if we get fired again - avoids double animations
				if (hideDelayTimer) clearTimeout(hideDelayTimer);

				// store the timer so that it can be cleared in the mouseover if required
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					popup.animate({
//					top: '-=' + distance + 'px',
					opacity: 0
					}, time, 'swing', function () {
						// once the animate is complete, set the tracker variables
						shown = false;
						trigger.data('shown', false);
						// hide the popup entirely after the effect (opacity alone doesn't do the job)
						popup.css('display', 'none');
						trigger.removeClass('selected');

					});
				}, hideDelay);
			});
		});

		$menuoptions.filter('.no_layer').click(function () {
			window.open($(this).attr('href'));
			return false;
		});
	}
