var basePath = '/';
var slashPos;
var curSlash

function setPage(ref){
	
	// get complete url
	var url = document.location.pathname + document.location.search;
	
	// exclude '/vh/?pg='
	url = url.replace(/\/sundholmen\/\?pg\=/i,'');
	
	// if no # is set, and current location is not the front page		
	
	if (!window.location.hash && url != basePath){	
		top.location = basePath+'#/'+ url;
		init();
	}

		
	// out-animation for current content
	
	if (slashPos < 0){

		$('#footer').slideUp(600);
	
		$('#content').slideUp(600, function(){
			
			// load next content
			loadContent(makeLoc());
			curSlash = 0;
		});
		
	} else {
		if (curSlash != slashPos){
			loadContent(makeLoc());
			curSlash = slashPos;
		}
	}

	
	// activate menu
	setMenu(ref);
	
	
};

function loadContent(content){
	
	$.ajax({  
	
		type: "GET",
        url: 'pages/'+content+'.php', 
        dataType: "html", 
        success: function(msg){
									
            if(msg)    //if no errors
            {
                $('#content').html(msg);    //load the returned html into pageContet
				$('#content, #footer').slideDown(600, function(){
					//setBackground(content);
				});
				
                $('#loading').css('visibility','hidden');   //and hide the rotating gif
            } else {
				$('#content').html('error!')
				
			}
			
        }
    });
}

function makeLoc(){
	var loc = window.location.hash.substr(2)
	slashPos = loc.indexOf('/');
	if (slashPos > 0) loc = loc.substr(0, slashPos);
	if (loc == "") loc = 'hem';
	return loc;
}

$(window).hashchange( function(e) {
	setPage(makeLoc());
});



$(document).ready(
  function()
  {  
  
  	// replace href with onClick for links in topmenu
	  $('#navigation a, #header a').click(function () {
        
		// find link
		var hash = $(this).attr('href');	
		
		// exclude '?pg='
        hash = hash.replace(/\?pg\=/i,'#/');
				
		
		// insert action
		if (window.location.hash != hash){
			window.location.href = hash;
		}
		
		
		
		// make sure page does not change via href attr.
		return false;
			 
	  });
	  
	  // load first page - rewrite url 
	  setPage(makeLoc());
	
  }
);
