﻿/*
	The terrible monster child of Lightbox and the YAHOO js libraries.
	Dan Messing
	http://www.stuntsoftware.com/
	
	
	Lightbox details:
	
	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/
	(basically, do anything you want, just leave my name and link)
*/



//
// Configuration
//

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = 'spinner.gif';		
var closeButton = 'close.png';
var isIE = false;
if ( navigator.userAgent.indexOf("MSIE") != -1 ) {
	isIE = true;
}


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


function showImagePopup(objLink)
{
	if ( document.getElementById )
	{
		var objOverlay = document.getElementById('overlay');
		var objLoadingImage = document.getElementById('loadingImage');
		var popupImage = document.getElementById('popupImage');
		var extraInfo = document.getElementById('extraInfo');
		var closeButton = document.getElementById('closeButton');
		var centerCaption = document.getElementById('centerCaption');
		var captionText = document.getElementById('captionText');

		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();

		// center loadingImage if it exists
		if (objLoadingImage) {
			objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - objLoadingImage.height) / 2) + 'px');
			objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
			objLoadingImage.style.display = 'block';
		}
		popupImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3]) / 2) + 'px');
		popupImage.style.left = (((arrayPageSize[0] - 20) / 2) + 'px');

		// set height of Overlay to take up whole page and show
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		objOverlay.style.opacity = '0.0';
		objOverlay.style.filter = 'alpha(opacity=0)';
		objOverlay.style.display = 'block';

		// set up the close button
		extraInfo.style.opacity = '0.0';
		extraInfo.style.filter = 'alpha(opacity=0)';
		extraInfo.style.display = 'block';
		extraInfo.style.height = (arrayPageSize[1] + 'px');
		
		// set up the caption
		if(objLink.getAttribute('title')){
			centerCaption.style.display = 'block';
			captionText.innerHTML = objLink.getAttribute('title');
		} else {
			centerCaption.style.display = 'none';
		}

		var attributes = {
			opacity: { to: 0.8 }
		}
		var fadeAnim = new YAHOO.util.Anim('overlay', attributes, 0.6, YAHOO.util.Easing.easeOut);

		fadeAnim.animate();

		var imgPreload = new Image();
		imgPreload.onload=function()
		{
			closeButton.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 45 - imgPreload.height) / 2) + 'px');
			closeButton.style.left = (((arrayPageSize[0] - 25 - imgPreload.width) / 2) + 'px');
/*			centerCaption.style.left = '5px';*/
			centerCaption.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] + imgPreload.height + 20) / 2) + 'px');
			popupImage.src = objLink.href;
			popupImage.style.width = "48px";
			popupImage.style.height = "48px";

			var endPoint = [((arrayPageSize[0] - imgPreload.width) / 2),
							arrayPageScroll[1] + (arrayPageSize[3] - 20 - imgPreload.height) / 2];

			var attributes = {
				points: { to: endPoint },
				width: { to: imgPreload.width + 1 },
				height: { to: imgPreload.height + 1 }
			}
			var anim = new YAHOO.util.Motion('popupImage', attributes, .3, YAHOO.util.Easing.backOut);
		//	var anim = new YAHOO.util.Motion('popupImage', attributes, .3, YAHOO.util.Easing.easeIn);

			if (objLoadingImage) { objLoadingImage.style.display = 'none'; }

			popupImage.style.display = 'block';
		
			// 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);
			} 

			anim.animate();

			var showExtraInfoFunction = function() {
				var extraInfoAtts = { opacity: { to: 0.99 } }	// 1.0 causes a flicker in firefox
				var closeAnim = new YAHOO.util.Anim('extraInfo', extraInfoAtts, 0.8, YAHOO.util.Easing.easeOut);
				closeAnim.animate();
			}
		
			anim.onComplete.subscribe(showExtraInfoFunction);
		
			// After image is loaded, update the overlay height as the new image might have
			// increased the overall page height.
			arrayPageSize = getPageSize();
			objOverlay.style.height = (arrayPageSize[1] + 'px');

			return false;
		}
		imgPreload.src = objLink.href;
	}
}


//
// hidePopup()
//
function hidePopup()
{
	if ( document.getElementById )
	{
		// get objects
		var objOverlay = document.getElementById('overlay');
		var objPopupImage = document.getElementById('popupImage');
		var extraInfo = document.getElementById('extraInfo');
		
		var closeButton = document.getElementById('closeButton');

		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();

		var endPoint = [((arrayPageSize[0] - 24) / 2),
						arrayPageScroll[1] + (arrayPageSize[3] - 20 - 24) / 2];

		var fadeAtts = { opacity: { to: 0.0 } }
		var fadeBgAnim = new YAHOO.util.Anim('overlay', fadeAtts, 0.2, YAHOO.util.Easing.easeIn);
		var fadeOutExtrasAnim = new YAHOO.util.Anim('extraInfo', fadeAtts, 0.4, YAHOO.util.Easing.easeOut);
		fadeBgAnim.animate();
		fadeOutExtrasAnim.animate();

		var hideExtras = function() {
			extraInfo.style.display = 'none';
			objOverlay.style.display = 'none';
		}

		fadeBgAnim.onComplete.subscribe(hideExtras);

		var attributes = {
			points: { to: endPoint },
			width: { to: 48 },
			height: { to: 48 }
		}
		var anim = new YAHOO.util.Motion('popupImage', attributes, .3, YAHOO.util.Easing.backIn);
	//	var anim = new YAHOO.util.Motion('popupImage', attributes, .3, YAHOO.util.Easing.easeIn);

		anim.animate();

		var hideImage = function() {
			objPopupImage.style.display = 'none';
		}
		anim.onComplete.subscribe(hideImage);
	}
}




//
// initPopup()
// 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 initPopup()
{
	if (!document.getElementsByTagName){ return; }
	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") == "imagepopup")){
			anchor.onclick = function () {showImagePopup(this); return false;}
		}
	}
	

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	//		<a href="#" onclick="hidePopup(); return false;"><img id="loadingImage" /></a>
	//	</div>
	// <div id="extraInfo">
	//		<a href="#" onclick="hidePopup(); return false;" title="Click anywhere to close image">
	//			<img id="closeButton" />		
	//		</a>
	//
	//	<div id="centerCaption">
	//	<table align="center" border="0" cellpadding="0" cellspacing="0">
	//		<tr>
	//			<td><img src="images/caption-cap-left.png" width="12" height="25"></td>
	//			<td id="captionText"></td>
	//			<td><img src="images/caption-cap-right.png" width="12" height="25"></td>
	//		<tr>
	//	</table>
	//	</div>
	//
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hidePopup(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	// create the popup image;
	var popupImage = document.createElement("img");
	popupImage.setAttribute('id','popupImage');
	popupImage.style.position = 'absolute';
	popupImage.style.display = 'none';
	popupImage.onclick = function () {hidePopup(); return false;}
	popupImage.style.zIndex = '200';
	objBody.insertBefore(popupImage, objOverlay.nextSibling);

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hidePopup(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImageLink.appendChild(objLoadingImage);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}
	imgPreloader.src = loadingImage;

	// create extra info div, same note about styles as above
	var objExtraInfo = document.createElement("div");
	objExtraInfo.onclick = function () {hidePopup(); return false;}
	objExtraInfo.setAttribute('id','extraInfo');
	objExtraInfo.style.display = 'none';
	objExtraInfo.style.position = 'absolute';
	objExtraInfo.style.top = '0';
	objExtraInfo.style.left = '0';
	objExtraInfo.style.zIndex = '250';
	objExtraInfo.style.width = '100%';
	objBody.insertBefore(objExtraInfo, objOverlay.nextSibling);
	
	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Click to close');
	objLink.onclick = function () {hidePopup(); return false;}
	objExtraInfo.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload = function(){

		var objCloseButton = document.createElement("img");
		if (isIE) {
			objCloseButton.src = "transparent-close.gif";
		} else {
			objCloseButton.src = "close.png";
		}
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		if (isIE) {
			objCloseButton.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',src='close.png')";
		}
		objLink.appendChild(objCloseButton);

		return false;
	}
	imgPreloadCloseButton.src = closeButton;
	
	var centerCaption = document.createElement("div");
	centerCaption.setAttribute('id','centerCaption');
	centerCaption.style.position = 'absolute';
	centerCaption.style.top = '100';
	centerCaption.style.left = '0';
	centerCaption.style.width = '100%';  
	
	objExtraInfo.appendChild(centerCaption);

	if (isIE) {
		var captionLeftImg = "transparent.gif";
		var captionLeftStyle = "style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',src='caption-cap-left.png');\"";
		var captionRightImg = "transparent.gif"
		var captionRightStyle = "style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',src='caption-cap-right.png');\"";
	} else {
		var captionLeftImg = "caption-cap-left.png";
		var captionLeftStyle = "";
		var captionRightImg = "caption-cap-right.png"
		var captionRightStyle = "";
	}

	centerCaption.innerHTML = '<table align="center" border="0" cellpadding="0" cellspacing="0"><tr><td><img src="' + captionLeftImg + '" ' + captionLeftStyle + ' width="12" height="25"></td><td id="captionText">hello</td><td><img src="' + captionRightImg + '" ' + captionRightStyle + ' width="12" height="25"></td><tr></table>';
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

addLoadEvent(initPopup);	// run initPopup onLoad

function youTubePop(ytURL)
{
	testwindow= window.open (ytURL, "YouTube", "scrollbars=no,resizable=no,toolbar=no,menubar=no,status=no,location=no,width=630,height=630");
}
