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

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

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

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.content = ctrl.rel;
		//this.container = container;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			//this.prepareIE('100%', 'hidden');
			//this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("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){
		$('overlay').style.display = display;
		if(browser == 'Internet Explorer'){
			//alert('setting top to '+this.yPos);
			//$('overlay').style.top = this.yPos;
			var arrayPageSize = getPageSize();
			$('overlay').style.height = arrayPageSize[1]+'px';
			popup_array = document.getElementsByClassName('popup_container');
			for(i=0;i<popup_array.length;i++){
				popup_array[i].style.height = arrayPageSize[3]+'px';
			}
		}
		//$('table_container').style.display = display;
		//$('popup_container').style.display = display;
		//$(this.container).style.display = display;
		$(this.content).style.display = 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++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			if ((' '+lbActions[i].className+' ').indexOf("close_uploader") != -1) {
				lbActions[i].onclick = function(){window.location.reload(true);};
			}else{
				lbActions[i].onclick = function(){return false;};
			}
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	// some of these functions aren't executing so do the code manually
	deactivate: function(){
		if (browser == "Internet Explorer"){
			//this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
			//window.scrollTo(0,this.yPos);
		}
		// must set content to invisible when closing
		$(this.content).style.display = "none";
		this.displayLightbox("none");
	}
}

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

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

function addLightbox(id){
	nbox = document.getElementById(id);
	valid = new lightbox(nbox);
	valid.activate();
}

function addPopUpDiv(popupcontent,img_source) {
	var bod = document.getElementsByTagName('body')[0];
	var popup = document.getElementById('popup_editor');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	popup_array = document.getElementsByClassName('popup_container');
	for(i=0;i<popup_array.length;i++){
		popup_array[i].style.top = arrayPageScroll[1]+'px';
	}
	
	if(popup == null){
		popup = document.createElement('div');
		popup.id = 'popup_editor';
		bod.appendChild(popup);
	}
	
	
	//loadingbar.style.visibility = 'hidden';
	
	//popup.className = 'lightbox';
			
	if(img_source){	
		var loadingbar = document.getElementById('loadingbar');
		if(loadingbar == null){
			loadingbar = document.createElement('div');
			loadingbar.id = 'loadingbar';
			popup.appendChild(loadingbar);
		}
		
		var popimgbox = document.getElementById("image_popuper");		
		popimgbox.style.visibility='hidden';
				
		var popimg = document.getElementById("image_popup_src");
		
		//popimg.setAttribute("src",img_source);
		
		imgPreloader = new Image();
		
		// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			var size = getPageSize();
			//alert("page width etc = "+size[0]+" "+size[1]+" "+size[2]+" "+size[3]);
			//alert("onload size = "+imgPreloader.width+" "+imgPreloader.height);
						
			var aspect = imgPreloader.width/imgPreloader.height;
			
			// REMEMBER TO CLEAR THE WIDTH AND HEIGHT ATTRIBUTES BETWEEN IMAGES!!!!!!
			var img_width = "";
			var img_height = "";
			var box_width = imgPreloader.width+"px";
						
			//popimgbox.style.height = "80%";
			
			var wratio = (imgPreloader.width+40)/size[2];
			var hratio = (imgPreloader.height+40)/size[3];
			
			if(wratio>1 && hratio>1){
				if(wratio>hratio){
					img_width = size[2]-100+"px";
					img_height = "";
					box_width = img_width;
				}else{
					img_height = size[3]-100+"px";
					img_width = "";
					box_width = (size[3]-100)*aspect+"px";
				}
			}else if(wratio>1){
				img_width = size[2]-100+"px";
				img_height = "";
				box_width = img_width;
			}else if(hratio>1){
				img_height = size[3]-100+"px";
				img_width = "";
				box_width = (size[3]-100)*aspect+"px";
			}
						
			//alert('set popup to '+popimgbox.style.width);
			
			/*if(img_width=="") {popimg.setAttribute("width","10px");popimg.removeAttribute("width");}
			else popimg.setAttribute("width",img_width);
			
			if(img_height=="") popimg.removeAttribute("height");
			else popimg.setAttribute("height",img_height);*/
			
			//alert("img width = "+popimg.getAttribute("width")+" img height = "+popimg.getAttribute("height")+" "+box_width);
			
			popimgbox.style.width = box_width;
			
			popimgbox.style.border = "1px solid #000";
			
			//alert("image source in explorer = "+popimg.src);
			var new_width = "";
			var new_height = "";
			if(img_width!="") new_width = " width='"+img_width+"px'";
			if(img_height!="") new_height = " height='"+img_height+"px'";
			
			popimg.innerHTML = "<img id='popimg_src'"+"src='"+img_source+"'"+new_width+new_height+"></img>";
			
			//"style=\"width:"+new_width+"; height:"+new_height+"\"";
			
			//popimgbox.style.width=new_width+"%";
			//popimgbox.style.height=new_height+"%";
			
			popimgbox.style.visibility='visible';
			//loadingbar.style.visibility = 'hidden';
			popup.removeChild(loadingbar);
			
			
			//alert("width is "+imgPreloader.width+"height is "+imgPreloader.height);
			
			imgPreloader.onload=function(){};	//	clear onLoad, IE behaves irratically with animated gifs otherwise 
		}
		imgPreloader.src = img_source;
		
		
	}
		
	popup.rel = popupcontent;
	
	addLightbox("popup_editor");
}

// defined in prototype.js
/*document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}*/

// getPageSize()
// Returns array with page width, height and window width, height
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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;
	
//	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(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

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

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

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

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {

	bod 				= document.getElementsByTagName('body')[0];

	overlay 			= document.createElement('div');
	overlay.id			= 'overlay';

	bod.appendChild(overlay);
}
