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

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

/*-------------------------------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', getBrowserInfo.bindAsEventListener(this), false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(lightbox_name, lightbox_depth, href) {
		this.lightbox_name = lightbox_name;
		this.z_index = 9000 + (lightbox_depth * 2);
	  if (href) {
  		this.href = href;
  	}
    // Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
    // ctrl.onclick = function(){return false;};
    this.addLightboxMarkup();
		this.onload = function() {};
	},
	
	// 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.
	addLightboxMarkup: function() {
		bod 				= document.getElementsByTagName('body')[0];
		lb					= document.createElement('div');
		lb.id				= 'lightbox_' + this.lightbox_name;
		lb.className 	= 'lightbox';
		lb.style.display = 'none';
		lb.style.zIndex = this.z_index;
		lb.innerHTML	= '<div id="lbLoadMessage_' + this.lightbox_name + '">' +
							  '<p>Loading</p>' +
							  '</div>';
		bod.appendChild(lb);
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'auto');
			this.hideSelects('hidden');
		}
		this.loadInfo();
	},
	
	// 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() {
		this.getScroll();
		if (this.yPos > 50) {
		  new Effect.ScrollTo('lbContent_' + this.lightbox_name, { duration: 0.2 });
		}
    // window.scrollTo(x, y); 
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		var myAjax = new Ajax.Request(
        this.href,
        {method: 'get', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
		
	},
	
	// Display Ajax response
	processInfo: function(response){
		$('overlay').style.zIndex = this.z_index - 1;
		if (!Element.visible('overlay')) {
			Element.show('overlay');
			$$('body')[0].addClassName('faded');
		}
		info = "<div id='lbContent_" + this.lightbox_name + "'>" + response.responseText + "</div>";
		if ($('lbContent_' + this.lightbox_name) != null) {
		  Element.replace('lbContent_' + this.lightbox_name, info);
	  } else {
  		new Insertion.Before($('lbLoadMessage_' + this.lightbox_name), info);
		}
		$('lbLoadMessage_' + this.lightbox_name).style.display = 'none';
		$('lightbox_' + this.lightbox_name).style.display = 'block';
		this.setScroll();
		this.actions();
		this.onload();
		initLoneClick();
	},
	
	// 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);
			lbActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	insert: function(e){
	   link = Event.element(e).parentNode;
	   Element.remove($('lbContent'));
	 
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	deactivate: function(){
		this.hide();
		Element.remove('lightbox_' + this.lightbox_name);
	},
	
	hide: function() {
		
		if (browser == "Internet Explorer") {
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		$('overlay').style.zIndex = this.z_index - 3;
		Element.hide('lightbox_' + this.lightbox_name)
	}
}

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

var all_lightboxes = new Array();
var active_lightboxes = new Object();
var active_lightbox_count = 0;

function addLightbox(lightbox_name, element, visible) {
  lightbox_depth = ++active_lightbox_count;
  box = new lightbox(lightbox_name, lightbox_depth, element);
  active_lightboxes[lightbox_name] = box;
	if (!visible) {
		--active_lightbox_count;
	}
}

function showLightbox(lightbox_name, activity_indicator_el, element, oncomplete) {
	loadAndShowLightbox(lightbox_name, activity_indicator_el, element.href, oncomplete);
}

function loadAndShowLightbox(lightbox_name, activity_indicator_el, href, oncomplete) {
	lightbox_depth = ++active_lightbox_count;
	showActivityIndicator(activity_indicator_el);
	box = active_lightboxes[lightbox_name];
	box = new lightbox(lightbox_name, lightbox_depth, href);
	box.onload = function() {
	 	hideActivityIndicator(activity_indicator_el); if (oncomplete != null) { oncomplete(); }
	};
	active_lightboxes[lightbox_name] = box;
	all_lightboxes.push(box);
	box.activate();
}

function closeLightbox(lightbox_name) {
  box = active_lightboxes[lightbox_name];
  if (box != null) {
    active_lightboxes[lightbox_name] = null;
    box.deactivate();
    active_lightbox_count--;
    if (active_lightbox_count == 0) {
      Element.hide('overlay');
			$$('body')[0].removeClassName('faded');
    }
  }
}

function hideLightbox(lightbox_name) {
	box = active_lightboxes[lightbox_name];
	if (box != null) {
	  box.hide();
	  active_lightbox_count--;
	  if (active_lightbox_count == 0) {
	    Element.hide('overlay');
			$$('body')[0].removeClassName('faded');
			toggleSelectVisibility(true);
	  }
	}
}

function closeAllLightboxes() {
	all_lightboxes.each(function(l) { closeLightbox(l.lightbox_name)});
	all_lightboxes = new Array();
}