var heightArray = [];
var pageinit = false;
var backheight = 700;
var state = 'shown';
var backWidth = 750;
var menuZoomState = 'big';
var newH;
var oldH;


function setPage(ref){
	
	
	
	activateMenus(ref);
	
	var newref = ref.replace('&newsid=', '/').substr(4);
	window.location.href = '#/'+newref;
	document.title = "Erik Wallin | "+newref.replace('_', ' ').replace('/', ' nr. ');
		
	var newsIndex = ref.search('&newsid=');
	var newsData = ref.substr(newsIndex+1);
	var senddata;
	
	if (newsIndex > 0){
		senddata = newsData;
		newref = 'nyhet';
	}
	
	 $('#innercontent').slideUp(400, function(){
		pageinit = false;
		loadContent(newref, senddata);		
	 });
	
	
	
};

function loadContent(content, senddata){
			
	$.ajax({  
	
		type: "GET",
        url: content+'.php', 
        dataType: "html",
		data: senddata,  
        success: function(msg){

            if(parseInt(msg)!=0)    //if no errors
            {
                $('#innercontent').html(msg);    //load the returned html into pageContet
				$('#innercontent').slideDown(400, function(){
					setBackground(content);
					
			
				});
				
				backshow();
				
                $('#loading').css('visibility','hidden');   //and hide the rotating gif
            } else {
				$('#innercontent').html('error!')
			}
			
        }
    });
}


function activateMenus(ref){
	
	
	var checkFromSub
	
	
	if ((ref == "?pg=start") || (ref == "?pg=")){
		$('#start').addClass('activeMenu');
		checkFromSub = $('#start')
		setBlurfieldHeight(300);
		zoomMenuBig();
		
		} else {
			zoomMenuSmall();
	}	
	
	if (ref == '?pg=kontakt'){
		$('#kontakt').addClass('activeMenu');
		checkFromSub = $('#kontakt')
		setBlurfieldHeight(-10);
	}
	
			 
	
	$.each($('.submenu'), function(index){
		
		$(this).removeClass('activeMenu');
				
		if ($(this).attr('href') == ref){
			
			setBlurfieldHeight(heightArray[$(this).parent().parent().parent().index()]);
						
			$(this).addClass('activeMenu');
			
			$(this).parent().parent().show();
			$(this).parent().parent().parent().find('a:first').addClass('activeMenu');
			
			checkFromSub = $(this).parent().parent().parent().find('a:first')
			
			
		} 
		
	});	
	
	$.each($('.topmenu'), function(indexs){
		if ($(this).attr('href') == checkFromSub.attr('href')){
			$(this).next().slideDown();
			$(this).addClass('activeMenu');	
			
		} else {
			$(this).next().slideUp();
			$(this).removeClass('activeMenu');						
		}
	});	
	
	

}

function setBlurfieldHeight(hh){
	if (!hh) hh = 300;
	hh += 400;
	backheight = hh;
	
	newH = $(window).height();
	var maxH = 701;
	var minH = 491;
	
	if (newH > minH && newH < maxH && menuZoomState == 'big'){
		backheight = newH
	}
	
	$('#backshow .blur').animate({
		height:backheight
	}, 400, "easeInOutQuad");
	
	$('#wallin_logo').animate({
		top:backheight-53
	}, 400, "easeInOutQuad");
	
	//alert(backheight-53);
	
	$('.caption').each(function(){
		$(this).animate({
			top:30
		}, 400, "easeInOutQuad");
	});
	
	$('#cross').animate({
		top:backheight-35
	}, 400, "easeInOutQuad");
	
	$('#newsbox').animate({
		top:backheight-138
	}, 400, "easeInOutQuad");
	
	
}


$(window).scroll(function () {
	var topPos = Number($(window).scrollTop());
	topPos = backheight-topPos;	
	$('#backshow .blur').css('height',topPos);
});

$(window).resize(function() {
	
	reposDivs()
	
});

function reposDivs(){
	newH = $(window).height();
	var maxH = 701;
	var minH = 491;
	
	
	
	if (newH > minH && newH < maxH && menuZoomState == 'big'){
	
		$('#backshow .blur').css('height',newH);
		
		$('#wallin_logo').css('top',newH-53);
		
		$('.caption').each(function(){
			$(this).css('top',newH+83)
		});
		
		$('#cross').css('top',newH-35)
		
		$('#newsbox').css('top',newH-138)
				
	}
}

function zoomMenuBig(){
	
	menuZoomState = 'big'
	backWidth = 750;
	
	startStartSlideShow();
			
	$('#newsbox').show(400, 'easeInOutQuad');
	$('#subcontent').hide(400, 'easeInOutQuad');
	$('#cross').animate({
		left: '715'
	 }, 400);
	 
	 $('#wallin_logo img').animate({
		width: '223',
		height: '69',
		marginTop: '0'
	 }, 400);
	 
	 $('#caption').animate({
		left: '490'
	 }, 400);
	
	$.each($('.topmenu'), function(){
		$(this).animate({
			fontSize:'76px',
			lineHeight:'70px'
		 }, 400, 'easeInOutQuad', function() {
		 });
	 });
	 
	 
	 reposDivs();
	 
}

function zoomMenuSmall(){
	
	menuZoomState = 'small'
	
	stopStartSlideShow();
	
	backWidth = 305;
			
	$('#newsbox').hide(400, 'easeInOutQuad');
	$('#subcontent').show(400, 'easeInOutQuad');
	$('#cross').animate({
		left: '285'
	 }, 400);
	 
	 $('#wallin_logo img').animate({
		width: '162',
		height: '51',
		marginTop: '15'
	 }, 400);
	 
	 $('#caption').animate({
		left: '30'
	 }, 400);
	
	$.each($('.topmenu'), function(){
		$(this).animate({
			fontSize:'38px',
			lineHeight:'40px'
		 }, 400, 'easeInOutQuad', function() {
		 });
	 });
}


function initMenus() {
	
	
	$.each($('ul.dropdown'), function(index){
		
		heightArray.push($(this).height());
		$(this).hide();	
		
	});
	
	
	$('ul.menu li a').click(
		function() {
			
			var ref = $(this).attr('href');
			ref = ref.replace('&newsid=', '/').substr(4);
			window.location.href = '#/'+ref;
			
			return false;
			
		}
	);
	

}


$(window).hashchange( function(e) {

	exitFullscreen();
	setPage(makeLoc());
});



function makeLoc(){
	var loc = window.location.hash.substr(2)
	loc = loc.replace('nyhet/', 'nyhet&newsid=');
	loc = '?pg='+loc
	return loc;
}

$(document).ready(
  function()
  {
	  if (!window.location.hash){
		window.location.href += '#/'+activeMenu;
	  }
 
	 	  
	  initMenus();
	  activateMenus(makeLoc());
	  setPage(makeLoc());
	  
	  
	  $(document).mousemove(function(e){
		   
		  if ((Number(e.pageX) < backWidth ) && (Number(e.pageY) < backheight)){
			  if (state == 'hidden'){
				  $('.blur').find('img').animate({opacity:1}, 200)
				  state = 'shown';
			  }
		  } else {
			  if (state == 'shown'){
				  $('.blur').find('img').animate({opacity:0}, 300)
				  state = 'hidden';
			  }
		  }

	  }); 
	  // initial functions for all pages
  }
);

