/*
	PopUpBox JS: Image Trailer 
	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/
	(basically, do anything you want, just leave my name and link)
	
	----------------------------------------------------------------
	Simple Image Trail script- By JavaScriptKit.com
	Visit http://www.javascriptkit.com for this script and more
	This notice must stay intact
	----------------------------------------------------------------
	
	Usage:
	- Add the attribute "imglnk" containing your popup image path.
	- Add the attribute "rel" containing the value "popupbox".
	- Use the title attribute for the popup text display.
	Example:
	<img imglnk="POPUP IMAGE LINK" rel="popupbox" title="POPUP DISPLAY TEXT" src="THUMBSOURCE">
	
	- Add the script tag right after the body tag.
	Example:
	<script type="text/javascript" src="popupbox/popupbox.js"></script>
	
	- Add the following to the pages onload function
	Example:
	initPopUpBox();
	
	Table of Contents
	-----------------
	Configuration
	
	Functions
	- getPageScroll()
	- getPageSize()
	- pause()
	- showPopUpBox()
	- hidePopUpBox()
	- initPopUpBox()

*/



//
// Configuration
//
var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 300;	// maximum image size.
var displaytime =5000; //time to display popup.
var timer;

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


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


//
// showPopUpBox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showPopUpBox(objLink)
{
	// prep objects
	var objPopUpBox = document.getElementById('popupbox');
	var objImage = document.getElementById('popupboxImage');
	var objCaption = document.getElementById('caption');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload image
	imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.getAttribute("imglnk");

		// center popupbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		//var popupboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		//var popupboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
		
		//objPopUpBox.style.top = (popupboxTop < 0) ? "0px" : popupboxTop + "px";
		//objPopUpBox.style.left = (popupboxLeft < 0) ? "0px" : popupboxLeft + "px";
		
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			//objCaption.style.width = imgPreload.width + 'px';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}
		
		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		} 

		objPopUpBox.style.display = 'block';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();

		return false;
	}
	
	timer = setTimeout('hidePopUpBox()', displaytime);
	
	currentimageheight = objImage.height + 25;
	document.onmousemove=followmouse;
	imgPreload.src = objLink.getAttribute("imglnk");	
}


//
// hidePopUpBox()
//
function hidePopUpBox()
{
	// get objects
	objPopUpBox = document.getElementById('popupbox');

	// hide popupbox and overlay
	objPopUpBox.style.display = 'none';
	document.onmousemove='';
	clearTimeout(timer);
	timer="";
}




//
// initPopUpBox()
// 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 initPopUpBox()
{
	if (!document.getElementsByTagName){ alert("problems"); return; }
	var getImages = document.getElementsByTagName('img');
	// loop through all image tags
	var imgCnt = getImages.length;
	for (var i=0; i<imgCnt; i++){
		var myImage = getImages[i];
		if (myImage.getAttribute("imglnk") && (myImage.getAttribute("rel") == "popupbox")){
			//myImage.onclick = function () {showPopUpBox(this); return false;}
			myImage.onmouseover = function () {showPopUpBox(this); return false;}
			myImage.onmouseout = function () {hidePopUpBox(); return false;}
		}
	}
	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="popupbox">
	//		<img id="popupboxImage" />
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// create lightbox div, and hardcode some functional styles (aesthetic styles are in CSS file)
	var objPopUpBox = document.createElement("div");
	objPopUpBox.setAttribute('id','popupbox');
	objPopUpBox.style.display = 'none';
	objPopUpBox.style.position = 'absolute';
	objPopUpBox.style.background = '#FFFFFF';
	objPopUpBox.style.border = 'solid 1px #000000';
	objPopUpBox.style.zIndex = '100';	
	objBody.insertBefore(objPopUpBox, objBody.firstChild);

	// create image
	var objImage = document.createElement("img");
	objImage.setAttribute('id','popupboxImage');
	objImage.style.margin = '5px 5px 5px 5px';
	objImage.onclick = function(){hidePopUpBox(); return false;}
	objPopUpBox.appendChild(objImage);
	
	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','caption');
	objCaption.style.display = 'none';
	objCaption.style.margin = '0px 5px 5px 5px';
	objPopUpBox.appendChild(objCaption);


}
function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function gettrailobj(){
if (document.getElementById)
return document.getElementById("popupbox").style
else if (document.all)
return document.all.popupbox.style
}

function gettrailobjnostyle(){
if (document.getElementById)
return document.getElementById("popupbox")
else if (document.all)
return document.all.popupbox
}

function followmouse(e){

	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]
	

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15;
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);
	//var docwidth = docArray[0];
	//var docheight = docArray[1];

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < 380){
			xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + 110)){
			// truebody().scrollTop is always zero in Safari 3.1, so we us documnet.body.scrollTop instead
			if ( document.body ){
				scrollTop = Math.max(truebody().scrollTop, document.body.scrollTop);
			} else {
				scrollTop = truebody().scrollTop;
			}
			ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < 380){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px";
	gettrailobj().top=ycoord+"px";

}

