/**
 * Navigate Plugin coded for LTB by H2G Internetagentur, 5000 Aarau
 *
 * Plugin to handle head navigation with fancy slide down
 * effects. This plugin is enhanced with the color plugin
 * authorized by John Resig. Sources can be found on:
 *
 * http://plugins.jquery.com/project/color
 *
 * The colorize effect simply can be deactivated by set-
 * ting the boolean value in the defaults [true/false].
 *
 * @author michael.kuck@h2g.ch
 * @version v1.00 17-11-2010
 * @version v1.10 20-12-2010 : highlight active menu entry
 */
(function($) {
	// set global options defaults
	var o = false;
	// define the navigate plugin
	$.fn.navigate = function(options) {
		// build main options before initial procedure
		var opts = $.extend({}, $.fn.navigate.defaults, options);
		// initialize navigation plugin
		return this.each(function() {
			var $this = $(this);
			// adding motion listener
			var motion = false;
			// build element specific options
			o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			// calculating max amount of list elements
			var el = 0; $(this).find(".left").children("li").each(function() {
				el = ($(this).find("li").size() > el) ?
					$(this).find("li").size() : el;
			});
			
			// formatting list heights of each list element
			$(this).find(".left").find("ul").each(function() {
				$(this).css({'height': function() {
					return ((el*o.lineheight)+o.listoffset)+'px';	
				}});
			});
			
			// handle active class on layer
			$(this).find(".left").children("li").mouseenter(function() {
				var li = $(this);
				// removing all active states on all menues
				$($this).find(".left").children("li").each(function() {
					if ($(this).hasClass(o.classes['active']))
						$(this).removeClass(o.classes['active']);
				}); // activating current menu element
				$(this).addClass(o.classes['active']);
				// highlighting active menu entry
				if (o.colorize) {
					// getting default color
					var color = getRGB($(this).children("a").css('color'));
					// highlighting current main menu entry
					$(this).children("a").animate({
						'color':o.color['hover']
					}, o.color['speed'], function() { });
					// add listener to remove highlighting later
					var intID = setInterval(function() {
						if (!$(li).hasClass(o.classes['active'])) {
							$(li).children("a").animate({
								'color':color.toString()
							}, o.color['speed'], function() {
								$(this).removeAttr('style');
							}); clearInterval(intID);
						}
					}, 10);
				}				
			});
			
			var intID; // observe navigation layer motion
			// fading (in/out) navigation head parts
			$(this).parent().mouseenter(function() {
				motion = true; // disable motion
				clearInterval(intID); // clear motion listener
				// begin slidedown motion event
				$($this).find(".left").find("ul").slideDown(o.speed, function() {
					motion = false; // engage motion event again
				});
			}).mouseleave(function() {
				if (!motion) {
					// begin slideup motion event
					$($this).find(".left").find("ul").slideUp(o.speed, function() {
						$($this).find(".left").children("li").each(function() {
							$(this).removeClass(o.classes['active']);
						});
					});
				} else {
					// set interval to observe navigation layer
					intID = setInterval(function() { if (!motion) {
						$($this).trigger('mouseleave');
						clearInterval(intID);
					}}, 300);
				}
			});
			
			// colorizing (highlighting) left navigation entries
			if (o.colorize) { $(this).find(".left li a").each(function() {
				// set default color before highlighting
				var color;
				$(this).mouseenter(function() {
					if (!motion) {
						color = getRGB($(this).css('color'));
						$(this).animate({'color':o.color['hover']},
							o.color['speed'], function() { });
					}
				}).mouseleave(function() {
					if (!motion) {
						if (!$(this).parent().hasClass(o.classes['active'])) {
							$(this).animate({'color':color.toString()},
								o.color['speed'], function() {
									$(this).removeAttr('style');
								});
						}
					}
				});
			}); }
			
			// colorizing (highlighting) right navigation entries
			if (o.colorize) { $(this).find(".right li a").each(function() {
				
				// set default color before highlighting
				var color;
				$(this).mouseenter(function() {
					// removing all active states on left menue
					$($this).find(".left").children("li").each(function() {
						if ($(this).hasClass(o.classes['active']))
							$(this).removeClass(o.classes['active']);
					});
					color = getRGB($(this).css('color'));
					$(this).animate({'color':o.color['hover']},
						o.color['speed'], function() { });
				}).mouseleave(function() {
					$(this).animate({'color':color.toString()},
						o.color['speed'], function() {
							$(this).removeAttr('style');
						});
				});
			}); }
		});
	};
	function getRGB(color) {
		// look for rgb(0,0,0) values
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) {
			return new String('#'+decimalToHexString(parseInt(result[1]))+
			                      decimalToHexString(parseInt(result[2]))+
			                      decimalToHexString(parseInt(result[3])));
		}
		// look for #000000 values
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) {
			return new String('#'+result[1]+result[2]+result[3]);
		}
		// return default gray value
		return new String(o.color['passive']);
	};
	function decimalToHexString(number) {
		if (number < 0) {
			number = 0xFFFFFFFF + number + 1;
		}
		return number.toString(16).toUpperCase();
	};
	function debug($msg) {
		if (window.console && window.console.log)
			window.console.log('debug: '+$msg);
	}
	$.fn.navigate.defaults = {
		classes: {
			'active': 'hover',
			'selected': 'active'
		},
		speed: 300,
		lineheight: 16,
		listoffset: 8,
		colorize: true,
		color: {
			'speed': 150,
			'hover': '#e53829',
			'active': '#484848',
			'passive': '#848484'
		}
	}
})(jQuery);
