/** jQuery jMenu plugin
	* Written by J. Wodzynski/Entireweb
	* License: same as jQuery, see: http://jquery.com/license
	
	* Now with some options: exact === true will match pathname with href exactly, 
	* else, we only look for indexOf() and if !=-1, we make $t active. 
	* NB: THIS IS NOT DONE FOR THE INNERLINK list. Innerlink will instead check if 
	* the first URL segment matches the href, OR if the innerlink's parent matches the link
	* and URL is somewhere inside the pathname.
	*/
(function($) {
	$.fn.jmenu = function(options) {
	// build main options before element iteration
	var opts = $.extend({}, $.fn.jmenu.defaults, options);
	// iterate and reformat each matched element
		return this.each(function() {
			$headlink = $('li.headlink', this);
			$innerlink =  $('li.headlink ul li', this);
						
			// add/remove .over class for .hover and show/hide the dropdown inside (if any)
			$headlink
  			.hover(
  				function() {
  					//$('a', this).addClass('over');
  					$(this).addClass('over');
  					$('ul', this).css('display', 'block'); // show any dropdowns inside this menu, if there are any
  				},
  				function() { 
  					$(this).removeClass('over');				
  					//$('a', this).removeClass('over');
  					$('ul', this).css('display', 'none'); 
  				}
  			)
  			// add .active class if document.location matches the current href
  			.children('a').each(function() {	
  				$t = $(this);
  				//if($t.attr('href') == document.location.pathname)
  				if(opts.exact === true) {
  				  //debug('Exact match mode for:', $t);
  				  if($t.attr('href') == document.location.pathname)  {
  		        $t.parent().addClass('active');		
  		        //debug($t.attr('href'), document.location.pathname);
  				  }
  				}
  				else if(document.location.pathname.indexOf($t.attr('href')) !== -1) {
  		      $t.parent().addClass('active');
  		    }
  			});
			
			// add/remove .over class for the dropdown items and bind the click event for the whole <li>
			$innerlink
				.hover(
					function() {
						$(this).addClass('over');
					},
					function() {
						$(this).removeClass('over');
					}
				)
				.bind('click', function(e) {
					document.location = $(this).children('a').attr('href');
				})
				//loop through links and set .active on the parent <li> if it matches the pathname
				.children('a').each(function() {
					var $t = $(this);
					var href = $t.attr('href');
					
					// Pick the starting segment (from first to second slash)
					var split = document.location.pathname.indexOf('/', 1) +1; // kompensera fšr att vi inte bšrjar i offset 0
					var path =  document.location.pathname.substr(0, split);

					// If the inner link links to the same as current path seems to be
					// OR
					// If, has a parent that matches the URL and document.location.pathname.indexOf($t.attr('href')) !== -1
					// add .active to innerlink and its parent (as the parent is not always matched by the headlink match)
					if((href == path) || ($t.parents('.headlink').children('a').attr('href') == path && document.location.pathname.indexOf(href) !== -1)) {
						$t.parent().addClass('active');
						$t.parents('.headlink').addClass('active');
				  }
				});

			// build element specific options
			//var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

			// update element styles
			//$this.css({
			//	backgroundColor: o.background,
			//	color: o.foreground
			//});
			
			//var markup = $this.html();
			
			// call our format function
			//markup = $.fn.jmenu.format(markup);
			//$this.html(markup);
		});
	};
	//
	// private function for debugging
	//
	function debug(a, b) {
		if (window.console && window.console.log)
			window.console.log(a, b);
	};
	//
	// define and expose our format function
	//
	//$.fn.jmenu.format = function(txt) {
	//	return '<strong>' + txt + '</strong>';
	//};
	//
	// plugin defaults
	//
	//$.fn.jmenu.defaults = {
	//	foreground: 'red',
	//	background: 'yellow'
	//};
})(jQuery);