(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');
  //   };
  
  // 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
	
	$(".header > .title > h2").html("<img src=\"files/img/web/title-loading.png\" alt=\"laden...\">");
    $.ajax({
	  url: "files/php/header.php?query="+event.value.substr(1),
	  cache: false,
	  success: function(html){
	    $(".header > .title > h2").html(html);
	  }
	});
	
    $(".article").html("");
    $.ajax({
	 url: "files/php/content.php?query="+event.value.substr(1),
	 cache: false,
	 success: function(html){
	  $(".article").html(html);
	
	  /* in case there is a form */
	  $('#form').ajaxForm({ 
	   url: 'files/php/form.php',
	   target: '#response', 
	   success: function() { 
        $('#form').resetForm();                           
	   } 
	  });

	  // clear input on focus
	  $('.input').focus(function() {
	   if($(this).val()==$(this).attr('title')){
	    $(this).val('');
	   }
	  });
	  $('.input').blur(function() {
	   if($(this).val()=='') {
	    $(this).val($(this).attr('title'));
	   }
	  });
	  //
	
	 }
	});
   }

  }).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 */
 
  /* quotes */  
  function updateQuote(){
   $('.quote > blockquote').load('files/php/quote.php');
  }
  window.setInterval(updateQuote, 10000 );
  /* /quotes */
 });

})(jQuery);

/* for email obfuscation */
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>");
}