(function($) {

 $(document).ready(function() {
	
  /* history */
  // var log = function(msg) {
  //    if (!$('.log').size()) {
  //     $('<div class="log" />').appendTo('.debug');
  //    }
  //    $('.log').append(msg.replace(/^([^:]*):(.*)$/, '<p><b>$1:</b> <span class="$1">$2</span></p>'))
  //     .attr({scrollTop: $('.log').attr('scrollHeight')})
  //     .find('p:nth-child(even)').addClass('even');
  //   };

  $('#overlay').click(function() {
   if ($('#site').css("visibility") == "hidden") {
    $('#site').css("visibility","visible");
   } else {
    $('#site').css("visibility","hidden");
   }
  });
  
  // Event handlers
  $.address.init(function(event) {
   //log('init: "' + event.value + '"');
   $("a[class='ajax']").address(function() {
       return $(this).attr('href').replace(location.pathname, '/');
   });
  }).change(function(event) {
   //log('change: "' + event.value + '"');
   var text = (event.value == '') ? 'Home' : 
       event.pathNames[0].substr(0, 1).toUpperCase() + 
       event.pathNames[0].substr(1);
   $('a').each(function() {
    // $(this).addClass('selected', $(this).text() == text); // conflicting css
   });

   if (event.value) { // make ajax request

    $('#site').css("visibility","visible");	
    $("#content > .content").html("<h2>&nbsp;</h2>");
    $.ajax({
	  url: "files/php/content.php?query="+event.value.substr(1),
	  cache: false,
	  success: function(html){
	    $("#content > .content").html(html);
	    /* in case there is a form */
	    $('#form').ajaxForm({ 
	     url: 'files/php/form.php',
	     target: '#response', 
	     success: function() {
		  if ($('#response > span').attr("class") != "error") {
           $('#form').css("display","none");              
          }             
	     } 
	    });
	
	  }
	});
   }

  }).internalChange(function(event) {
   //log('internalChange: "' + event.value + '"');
  }).bind('externalChange', {msg: 'The value of the event is "{value}".'}, function(event) {
   //log('externalChange: ' + event.data.msg.replace(/\{value\}/, event.value));
  });
 });
 /* /history */

})(jQuery);
