$(function(){
   // Global
   $.gaTracker.init("A-4060922-13");
   $("a[href=#]").click(function(){ return false; });
   $("a.ext").attr("target","_blank");
   
   $.get("index.php?title=1", function(data){ sitetitle = data; });
   var templateURL = $("#header h1 a").attr("href");
   
   // Menu
   $("#menu li a").click(function(){
      $("#menu li").removeClass("active");
      $(this).parent("li").addClass("active");
   });
   
      // Rollover
      function menu(){
         $("#menu li:not(.active) a").css({top:'0'});
         $("#menu li:not(.active) a").hover(function(){
               $(this).stop().css({display:"block"}).animate({top:"5px"}, 150);
            }, function(){         
               $(this).stop().animate({top:"0"}, 350);
         });
      }
   
   
   // Pagination
   function paginate(sel, bgpB, bgpE){
      var base = "-40px";
      if (sel == ".prev"){
         var anim = "marginLeft";
         $("#pagination "+ sel +" span").css({marginLeft:base});
      }
      else{
         var anim = "marginRight";
         $("#pagination "+ sel +" span").css({marginRight:base});
      }
      
      var animBase = {}, animEnd = {};
          animBase[anim] = "0px";
          animBase["opacity"] = 1;
          animEnd[anim] = base;
          animEnd["opacity"] = 0;
      
      $("#pagination "+ sel +"").hover(function(){            
         $(this).find("span").stop().animate(animBase, 150);
         $(this).find("em").css({backgroundPosition:bgpB});

      }, function(){
         $(this).find("span").stop().animate(animEnd, 300).css({display:"block"});
         $(this).find("em").css({backgroundPosition:bgpE});
      });
   }
   
      function pagination(){
         paginate(".prev", "bottom left", "top left");
         paginate(".next", "bottom right", "top right");
      }
   
   // Load page
   function loadPage(url){
      var height = $("#picture").height();
      $("#menu li").removeClass("active");
      $("#main").removeClass("home").addClass("photo");
          
      $("#load").fadeIn(function(){   
         $("#picture").height(height);
      });
      
      $.get(url, function(data){
         var $data = $(data);
         var photo = $data.find("#img img").attr("src"),
             legend = $data.find("#legend > *"),
             pages = $data.find("#pagination > *"),
             infos = $data.find("#infos > *");
         
         $("#img img").fadeOut(300, function(){
            $(this).load(function(){
               $(this).fadeIn();
               var newHeight = $(this).height();
               $("#picture").animate({height:newHeight}, 150);
               $("#load").fadeOut();
            }).attr("src", photo);
         });
         
         $("#legend").html(legend);
         $("#pagination").html(pages);
         $("#infos").html(infos);
         
         $("title").text($("#legend #text").text().split("-")[0]+ " | " +sitetitle.split("|")[0]);
      });
      
      $("html, body").animate({scrollTop:0});
      $.gaTracker.track(url);
      
      return false;
   }

   // Links with ajax loading
   $("#pagination a").live("click", function(){
      var url = $(this).attr("href"),
          hash = window.location.hash = url;
      
      $("html, body").animate({scrollTop:0}, 300);

      return false;
   });

   // On hash change : browser buttons and ajax loading
   $(window).hashchange(function(){
     var hash = window.location.hash.substr(1),
         loadURL = templateURL + hash;

      if (hash.length >= 1){
         loadPage(loadURL);
      }
   });
   
   // Redirection to real hash url if direct hash present
   var hashDirect = window.location.hash.substr(1);
   if (hashDirect.length >= 1){
      var hash = document.location.hash;
      window.location = templateURL + hashDirect;
   }
   
   
   // Init
   function init(){
      menu();
      pagination();
   }
   init();
});

$(document).keydown(function(key){ //alert(key.keyCode);
   if (key.keyCode == 37 && $(".prev").click().length == 0);
   if (key.keyCode == 39 && $(".next").click().length == 0);

   if (window.addEventListener){
      var kkeys = [],
          konami = "38,38,40,40,37,39,37,39,66,65";
          
      window.addEventListener("keydown", function(e){
         kkeys.push(e.keyCode);
         if (kkeys.toString().indexOf(konami) >= 0) $("body").addClass("konami");
      }, true);
   }
});
