/*
 * Copyright (c) 2009 Jurriaan Topper (iksi.tv)
 *
 * Version: 0.0.3
 */
	
(function($) {
	
 var _splash;
 var theSplash;
 var prevhash;

 function pageload(hash) {
	
  // reset blog marginTop
  $(".content").css("marginTop","");

  if (theSplash) {
   clearInterval(theSplash);	
  }
  
  if (!hash && prevhash != "tickets") {
   if (_splash == "1") {
	hash = "/splash";	
   } else {
    hash = "/blog";
   }
  }


  var vars = hash.split("/");
  var _href = vars[1];

  if (prevhash != "tickets" || _href == "news" || _href == "blog" || _href == "concepts" || _href == "parties" || _href == "media" || _href == "shop" || _href == "pictures" || _href == "videos" || _href == "podcasts" || _href == "bookings" || _href == "about" || _href == "contact" || _href == "links" || _href == "search") {
  //$("#debug").html('hoppa '+_href+" - "+prevhash);	
   prevhash = _href;	
  //
	
  if (vars[2]) {
   // second variable
   var _check = vars[2].substr(0,4);
   if (_check == "page") {
    // page
    var _page = vars[2].substr(4,9);		
    var _url = "files/ajax/"+_href+".php?page="+_page;
    if (_href == "blog") {
     $(".content").css("marginTop","115px");
    }
   } else if (_check == "view") {
    var _view = vars[2].substr(4,9);		
    var _url = "files/ajax/"+_href+".php?view="+_view;
   } else {
   	var _check = vars[2].substr(0,7);	
	if (_check == "archive") {
	 // archive
	 if (vars[2].substr(7) && vars[2].substr(7).length == 6) {
	  var _url = "files/ajax/"+_href+".php?archive=true&date="+vars[2].substr(7);		
	 } else {
	  var _url = "files/ajax/"+_href+".php?archive=true";		
	}
	} else {
	 // id
    if (_href == "blog") {
     $(".content").css("marginTop","115px");
    }	
	 var _url = "files/ajax/"+_href+".php?id="+vars[2];		
	}
   }
		
   if (vars[3]) {
    //_url = _url+"&item=100";
    var _check = vars[3].substr(0,4);
    if (_check == "page") {
     // page
     var _page = vars[3].substr(4,9);		
     var _url = _url+"&page="+_page;
    } else {
	 if (vars[2] != "archive") {
      var _url = _url+"&item="+vars[3];
      if (_href == "blog") {
	   var _scrollto = vars[3];
      }
     } else {
      var _url = _url+"&id="+vars[3];
     }
    }		
   }

  } else {
    // just the page
    var _url = "files/ajax/"+_href+".php";	
  }	
   // set loading
   $("#content > .content").css("display","none");
   $("#content > .content").html("<img src='files/images/web/base/loader_ajax.gif' width='16' height='16' border='0' />");
   $('#content > .content').attr("id","content_loading");
   $("#content > .content").css("display","block");

   $.ajax({
    url: _url,
    cache: false,
    success: function(html){
	 // set content
   	 $("#content > .content").css("display","none");	
     $("#content > .content").html(html);
   	 $('#content > .content').attr("id","content_"+_href);  
	 //$("#content > .content").fadeIn(300);
   	 $("#content > .content").css("display","block");		
     $('.page-content').jScrollPane({scrollbarWidth:8});
     $('.body').jScrollPane({scrollbarWidth:8});

     if (_href == "blog") {
		$(".item").siblings().each(function(){
			if ($(this).attr("class") == "item_left") {
 			 $(this).css("height",$(this).next().height()+10+"px");
            } else if ($(this).attr("class") == "item_right") {
             $(this).css("height",$(this).prev().height()+10+"px");	
	        }
		});
		if (_scrollto) {
	     $("#debug").html(_scrollto);
	     // scroll
         $.scrollTo("#news_"+_scrollto,0 ,{offset: -150});	
		}
     }
    
    // interval to clear splash
	if (hash == "/splash") {
	  // theSplash = setInterval(
	  // 	    function endSplash() {
	  // 			pageload("/blog");
	  // 			clearInterval(theSplash);
	  // 		}, 10000);
	}
	// end interval

    }
   });
	  
   var parts = document.title.split(" :: ");
   var siteTitle = parts[0];
   // set title
   var pageTitle = _href.substr(0,1).toUpperCase()+_href.substr(1);
   document.title = siteTitle+" :: "+pageTitle;	
	
   // set body id
   $('#wrapper').parent().attr("id",_href);

   // analytics
   pageTracker._trackPageview(hash);

  }
 }	
	
$(document).ready(function() {
	
	// check for splash
	$.ajax({
	 url: 'files/splash.php',
     cache: false,
     success: function(html){
      _splash = html;

	  // Initialize history plugin.
	  $.history.init(pageload);
	  $("a.ajax").live("click",function(){
	   var hash = $(this).attr("rel");
	   if (hash) {		
	    $.history.load(hash);
	    return false;
	   }
	  });
	
     }
    });
    //

	/** mailinglist **/
	$("#ml_subscribe").mouseover(function(event){
		$("#ml_subscribe").css('backgroundPosition','0px -12px');
	});
	
	$("#ml_subscribe").mouseout(function(event){
		$("#ml_subscribe").css('backgroundPosition','0px 8px');
	});		
		
	$("#ml_unsubscribe").mouseover(function(event){
		$("#ml_unsubscribe").css('backgroundPosition','0px -12px');
	});		
		
	$("#ml_unsubscribe").mouseout(function(event){
		$("#ml_unsubscribe").css('backgroundPosition','0px 8px');
	});	
	
	$("#ml_subscribe").click(function(event) {
		$('#ml_feedback').html("<img src=\"/files/images/web/fb-loading.png\" border=\"0\" />");
	});
	
    $('#mailinglistform').ajaxForm( {
		target: '#ml_feedback',
		success: function() { 
			$('#mailinglistform').resetForm();                           
		}
    });
	/** end mailinglist **/
	
	/** search **/
    $('#searchform').ajaxForm( {
		target: '#content > .content',
		success: function() { 
   	 		$('#content > .content').attr("id","content_search");			
			$('#searchform').resetForm();                           
		}
    });	
	/** end search **/
	

});

})(jQuery);


