/**
 * Navigation and Accessibility features.
 *
 * @version   1.00.090526
 * @author    LBI Lost Boys
 */
NS(function($){

	NS.Navigation = {
		initialize: function() {
			this.menu = new Navigation();
			this.contrast = new HighContrast();
			this.scaler = new FontScaler(); 
		}
	};
	
	/**
	 * Primary and Secondary navigation
	 */
	function Navigation(){
		this.primaryNavigation();
		this.secondaryNavigation();
	}

	Navigation.prototype = {
		primaryNavigation:function(){
			var node = $('#navigation-primary')[0];
			this.primary = new NS.SimpleMenu(node, {
				hoverClass:  'hover',
				activeClass: 'open',
				itemType:    '#navigation-primary > li',
				menuType:    'div.navigation-menu'
			});
		},

		secondaryNavigation:function() {
			var node = $('#navigation-secondary ol')[0];
			this.secondary = new NS.SimpleMenu(node, {
				hoverClass:  'over',
				activeClass: 'open',
				itemType:    'ol > li',
				menuType:    'div.navigation-menu'
			});
		}
	};
	
	/**
	 * Contrast options
	 */
	function HighContrast() {
		this.stylesheets = $('link[rel*=stylesheet]');
		
		var tools = $('#navigation-tools');
		var title = /nl/i.test(NS.getLanguage())? 
			"Wissel tussen normale en hoog contrast weergave" : 
			"Toggle between normal and high contrast mode";
		
		tools.append('<li id="navigation-contrast"><a href="#" title="' + title + '">Contrast</a></li>');
		
		$('#navigation-contrast').click(this.handleClick.bind(this));
		
		if(/true/i.test(NS.getCookie('ns-contrast'))) {
			this.switchStyles(true);
		}
	}

	HighContrast.prototype = {
		handleClick:function(e) {
			var alternate = /true/i.test(NS.getCookie('ns-contrast'))? false : true;
			this.switchStyles(alternate);
			NS.setCookie('ns-contrast', alternate);
			e.preventDefault();
		},

		switchStyles:function(alternate) {
			this.stylesheets.each(function(){
				if(/screen/i.test(this.media)) { 
					this.disabled = alternate;
				}

				if(/alternat/i.test(this.rel)) {
					this.disabled = !alternate;
				}
			});
		}
	};

	/**
	 * FontScaler
	 */
	FontScaler = function() {
		this.body = $('body');
		this.currentFlag = NS.getCookie("ns-fontsize") || "text-small";

		var root  = $('#navigation-tools');
		var scalers = $('<li id="navigation-fontscaler"></li>');
		var flags = ["text-small", "text-medium", "text-large"];
		var titles = /nl/i.test(NS.getLanguage())? 
			["Kleine lettergrootte", "Medium lettergrootte", "Grote lettergrootte"] : 
			["Small font size","Medium font size","Large font size"];
		
		for(var i=0; i<flags.length; i++) {
			var flag = flags[i];
			var link = $('<a href="#" class="' + flag + '" title="' + titles[i] + '">A</a>');
			if(flag == this.currentFlag) {
				link.addClass('active');
				this.body.addClass(flag);
			}
			scalers.append(link);
		}

		root.append(scalers);
		scalers.click(this.handleClick.bind(this));
	};

	FontScaler.prototype = {
		handleClick:function(e) {
			var link = $(e.target);
			var flag = /text-[a-z]+/.exec(link[0].className)[0];
			link.parent().find('a').removeClass('active');
			link.addClass('active');
			this.scale(flag);
		},

		scale:function(flag) {
			if(this.currentFlag) {
				this.body.removeClass(this.currentFlag);
			}	
			
			this.body.addClass(this.currentFlag = flag);
			NS.setCookie("ns-fontsize", flag);
		}
	};
	
	/**
	 * Bind to NS.initialize
	 */
	NS.subscribe('initialize', function(){
		NS.Navigation.initialize();
	});
});