

// Offering a Custom Alias suport - More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
(function($) {
	
	/**
	 * $ is an alias to jQuery object
	 *
	 */
	$.fn.ipodmenu = function(settings) {
		
		var settings = jQuery.extend(
			{
			
				width: 250,		// width in px
				li_padding: 10,  // li element padding (left and right summarized)
				current_item_id: null,
				cross_speed: 300,
				top_link_text: 'Show all',
				next_level_text: 'Go to the next level',
				next_menu_class: 'ipod-menu-next-link',
				item_hover_class: 'hover'
			}
			, settings); // settings
		
		$this = null;
		
		var breadCrumb = null;
		
		
		return this.each(function()
		{

		      $this = $(this);
		      //console.log('id: ' + $this.attr('id'));
		      //console.log($this.html());
		      
		      init();
		});
		
		
		function init()
		{
			//console.log('init: nexbuttons');
			// add next link.
			$this.find('li:has(ul)').each(
				function()
				{
					$(this).find('a:first').after('<a href="#" class="'+settings.next_menu_class+'">' + settings.next_level_text + '</a>');
				}
			);
			
			// if there are multiple levels, create a drilldown menu
			
			//console.log($this.find('ul').size());
			
			build();
			
			if ($this.find('ul').size() > 0) 
			{ 
				// we have more submenu
				start();
			};	
			
			
			if (settings.item_hover_class) {
				$this.find('li').hover(
					function(){
						$(this).siblings().removeClass(settings.item_hover_class);
						$(this).addClass(settings.item_hover_class);
					},
					function(){ $(this).removeClass(settings.item_hover_class); }
				);
			};	// end if (settings.itemHover) 
			
		} // init()
		
		
		function start()
		{
			// when category links are not selectable (only link to next level)
			// FIXME: need this?
			/*
			if (!settings.selectCategories) {
				menu.find('.ddMenu li a').each(function(){
					if ($(this).next().is('ul')) { 
						$(this).addClass('menuIndicator').click(function(){
							showNextLevel(this, menu);
							return false;
						});
					};
				});
			};	
			*/
			
			$this.find('.' + settings.next_menu_class).click(
				function()
				{
					//console.log(this);
					_show_next_level(this, settings);
					return false;
				}
			);	
			
			
			if (settings.current_item_id)
			{
				var parents = [];
				
				//console.log('current: '+ settings.current_item_id);
				
				
				var currentElement = $this.find('a#' + settings.current_item_id);
				//                           li       ul         li
				var parent = currentElement.parent().parent().parent();
				

				while(parent.is('li')) {
					//parent.css('background-color', 'red');
					//console.log('parent is li, get the links');
					
					var links = $(parent).children('a');
					//console.log(links);
					
					//var e = links[i];
					// fixme
					
					parents.unshift(links[1]);
					
					/*
					if (!settings.selectCategories) 
					{
						parents.unshift(links[0]);
					}
					else 
					{
						parents.unshift(links[1]);
					}
					*/	

					//console.log($(links[0]).attr('href'));
					parent = $(links[0]).parent().parent().parent();
				}
				//console.log(parents);
				
				for (var i=0; i < parents.length; ++i)
				{
					_show_next_level(parents[i], settings);
				}
				
				
			} // we has a current element
				
			
		} // start()
		
		function build()
		{
			
			//console.log('build()');
			
			breadcrumb = $('<ul class="ipod-menu-breadcrumb clearfix" style="display: none;"></ul>');
			var container = $('<div class="ipod-menu-container"></div>');
			var items_container = $('<div class="ipod-menu-items-container"></div>');
			
			
			$this.css({ overflow: 'hidden' }).wrap(container);
			$this.before(breadcrumb);
			$this.css({ overflow: 'hidden' }).wrap(items_container);
			
			//breadcrumb.show();
			
			var max_height = $this.height();
			$this.find('ul').each(
				function(i)
				{
					var h = $(this).height();
					//console.log($(this).parent().find('a:first').text());
					//console.log(h);
				 	if (h > max_height )
			 		{
				 		max_height = h;
			 		};	 
				}
			);
			
			
			// FIXME: max_height and settings.max_height!
			
			//console.log('max h: ' + max_height);
		
			// set width - height
			$this.css({ height: max_height, width: settings.width, visibility: 'visible' });
			
			// set width/height to all sub element
			$this.find('ul').css({ height: max_height, width: settings.width });
			
			// set item container height
			// for the right height of the whole menu
			$this.parent().css({ height: max_height, width: settings.width });
			$this.parent().parent().css({width: settings.width });
			
			
			$this.find('li a:not(.' + settings.next_menu_class +')').each(
				function()
				{
					if (!$(this).next().is('.'+settings.next_menu_class)) 
					{
						$(this).addClass('ipod-menu-single-link').css({ width: settings.width - settings.li_padding} );
					}
					else
					{
						$(this).css({ width: settings.width-16 - settings.li_padding } );
					}
					
				}
			);
			
		}
	}; // ipod-menu()
	
	function _show_next_level(el, settings)
	{
		
		var this_link = $(el);
		
		//console.log('this_link: ' + this_link);
		//add all categories link to breadcrumb
		/*
		
		*/
		
		_create_breadcrumb(settings);
		
		
		// if the breadcrumb container is hidden, show it and add the first crumb
		if (breadcrumb.css('display') == 'none') 
		{
			breadcrumb.slideDown();
			_add_new_crumb(this_link, settings);
		}
		else 
		{ 
			_add_new_crumb(this_link, settings); 
		};
		
		//var thisLink = $(el);
		//var thisList = $(el).parents('ul:eq(0)');
		var next_list = $(el).next('ul:eq(0)');
		
		// show the next list
		next_list.css({
			visibility: 'visible',
			left: settings.width
		}).animate({ left: 0 }, settings.cross_speed);
		
	}; // show_next_level()
	
	
	/**
	 * inset a new element to the end of the breadcrumb
	 */
	function _add_new_crumb(this_link, settings) 
	{
		var crumb_text;
		
		// this is the real link
		var go_to_link;
		
		
		if (this_link && this_link.prev().is('a')) 
		{ 
			// this is a next_level link element
			crumb_text = this_link.prev().html();
			go_to_link = this_link.prev().attr('href');
			
		}
		else 
		{ 
			crumb_text = this_link.html(); 
			go_to_link = this_link.attr('href');
		};
		
		//console.log('goto: ' + go_to_link);
		
		//var newCrumb = $('<li class="currentCrumb" style="display: none;"><a href="javascript://" class="crumb">'+crumbText+'</a></li>');
		var newCrumb = $(
				'<li class="currentCrumb" style="display: none;"><a href="' 
				+ go_to_link 
				+ '" class="crumb">' 
				+ crumb_text
				+ '</a></li>'
		);
		
		$('.currentCrumb').removeClass('currentCrumb');
		breadcrumb.append(newCrumb);
		newCrumb.show();

		newCrumb.find('a').click(function(){
			
			if($(this).parent().is('.currentCrumb'))
			{
				
				
				// Den commented out
				//alert('You chose '+$(this).text());
				//$('.menuBtn').children(':last').text($(this).text());
				
				// Den commented out
				//menu.kill();
				
				
				//other actions could go here
				return true;
				//return false;
			}
			else 
			{
				var next_list = $(this_link).next();
				//
				next_list.find('ul').css({ visibility: 'hidden' });
				
				$(this).parent().nextAll().css({ visibility: 'hidden' }).slideUp(
					settings.cross_speed, 
						function(){
							$(this).remove();
						}
					);
				
				$(this).parent().addClass('currentCrumb');
				return false;
			}
		});
	}; // addNewCrumb()
	
	
	/**
	 * 
	 * Creates a breadcrumb element
	 * 
	 */
	function _create_breadcrumb(settings)
	{
		if (breadcrumb.find('li').size()<1)
		{
			var allCrumb = $('<li class="all"><a href="#">' + settings.top_link_text + '</a></li>');
			
			allCrumb.click(function()
			{
				//$this.find('ul').not('ul.' + settings.css_for_ul + ', .ddBreadcrumb').css({ visibility: 'hidden' });
				$this.find('ul').css({ visibility: 'hidden' });
				breadcrumb.empty().hide();
				return false;		
			});
			breadcrumb.append(allCrumb);
		};
	} // init_breadcrumb()
	
})(jQuery); // Call and execute the function immediately passing the jQuery object

