
var powerboxObjArray   = new Array();
var powerboxOrderArray = new Array();
var powerboxActiveId   = 0;

var powerboxDefaultPrefix = "mz_";

function powerboxDisable(id) {
  id = updateVariable(id, null, powerboxActiveId);

  if (typeof(powerboxObjArray["id_"+id]) != "undefined") {
    var objLightbox = document.getElementById("lightbox_"+id);
    var objLightboxContent = document.getElementById("lightboxContent_"+id);
    var pboxZindex = objLightbox.style.zIndex +98;
    var objBody = document.getElementsByTagName("body").item(0);
    var objOverlayBox = document.getElementById("overlay_"+id);
    if (objLightbox && !objOverlayBox) {
      // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
      var objOverlayBox = document.createElement("div");
      objOverlayBox.setAttribute('id','overlay_'+id);
      objOverlayBox.setAttribute('class', 'overlay_pbox');
      objOverlayBox.setAttribute('className','overlay_pbox');
//      objOverlayBox.onclick = function () { powerboxEnable(id); return false; }
      objOverlayBox.style.display = 'none';
      objOverlayBox.style.position = 'absolute';
      objOverlayBox.style.zIndex = ''+pboxZindex;
      objLightbox.appendChild(objOverlayBox);
      objOverlayBox.style.display = 'block';
    }

  }
}


function powerboxEnable(id) {
  id = updateVariable(id, null, powerboxActiveId);

  if (typeof(powerboxObjArray["id_"+id]) != "undefined") {
    var objLightbox = document.getElementById("lightbox_"+id);
    var objOverlayBox = document.getElementById("overlay_"+id);
    if (objOverlayBox) {
      objOverlayBox.style.display = 'none';
      if (objLightbox) {
        objLightbox.removeChild(objOverlayBox);
      }
    }
  }
}


function powerboxHide(id) {
  id = updateVariable(id, null, powerboxActiveId);
  if (typeof(powerboxObjArray["id_"+id]) != "undefined") {
    powerboxObjArray["id_"+id].display = "none";
    if (powerboxObjArray["id_"+id].closeonhide ||
        powerboxOrderArray.length == 1) {
      powerboxObjArray["id_"+id].close();
    }
    else {
      powerboxObjArray["id_"+id].bottom();
    }
  }
}


function powerboxCloseAll(reload) {
  while (powerboxOrderArray.length > 0) {
    powerboxClose(powerboxOrderArray[0]);
  }
  lightboxIsActive = false;
  if (reload === true) {
    location.reload(true);
  }
}

function powerboxClose(id) {
  id = updateVariable(id, null, powerboxActiveId);
  if (typeof(powerboxObjArray["id_"+id]) != "undefined") {
    var objBody = document.getElementsByTagName("body").item(0);
    var objLightbox = document.getElementById("lightbox_"+id);
    var objLightboxContent = document.getElementById("lightboxContent_"+id);

    objLightboxContent.innerHTML = '';
    objLightbox.removeChild(objLightboxContent);
    objBody.removeChild(objLightbox);

    var temp = new Array();
    for (i=0 ; i < powerboxOrderArray.length ; i++) {
      if (powerboxOrderArray[i] != id) {
        temp[temp.length] = powerboxOrderArray[i];
      }
    }    
    powerboxOrderArray = temp;

    if (!isUnused(powerboxObjArray["id_"+id].parent)) {
      powerboxObjArray["id_"+powerboxObjArray["id_"+id].parent].show();
    }
    delete(powerboxObjArray["id_"+id]);
    powerboxToBottom(id);
  }
}


function powerboxOverlayClose() {
  var objOverlay = document.getElementById("overlay");
 	if (objOverlay) {
    if (powerboxOrderArray.length == 0) {
      var objBody = document.getElementsByTagName("body").item(0);
      objBody.removeChild(objOverlay);
    }
    else {
      objOverlay.style.display = 'none';
    }
  }
}


function powerboxToTop(id) {
  id = updateVariable(id, null, powerboxActiveId);

  var temp = new Array();
  temp[temp.length] = id;
  for (i=0 ; i < powerboxOrderArray.length ; i++) {
    if (powerboxOrderArray[i] != id) {
      temp[temp.length] = powerboxOrderArray[i];
    }
  }
  powerboxOrderArray = temp;
  powerboxActiveId = powerboxOrderArray[0];
  powerboxShowOrder();
}


function powerboxToBottom(id) {
  id = updateVariable(id, null, powerboxActiveId);

  var temp = new Array();
  for (i=0 ; i < powerboxOrderArray.length ; i++) {
    if (powerboxOrderArray[i] != id) {
      temp[temp.length] = powerboxOrderArray[i];
    }
  }
  if (powerboxObjArray["id_"+id]) {
    temp[temp.length] = id;
  }
  powerboxOrderArray = temp;
  if (powerboxOrderArray.length > 0) {
    powerboxActiveId = powerboxOrderArray[0];
  }
  else {
    powerboxActiveId = 0;
  }
  powerboxShowOrder();
}


function powerboxZorder(id) {
  id = updateVariable(id, null, powerboxActiveId);
  var j = 0;
  for (i = (powerboxOrderArray.length - 1) ; i >= 0 ; i--) {
    if (id == powerboxOrderArray[i]) {
      break;
    }
    j++;
  }
  return j;
}

function powerboxShowOrder() {
	var objOverlay = document.getElementById("overlay");
  var j = 0;
  var visibleCount = 0;
  for (i = (powerboxOrderArray.length - 1) ; i >= 0 ; i--) {
    id = powerboxOrderArray[i];
    objLightbox = document.getElementById("lightbox_"+id);
    objLightboxContent = document.getElementById("lightboxContent_"+id);
    objLightbox.style.zIndex = 1001 + (j * 100);
    objLightbox.style.display = powerboxObjArray["id_"+id].display;
    if (powerboxObjArray["id_"+id].display != "none")
      visibleCount++;
    if (powerboxObjArray["id_"+powerboxOrderArray[i]].overlay)
      objOverlay.style.zIndex = 1000 + (j * 100);
    j++;
  }
  if (visibleCount > 0) {
    // Hide select boxes as they will 'peek' through the image in IE
    var topZindex = j - 1;
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
      if (selects[i].style.zIndex < (1000 +(topZindex * 100))) {
        selects[i].style.visibility = "hidden";
      }
    }
  }
  else if (visibleCount == 0) {
    powerboxOverlayClose();
  }
  powerboxShowSelects(powerboxActiveId);
}


function powerboxShowSelects(id) {
  if (powerboxActiveId != 0) {
    startobject = document.getElementById('lightbox_' + powerboxActiveId);
  }
  else {
    startobject = document;
  }

  selects = startobject.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "visible";
  }
}


function powerboxGetNewId() {
  var highId = 0;
  for (var idx in powerboxObjArray) {
    if (typeof(powerboxObjArray[idx]) == "object" && powerboxObjArray[idx].id && powerboxObjArray[idx].id > highId) {
      highId = powerboxObjArray[idx].id;
    }
  }
  return highId + 1;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



function mzLightboxShow(arguments) {
  // TODO: remove when all personal devs are merged
  // i.e., not used and all calls are direct to powerboxShow
  return powerboxShow(arguments);
}


function powerboxShow(arguments, powerboxId) {
  if (typeof(powerboxObjArray["id_ajaxLoading"]) != "undefined") {
    powerboxObjArray["id_ajaxLoading"].close();
  }

  if(typeof powerboxId =='undefined' || powerboxId==null)
  {
    arguments["id"] = updateVariable(arguments["id"], null, powerboxGetNewId());
    id = arguments["id"];
  }
  else
  {
    id = powerboxId;
  }
  powerboxActiveId = id;

  if (typeof(powerboxObjArray["id_"+id]) == "undefined") {
    // Create new
    powerboxObjArray["id_"+id] = new powerboxObj(arguments);
    powerboxOrderArray.unshift(id);
  }
  else {
    // Update existing
    powerboxObjArray["id_"+id].setValues(arguments);
  }

  if (powerboxObjArray["id_"+id].overlay) {
    powerboxInit(powerboxObjArray["id_"+id].overlay, powerboxObjArray["id_"+id].overlaycss);
  }

  powerboxAdjustOverlay();

  var objLightbox = document.getElementById('lightbox_' + id);
  var objLightboxContent = document.getElementById('lightboxContent_' + id);

  if (objLightbox) {
    objLightbox.style.display = 'block';

    if (objLightboxContent) {
	  if (powerboxObjArray["id_"+id].contentType == "ajax") {
		  objLightboxContent.innerHTML = powerboxObjArray["id_"+id].getContent();
		  $.get(powerboxObjArray["id_"+id].ajaxUrl, function(data) { 
			powerboxObjArray["id_"+id].content = data;
			objLightboxContent.innerHTML = powerboxObjArray["id_"+id].getContent();
			// Run possible inline script block's
			try {
				var myScript = new Function($('script',objLightboxContent.innerHTML).html());
				myScript();
			} catch (e) {
				//alert(e);
			}
			
			// Center the lightbox, if the sticky mode is not enabled
			if (powerboxObjArray["id_"+id].stickToElement == "") {
			    if (objLightbox) {
				  var lightboxWidth = objLightbox.offsetWidth + 0;
				  var lightboxHeight = objLightbox.offsetHeight;
				  var lightboxLeft = ((arrayPageSize[0] - lightboxWidth) / 2);
				  var lightboxTop = adjustTopForAffiliates(90);
				  if(f_scrollTop()>lightboxTop + 50)
				    lightboxTop = f_scrollTop();
				  var parentId = powerboxObjArray["id_"+id].parent;
				  lightboxTop = (powerboxObjArray["id_"+parentId]) ? lightboxTop+50: lightboxTop;

			      objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
			      objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
		    	}
			}
			
		  });
	  }
	  else {
		  objLightboxContent.innerHTML = powerboxObjArray["id_"+id].getContent();
	  }
    }

    // position lightbox below an element
    if (objLightbox && powerboxObjArray["id_"+id].stickToElement != "") {
    	var el = jQuery("#"+powerboxObjArray["id_"+id].stickToElement);
    	var offset = el.offset();
    	if (powerboxObjArray["id_"+id].stickToElementAlign == "right") {
    		var lightboxLeft = offset.left - powerboxObjArray["id_"+id].width + jQuery("#"+powerboxObjArray["id_"+id].stickToElement).width();
    	}
    	else {
    		var lightboxLeft = offset.left;
    	}
    	var lightboxTop = offset.top + el.outerHeight();
    }
    // center lightbox
    else {
	    if (objLightbox) {
	      var lightboxWidth = objLightbox.offsetWidth + 0;
	      var lightboxHeight = objLightbox.offsetHeight;
	      var lightboxLeft = ((arrayPageSize[0] - lightboxWidth) / 2);
	      var lightboxTop = adjustTopForAffiliates(90);
	      if(f_scrollTop()>lightboxTop + 50)
	        lightboxTop = f_scrollTop();
	      var parentId = powerboxObjArray["id_"+id].parent;
	      lightboxTop = (powerboxObjArray["id_"+parentId]) ? lightboxTop+50: lightboxTop;
	    }
    }

    objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
    objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
    powerboxObjArray["id_"+id].show();

    // Update the overlay height as the lightbox might have increased the overall page height.
    powerboxAdjustOverlay();
  }

  // Return the id number of the box to make it possible for external direct reference
  return id;
}


// The "arguments" variable can contain settings to adjust the visual layout
function powerboxObj(arguments) {
  // Init
  this.title    = "";
  this.subtitle = "";
  this.content  = "";
  this.footer   = "";
  this.contentType = "";
  this.ajaxUrl = "";
  this.stickToElement = "";
  this.stickToElementAlign = "";

  // Define methods
  this.show           = function () { powerboxEnable(this.id);powerboxToTop(this.id)};    // Ok
  this.hide           = function () { powerboxHide(this.id)};     // Ok
  this.close          = function () { powerboxClose(this.id)};    // Ok
  this.top            = function () { powerboxToTop(this.id)};    // Ok
  this.bottom         = function () { powerboxToBottom(this.id)}; // Ok
  this.disable        = function () { powerboxDisable(this.id)};  // Ok
  this.enable         = function () { powerboxEnable(this.id)};   // Ok

  this.setValues      = function (arguments) {
    // Set default values of powerbox for unsupplied settings
    this.id             = updateVariable(arguments["id"], this.id, powerboxGetNewId());

    this.css            = updateVariable(arguments["css"], this.css, "");
    if (!isUnused(arguments["prefix"])) {
      arguments["prefix"] += "_";
    }
    this.stickToElement = updateVariable(arguments["stickToElement"], this.stickToElement, "");
    this.stickToElementAlign = updateVariable(arguments["stickToElementAlign"], this.stickToElementAlign, "left");
    this.prefix         = updateVariable(arguments["prefix"], this.prefix, powerboxDefaultPrefix);
    this.border         = updateVariable(arguments["border"], this.border, 0);
    this.dragable       = updateVariable(arguments["dragable"], this.dragable, false);
    this.width          = updateVariable(arguments["width"], this.width, 100);
    this.display        = updateVariable(arguments["display"], this.display, "block");
    this.overlay        = updateVariable(arguments["overlay"], this.overlay, true);
    if (!isUnused(arguments["overlaycss"]))
    {
      arguments["overlaycss"] += "_";
    }
    this.overlaycss     = updateVariable(arguments["overlaycss"], this.overlaycss, "");

    this.title          = updateVariable(arguments["title"], this.title, "");
    this.titleAlign     = updateVariable(arguments["titleAlign"], this.titleAlign, "center");
    this.titleUse       = updateVariable(arguments["titleUse"], this.titleUse, true);
    this.subtitle       = updateVariable(arguments["subtitle"], this.subtitle, "");
    this.subtitleAlign  = updateVariable(arguments["subtitleAlign"], this.subtitleAlign, "left");

    this.closeButton        = updateVariable(arguments["closeButton"], this.closeButton, true);
    this.closeButtonPos     = updateVariable(arguments["closeButtonPos"], this.closeButtonPos, "top");
    if (!isUnused(arguments["closeButtonCSS"])) {
      arguments["closeButtonCSS"] += "_";
    }
    this.closeButtonCSS      = updateVariable(arguments["closeButtonCSS"], this.closeButtonCSS, powerboxDefaultPrefix);
    if (!isUnused(arguments["closeButtonOnClick"])) {
      arguments["closeButtonOnClick"] += ";";
    }
    this.closeButtonCustom   = updateVariable(arguments["closeButtonOnClick"], this.closeButtonCustom, "");
    this.closeButtonOnClick  = this.closeButtonCustom + 'powerboxClose(\''+ this.id +'\');';

    this.contentType = updateVariable(arguments["contentType"], this.contentType, "html");
    
    if (this.contentType == "ajax") {
    	this.ajaxUrl = updateVariable(arguments["content"], this.ajaxUrl, "");
    	var ajaxLoadingContent = "<span id='"+ id +"'><table width=100%>" +
        	"<tr><td align=center>"+ ajaxLoader +"</td></tr>" +
        	"<tr><td align=center><img src=\"img/ajax_loader.gif\" alt=\"Loading\" width=\"16\" height=\"16\"></td></tr>" +
        	"</table></span>"; 
    	this.content        = updateVariable(ajaxLoadingContent, this.content, "");
    }
    else {
    	this.content        = updateVariable(arguments["content"], this.content, "");
    }

    this.contentAlign   = updateVariable(arguments["contentAlign"], this.contentAlign, "left");
    this.contentPadding = updateVariable(arguments["contentPadding"], this.contentPadding, 0);

    this.footer         = updateVariable(arguments["footer"], this.footer, "");
    this.footerAlign    = updateVariable(arguments["footerAlign"], this.footerAlign, "right");

    // Link two boxes together
    this.child          = updateVariable(arguments["child"], this.child, "");
    if (!isUnused(this.child) && powerboxObjArray[this.child]) {
      powerboxObjArray[this.child].parent = this.id;
    }
    this.parent         = updateVariable(arguments["parent"], this.parent, "");
    if (!isUnused(this.parent) && powerboxObjArray["id_"+this.parent]) {
      powerboxObjArray["id_"+this.parent].child = this.id;
    }

    return this.id;
  }
 
  // A function to set up the box layout and content for this object
  this.getContent    = function () {

    this.titleDiv       = '<div id=' + this.prefix +'powerboxTitle_'+ this.id +' class='+ this.prefix +'powerboxTitle><nobr>'+ this.title +'</nobr></div>';

    this.subtitleDiv    = '';
    if (this.subtitle != "") {
      this.subtitleDiv   += '<div id=' + this.prefix +'powerboxSubtitle_'+ this.id +' class='+ this.prefix +'powerboxSubtitle><nobr>'+ this.subtitle +'</nobr></div>';
    }

    this.contentDiv     = '<div width=100% class='+ this.prefix +'powerboxContent>'+ this.content +'</div>';

    this.footerDiv      = '<div id=' + this.prefix +'powerboxFooter_'+ this.id +' class='+ this.prefix +'powerboxFooter><nobr>'+ this.footer +'</nobr></div>';

    this.closeButtonDiv = '';
    if (this.closeButton) {
      this.closeButtonDiv = '<div style=\"z-index:4;position:relative;\">' +
                            '<div class='+ this.closeButtonCSS +'powerboxCloseButton ' +
                            'id='+ this.prefix +'powerboxCloseButton_'+ this.id +' onClick=\"'+ this.closeButtonOnClick +'\">' +
                            '<img width=1 height=1 src=img/trans.gif id='+ this.closeButtonCSS +'powerboxCloseButtonImage></div></div>';
    }


    if (this.closeButtonPos == "top") {
      topRight = this.closeButtonDiv;
      topRight += '<img width=1 height=1 id='+ this.prefix +'powerboxTopRightImage src=img/trans.gif>';
      bottomRight = '<img width=1 height=1 id='+ this.prefix +'powerboxBottomRightImage src=img/trans.gif>';
    }
    else {
      topRight = '<img width=1 height=1 id='+ this.prefix +'powerboxTopRightImage src=img/trans.gif>';
      bottomRight = this.closeButtonDiv;
      bottomRight += '<img width=1 height=1 id='+ this.prefix +'powerboxBottomRightImage src=img/trans.gif>';
    }


    var subTitleRow = '';
    if (this.subtitleDiv != "") {
      subTitleRow = '    <tr>' +
                    '     <td class='+ this.prefix +'powerboxMiddleUpper width=100%>' + this.subtitleDiv + '</td>' +
                    '    </tr>';
    }

//this.border=1;
    boxDiv = '<div style=\"position:relative; border: 3px solid black;\">' + this.closeButtonDiv +
             '<table border='+ this.border +' width='+ this.width +' height=100% cellpadding=0 cellspacing=0>' +

             // Top Row
             // ====================
             '<tr>' +
             ' <td valign=top width=100% height=100%>' +
             '  <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
             '  <tr>' +

             // Top Left Corner
//              '   <td class='+ this.prefix +'powerboxTopLeft valign=top height=100%>' +
//              '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxTopLeftUpperLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopLeftUpperLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxTopLeftUpperRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopLeftUpperRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxTopLeftLowerLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopLeftLowerLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxTopLeftLowerRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopLeftLowerRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    </table>' +
//              '   </td>' +

             // Top Title Section
             '   <td class='+ this.prefix +'powerboxTop valign=top width=100% height=100%>' +
             '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
             '    <tr>' +
             '     <td class='+ this.prefix +'powerboxTopUpper>' +
             '<img width=100% height=100% border=0 alt=\"\" id='+ this.prefix +'powerboxTopUpperImage src=img/mzlightbox/'+ this.prefix +'top_upper.png></td>' +
             '    </tr>' +
             '    <tr>' +
             '     <td class='+ this.prefix +'powerboxTopLower>' + this.titleDiv + '</td>' +
             '    </tr>' +
             '    </table>' +
             '   </td>' +

             // Top Right Corner
//              '   <td class='+ this.prefix +'powerboxTopRight valign=top height=100%>' +
//              '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxTopRightUpperLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopRightUpperLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxTopRightUpperRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopRightUpperRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxTopRightLowerLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopRightLowerLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxTopRightLowerRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxTopRightLowerRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    </table>' +
//              '   </td>' +

             '  </tr>' +
             '  </table>' +
             ' </td>' +
             '</tr>' +

             // Middle Row
             // ====================
             '<tr>' +
             ' <td width=100% height=100% valign=top>' +
             '  <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
             '  <tr>' +

             // Middle Left
//              '   <td class='+ this.prefix +'powerboxMiddleLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxMiddleLeftImage src=img/trans.gif></td>' +

             // Middle
             '   <td class='+ this.prefix +'powerboxMiddle valign=top>' +
             '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
             subTitleRow +
             '    <tr class='+ this.prefix +'powerboxMiddleBackground>' +
             '     <td class='+ this.prefix +'powerboxMiddleLower align='+ this.contentAlign +' width=100%>'+ this.contentDiv +'</td>' +
             '    </tr>' +
             '    </table>' +
             '   </td>' +

             // Middle Right
//              '   <td class='+ this.prefix +'powerboxMiddleRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxMiddleRightImage src=img/trans.gif></td>' +

             '  </tr>' +
             '  </table>' +
             ' </td>' +
             '</tr>' +


             // Bottom Row
             // ====================
             '<tr>' +
             ' <td width=100% height=100% valign=top>' +
             '  <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
             '  <tr>' +

             // Bottom Left Corner
//              '   <td class='+ this.prefix +'powerboxBottomLeft height=100% valign=top>' +
//              '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxBottomLeftUpperLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomLeftUpperLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxBottomLeftUpperRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomLeftUpperRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxBottomLeftLowerLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomLeftLowerLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxBottomLeftLowerRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomLeftLowerRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    </table>' +
//              '   </td>' +

             // Bottom Title Section (todo: fixate close div)
             '   <td class='+ this.prefix +'powerboxBottom width=100% height=100% valign=top>' +
             '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
             '    <tr>' +
             '     <td class='+ this.prefix +'powerboxBottomUpper>' + this.footerDiv + '</td>' +
             '    </tr>' +
             '    <tr>' +
             '     <td class='+ this.prefix +'powerboxBottomLower>' +
             '<img width=100% height=100% id='+ this.prefix +'powerboxBottomLowerImage src=img/mzlightbox/'+ this.prefix +'bottom_lower.png></td>' +
             '    </tr>' +
             '    </table>' +
             '   </td>' +

             // Bottom Right Corner
//              '   <td class='+ this.prefix +'powerboxBottomRight height=100% valign=top>' +
//              '    <table border='+ this.border +' width=100% height=100% cellpadding=0 cellspacing=0>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxBottomRightUpperLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomRightUpperLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxBottomRightUpperRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomRightUpperRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    <tr>' +
//              '     <td class='+ this.prefix +'powerboxBottomRightLowerLeft>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomRightLowerLeftImage src=img/trans.gif></td>' +
//              '     <td class='+ this.prefix +'powerboxBottomRightLowerRight>' +
//              '<img width=1 height=1 id='+ this.prefix +'powerboxBottomRightLowerRightImage src=img/trans.gif></td>' +
//              '    </tr>' +
//              '    </table>' +
//              '   </td>' +

             '  </tr>' +
             '  </table>' +
             ' </td>' +
             '</tr>' +


             '</table>' +
             '</div>';

    return boxDiv;
  }

  if (!isUnused(arguments)) {
    return this.setValues(arguments);
  }
  return false;
}


function powerboxInit(overlay,overlaycss) {
  // TODO: Add support for custom overlay class (with different background color/image)
	var objBody = document.getElementsByTagName("body").item(0);

  var objOverlay = document.getElementById("overlay");

  if (!objOverlay) {
    // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id','overlay');
    objOverlay.setAttribute('class', overlaycss+'overlay');
    objOverlay.setAttribute('className','overlay');
    //objOverlay.onclick = function () { lightboxHide(); return false; }
    objOverlay.style.display = 'none';
    objOverlay.style.position = 'absolute';
    objOverlay.style.top = '0';
    objOverlay.style.left = '0';
    objOverlay.style.zIndex = '1000';
    objBody.insertBefore(objOverlay, objBody.firstChild);
  }

  
  if (!document.getElementById("lightbox_" + powerboxActiveId)) {
    // create lightbox div, same note about styles as above
    var objLightbox = document.createElement("div");
    objLightbox.setAttribute('id','lightbox_' + powerboxActiveId);
    objLightbox.style.display = 'none';
    objLightbox.style.position = 'absolute';
    objLightbox.style.zIndex = 1001 + (powerboxZorder(powerboxActiveId) * 100);	
    objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	}
	

  if (!document.getElementById("lightboxContent_" + powerboxActiveId)) {
    // create lightbox content div
    var objLightboxContent = document.createElement("div");
    objLightboxContent.setAttribute('id','lightboxContent_' + powerboxActiveId);
    objLightboxContent.style.position = 'static';
    objLightboxContent.style.zIndex = '2';
    objLightboxContent.style.display = 'block';
    objLightbox.appendChild(objLightboxContent);


  }
	
}




function powerboxGetPageSize() {
  return new Array($(document).width(), $(document).height());
}


//
// powerboxGetPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function powerboxGetPageScroll() {

	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;
}



//
// powerboxAdjustOverlay()
// Update the overlay height as the lightbox might have increased the overall page height.
//
function powerboxAdjustOverlay()
{
  arrayPageSize = powerboxGetPageSize();
  var objOverlay = document.getElementById('overlay');
	if (objOverlay) {
    objOverlay.style.width = (arrayPageSize[0] + 'px');
    objOverlay.style.height = (arrayPageSize[1] + 'px');
    objOverlay.style.display = 'block';
  }
}


//
// powerboxSetPos()
//
function powerboxSetPos(x, y, Id) {
  id = updateVariable(id, null, powerboxActiveId);

	var objLightbox = document.getElementById('lightbox_'+id);
	if (objLightbox) {
    objLightbox.style.left = (x < 0) ? "0px" : x + "px";
    objLightbox.style.top = (y < 0) ? "0px" : y + "px";
  }

  powerboxAdjustOverlay();
}

//
// lightboxSetPosX()
//
function powerboxSetPosX(x, id) {
  id = updateVariable(id, null, powerboxActiveId);

	var objLightbox = document.getElementById('lightbox_' + lId);
	if (objLightbox) {
    objLightbox.style.left = (x < 0) ? "0px" : x + "px";
  }

  powerboxAdjustOverlay();
}

//
// powerboxSetPosY()
//
function powerboxSetPosY(y, id)
{
  id = updateVariable(id, null, powerboxActiveId);
  
  y = adjustTopForAffiliates(y);

  var objLightbox = document.getElementById("lightbox_"+id);
	if (objLightbox) {
    objLightbox.style.top = (y < 0) ? "0px" : y + "px";
  }

  powerboxAdjustOverlay();
}

function adjustTopForAffiliates(y) {
  var contentDiv = document.getElementById('contentDiv');
  if (contentDiv)
    y += contentDiv.offsetTop;
  return y;
}

function powerboxGetCurrentId()
{
  return powerboxActiveId;
}

function powerboxGetIdBeforeId(Id)
{
  for (i=0 ; i < powerboxOrderArray.length ; i++) {
    if (powerboxOrderArray[i] != id) {
      return powerboxOrderArray[i];
    }
  }
}

function powerboxGetIdAfterId(Id)
{
  for (i=0 ; i < powerboxOrderArray.length ; i++) {
    if (powerboxOrderArray[i] != id) {
      if((i + 1) < powerboxOrderArray.length)
        return powerboxOrderArray[i + 1];
      else
        return -1;
    }
  }
}

/**
* Return the with to bes used for the loading box
* Use the width from selected (id) or currently active box (powerboxActiveId) if present as base
*/
function powerboxGetLoadingWidth(id) {
  var id     = updateVariable(id, null, powerboxActiveId);
  var width  = 300; // minimum width
  var margin = 20; // margin from parent box width
  var objLightbox = document.getElementById("lightbox_"+id);
  if (objLightbox && typeof(powerboxObjArray["id_"+id]) != "undefined") {
    width = (objLightbox.offsetWidth > (300+margin)) ? objLightbox.offsetWidth-margin : 300;
  }
  return width;
}


/**
* Shows a loading animation and if responseFunction == "writeLayer" it replaces the contents of the lightbox with the reply of the ajax call.
* To avoid breaking something responseParameter has to be omitted for this to take action.
*/
function ajaxCallLightboxLoading(url, parameters, responseFunction, responseParameter) {
  var id = powerboxActiveId;

	if(responseFunction == "writeLayer" && responseParameter == null) {
		id = "lightboxLoadingTable" + powerboxActiveId;
		responseParameter = id;
	}

  var pbox            = new Object();
  pbox["id"]          = "ajaxLoading";
  if (typeof(powerboxObjArray["id_"+powerboxActiveId]) != "undefined") {
    pbox["parent"]      = powerboxActiveId;
  }
  pbox["width"]          = powerboxGetLoadingWidth(powerboxActiveId);
  pbox["prefix"]         = "old";
  pbox["closeButton"]    = false;
  pbox["closeButtonCSS"] = "old";

  pbox["content"] = "<span id='"+ id +"'><table width=100%>" +
                    "<tr><td align=center>"+ ajaxLoader +"</td></tr>" +
                    "<tr><td align=center><img src=\"img/ajax_loader.gif\" alt=\"Loading\" width=\"16\" height=\"16\"></td></tr>" +
                    "</table></span>";


  powerboxDisable(powerboxActiveId);

  lightboxIsActive = true;

  powerboxShow(pbox);
	
  ajaxCall(url, parameters, responseFunction, responseParameter);
}


