(function($) {
	
 function loadItem(_item,_img) {
  var _itemurl = "/files/ajax/item.php?i="+_item;
  if (_img) {
   _itemurl = _itemurl+"&img="+_img;	
  }
  // load content
  $.ajax({
   url: _itemurl,
   cache: true,
   success: function(html){
    $("#"+_item+" > .body").html(html);
    // set ajax links
    $("a[class='ajax']").unbind("click");
    $("a[class='ajax']").click(function(){
     var hash = this.rel;
     hash = hash.replace(/^.*#/, '');
     $.historyLoad(hash);
     return false;
    });
    //
    $("#"+_item).addClass("active");
    $("#"+_item+" > .body").slideDown('normal'); 
   }
  });
  //	
 }
	
 function pageload(hash) {

  // default
  if (!hash || hash == "/") {
   hash = "/work";
  }

  var vars = hash.split("/");
  var _href = vars[1];
  var _item = vars[2];
  var _img = vars[3];

  if ($("#"+_item).attr("class") != undefined)
  {
  	if ($("#"+_item).attr("class").indexOf('active') != -1 && !_img)
  	{
  	   $(".body").parent().removeClass("active");	
  	   $(".body").slideUp('normal'); // toggle
  	}
  }

  if (!_img) {
   // $(".body").parent().removeClass("active");	
   // $(".body").slideUp('normal'); // toggle
  }

  if (_href !=  $('#wrapper').parent().attr("id")) {
   var _url = "/files/ajax/content.php?p="+_href;
   // set loading
   $("#content > .content").css("display","none");
   $("#content > .content").html("Loading...");
   $('#content > .content').attr("id","content_loading");
   $("#content > .content").css("display","block");

   $.ajax({
    url: _url,
    cache: true,
    success: function(html){
   	// set content
     $("#content > .content").css("display","none");	
     $("#content > .content").html(html);
     $('#content > .content').attr("id","content_"+_href);
     if (_item) {
	  loadItem(_item,_img); // initial image is loaded by php
     }
     $("#content > .content").css("display","block");
     // for new ajax links:
     $("a[class='ajax']").unbind("click");
   	 $("a[class='ajax']").click(function(){
   	  var hash = this.rel;
   	  hash = hash.replace(/^.*#/, '');
   	  $.historyLoad(hash);
   	  return false;
   	 });
   
    }
   });
   $('#wrapper').parent().attr("id",_href); // set body id

  } else if (_item) {
   if ($("#"+_item+" > .body").css("display") == "none") { // if not already open
	//
	loadItem(_item,_img);	
   }

   if (_img) {
	$("#debug").html("load image: "+_img);	
    
	$.ajax({
	 url: "/files/ajax/image.php?i="+_item+"&img="+_img,
	 cache: true,
	 success: function(html){
	  $("#"+_item).find(".images").html(html);
	  // for new ajax links:
      $("a[class='ajax']").unbind("click");
   	  $("a[class='ajax']").click(function(){
   	   var hash = this.rel;
   	   hash = hash.replace(/^.*#/, '');
   	   $.historyLoad(hash);
   	   return false;
   	  }); 
     }
    });
   }

  }
 }	

 $(document).ready(function() {
	
  // Initialize history plugin
  $.historyInit(pageload, "jquery_history.html");
  // set onlick event for buttons
  $("a[class='ajax']").click(function(){
  	var hash = this.rel;
  	hash = hash.replace(/^.*#/, '');
  	$.historyLoad(hash);
  	return false;
  });
  // default beginpage or google link
  var _hash = window.location.toString().split("jeremyjansen.nl");
  _hash = _hash[1].replace(/^.*#/, '');
  pageload(_hash);

 });

})(jQuery);

// email obfuscation function
function obfuscate(coded,title) {
 cipher = "aZbYcXdWeVfUgThSiRjQkPlOmNnMoLpKqJrIsHtGuFvEwDxCyBzA1234567890";
 shift = coded.length;
 link = "";
 for (i=0; i<coded.length; i++) {
  if (cipher.indexOf(coded.charAt(i))==-1) {
   ltr = coded.charAt(i);
   link += (ltr);
  } else {     
   ltr = (cipher.indexOf(coded.charAt(i))-shift+cipher.length) % cipher.length;
   link += (cipher.charAt(ltr));
  }
 }
 if (coded == title) {
  title = link;
 }
 document.write("<a href='mailto:"+link+"'>"+title+"</a>");
}
