// changed 13.1.2010
(function($) {

 function pageload(hash) {

  if (!hash || hash == "/" || hash == "/iksi.php") {
   hash = "/blog";
  }

  var vars = hash.split("/");
  var _href = vars[1].replace(/\+/g,"-");
  // 
   var _id = vars[2];	

  $("#menu_"+_href).parent().siblings().find('.submenu').slideUp(300);
  $("#menu_"+_href).parent().children('.submenu').slideDown(300);

  // $("#"+_href+"_submenu").css("display","block");
  $("#menu_"+_href).parents('.submenu').css("display","block");

  $(".open").removeClass("open");
  $("#menu_"+_href+" > span").addClass("open");

  // get category and subcategory
  var _subs = $("#menu_"+_href).parents('.submenu');
  var _page = _href;
  var _php = new Array();
  _php.push(_href);
  for (var i = _subs.length - 1; i >= 0; i--){
   _php.push(_subs[i].id.replace("_submenu",""));
   if (i == 0) {
	_href = _php[0];		
   }
  }

  var parts = document.title.split(" / ");
  var siteTitle = parts[0];
  if ($("#menu_"+_href).parent().children('.submenu').length == 0 && _php.length < 2) { // page
   if (_href == "blog") {
	// check for id or page...
	
	if (vars[2]) {
	 // second variable
	 var _check = vars[2].substr(0,4);
	 if (_check == "page") {
	  // page
	  var _url = "files/ajax/blog.php?page="+vars[2].substr(4,9);
	 } else {
      var _url = "files/ajax/blog.php?id="+_id;		
	 }
	} else {
	 var _url = "files/ajax/blog.php";	
	}
    //
   } else {
    var _url = "files/ajax/page.php?p="+_href;	
   }
  } else { // portfolio
   if (_id) {
    var _url = "files/ajax/portfolio.php?p="+_href+"&id="+_id;
   } else {
    var _url = "files/ajax/portfolio.php?p="+_href;
   }
  }

  // ajax request
  $.ajax({
   url: _url,
   cache: false,
   success: function(html){
	if (html) {
     // set title
     if (_page != "first") {
      var pageTitle = _page.substr(0,1).toUpperCase()+_page.substr(1);
      if (pageTitle != "Iksi.php") { // remove
       document.title = siteTitle+" / "+pageTitle.replace(/-/g," ").replace(/\+/g," + ").capitalize();
      } else {
       document.title = siteTitle;	
      }
     }

     // set content	
     $(".content").html(html);

     $("a[class='ajax']").unbind("click"); 
	 $("a[class='ajax']").click(function(){
	  var hash = this.rel;
	  hash = hash.replace(/^.*#/, '');
	  $.historyLoad(hash);
	  return false;
	 });

    }
   }
  });

  //var html = "<pre>&gt; "+_url+"</pre>";

  // set body id
  $('#wrapper').parent().attr("id",_href.replace(/\+/g,"-"));
 }

 $(document).ready(function() {
	
   $(".submenu").css("display","none");

	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "jquery_history.html");
	// set onlick event for buttons
	$("a[class='ajax']").click(function(){
		// 
		var hash = this.rel;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});

  // default beginpage or google link
  var _hash = window.location.toString().split(".com");
  _hash = _hash[1].replace(/^.*#/, '');
  //if (_hash && _hash != "/iksi.php") {
   pageload(_hash);		
  //} 

 });

})(jQuery);

String.prototype.capitalize = function(){ //v1.0
 return this.replace(/\w+/g, function(a){
  return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
 });
};

/* in_array function */
function inArray(needle,haystack) {
 var key = "";
 for (key in haystack) {
  if (key == needle) {
   return haystack[key];
  }
 }
 return false;
}