jQuery(document).ready(function(){
	C.grabSubNav();
	C.grabLangSwitch();
	C.removeNavWidgetHeader();
//	C.SubNavMinHeight();
});



// "C" as namespace for custom scripts
var C = C || {};


/**
 * Grabs the subnavigation of the current active page
 * moves it onto an element in sidebar (#subNav)
 *
 * @return {void}
 */
C.grabSubNav = function()
{
	var subNavEl = jQuery('#subNav');
	var subNavActivePar = jQuery('ul.sf-menu li.current_page_parent ul');
	var subNavActiveCur = jQuery('ul.sf-menu li.current_page_item ul');
	var subNavActive = (subNavActivePar.length>0) ? subNavActivePar : subNavActiveCur;
		
	// remove header for widget
	subNavEl.parent().siblings('h3').remove();
	
	if(subNavActive.length>0 && subNavEl)
	{
		// move to new element
		var subNavParent = subNavActive.eq(0);
		subNavActive.removeAttr('style');
		subNavEl.html(jQuery('<div>').append(subNavParent).html()).addClass('active');
/*
		subNavEl.append(subNavParent).addClass('active');
		subNavParent.css({
			display : 'block',
			visibility : 'visible'
		});
*/
	}
	
	// remove all submenues for other (non-active) pages
	jQuery('ul.sf-menu ul').remove();
};




/**
 * @requires WPML Language Switcher widget somewhere on the page
 *
 * Grabs URL given by widget when present
 * Puts URL i apropiate tab-a-like button, activates them and so forth...
 *
 * @return {void}
 */
C.grabLangSwitch = function()
{
	var pathMatch = '/en/';
	var langSwitch = jQuery('#lang_sel');
	var url;
	
	if(langSwitch)
	{	
		// remove widget
		langSwitch.remove();
		
		if(url = jQuery('ul ul li a',langSwitch).attr("href"))
		{
			// switch-url provided, so Ung-mode is available
			jQuery('#ungSwitch').removeClass('disabled');
			// place url into right button
			if(url.indexOf(pathMatch) != -1)
				jQuery('#ungSwitch a').attr('href',url);
			else
				jQuery('#voksenSwitch a').attr('href',url);
			
			// who's active?
			var currPath = document.location.pathname;
			if(currPath.indexOf(pathMatch) != -1)
				jQuery('#modeSwitch li').toggleClass('active');
				
			// home buttons in correct mode
			var noTrailingSlashHref = function(node)
			{
				var href = jQuery(node).attr('href');
				var len = href.length;
				if(href.charAt( len-1 ) == '/')
					href.substring(0,len);
				console.log(href.substring(0,1))
				jQuery(node).attr('href',href);
				return href;
			};
/*
			if(currPath.indexOf(pathMatch) != -1)
			{
				var awHref = noTrailingSlashHref('li.aw a');
				console.log(awHref)
//				jQuery('li.aw a').attr('href',awHref + pathMatch)//awHref += pathMatch;
			}
*/
		}
		
	}
};





/**
 * Quite a mouthfull, but this function does the following:
 * Sees into #sidebar looks for element with class-name ".page_item"
 * finds its parents previous sibling, which must me a H3 node
 * To its parent we add class-name 'active', remove class-name 'item' and make it #subNav
 * remove its child which is a H3 node
 * 
 * @return {void}
 */
C.removeNavWidgetHeader = function()
{
	var headerNodeName = 'h3';
	jQuery('#sidebar .page_item').parent().prev(headerNodeName)
		.parent().addClass('active')
		.attr('id','subNav')
		.removeClass('item')
		.children(headerNodeName).remove();
};



/**
 *
 *
 */
C.SubNavMinHeight = function()
{
	var height = jQuery('img.headerImg').height();
	if(!height) return;
	
//	var offset = jQuery('#sidebar .item').css('margin-bottom');
	
	jQuery('#subNav').css({
		'min-height' : height+'px',
		'height' : 'auto !important',
		'min-height' : height+'px'
	});
};
