function pageload(hash) {
  if(hash) {
    $('#content').load(hash);
  }  
}


$(document).ready(function() {
  
  $.history.init(pageload);
  
  //Rebind slimbox after ajax
/*  $("#content").ajaxComplete(function(event,request, settings){
    $("#content a[rel^='lightbox']").slimbox({}, null, function(el) {
      return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
    if (settings.url != "ajax/email" && $("div.email").length) {
      $("div.email").load("ajax/email");
    }
    $("a.arrow").hover(
      function () {
        $(this).stop();
        this.style.textDecoration = "underline";
        $(this).animate({ backgroundPosition: "3px 4px"}, 200 );    
      },
      function () {
      $(this).stop();     
      this.style.textDecoration = "none";
      $(this).animate({ backgroundPosition: "0px 4px"}, 200 );    
    });
  });
      
      */
  //create ajax links
  $("a.navi-link").each(function(i) {
    var segs = this.href.split("/");
    this.ajaxLink = "ajax/"+segs[segs.length-1];
    this.href = "#"+this.ajaxLink;
  }); 
    
  $(".navi-link").css("textDecoration","none");
    
  //päänavi-fx
 /* $("li.navi-w").hover(
    function () {
      $(this).stop();
      $(this).find("span").stop();         
      $(this).animate({ top: "1px"}, 200);    
    }, 
    function () {
      $(this).stop();
      $(this).find("span").stop();
      $(this).animate({ top: "13px"}, 200 );  
    }
  );
  */
  
  var wNaviMouseEnter = function () {
		$(this).stop();
		$(this).find("span").stop();         
		$(this).animate({ top: "1px"}, 200);    
  };
  
  var wNaviMouseLeave = function () {
		$(this).stop();
		$(this).find("span").stop();
		$(this).animate({ top: "13px"}, 200 );  
  };
  
  $("li.navi-w").mouseenter(wNaviMouseEnter).mouseleave(wNaviMouseLeave);
  
  $('li.navi-w a').click(function() {
		var selected = $('#navi-ct .selected');
  	var clicked = $(this).parents(".navi-w");

  	if (selected[0]==clicked[0]) {
  		return;
  	}
  	if ($(this).children('span').text()=="Referenssit") {
  		$('#refe-navi-ct').animate({ left: "-120px"}, 200 );
  		$('#refe-navi-ct .sovellukset').animate({ left: "10px"}, 200 );
  		$('#refe-navi-ct .sovellukset a').click();
  	} else {
  		$('#refe-navi-ct').animate({ left: "10px"}, 200 );
  	}
  	
		$.history.load(this.href.replace(/^.*#/, ''));
		selected.mouseenter(wNaviMouseEnter).mouseleave(wNaviMouseLeave);
		selected.animate({ top: "13px"}, 200);
		selected.removeClass('selected');
		clicked.unbind('mouseenter mouseleave');
		clicked.addClass('selected');
    return false;
  });
  
  
  
  
  //refenavi-fx
  var leavePos = '100px';
  var enterPos = '10px';
  
  var hNaviMouseEnter = function () {
    $(this).stop();        
  	$(this).animate({ left: enterPos}, 200);    
  };
  
  var hNaviMouseLeave = function () {
    $(this).stop();
    $(this).animate({ left: leavePos}, 200 );  
  };
  
  $("li.navi-h").mouseenter(hNaviMouseEnter).mouseleave(hNaviMouseLeave);
  
  $("li.navi-h a").click(function() {
  		var selected = $('#refe-navi-ct .selected');
  		var clicked = $(this).parent();

  		if (selected[0]==clicked[0]) {
  			return;
  		}
  		$.history.load(this.href.replace(/^.*#/, ''));
  		selected.mouseenter(hNaviMouseEnter).mouseleave(hNaviMouseLeave);
  		selected.animate({ left: leavePos}, 200 );
  		selected.removeClass('selected');
  		clicked.unbind('mouseenter mouseleave');
  		clicked.addClass('selected');
  	}
  );
  
});

