/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
And the lightbox gone wild by ParticleTree at http://particletree.com/features/lightbox-gone-wild/

*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

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

/*-----------------------------------------------------------------------------------------------*/

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


/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', getBrowserInfo, false);


var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
			this.content = ctrl.rel;
			Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
			ctrl.onclick = function(){getLinkData(this.id); return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			//this.prepareIE('100%', 'hidden');
			this.setScroll(0,this.yPos);
			this.hideSelects('hidden');
		}
		$('LB').style.display = 'block';
	},
	
	// 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;
		}
	},
	
	// 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); 
	},
	
	displayLightbox: function(display){
		if(display != 'none') this.actions();		
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');
		for(i = 0; i < lbActions.length; i++) {
			if (firstBoot) {
				Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
				lbActions[i].onclick = function(){this.deactivate;return false;};
				firstBoot = false;
			}
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			//this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		hideLB();
		refreshTab();
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){	
	var arrayPageSize = getPageSize();

	if (parseInt((90*arrayPageSize[2])/100) < 800) {
		$('contentLB').style.width = parseInt(((90*arrayPageSize[2])/100)-20) + 'px';
		$('LB').style.left = parseInt((5*arrayPageSize[2])/100) + 'px';
	}
	else {
		$('contentLB').style.width = '800px';
		$('LB').style.left = '80px';
	}
	$('LB').style.top = parseInt((5*arrayPageSize[3])/100) + 'px';

	window.scrollTo(0, 0);

	lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
	if ($('LB').style.display == '') 
		$('overlay').style.display = 'none';

	if (parseInt($('contentLB').clientHeight + 600) > parseInt((90*arrayPageSize[3])/100))
		$('contentLB').style.height = parseInt((90*arrayPageSize[3])/100) + 'px';
}

function showOverlay() {
	var arrayPageSize = getPageSize();
	$('overlay').style.width = "980px";
	$('overlay').style.height = arrayPageSize[1] +"px";
	hideSelectBoxes();
	$('overlay').style.display = 'block';
}

function hideOverlay() {
	$('overlay').style.display = 'none';
	$('overlayLB').style.display = 'none';
	//document.getElementsByTagName('body')[0].style.overflow = 'auto';
	showSelectBoxes();
}

function showLBOverlay() {
	var arrayPageSize = getPageSize();
	$('overlayLB').style.width = "980px";
	$('overlayLB').style.height = arrayPageSize[1] +"px";
	hideSelectBoxes();
	//document.getElementsByTagName('body')[0].style.overflow = 'hidden';
	$('overlayLB').style.display = 'block';
}

function showLB() {
	$('LB').style.display = 'block';
}

function hideLB() {
	$('LB').style.display = 'none';
}

function closeLB() {
	setInnerHTML($('contentLB'), '');
	hideLB(); 
	hideOverlay();
}

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function getPageSize(){
	
	var scrollTab = getPageScroll();
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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(scrollTab[1] < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = scrollTab[1];
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(scrollTab[0] < windowWidth)
		pageWidth = scrollTab[0];		
	else
		pageWidth = windowWidth;
	
	arrayPageSize = new Array(pageWidth,$('screen').clientHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}
