
/*
 * @title The naviGator
 * @author jt4dma
 * @version 0.5 beta
 * @description Based on DrillDownMenu, flexible Lists
 *
 */

var naviGator = {
	config: { 
		container:			'#naviGator',
		wrapper:			'#naviGatorWrapper',
		activeItem:			'a.activeElement',
		cookieName:			'altNavi',
		maxLists:			2,
		speed:				600,
		delay:				300,
		width:				250,
		containerHeight:	200,
		spacing:			0
	},
	
	init: function() { 
		
		var $container = $(naviGator.config.container),
			$wrapper   = $(naviGator.config.wrapper),
			$ulItems   = $wrapper.find('li');
		
		$('#drillBackWrapper li').each(function(){
			$(this).find('span:last').addClass('inactive');
		});
		
		//Delegating Items with Events
		$ulItems.delegate('a', 'mouseover mouseout', naviGator.showSub);
		$container.delegate('#naviGatorWrapper', 'mouseleave', naviGator.autoMove);
		
		//Tool Box
		$container.delegate('#drillBack',  'click', naviGator.drillBack);
		$container.delegate('li.toggle', 'click', naviGator.drillToggle);
		
		//Check if there is an Element which is Active
		$('ul.firstLevel').show().addClass('activeList');
		
		naviGator.autoMove();
		naviGator.setCookie();
	},
	
	setCookie: function(){
		var cookieName  = naviGator.config.cookieName;
		var options     = { path: '/', expires: 90 };
		
		//Initial Status = open
		var status      = '';
		
		if($.cookie(cookieName) == null){
			$.cookie(cookieName, status, options);
		} else {
			var status  = $.cookie(cookieName);
			if(status == 'closed'){
				$('#drillToggle').click();
			}
		}
	},
	
	drillToggle: function(){
		var cookieName     = naviGator.config.cookieName;
		var todo           = $(this).attr('id');
		var wordingElement = $(this).find('.inactive');
		var container      = $(naviGator.config.wrapper);
		var todoAction     = (todo == 'drillHigher') ? 'higher' : 'closed' ;
		
		//toggling wording
		wordingElement.removeClass('inactive').siblings().addClass('inactive');

		//toggling what todo
		if(container.hasClass(todoAction)){
			if(todoAction == 'closed'){
				$('#drillBack, #drillHigher').show();
				$.cookie(cookieName, 'open');
			}
			container.removeClass(todoAction);
			container.parent().removeClass(todoAction);
		} else {
			if(todoAction == 'closed'){
				$('#drillBack, #drillHigher').hide();
				$.cookie(cookieName, 'closed');
			}
			container.addClass(todoAction);
			container.parent().addClass(todoAction)
		}
	},
	
	autoMove: function(){
		var $wrapper   = $(naviGator.config.wrapper);
		var activeItem = naviGator.getElement($wrapper, naviGator.config.activeItem);
		
		$.each(activeItem[0], function(i){
			$(activeItem[0][i]).mouseover();
		});
	},
	
	drillBack: function(e){
		e.preventDefault();
		var $wrapper = $(naviGator.config.wrapper);

		//The drillBack Item 
		drillBackItem  = naviGator.getElement($wrapper, 'ul.inactiveList');
		drillBackWidth = drillBackItem[0].width(); 
		
		$(drillBackItem[0][(drillBackItem[1] - 1)]).animate({
			left: '+=' + drillBackWidth
		}, 300).removeClass('inactiveList').addClass('activeList');
		
		//Item to Remove, when drillBack is done (Last Element of activeLists)
		var activeLists = naviGator.getElement($wrapper, 'ul.activeList');
		if(activeLists[1] > 2){
			$(activeLists[0][(activeLists[1] - 1)]).removeClass('activeList');
		}
	},
	
	showSub: function(e){
		
		//It must be an Object...
		var $activeLink     = $(this),
			$orgList        = $activeLink.parent().parent(),
			$subList        = $activeLink.next(),
			eventType       = e.type;
		
		if(eventType == 'mouseover'){
			$activeLink.addClass('activeItem');
		} else if( eventType == 'mouseout'){
			$activeLink.removeClass('activeItem');
		}

		var $wrapper     = $(naviGator.config.wrapper),
			visibleLists = naviGator.getElement($wrapper, 'ul.activeList');
		//Condition to show not more than X Lists
		if($orgList.hasClass('activeList') && $subList.is('ul')){
			var scrollItTop  = false,
			    orgListWidth = $orgList.width();
			
			//Visible are more than the Limit
			if(visibleLists[1] > naviGator.config.maxLists){
				naviGator.moveBack(visibleLists[0][0], '-=' + orgListWidth);
				scrollItTop = true;
			}
			
			//Visibile are equal with the Limit
			if(visibleLists[1] == naviGator.config.maxLists){
				$orgList.find('.activeItem').removeClass('activeItem');
				//check if already one exists on this level
				$orgList
					.find('ul.activeList')
					.removeClass('activeList');

			}
			naviGator.moveForward($subList, orgListWidth, scrollItTop);
			$activeLink.addClass('activeItem');
		} else if (visibleLists[0][0] == $orgList[0]) {
			$orgList
				.find('ul.activeList')
				.removeClass('activeList');
		}

	},
	
	moveForward: function(item, width, scrollItTop){
		var $wrapper  = $(naviGator.config.wrapper);
		
		$(item)
			.css({left: (width + naviGator.config.spacing), top: $wrapper.scrollTop()})
			.addClass('activeList');
			
		if(scrollItTop){
			$(item)
				.parent()
				.parent()
				.css('top', '0');
				
			$wrapper.scrollTop(0);
		}
	},
	
	moveBack: function(item, width){
		$(item)
			.stop(true, false)
			.removeClass('activeList')
			.animate({left: width}, naviGator.config.speed)
			.addClass("inactiveList");
	},
	
	getElement: function(container, toFind){
		var elements = container.find(toFind),
		    elLength = elements.length,
		    elementData  = [elements, elLength];
		
		return elementData;
	}
};

jQuery.cookie = function (key, value, options) {

	// key and value given, set cookie...
	if (arguments.length > 1 && (value === null || typeof value !== "object")) {
		options = jQuery.extend({}, options);
	
		if (value === null) {
			options.expires = -1;
		}
	
		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
	
		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? String(value) : encodeURIComponent(String(value)),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}
	
	// key and possibly options given, get cookie...
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


$(document).ready(function() {	
	if($('#naviGator').length){
		naviGator.init(); 
	}
});
