

var borderSize = 10;
var	IFRAMEData = new Object();

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});



//	IframeLightbox Class Declaration
//	- initialize()
//	- start()
//	- IFRAMEchangeImage()
//	- IFRAMEresizeImageContainer()
//	- IFRAMEshowImage()
//	- IFRAMEupdateDetails()
//	- end()
//

var IframeLightbox = Class.create();

IframeLightbox.prototype = {
	
	// initialize() Sets up any link with a rel of 'iframe' and creates the lightbox on the page.
	initialize: function() {	
		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];
			var relAttribute = String(anchor.getAttribute('rel'));
			// catch 'iframe' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.indexOf('iframe')!=-1)){
				anchor.onclick = function () {myIframeLightbox.start(this,this.getAttribute('width'),this.getAttribute('height')); return false;}
			}
		}

		//Insert html onto page

		var IFRAMEobjBody = document.getElementsByTagName("body").item(0);
		
		var IFRAMEobjoverlay = document.createElement("div");
		IFRAMEobjoverlay.setAttribute('id','overlay');
		IFRAMEobjoverlay.style.display = 'none';
		//IFRAMEobjoverlay.onclick = function() { myIframeLightbox.end(); return false; }
		IFRAMEobjBody.appendChild(IFRAMEobjoverlay);
		
		var IFRAMEobjLightbox = document.createElement("div");
		IFRAMEobjLightbox.setAttribute('id','IFRAMELightbox');
		IFRAMEobjLightbox.style.display = 'none';
		IFRAMEobjBody.appendChild(IFRAMEobjLightbox);

		var IFRAMEobjImageDataContainer = document.createElement("div");
		IFRAMEobjImageDataContainer.setAttribute('id','IFRAMEimageDataContainer');
		IFRAMEobjImageDataContainer.className = 'clearfix';
		IFRAMEobjLightbox.appendChild(IFRAMEobjImageDataContainer);
	
		var IFRAMEobjOuterImageContainer = document.createElement("div");
		IFRAMEobjOuterImageContainer.setAttribute('id','IFRAMEouterImageContainer');
		IFRAMEobjOuterImageContainer.setAttribute('class','IFRAMEiframeOutImageContainer');
		IFRAMEobjLightbox.appendChild(IFRAMEobjOuterImageContainer);

		var IFRAMEobjImageContainer = document.createElement("div");
		IFRAMEobjImageContainer.setAttribute('id','IFRAMEimageContainer');
		IFRAMEobjOuterImageContainer.appendChild(IFRAMEobjImageContainer);
			
		var IFRAMEobjLightboxIframe = document.createElement("iframe");
		IFRAMEobjLightboxIframe.setAttribute('id','IframeLightboxIframe');
		IFRAMEobjLightboxIframe.style.borderStyle = 'none';
		IFRAMEobjLightboxIframe.setAttribute('scrolling', 'no');
		IFRAMEobjLightboxIframe.setAttribute('frameborder', '0');
		IFRAMEobjLightboxIframe.setAttribute('background-color', '#666666');
		IFRAMEobjLightboxIframe.setAttribute('color', '#666666');
		IFRAMEobjImageContainer.appendChild(IFRAMEobjLightboxIframe);
    
		var IFRAMEobjLightboxImage = document.createElement("img");
		IFRAMEobjLightboxImage.setAttribute('id','IframeLightboxImage');
		IFRAMEobjImageContainer.appendChild(IFRAMEobjLightboxImage);

		var IFRAMEobjImageData = document.createElement("div");
		IFRAMEobjImageData.setAttribute('id','IFRAMEimageData');
		IFRAMEobjImageDataContainer.appendChild(IFRAMEobjImageData);
	
		var IFRAMEobjImageDetails = document.createElement("div");
		IFRAMEobjImageDetails.setAttribute('id','IFRAMEimageDetails');
		IFRAMEobjImageData.appendChild(IFRAMEobjImageDetails);
	
		var IFRAMEobjCaption = document.createElement("span");
		IFRAMEobjCaption.setAttribute('id','IFRAMEcaption');
		
		
		IFRAMEobjOuterImageContainer.appendChild(IFRAMEobjCaption);
	
		var IFRAMEobjTopNav = document.createElement("div");
		IFRAMEobjTopNav.setAttribute('id','IFRAMEtopNav');
		IFRAMEobjImageData.appendChild(IFRAMEobjTopNav);
	
		var IFRAMEobjTopNavCloseLink = document.createElement("a");
		IFRAMEobjTopNavCloseLink.setAttribute('id','IFRAMEtopNavClose');
		IFRAMEobjTopNavCloseLink.setAttribute('href','#');
		IFRAMEobjTopNavCloseLink.onclick = function() { myIframeLightbox.IFRAMEend(); return false; }
		IFRAMEobjTopNav.appendChild(IFRAMEobjTopNavCloseLink);
	
		var IFRAMEobjTopNavCloseImage = document.createElement("img");
		IFRAMEobjTopNavCloseImage.setAttribute('id','closeImage');
		IFRAMEobjTopNavCloseImage.setAttribute('src', 'flvPlayer/close.jpg');
		IFRAMEobjTopNavCloseLink.appendChild(IFRAMEobjTopNavCloseImage);
		
		//Style html
		updateCSS();

	},
	
	//
	//	start()
	//	Display overlay and IframeLightbox.
	//
	start: function(IFRAMEimageLink,wid,hei) {

		// stretch overlay to fill page and fade in
		var arrayPageSize = IFRAMEgetPageSize();
		Element.setHeight('overlay', arrayPageSize[1]);
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 });		

		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		
		IFRAMEData.flv=IFRAMEimageLink.getAttribute('href');
		IFRAMEData.caption='';

		// calculate top offset for the IframeLightbox and display 
		var arrayPageSize = IFRAMEgetPageSize();
		var arrayPageScroll = IFRAMEgetPageScroll();
		var IframeLightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

		Element.setTop('IFRAMELightbox', IframeLightboxTop);
		Element.show('IFRAMELightbox');
		
		this.IFRAMEchangeImage(wid,hei);
	},

	//
	//	IFRAMEchangeImage()
	//	Hide most elements and preload image in preparation for resizing image container.
	//
	IFRAMEchangeImage: function(wid,hei) {	

		// hide elements during transition
		Element.hide('IframeLightboxImage');
		Element.hide('IframeLightboxIframe');
		Element.hide('IFRAMEimageDataContainer');	
		
		//Grab site url to pass along to the iframe
		var earl=window.location.href;
		var splitme=earl.split("/");
		if ((earl.indexOf("proof.")!=-1) || (earl.indexOf("wip.")!=-1)) {
			var firstHalfUrl="http://"+splitme[2]+"/"+splitme[3];
		} else {
			if (splitme[2].indexOf("?")!=-1) {
				splitme[2]=splitme[2].slice(0,splitme[2].indexOf("?"));
			}
			var firstHalfUrl="http://"+splitme[2];
		}
		
		//Crate url with parameters for iframe appended to the end
		var fullURL="flvPlayer/FLVPlayer.html?from="+firstHalfUrl+"&flv="+IFRAMEData.flv+"&reload=true&wid="+wid+"&hei="+hei+"&rt=0:30";
		
		//Load iframe content
		Element.setSrc('IframeLightboxIframe', fullURL);
		
		myIframeLightbox.IFRAMEresizeImageContainer(parseFloat(wid), parseFloat(hei));

	},

	//
	//	IFRAMEresizeImageContainer()
	//
	IFRAMEresizeImageContainer: function( imgWidth, imgHeight) {
		//Adjust height and widtht to accommodate iframe.
		imgWidth+=30;
		imgHeight+=50;
		
		$('IframeLightboxIframe').style.height = imgHeight + 'px';
		$('IframeLightboxIframe').style.width = imgWidth + 'px';
		$('IframeLightboxIframe').style.border = 'none';


		Element.setWidth( 'IFRAMEimageDataContainer', imgWidth + (borderSize * 2));
		Element.setWidth( 'IFRAMEouterImageContainer', imgWidth+(borderSize * 2));
		Element.setHeight( 'IFRAMEouterImageContainer', imgHeight + (borderSize * 2));		

		this.IFRAMEshowImage();
	},
	
	//
	//	IFRAMEshowImage()
	//	Display image and begin preloading neighbors.
	//
	IFRAMEshowImage: function(){
		jQuery("#IFRAMEimageDataContainer").css("display","block");
		new Effect.Appear('IframeLightboxIframe', { duration: 0.5, queue: 'IFRAMEend', afterFinish: function(){	myIframeLightbox.IFRAMEupdateDetails(); } });
	},

	//
	//	IFRAMEupdateDetails()
	//	Display caption and top nav.
	//
	IFRAMEupdateDetails: function() {	
		Element.show('IFRAMEcaption');
		Element.setInnerHTML( 'IFRAMEcaption', IFRAMEData.caption);
	},


	//
	//	IFRAMEend()
	//
	IFRAMEend: function() {
		Element.hide('IFRAMELightbox');
		new Effect.Fade('overlay', { duration: 0.2});
    Element.setSrc('IframeLightboxIframe', 'about:blank');
	}
}

// -----------------------------------------------------------------------------------

//
// IFRAMEgetPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function IFRAMEgetPageScroll(){

	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;
}


//
// IFRAMEgetPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function IFRAMEgetPageSize(){
	
	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 IFRAMEwindowWidth, IFRAMEwindowHeight;
	if (self.innerHeight) {	// all except Explorer
		IFRAMEwindowWidth = self.innerWidth;
		IFRAMEwindowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		IFRAMEwindowWidth = document.documentElement.clientWidth;
		IFRAMEwindowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		IFRAMEwindowWidth = document.body.clientWidth;
		IFRAMEwindowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < IFRAMEwindowHeight){
		IFRAMEpageHeight = IFRAMEwindowHeight;
	} else { 
		IFRAMEpageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < IFRAMEwindowWidth){	
		IFRAMEpageWidth = IFRAMEwindowWidth;
	} else {
		IFRAMEpageWidth = xScroll;
	}


	arrayPageSize = new Array(IFRAMEpageWidth,IFRAMEpageHeight,IFRAMEwindowWidth,IFRAMEwindowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------


function updateCSS() {

	jQuery("#IFRAMELightbox").css({'position' : 'absolute', 'top' : '20px', 'width' : '100%', 'z-index' : '100', 'left' : '0', 'color' : '#151410', 'font-weight' : 'normal', 'text-align' : 'center'});

	jQuery("#IFRAMEimageContainer").css({'left' : '10px', 'position' : 'relative'});

	jQuery("#IFRAMEtopNavClose").css({'float' : 'right', 'padding-right' : '10px'});

	jQuery("#IFRAMEouterImageContainer").css({'background-color' : '#666666', 'margin' : '0 auto', 'position' : 'relative', 'padding-bottom' : '20px'});

	jQuery("#IframeLightboxIframe").css({'border' : '0px', 'border-color' : '#666666', 'color' : '#666666', 'background-color' : '#666666'});

	jQuery("#IframeLightboxImage").css({'background-color' : '#666666', 'border-color' : '#666666'});

	jQuery("#IFRAMEimageDataContainer").css({'-x-system-font' : 'none', 'background-color' : '#666666', 'font-family' : 'Verdana,Helvetica,sans-serif', 'font-size' : '10px', 'font-size-adjust' : 'none', 'font-stretch' : 'normal', 'font-style' : 'normal', 'font-variant' : 'normal', 'font-weight' : 'normal', 'line-height' : '1.5em', 'margin' : '0 auto', 'padding' : '15px 0 0'});

	jQuery("#closeImage").css("border", "none");
	
	jQuery("#IFRAMEcaption").css('display','none');

}

function initIframeLightbox() { myIframeLightbox = new IframeLightbox(); }
Event.observe(window, 'load', initIframeLightbox, false);
