/*
   Peekok javascript Lightbox effect library
   Namespace: p_lightbox
   Reserved Ids: blackOverlayId, lightboxHeaderId, lightbboxoxId, lightboxLoaderId, lightboxIframeWrapper, lightboxIframeId, hideselectId
   3/17/09 version 1.0
*/

var p_lightbox = function() {
  var blackOverlayId = 'p_lightbox_black';
  var lightboxHeaderId = 'p_lightbox_header';
  var lightboxId = 'p_lightbox_static';
  var lightboxIframeWrapper = 'p_lightbox_iframe_wrapper';
  var lightboxIframeId = 'p_lightbox_iframe';
  var lightboxLoaderId = 'p_lightbox_loader';
  var hideselectId = 'p_lightbox_hideselect';
  var loadingImage = 'http://www.peekok.com/images/p_lightbox/loader.gif';
  var closeImage = 'http://www.peekok.com/images/p_lightbox/close.gif';

  var p1 = new Image();
  p1.src = loadingImage;
  var p2 = new Image();
  p2.src = closeImage;
  
  return {

    launchLightbox : function(url) {
      // check to make sure lightbox doesnt exist
      if(document.getElementById(blackOverlayId) || document.getElementById(lightboxHeaderId) || document.getElementById(lightboxId)) {
	this.closeLightbox();
      }

      if(typeof document.body.style.maxHeight === "undefined") {//if IE 6
        document.getElementsByTagName("html")[0].style.height = "100%";
	document.getElementsByTagName("html")[0].style.width = "100%";
	document.getElementsByTagName("html")[0].style.overflow = "hidden";
        document.body.style.height = "100%";
  	document.body.style.width = "100%";

        if (document.getElementById(hideselectId) === null) {//iframe to hide select elements in ie6
	  var hs = document.createElement('iframe');
	  hs.setAttribute('id', hideselectId);
	  hs.setAttribute('name', hideselectId);
	  document.body.appendChild(hs);
	}
      }

      // create black overlay
      var overlay = document.createElement('div');
      overlay.setAttribute('id', blackOverlayId);
      overlay.style.display = 'block';
      document.body.appendChild(overlay);
      
      // create static div
      var d = document.createElement('div');
      d.setAttribute('id', lightboxId);

      // create header div
      var header = document.createElement('div');
      header.setAttribute('id', lightboxHeaderId);
      header.style.display = 'block';
      header.innerHTML = '<a href="javascript:void(0)" onClick="p_lightbox.closeLightbox();"><img border="0" src="'+ closeImage +'"></a>';
      document.body.appendChild(header);

      if(typeof document.body.style.maxHeight != "undefined") {//if NOT IE 6
        var loader = document.createElement('div');
        loader.setAttribute('id', lightboxLoaderId);
        loader.innerHTML = '<center><img src="' + loadingImage + '"><br>loading</center>';
        d.appendChild(loader);
      }

      // add iframe
      var wrapper = document.createElement('span');
      wrapper.setAttribute('id', lightboxIframeWrapper);
      wrapper.innerHTML = '<iframe onload="p_lightbox.showIframe()" scrolling="auto" id="' + lightboxIframeId + '" name="' + lightboxIframeId + '" style="border: 0px; width: 100%; height: 100%; display: none;" src="' + url + '"> </iframe>';
      d.appendChild(wrapper);
      d.style.display = 'inline';
      document.body.appendChild(d);
      // IE 6 Hack
      var p_iframe = document.getElementById(lightboxIframeId);
      if(typeof document.body.style.maxHeight === "undefined") {//if IE 6
        p_iframe.style.display = 'block';
        p_iframe.src = url; // load URL after appended ie6
      }
    },

    showIframe : function() {
      var loader = document.getElementById(lightboxLoaderId);
      var d = document.getElementById(lightboxId);
      d.style.opacity = 1;
      d.style.MozOpacity = 1;
      d.style.filter = "alpha(opacity=100)";
      if(loader && d) { d.removeChild(loader); }
      var iframe = document.getElementById(lightboxIframeId);
      iframe.style.display = 'inline';
      iframe.style.width = '100%';
      iframe.style.height = '100%';
    },
    
    closeLightbox : function() {
      if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
        document.getElementsByTagName("html")[0].style.height = "auto";
        document.getElementsByTagName("html")[0].style.width = "auto";
	document.getElementsByTagName("html")[0].style.overflow = "";
        document.body.style.height = "auto";
        document.body.style.width = "auto";
	var select = document.getElementById(hideselectId);
	if(select) { document.body.removeChild(select); }
      }
      var a = document.getElementById(blackOverlayId);
      if(a) { document.body.removeChild(a); }	
      var b = document.getElementById(lightboxId);
      if(b) { document.body.removeChild(b); }
      var c = document.getElementById(lightboxHeaderId);
      if(c) { document.body.removeChild(c); }
    }

  };
} ();
