// Slip-N-Slide v0.1
// 1/8/10

/*function alert(what){
	$('#dbugger').html(what);
}
var mt_path;
*/
var holder=new Object();
(function($){
	$.fn.slipnslide = function(options) {
		var defaults = {
			speed:200,
			autoHide:true,
			dropDown:false,
			memory:false
		};
		
		
		var options = $.extend(defaults, options);
		$(this).find('li:has(ul)').addClass('clickSize');
		$(this).find('a').addClass('clickSize');
		return this.each(function() {
			
			
			var loc= $(this).offset();
			var momma=$(this);
			var menus=$(this).find('ul');
			menus.addClass('slipnslide_menu');
			var home=$(this).find('ul:first');
			home.addClass('slipnslide_home');
			home.css('display','block');
			var depth=0;
			var current=momma.find('.slipnslide_menu:first');
			
			var path=new Array();
			path[0]=new Object({'path':current,'title':'','height':current.height()});
			
			checkDepth();
			$(this).find('li ul').parent().addClass('hasChildren');
			
			$(this).find('li:has(ul)').click(function(){
				if($(this).children().length>0){
					var title=$(this).html().split('<')[0];
					momma.find('span .parent').html(title);
					current=$(this).find("ul:first");
					depth++;
					path[depth]=new Object({'path':current,'title':title,'height':current.height()});
					$(this).find("ul:first").animate({
						marginLeft:'0px'
					},options.speed,function(){
					checkDepth();});
					
				}
				
				return(false);
			});
			$(this).find('.home').click(function(){
				goHome();
				return(false);
			});
			
			function goHome(){
				for(var i=0;i!=path.length;i++){
					$(momma).find('.parent').trigger('click');
				}
		
				momma.find('span .parent').html('');
				checkDepth();
			};

			
			if (options.memory){
				var pathtaken=($(this).find('.path'));
				for(var i=0;i!=pathtaken.length;i++){
					$(pathtaken[i]).parent().trigger('click');
				}
				checkDepth();
				holder.path=path;
			}

			$(this).bind('resetMenu',function(){
				goHome();
			});
			
			
			
			$(this).find('a').click(function(){
				window.location=$(this).attr('href');
				return(false);
			});
			
			$(this).find('.parent').click(function(){
				
				if (depth>0){
					current.animate({
						marginLeft:'220px'
					},options.speed);
					
					depth--;
					current=path[depth].path;
					momma.find('span .parent').html(path[depth].title);
					checkDepth();
					
				}
				return(false);
			});
			
			function checkDepth(){
				
					momma.find('.slipnslide_home').animate({height:(path[depth].height)},100);
				
				if(options.autoHide){
					if (depth>0){
						momma.find('span .parent').slideDown(options.speed);
						momma.find('.miniMenu').slideDown(options.speed);
					}else{
						momma.find('span .parent').slideUp(options.speed);
						momma.find('.miniMenu').slideUp(options.speed);
					}
				}

			}
			$(this).css('margin-left','0');
			
			if(options.dropDown){
				
				$(this).hide(0);
			}
		});
		
		
		
		return this;
	};
})(jQuery);


