// JavaScript Document

// jQuery DHTML Layer Manager
// (Adapted from prototype.js code example)



function lightbox_init()
{
  // Bind lightbox layer function
  jQuery(".lightbox").click(function(){
    //alert('opening '+ $(this).attr("href") );
    LayerManager.showLayer( jQuery(this).attr("href")  );
    return false;
  }); 
  // Focus hits top of lightbox
  jQuery("#popup").focus(function(){
    LayerManager.checkLayer('popup'); return false;
  }); 
  // Focus leaves lightbox
  jQuery("#popupDivert").focus(function(){
    LayerManager.checkLayer('popupDivert');
    return false;
  }); 
}



var LayerManager = 
{
  showLayer:function(url)
  {
    // Set vars
    var s_GET = querystring_to_array(url); 
    var CSSwidth = (s_GET['width'] && s_GET['width'] != "") ? parseInt(s_GET['width']) : 600;
    var CSSheight = (s_GET['height'] && s_GET['height'] != "") ? parseInt(s_GET['height']) + 25 : 400;
    var docWidth = jQuery(document).width();
    var docHeight = jQuery(document).height();
    var CSSleft = Math.floor( (docWidth/2) - (CSSwidth+22) / 2);
    //var CSStop = Math.floor( (docHeight/2) - (CSSheight/2) - 37);
    var CSStop = 50; 

    // Hide select boxes for IE6 users
    jQuery("select").hide(); 
    // Disable page scrolling
    jQuery("html").css("overflow", "auto");
    // Init tabIndex
    jQuery("#popup").attr("tabIndex", 0);
    jQuery("#layer").attr("tabIndex", 0);
    jQuery("#popupDivert").attr("tabIndex", 0);

    // Init attributes
    jQuery("#popup").css("top", CSStop + "px");
    jQuery("#popup").css("left", CSSleft + "px");
    jQuery("#popup").css("width", CSSwidth + "px");
    jQuery("#popup").css("height", CSSheight + "px");
    
    // Show the loading icon
    jQuery("#popup").append('<div class="loadicon"><img src="../images/loading.gif" alt="Loading" title="" /></div>');

    // Show the layer
    jQuery("#layer").show();

    // Load the data
    jQuery.get( url, function(data){ LayerManager.loadContent(data) } ); 
	
    jQuery("#popup").addClass("popup"); 

    // Set timout to ensure nodes are added to element before focus given, else they are missed
    var to = window.setTimeout(function(){
      jQuery("#popup").focus();
      window.clearTimeout(to);
    }, 100);  
  },
  


  closePopup:function(event)
  {
    jQuery("#popup").attr("tabIndex", null);
    jQuery("#layer").attr("tabIndex", null);
    jQuery("#popupDivert").attr("tabIndex", null);
    jQuery("#layer").css("display", "none");
    jQuery("#popup").removeClass("popup");
    jQuery("#popup").empty();
    jQuery("#popup").css("height", 0);
    jQuery("#popup").css("width", 0);
    jQuery("#layer").hide();

    // Show the select boxes which were hidden
    jQuery("select").show();
    // Re-enable page scrolling
    jQuery("html").css("overflow", "auto");
  }, 
  
  checkLayer:function(event)
  {
    var popup = document.getElementById("popup");
    if( jQuery("#popup").hasClass("popup") && event == "popup") {
      try {
        document.getElementById("closePopup").focus();
      } catch(e) {}
    }
    else if( jQuery("#popup").hasClass("popup")) {
      popup.focus();
    }
  },



  loadContent:function(dat)
  {
    // If the browser is IE6, scroll to the top of the window
    if(jQuery.browser.msie && jQuery.browser.version == "6.0") {
      scrollTo(0,0);
    } 

    var contentHeight = jQuery("#popup").height() - 20;
    var innerDat = jQuery(dat).find("#content").html(); 
	//alert(innerDat);
    jQuery("#popup").empty();
    jQuery("#popup").append('<div class="popup_header"><a href="#" id="closePopup"><img src="../images/closebtn.gif" border="0" /></a></div>');
    jQuery("#popup").append('<div id="popup_content">' + innerDat + '</div>');
    jQuery("#popup_content").css("height", contentHeight + "px" );
    // Bind close button
    jQuery("#closePopup").click(function(){
      LayerManager.closePopup();
      return false;
    });
    // Bind escape key to close function
    jQuery(document).keyup(function(event){

      if(event.keyCode == 27) {
        LayerManager.closePopup();
        return false;
      }
    });

  }
} 



// Convert a querystring to an associative array
function querystring_to_array(q)
{
  var arr0 = q.split('?');
  var arr1 = arr0[1].split('&');
  var arr2 = new Array();
  var arr3 = new Array();

  for(var i=0; i<arr1.length; i++) {
    arr2 = arr1[i].split('=');
    arr3[arr2[0]] = arr2[1]; } 
  return arr3;
} 


