/*
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
*/


//
// quirksmode.org code
//
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	
  return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

  return curtop;
}


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



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


//
// showLightbox()
//
function showLightbox()
{
  var winWidth = getPageSize()[0];

  // position and show the "bubble" tip above it's caller
  var lightboxTip = document.getElementById("lightboxTip");
  lightboxTip.style.display = "block";
  lightboxTip.style.top = findPosY(this) - lightboxTip.offsetHeight - 3 + "px";
  lightboxTip.style.left = findPosX(this) - (lightboxTip.offsetWidth / 2) + (this.offsetWidth / 2) + "px";

  // position and show the "bubble" itself above the tip
  var lightbox = document.getElementById("lightbox");
  lightbox.firstChild.firstChild.innerHTML = document.getElementById(this.id.replace("lb", "bubble")).innerHTML;
  lightbox.style.display = "block";

  // Must reset the width of the lightbox to its default everytime in case the width is increased for too much content
  
  lightbox.style.width = "242px";
  
  //homepage refundable fares bubble - booktravel skylights bubble
  if(this.id == 'lb40' || this.id == 'lb41' || this.id == 'lbFl1')  lightbox.style.width = "700px";
  
  // IF the bubble cannot fit into the space, continually increase the width until it can
  // limit the width from increasing beyond 1000px
  var currH = lightbox.offsetHeight;
  var maxH = findPosY(lightboxTip) - 5;

  if(currH > maxH) {
    lightbox.style.width = ((lightbox.offsetWidth * currH) / maxH) + "px";
    var tempH = lightbox.offsetHeight;
    while(tempH > maxH && lightbox.offsetWidth < 1000) {
      lightbox.style.width = lightbox.offsetWidth + 10 + "px";
      tempH = lightbox.offsetHeight;
    }
  }

  // position the "bubble" to prevent horizontal scrolling or from being cut off on the left when trying to center over caller
  // unless the "bubble" is just too large for the wiewport than we have no choice but to scroll
  var lbWidth = lightbox.offsetWidth;
  var defaultX = findPosX(this) - (lbWidth / 2) + (this.offsetWidth / 2);
  var diff = winWidth - (defaultX + lbWidth);

  if((defaultX < 0) || (lbWidth >= winWidth))
    lightbox.style.left = "10px";
  else
    lightbox.style.left = (diff < 0) ? defaultX + diff - 20 + "px": defaultX + "px";

  lightbox.style.top = findPosY(lightboxTip) - lightbox.offsetHeight + 5 + "px";
}


//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
  var lb = document.getElementById("lightbox");
  var lbTip = document.getElementById("lightboxTip");

	// hide lightbox and overlay
	lb.style.display = "none";
  lbTip.style.display = "none";
}


//
// lightbox offclick handler
// onclick of anywhere on body, check if mouse is positioned over the lightbox
// if mouse x/y is outside x/y of lightbox, hide - else do nothing
// if clicking on the view details link itself, do not execute the function
//
function ltBoxOffClick(e) {
  if(!e) e = window.event;

  var caller = e.srcElement ? e.srcElement : e.target;
  if(caller.className.indexOf('phantom') != -1) return;

  var mx, my, lx, ly, lWidth, lHeight;
  if(e.pageX && e.pageY) {
    mx = e.pageX; my = e.pageY;
  }

  else if(e.clientX && e.clientY) {
    mx = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    my = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  }

  else return;
  
  var lb = document.getElementById("lightbox");

  lx = findPosX(lb); ly = findPosY(lb);
  lWidth = lb.offsetWidth; lHeight = lb.offsetHeight;
  
  if(mx < lx || mx > (lx + lWidth) || my < ly || my > (ly + lHeight))
    hideLightbox();
}


//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox(version)
{	
	if (!document.getElementsByTagName){ return; }
  var objBody = document.getElementsByTagName("body").item(0);
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")) {
       
      anchor.onclick = showLightbox;
		}
	}

  // check to see if a lightbox doesnt exist already. 
  if(document.getElementById("lightbox"))
    return;

  // create the lightbox 
  var lb = document.createElement("div"); 
  lb.id = "lightbox";
  var lbTR = document.createElement("div");
  lbTR.id = "lbTopR";
  var lbTL = document.createElement("div");
  lbTL.id = "lbTopL";
  var lbBR = document.createElement("div");
  lbBR.id = "lbBotR";
  var lbBL = document.createElement("div");
  lbBL.id = "lbBotL";
  var closeHref = document.createElement("a"); closeHref.href = "javascript:void(0)"; closeHref.onclick = hideLightbox;
  var closeBtn = document.createElement("img");
  closeBtn.src = "";
  
  closeHref.appendChild(closeBtn);
  lbBL.appendChild(closeHref);
  lbBR.appendChild(lbBL);
  lbTR.appendChild(lbTL);
  lb.appendChild(lbTR);
  lb.appendChild(lbBR);

  objBody.insertBefore(lb, objBody.firstChild);

  var lbTip = document.createElement("div");
  lbTip.id = "lightboxTip";
  var lbTipImg = document.createElement("img"); 
  lbTipImg.src = "";
  
  lbTip.appendChild(lbTipImg);
  objBody.insertBefore(lbTip, objBody.firstChild);

  YAHOO.util.Event.addListener(document.body, "click", ltBoxOffClick);
}
