/**
 * Lohmann Navigation
 * author: Frank Matuse <frank@extrastyle.de>
 */

var t = null;
var currBoxes = new Array(0,0,0,0);
var overBoxes = new Array(0,0,0,0);
var overLink;

$(document).ready(function()
{
	// Collecting needed stuff
	var mn  = $('#main-nav');
	var mnl = $('#main-nav li');
	var mna = $('#main-nav a');
	// preparing the user's pre-selection
	$.prepareSelection();
	// applying initial nav-item-events
	$.applyEvents(mna);
});


$.applyEvents = function(elem) 
{
	elem.mouseover(function() {
		$.clearNavTimer();

		var thisID = <?php echo $this->req["itid"]?>;
		var l      = $(this);
		var p      = $(this).parent();
		var pLevel = parseInt($(this).parent().parent().parent().attr('class').replace('l',''));

		var catID = $(p).attr('id').replace('c','');

		forceReload = true;
		if(l.is('.loaded') || l.is('.childless')) forceReload = false;

		$('.l' + pLevel + ' li').removeClass('curr-trail');
		p.addClass('curr-trail');

		if(forceReload) {
			$.ajax({
				type: "GET",
				url: path.root + 'ajax.php',
				data: sn + '=' + sid + '&ajax_cat_load=1&itid=' + thisID + '&lang_short_set=' + langShort + '&parent_cat_id=' + catID + '&cat_design=0&parent=' + catID + '&class=l' + (pLevel+1),
				global: 'false',
				beforeSend: function() {
				},
				success: function(result) { 
					if(result != '') {
						l.addClass('loaded');
						$('#p' + catID).addClass('current');
						$('#main-nav').append(result);
						$.applyEvents($('#p' + catID + ' a'));
						$.moveIn(catID, pLevel);
					} else {
						$.moveOut(catID, pLevel);
						l.addClass('childless');
					}
				},
				error: function(result) { 
				}
			});
		} else {
			if($('#p' + catID).length) {
				$.moveIn(catID, pLevel);
			} else {
				$.moveOut(catID, pLevel);
			}
		}
	});

	elem.mouseout(function() {
		$.setNavTimer();
	});
}


$.moveIn = function(cID, pLevel) {
	
	aStart  = ((pLevel-1) * 190);
	aStop   = aStart + 190;

	if(overBoxes[pLevel] != cID) {
		$('#p' + cID).css({display: 'block'});
		$('#p' + cID).addClass('current');

		$('#p' + cID).css({left : aStart + 'px'}).animate({ 
			left: aStop + 'px'
		}, 300 );

		if(overBoxes[(pLevel)] != 0) {
			$.moveOut(overBoxes[pLevel], pLevel);
		}
	}
	overBoxes[pLevel] = cID;
}


$.moveOut = function(cID, pLevel) 
{
	for(j = (pLevel); j < 4; j++) {
		$.cleanUp('.l' + (j+2));
		overBoxes[j+1] = 0;
	}
	
	if(!$('#p' + cID).length) { 
		$.cleanUp('.l' + (pLevel + 1));
		overBoxes[pLevel] = 0;
	}
	else {
		$.cleanUp('#p' + cID);
		overBoxes[pLevel] = 0;
	}
}


$.prepareSelection = function() 
{
	$('li.selected').addClass('curr-trail');
	$('li.selected a').addClass('loaded');
	var s = $('.sel');
	for(var i = 0; i < s.length; i++) {
		currBoxes[i] = $(s[i]).attr('id').replace('p', '');
		overBoxes[i] = $(s[i]).attr('id').replace('p', '');
	}
}


$.cleanUp = function(item, visible) {
	$(item).removeClass('current');
	$(item + ' li').removeClass('curr-trail');
	if(undefined == visible) $(item).fadeOut('fast');
}

$.reset = function() {
	$.moveOut(1,1); 
	$.cleanUp('.l1',1);
	
	window.setTimeout(function(){ 
		for(var r = 1; r < currBoxes.length; r++) {
			if(currBoxes[r]) {
				$('#p' + currBoxes[r]).css({display: 'block'});
				$('#p' + currBoxes[r]).css({left : 0 + 'px'}).animate({ 
					left: (r*190) + 'px'
				}, 400 );
			}
			overBoxes[r] = currBoxes[r];
		}
		$('li.selected').addClass('curr-trail');
	}, 400);
}

$.setNavTimer   = function() {t = window.setTimeout(function(){ $.reset(); }, 4000);};
$.clearNavTimer = function() {window.clearTimeout(t);t = null;};

