jQuery.fn.pageNav = function(params){
	
	var defaults = {
		loader : '<div class="loader"><img src="assets/images/framework/ajax-loader.gif" alt="Laddar.." /></div>'	
	}	
	var params = $.extend(defaults,params);	
	var menu;
	var current = {};
	var next = {};
	var timeouts = [];
	
	/*
	** Set next, current page  
	*/
	var navigate = function(){
		
		// to prevent animation overlap
		$('#room_navigation, #main_navigation, #page_berattelser .content').append(lockOverlay);
		if($('#welcomeMessage').is(':visible')) $('#welcomeMessage').fadeOut();
		
		//Add loader 
		$('body').append(params.loader);
		
		clearTimeouts();
		nav_event = 'nav';
		current.page = ($('#main_pages').find('.page:visible.active:last').size() > 0) ? $('#main_pages').find('.page:visible.active:last') : $('#main_pages').find('.page:last');
				
		next.obj = $(this);
	 	next.alias = getAliasFromHREF(next.obj.attr('href'));
	 	next.id	= next.obj.attr('id').replace('goto_', '');
		next.index	= next.obj.parent('li').index();
		next.pageID	= 'page_'+next.alias;
			
		// Reload if intro
		switch(next.id) {
			case '28':
			case '194':
				next.alias = 'nyheter';
				next.pageID	= 'page_'+next.alias;
				break;
			case '4':
			case '122':
			case 'en':
			case 'sv':
			case 'hem':
				return true;
				break;	
		}		
		
		next.id	= parseInt(next.id);	
		
		if(current.page.attr('id') != next.pageID){			
			timeouts.push(setTimeout(getContent, 12));	
		}
		else {
			$('#room_navigation, #main_navigation').find('.lockOverlay').remove();		
		}
		
		return false;
	}
	
	
	/*
	** Load content by id	
	*/
	var getContent = function(){
		//alert('getContent');
		
		$.get(baseHREF+'/', {id:next.id}, function(data){
			var html = $(data).find('.page > .content');
			var classes = $(data).find('.page').attr('class');
			next.title = (/<title>(.*?)<\/title>/m).exec(data)[1];
			
			setPage();
			next.page.append(html);				
			next.page.addClass(classes).removeClass('active');
			
			timeouts.push(setTimeout(onContentLoaded, 12));
		});
	}	
	
	
	var setPage = function(){
		//alert('set page');
		
		if($('#'+next.pageID).size() > 1){
			$('#'+next.pageID).remove();	
		}
		
		next.page = $('#'+next.pageID).size() > 0 ? $('#'+next.pageID) : $('<div id="'+next.pageID+'" class="page"></div>');
		next.page.find('.content').remove();
			
		current.page.after(next.page);
		next.page.show();		
	}
		
	var onContentLoaded = function(){
		//alert('onContentLoaded');
		
		// If loaded slideshow
		if(next.page.find('.slideshow').size() > 0){
			ss_id = next.page.find('.slideshow').attr('id');
			slideshow = new Slideshow(ss_id);
			slideshow.layout();
			slideshow.Stop();
		}
				
		if(next.page.find('.berattelserBackLink').size() > 0){
			next.page.find('.berattelserBackLink').pageNav();
		}
		
		switch(next.id){
			case 42:
			case 198:
				next.page.find('ul').pageNav();
				break;			
			case 28:	
			case 194:
				var newsSlide = $('#slideID'+next.obj.attr('newsid'));
				slideshow.navigation.find('li:eq('+newsSlide.index()+') a.tab').trigger('click');
				break;
			case 3:
			case 121: // Kontakt
				gMapInit();
				break;
		}	
				
		timeouts.push(setTimeout(initStory, 12));
		timeouts.push(setTimeout(centerContent, 12));
		timeouts.push(setTimeout(pageSlide, 12));
	}
	
	var centerContent = function(){
		//alert('centerContent');
		next.page.find('.content').not('.full').each(function(){
			$(this).css({
				'top' : '50%',
				'margin-top' : '-'+(main_nav_height + $(this).outerHeight() / 2)+'px'
			});	
		});
	}

	/*
	** Slide page	
	*/
	var pageSlide = function(){
		//alert('pageSlide');
		
		$('#main_pages').stop(true, true).animate({
			left: '-'+theWindow.width()+'px'
		}, 800, function(){
			
			$('#main_pages').css({
				left : 0
			});			
			
			current.page.hide().siblings().not(next.page).hide().find('.content').remove();
			current.page.find('.content').remove();
			
			timeouts.push(setTimeout(onAfterSlide, 12));	
		});
	}		
	
	var onAfterSlide = function(){
		//alert('onAfterSlide');
		windowHash();
		hideList();
		
		switch(next.id){
			case 3: //Kontakt
			case 121: //Contact
				timeouts.push(setTimeout("showList('contact');", 600));
				hideRoomNav();
				break;
			case 27:
			case 193: //Våra rum
				showRoomNav();
				lock_room_navigation = false;
				break;
			case 9:
			case 129: //Hem		
				timeouts.push(setTimeout("showList('news');", 600));
				showRoomNav();
				slideshow.Start();
				lock_room_navigation = false;
				break;
			default:
				hideRoomNav();	
				break;
		}		
		
		timeouts.push(setTimeout(activate, 12));
	}
	
	
	var activate = function(){	
		//alert('activate');	
		
		//Remove loader
		$('.loader').remove();
		
		$('#room_navigation, #main_navigation').find('.lockOverlay').remove();
		$('#room_navigation .active, #main_navigation .active').removeClass('active');
		current.page.removeClass('active')
		
		next.obj.parent('li').addClass('active');		
		next.page.addClass('active');
		
		// remove active from room_navigation
		if(next.obj.parents('#room_navigation').size() > 0){
			$('a#goto_27').parent('li').addClass('active');
		}
		
		switch(next.id){
			case 28:
				$('a#goto_9').parent('li').addClass('active');
				break;
			case 194:
				$('a#goto_194').parent('li').addClass('active');
				break;
			case 83:
			case 90:
			case 86:
			case 93:
			case 42:
				$('a#goto_42').parent('li').addClass('active');
				break;
			case 198:
			case 199:
			case 206:
			case 202:
			case 209:
				$('a#goto_198').parent('li').addClass('active');
				break;
		}
	}
	
	var windowHash = function(){
		//alert('windowHash');
		window.location.hash = next.alias;		
		document.title = next.title;
		timeouts.push(setTimeout("nav_event = ''", 400));
	}
	
	var clearTimeouts = function(){
	  for(key in timeouts ){
		clearTimeout(timeouts[key]);
	  }
	}
	
	this.each(function() {
		menu = $(this);
		
		if(menu.hasClass('berattelserBackLink') || menu.hasClass('lasMer')){
			menu.bind('click', navigate);
		}
		else {
			menu.items = menu.find('li');
			menu.items.find('a').click(navigate);
		}
	});
	
	return this;
};  

/*
** ROOM NAVIGATION
*/
var showRoomNav = function()
{
	//console.log('showRoomNav');
	if($('#room_navigation .toggle').is(':hidden'))
	{
		$('#room_navigation .toggler').trigger('click');
	}
}

var hideRoomNav = function()
{
	//console.log('hideRoomNav');
	if($('#room_navigation .toggle').is(':visible') && lock_room_navigation == false)
	{
		$('#room_navigation .toggler').trigger('click');
	}
}

var toggleRoomNav = function(event){
	//console.log('toggleRoomNav');
	
	var toggler = $(this);
	var pos = (is_frame) ? 10 : 10;		
		
	$('#room_navigation').fadeIn(400);
	
	if(toggler.next('.toggle').is(':visible'))
	{		
		toggler
		.next('.toggle').slideUp(function(){
			toggler
				.addClass('minimized')
				.removeClass('maximized')
				.find('.indicator').text('+');
			toggler.parent().animate({
				top		: pos+'px',
				left	: pos+'px'	
			});	
		});				
	}		
	
	else
	{
		// Lock menu if click from user
		if(typeof(event.originalEvent) == 'object')
		{
			lock_room_navigation = true;
		}
		
		//console.log(room_navigation_top);
		toggler.parent().animate({
			top		: room_navigation_top+'px',
			left	: room_navigation_left+'px'	
		}, function(){
			toggler
				.addClass('maximized')
				.removeClass('minimized')
				.find('.indicator').text('-');
			toggler.next('.toggle').slideDown();				
		});
	}	
	
}


/*
** NEWS & CONTACT
*/

var news_height = 120;
var contact_height = 130;
var direct_contact_height = 245;
var main_nav_height = 70;


var toggleList = function(type){
	//console.log('toggle');
	if($('#'+type).is(':hidden')){
		showList(type);
	}
	else {
		hideList(type);
	}
}

var hideList = function(type){
//	console.log('hide');
	$('#main_navigation').animate({
		height : main_nav_height+'px'
	}, function(){		
		$('#'+type).hide();
	});
}

var showList = function(type){
	var type_height = 0;
		
	switch(type){
		case 'news':
			type_height = news_height;
			hideList('contact');
			hideList('direct_contact');
			break;
		case 'contact':
			type_height = contact_height;
			hideList('news');
			hideList('direct_contact');
			break;
		case 'direct_contact':
			type_height = direct_contact_height;
			hideList('contact');
			break;
	}
	
	$('#'+type).delay(400).show(10, function(){
		$('#main_navigation').animate({
			height : (type_height+main_nav_height)+'px'
		});
	});
}
