/*
04-19-09
hideSelect visible/hidden
playMovie
*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";
	if (!version) version = detect.charAt(place + thestring.length);
	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 						= "an unknown operating system";
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function findPos(obj) {
	
	//alert(obj);
       
		var curleft = 0
		var curtop = 0;

        if (obj.offsetParent) {
		
		
		
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
			
			while (obj = obj.offsetParent);
			
			
			return [curleft, curtop];
		}
}
/*-----------------------------------------------------------------------------------------------*/
Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);
var lightbox = Class.create();
lightbox.prototype = {
	yPos : 0,
	xPos : 0,
	initialize: function(ctrl) {
		
		this.activebutton = ctrl.id;
		this.content = ctrl.rel;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
			
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		this.tabPanelShowHide();
		//display,zindexLightbox,buttonClassname,buttonZindex,zindexOverlay
		return false;
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		//bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		//htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
		
		iframes = document.getElementsByTagName('iframe');
		for(i = 0; i < iframes.length; i++) {
			iframes[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	
	playMovie: function() {
		var ol = $('overlay');
		var olay = new SWFObject("wp-content/themes/default/js/overlay.swf", "lb", "100%", "100%", "9", "#FFFFFF");
		olay.addParam("scale", "noscale");
		olay.addParam("allowFullScreen", "true");
		olay.addParam("wmode", "transparent");
		olay.write(ol);
	},


	tabPanelShowHide: function(){
		
	
		var ov = document.getElementById('overlay');
		var buttonId= this.activebutton;
		
		var but = document.getElementById(buttonId +'-on');
		//If overlay exists, remove it.
		
		if(ov) {


			document.getElementsByTagName('body')[0].removeChild(ov);
			document.getElementsByTagName('body')[0].removeChild(but);

			$(this.content).style.display = 'none';
			$(this.content).style.zIndex = 1;
	
		
		
		} else {

			//If overlay does not exist, create the overlay.
			bod 				= document.getElementsByTagName('body')[0];
			overlay 			= document.createElement('div');
			overlay.id			= 'overlay';
			overlay.style.zIndex = 8;
			
			//Create active tab
			var buttonId= this.activebutton;
			button 			= document.createElement('div');
			button.id		= buttonId +'-on';
			button.style.zIndex = 11;
			button.style.display = 'block';
			
			//Suppress input field
			this.hideSelects('hidden');
			
			//Add objects to DOM
			bod.appendChild(overlay);
			bod.appendChild(button);
	
			//Get active button x and y and apply to active button div container
			activeButtonObj = $(buttonId);

			button.style.top=findPos(activeButtonObj)[1]+'px';
			button.style.left=findPos(activeButtonObj)[0]+'px';

	
			$(this.content).style.marginLeft = '-9998px'; //move it out of view until the calculations are done to avoid the flicker
			$(this.content).style.display = 'block';
			$(this.content).style.zIndex = 10;
			$(this.content).parentNode.style.zIndex = 10;
			var leftPositionOfActiveButton = findPos(activeButtonObj)[0];
			var buttonWidthHalved = activeButtonObj.offsetWidth/2;
			var panelWidthHalved = $(this.content).offsetWidth/2;
			var calculatedLeft = $(this.content).style.left= (leftPositionOfActiveButton - panelWidthHalved) + buttonWidthHalved;
			var containerObj = document.getElementById('lightbox-container');
			
				
			if (calculatedLeft<findPos(containerObj)[0]) {
					//alert('less');
					$(this.content).style.left=0;
			} else {
					//alert('okay');
					$(this.content).style.left=calculatedLeft-findPos(containerObj)[0]+'px';
					
			}
			$(this.content).style.marginLeft = '0'; 
			this.playMovie();
			this.actions();
			
			
		}

		
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){

		var overlay = document.getElementById('overlay');
		Event.observe(overlay, 'mouseover', this.deactivate.bindAsEventListener(this), false);
		overlay.onmouseover = function(){return false};
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
			this.hideSelects("visible");
			this.tabPanelShowHide();

	}
}
/*-----------------------------------------------------------------------------------------------*/
// Onload, make all links that need to trigger a lightbox active
function initialize(){
	lbox = document.getElementsByClassName('lightbox-navigation-link');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}

}


//For IE6 Flicker
try {
document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}
