
// Create the Safalra Bar
var safalraBar =
    new function(){

      var buttons = {};
      var interval = null;

      function ensureButtonIsInitialised(button){
        if (!(button.id in buttons)) buttons[button.id] = { 'state' : 0 }
        if (!interval) interval = window.setInterval(updateFade, 50);
      }

      this.fadeIn = function(button, captionId){
        ensureButtonIsInitialised(button);
        buttons[button.id].target = 4;
        if (captionId) this.fadeIn(document.getElementById(captionId));
      }

      this.fadeOut = function(button, captionId){
        ensureButtonIsInitialised(button);
        buttons[button.id].target = 0;
        if (captionId) this.fadeOut(document.getElementById(captionId));
      }

      function updateFade(){
        for (buttonId in buttons){
          var button = buttons[buttonId];
          if (button.state != button.target){
            button.state += (button.state < button.target ? 1 : -1);
            document.getElementById(buttonId).style.opacity = button.state / 8 + 0.5;
          }
        }
      }

    }();


var OnloadScheduler=new function(){var _1=new Array();var _2=new Array();window.onload=function(){for(var i=_1.length-1;i>0;i--){execute(_1[i]);}for(var i=0;i<_2.length;i++){execute(_2[i]);}OnloadScheduler=null;};function execute(_5){if(_5){for(var i=0;i<_5.length;i++){_5[i]();}}}this.schedule=function(_7,_8){if(_7 instanceof Function){if(!_8){_8=0;}if(_8<0){if(_1[-_8]){_1[-_8].push(_7);}else{_1[-_8]=[_7];}}else{if(_2[_8]){_2[_8].push(_7);}else{_2[_8]=[_7];}}}else{this.schedule(function(){eval(_7);},_8);}};};

// returns the inner (viewport) size of the visitor's browser
function getInnerSize() {
  var result = {
    'width'  : 0,
    'height' : 0
  };
  if (window.innerWidth){
    result.width  = window.innerWidth;
    result.height = window.innerHeight;
  }else if (document.documentElement && document.documentElement.clientWidth){
    result.width  = document.documentElement.clientWidth;
    result.height = document.documentElement.clientHeight;
  }else if (document.body && document.body.clientWidth){
    result.width  = document.body.clientWidth;
    result.height = document.body.clientHeight;
  }
  return result;
}

// creates a listener that clears the default value from an input element
function createInputFocusListener(element, defaultValue){
  return function(){
    if (element.value == defaultValue){
      element.value = ''
      element.className = '';
    }
  }
}

// creates a listener that restores the default value to an input element
function createInputBlurListener(element, defaultValue){
  return function(){
    if (element.value == ''){
      element.value = defaultValue;
      element.className = 'defaultInput';
    }
  }
}

// attaches listeners to an input element to clear and restore its default value
function addInputListeners(element, defaultValue){
  if (element.value == defaultValue) element.className = 'defaultInput';
  if (element.addEventListener){
    // attach events in a DOM-compliant browser
    element.addEventListener(
        'focus',
        createInputFocusListener(element, defaultValue),
        false);
    element.addEventListener(
        'blur',
        createInputBlurListener(element, defaultValue),
        false);
  }else if (element.attachEvent){
    // attach events in a non-DOM-compliant browser
    element.attachEvent(
        'onfocus',
        createInputFocusListener(element, defaultValue));
    element.attachEvent(
        'onblur',
        createInputBlurListener(element, defaultValue));
  }
}

// functions for positioning menu
var menuNode;
function initialiseMenu(){
  menuNode = document.getElementById('menu');
  if (window.addEventListener){
    window.addEventListener('scroll', updateMenu, false);
  }else if (window.attachEvent){
    window.attachEvent('onscroll', updateMenu);
  }
}
function updateMenu(){
  if (window.XMLHttpRequest){
    var offset = 0;
    if (window.pageYOffset){
    	offset = window.pageYOffset;
    }else if (document.documentElement && document.documentElement.scrollTop){
    	offset = document.documentElement.scrollTop;
    }else if (document.body){
    	offset = document.body.scrollTop;
    }
    if (offset < 160){
      var top = 192 - offset;
    }else{
      var top = 32;
    }
    menuNode.style.top = top + 'px';
  }
}


// function for statistics collection
function collectStatistics(){
  var innerSize = getInnerSize();
  var request;
  if (window.XMLHttpRequest){
    request = new XMLHttpRequest();
  }else if (window.ActiveXObject){
    request = new ActiveXObject('Msxml2.XMLHTTP');
  }
  if (request){
    request.open('post','/shared/compile-statistics.php');
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    request.send(
        'screen_width=' + encodeURIComponent(window.screen.width)
        + '&screen_height=' + encodeURIComponent(window.screen.height)
        + '&inner_width=' + encodeURIComponent(innerSize.width)
        + '&inner_height=' + encodeURIComponent(innerSize.height));
  }
}


// function for replacing initials
function replaceInitials(){

  var h1s = document.getElementsByTagName('h1');
  
  for (var i = 0; i < h1s.length; i++){
  
    var oldContent = h1s[i].innerHTML;
    var newContent = '';
    var replaceNext = true;

    for (var j = 0; j < oldContent.length; j++){
      
      var character     = oldContent.charAt(j);
      var characterCode = oldContent.charCodeAt(j);
      
      if (replaceNext){
      
        replaceNext = false;
        
        if (characterCode >= 65 && characterCode <= 90){
        
          newContent +=
              '<img class="initial" src="http://safalra.com/shared/initials/'
              + characterCode
              + '.png" alt="'
              + character
              + '">';
        
        }else{
        
          newContent += character;
        
        }
      
      }else{
      
        newContent += character;

      }
      
      if (character == ' ' || character == '-' || character == ';' || characterCode > 128){
        replaceNext = true;
      }
        
      
      
    }
    
    h1s[i].innerHTML = newContent;

  }

}

// schedule functions
if (window.XMLHttpRequest) OnloadScheduler.schedule(replaceInitials);
OnloadScheduler.schedule(initialiseMenu);
OnloadScheduler.schedule(collectStatistics);
OnloadScheduler.schedule(
    function(){
      addInputListeners(
          document.getElementById('email'),
          'Enter your e-mail address');
      addInputListeners(
          document.getElementById('message'),
          'Enter your message');
    });

