$(document).ready(function() {

  // Add 'active' css class to selected item
  setActiveItem();
  
  // Load flash headers
  applySifr();
  
  // Load history plugin
  $.historyInit(pageLoad);
  
  // fix png
  $(".lotus").supersleight({ shim: '/images/blank.gif' });
  
  // Make html and php links load with Ajax
  fixLinks();
  
  // Set default texts on inputs
  fixTextInputs();
});

function fixTextInputs(parent) {

  var textInputs = $((parent || '') + " input:text");
  
  textInputs.focus(function(){
    if ($(this).val() == $(this)[0].title)
    {
        $(this).removeClass("inactive");
        $(this).val("");
    }
  });
  
  textInputs.blur(function(){
    if ($(this).val() == "")
    {
        $(this).addClass("inactive");
        $(this).val($(this)[0].title);
    }
  });
  
  textInputs.blur();
}

function fixLinks(parent) {
  // Fix all links that are not absolute
  $((parent || '') + 'a:not([href^=http://])').click(function(){
    // 
    var hash = this.href;
    hash = hash.replace(/^.*?([^\/]+\.(html|php))/, '$1');
    // moves to a new page. 
    // pageload is called at once. 
    // hash don't contain "#", "?"
    $.historyLoad(hash);
    return false;
  });
}

function pageLoad(hash) {
  if (hash) {
  
    // jquery's $.load() function does't work when hash include special characters.
    if($.browser.msie) {
      hash = encodeURIComponent(hash);
    }
    
    var documentName = hash;
    $("#contentwrap").load(documentName + " #contentwrap>*", "", function() {
    
      // Add 'active' css class to selected item
      setActiveItem(documentName);
      
      // Load flash headers
      applySifr();
      
      // Make html and php links load with Ajax
      fixLinks('#contentwrap ');
      
      // Set default texts on inputs
      fixTextInputs('#contentwrap ');
    });
  }
}

function setActiveItem(documentName) {
  if (documentName == null) {
    documentName = document.location.href.replace(/^.*?([^\/]+\.(html|php))/, '$1');
  }
  
  // Update active menu item
  $('a:not([href *=' + documentName + ' ])').removeClass('active');
  $('a[href *=' + documentName + ' ]').addClass('active');
}

function applySifr() {
  if(typeof sIFR == "function"){

    sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"flash/myriadpro.swf", sColor:"#949495", sLinkColor:"#949495", sBgColor:"#FFFFFF", sWmode:"transparent", sHoverColor:"#949495", sFlashVars:""}));  
    sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"flash/myriadpro.swf", sColor:"#454b4c", sLinkColor:"#454b4c", sBgColor:"#FFFFFF", sWmode:"transparent", sHoverColor:"#454b4c", sFlashVars:""}));  

  };
}

