/*
 * Copyright (c) 2009 Jurriaan Topper (iksi.tv)
 *
 * Version: 0.0.4
 */
 
(function($) {
	
 function pageload(hash) {

  if (!hash) {
    hash = "/news";
  }
  
  var vars = hash.split("/");
  var _href = vars[1];

  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;
   } else {
	 // id
	 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 {
      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("loading...");
   $('#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");
     // scroll top top...
    }
   });

   $.ajax({
    url: "files/ajax/pushbox.php?page="+_href,
    cache: false,
    success: function(html){
	 // set pushbox
   	 $("#pushbox").css("display","none");	
     $("#pushbox").html(html);
   	 $("#pushbox").css("display","block");
    }
   });
	  
   var parts = document.title.split(" - ");
   var siteTitle = parts[0];
   // set title
   var pageTitle = _href.substr(0,1).toUpperCase()+_href.substr(1);
    document.title = "Streetlab"; //siteTitle+" - "+pageTitle;	
	
   // set body id
   $('#wrapper').parent().attr("id",_href);
   // reset scroll
   $.scrollTo("0",0);  
   pageTracker._trackPageview(hash);
 }	
	
$(document).ready(function() {
	
	  // Initialize history plugin.
	  $.history.init(pageload);
	  $("a.ajax").live("click",function(){
	   var hash = $(this).attr("rel");
	   if (hash) {		
	    $.history.load(hash);
	    return false;
	   }
	  });
	
	  // $('#searchform').ajaxForm( {
	  //  target: '#content > .content',
	  //  success: function() { 
	  //   $('#searchform').resetForm();
	  //   $('#content > .content').attr("id","content_search");  
	  //  }
	  // });
	
	
	// bind form using 'ajaxForm' 
    $('#searchform').ajaxForm(
     { beforeSubmit: showRequest }
    );

	// pre-submit callback 
	function showRequest(formData, jqForm, options) { 
	 // var queryString = $.param(formData); 
	 // alert('About to submit: \n\n' + form.search_query.value); 
     var form = jqForm[0];	
     var reg = /\s+/;
     var query = form.search_query.value
     if (form.search_query.value) {
	  //alert('About to submit: \n\n' + form.search_query.value); 
      $('#searchform').resetForm();	
      $.history.load("/search/"+query.replace(reg,'+'));
     }
	 return false; 
	} 

      $('#mailinglistform').ajaxForm( {
		target: '#ml_feedback',
		success: function() {
			$('#mailinglistform').resetForm();
		}
	  });
	
 // if (checkApp()) {
 //   var el = document.body; // for IE
 //  } else {
 //   var el = document; // for other browsers
 //  }
 // 
 //  $(el).scroll(function () { 
 //   // check values for IE
 //   // get available space
 // 
 //   if (checkApp()) {
 //    // for IE
 //    var height = document.documentElement.clientHeight;
 //    var scroll = document.documentElement.scrollTop;
 //   } else {
 //    // for other browsers
 //    var height = parseInt(window.innerHeight);
 //    var scroll = document.body.scrollTop;
 //    if (scroll == 0 && document.documentElement.scrollTop != 0) {
 //     var scroll = document.documentElement.scrollTop; //FF
 //    }
 //   }
 // 
 //   var scrollSpace = height-($("#logo").height());
 //   var setMargin = (scrollSpace+$("#logo").height())-$("#highlights_side").height();
 //   $("#debug").html(scroll+" // "+$("#highlights_side").height()+" // "+height+" // "+setMargin+" // "+($("#content").height()-scroll-350));
 //   // if (($("#content").height()-scroll-350) < 0) {
 //   //  $("#middle").css("position","fixed");	
 //   // } else {
 //   //  $("#middle").css("position","absolute");		
 //   // }
 //   if (scroll+scrollSpace < $("#highlights_side").height()) {
 //    $("#highlights_side").css("position","absolute");
 //    $("#highlights_side").css("marginTop",$("#logo").height()+"px");	
 //   } else {
 //    // if pageheight is larger than highlights!
 //    if ($("#content").height() > $("#highlights_side").height() ) {
 //     $("#highlights_side").css("position","fixed");
 //     if ($("#highlights_side").height() > scrollSpace) {
 //      $("#highlights_side").css("marginTop",setMargin+"px");	
 //     }
 //    }
 //   }
 //  });

});

})(jQuery);

// clear / restore
function clearDefault(el) {
 if (el.defaultValue==el.value) el.value = "";
}

function putDefault(el) {
 if (el.value=="") el.value = el.defaultValue;
}

function checkApp() {
 if (navigator.appName == 'Microsoft Internet Explorer') {
  return true;
 }
 return false;
}