/**
 * 
 * LightFrame by Strom (<petr.biza@gmail.com>)
 * 
 * Use: lFrame.show(object to show)
 * 
 * Version 0.1
 */

function lightframe() {
  var lfOverlayStyle = 'position:absolute;top:0;left:0;z-index:90;width:100%;height:500px;background:#000;zIndex:1000';
  
  
  var lastShowObject = null;
  var duration = 0.2;
  
  init();
  
  function init() {
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id','lfoverlay');
    setStyle(objOverlay, lfOverlayStyle);
    objOverlay.style.display = 'none';
    objOverlay.onclick = function() { hide(); return false; }
    document.body.appendChild(objOverlay);
  }
  
  
  this.hide = function() {
    return hide();
  }
  
  function hide() {
    new Effect.Fade('lfoverlay', { duration: duration});
    showSelectBoxes();
    disableKeyboardNav();
    
    lastShowObject.style.display = 'none';
  }
  
  this.show = function(obj_id) {
    return show(obj_id);
  }
  
  function show(obj_id) {
    hideSelectBoxes();

    var obj = $(obj_id);
    lastShowObject = obj;
    
    // stretch overlay to fill page and fade in
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();
    $('lfoverlay').style.height = arrayPageSize[1] +'px';

    new Effect.Appear('lfoverlay', { duration: duration, from: 0.0, to: 0.8 });

    window.setTimeout(function() {
      var obj = $(obj_id);
      obj.style.display = 'block';
      obj.style.zIndex = '1001';
      obj.style.position = 'absolute';
      //obj.style.top  = (arrayPageScroll[1] + (arrayPageSize[3] - obj.offsetHeight) / 2) + 'px';
      // trosku nahoru
      obj.style.top  = (arrayPageScroll[1] + (arrayPageSize[3] - obj.offsetHeight) / 2) - (arrayPageSize[3] / 15) + 'px';
      
      obj.style.left = (arrayPageScroll[0] + (arrayPageSize[2] - obj.offsetWidth) / 2) + 'px';
      enableKeyboardNav();
      
    }, duration * 1000 * 1.1
    );
  }
  
  function keyboardAction(e) {
    if (e == null) { // ie
      var keycode = event.keyCode;
    } else { // mozilla
      var keycode = e.which;
    }
    
    switch(keycode) {
      case 27:
        hide();
      break;
    }
  }
  
  function enableKeyboardNav() {
    document.onkeydown = keyboardAction; 
  }
  
  function disableKeyboardNav() {
    document.onkeydown = '';
  }
  
  function setStyle(obj, style) {
    var styles = style.split(';');
    for(var i=0; i<styles.length; i++) {
      var parse = styles[i].split(':');
    	obj.style[parse[0]] = parse[1];
    }
  }
  
  function showSelectBoxes(){
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
      selects[i].style.visibility = "visible";
    }
  }

  function hideSelectBoxes(){
  var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
      selects[i].style.visibility = "hidden";
    }
  }
  
  //
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
  function getPageSize(){
  
    var xScroll, yScroll;
    
    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    } 
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else { 
      pageHeight = yScroll;
    }
  
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }
  
  
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
  }

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
  function getPageScroll(){
    var yScroll;
  
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
    }
  
    arrayPageScroll = new Array('',yScroll) 
    return arrayPageScroll;
  }
}

var lFrame = null;
function initLightFrame() { lFrame = new lightframe(); }
//Event.observe(window, 'load', initLightFrame, false);
observer.addEvent(initLightFrame);