﻿jQuery("document").ready(function(){
//	jQuery('#colNormal').html('<a id="prevContent" >&lt;Forrig</a> &nbsp; <a id="nextContent" >N&aelig;ste &gt;</a>');
});



var contentPages = Array();
var currentContent;

function plaintext(str){
	return str.replace(/[^a-zA-ZæøåÆØÅßöÖöÜäÄ 0-9]+/g,'');
}

function endsWith(haystack,needle){
  return haystack.substr(haystack.length - needle.length,haystack.length) == needle;
}

function nextContent(event){
    if ( jQuery('#colMargin').children('div:last').hasClass("active") ){
        document.location='/'+jQuery('#nextContent').attr('href');
        return true;
    }
	jQuery('#colMargin').children('div:visible').fadeOut('slow').removeClass('active').next().fadeIn('slow').addClass('active');
    UpdateNav();
    return false;
}

function prevContent(){
    if ( jQuery('#colMargin').children('div:first').hasClass("active") ){
        document.location='/'+jQuery('#prevContent').attr('href')+'#last';
        return true;
    }
    jQuery('#colMargin').children('div:visible').fadeOut('slow').removeClass('active').prev().fadeIn('slow').addClass('active');//ugh..
    UpdateNav();
    return false;
}


function UpdateNav(){   
    if( jQuery('#colMargin').children('div:first').hasClass("active") ){
        var prev=currentContent>0?currentContent-1:contentPages.length-1;
        jQuery('#prevContent').attr('href',contentPages[prev]['href']).attr('title',contentPages[prev]['name']);
    }else{
		var text =plaintext(jQuery('#prevContent').text());
        jQuery('#prevContent').attr('href',contentPages[currentContent]['href']+'#'+text).attr('title',text);
    }
    if( jQuery('#colMargin').children('div:last').hasClass("active") ){
        var next=currentContent<contentPages.length-1?currentContent+1:0;
        jQuery('#nextContent').attr('href',contentPages[next]['href']).attr('title',contentPages[next]['name']);
    }else{
		var text =plaintext(jQuery('#nextContent').text());
        jQuery('#nextContent').attr('href',contentPages[currentContent]['href']+'#'+text).attr('title',text);
    }
}



jQuery(document).ready(function(){

	jQuery('#mainmenu li a').each(function(){
		contentPages[contentPages.length]={'name':jQuery(this).html(),'href':jQuery(this).attr('href')};
	});
	
	jQuery('#nextContent').click(function(event){event.preventDefault();nextContent();});
	jQuery('#prevContent').click(function(event){event.preventDefault();prevContent();});	
	currentContent=0;
	var previousPage=0;
	for(var i=0;i<contentPages.length;i++){
		if( endsWith(location.pathname,contentPages[i]['href']) ){
			currentContent=i;
		}
		if ( endsWith(document.referrer,contentPages[i]['href']) ){
			previousPage=i;
		}
	}			


	jQuery('#colMargin').children('.csc-textpic-clear').remove();
	jQuery('#colMargin').children().hide();
	if ( currentContent < previousPage || location.hash == "#last" ) {
		jQuery('#colMargin').children('div:last').show().addClass('active');
	}else{		
		jQuery('#colMargin').children('div:first').show().addClass('active');
	}
	jQuery('#colMargin').children().css({'position':'absolute'});
	UpdateNav();    
});
