/*
	Lustbox
	by Joseph Lust
	
	Taken from this thing, but had to fix it so much, I gave it my own name.
	And WTF? Uses Prototype, but not really? Can't you feel the power?
	
	Oh, and assumes name will be "Lustbox" - so much for making it a class
	- Replaced with closures, no name dependence

*/

// Hooks removed. If LB is called before DOM insertion of page, error b/c not yet extant. Plus, save needless DOM render time
Event.observe(window, 'load', function () {Lustbox.init();}, false); // Set hooks, remove from HTML page. Nicer!

var	hiddenSelectIDs = null; // Would there ever be more than that?
var Lustbox = {
	// state
	initialized : false,
	// 
    lightboxType : null,
	lightboxCurrentContentID : null,
	lightboxModal : false,
	
	showBoxString : function(content, boxWidth, boxHeight){
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		var contents = $('boxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},


	showBoxImage : function(href) {
		Lustbox.init();
		this.lightboxType = 'image';
		var contents = $('boxContents');
		var objImage = document.createElement("img");
		objImage.setAttribute('id','lightboxImage');
		contents.appendChild(objImage);
		imgPreload = new Image();
		imgPreload.onload=function(){
			objImage.src = href;
			Lustbox.showBox();
		}
		imgPreload.src = href;
		return false;
	},

	showBoxByID : function(id, boxWidth, boxHeight) {
		Lustbox.init();
		this.lightboxType = 'id';
		this.lightboxCurrentContentID = id;
		this.setLightboxDimensions(boxWidth, boxHeight);
		// Make a visible version
		var element = $(id);
        var container = $('boxContents'); 
        container.appendChild(element);
        element.style.display = 'block';//show
        // Closure ptr
	    var _this = this; 
        document.observe('keydown',function(e) {
            switch (e.keyCode) {
            case Event.KEY_ESC:
                 setTimeout('cancelNotify()',50);//_this.hideBox();
                break;
            }
        });
		this.showBox();
		return false;
	},

	showBoxByAJAX : function(href, boxWidth, boxHeight) {
		Lustbox.init();
		this.lightboxType = 'ajax';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');
		var myAjax = new Ajax.Updater(contents, href, {method: 'get'});
		this.showBox();
		return false;
	},
    
	showBoxByAJAJ : function(href, boxWidth, boxHeight) { // JSON
    	Lustbox.init();
    	this.lightboxType = 'ajaj';
    	this.setLightboxDimensions(boxWidth, boxHeight);
    	var contents = $('boxContents');
    	var myAjax = new Ajax.Request(href, {
    	                method: 'get',
                        onSuccess: function(transport) {
                            var rtn = transport.responseText.evalJSON(true); // w/ injection prevention
                			if(rtn.status=='ok') {
                				contents.innerHTML = rtn.data.HTML;
                			}
                        }
                    });
    	this.showBox();
    	return false;
	},
	
	// Open up a modal box with Y/N functionality
	// Launch either the Y or N function on click
	// The ability to Cancel is also given
	showBoxByModal : function(a) {
		Lustbox.init();
	    //msg,btnY,btnN,fcnY,fcnN,cancelible,fcnCancel
	    var _this = this; // Closure ptr
	    // Set text and visual
	    $('lb_msg').innerHTML = (a.msg) ? a.msg: 'No message set';
	    $('lb_buttonY').innerHTML = (a.btnY) ? a.btnY: 'Yes';
	    $('lb_buttonN').innerHTML = (a.btnN) ? a.btnN: 'No';
	    if(a.cancel==false) { $('lb_close').hide(); } // if false
	    else {
	        document.observe('keydown',function(e) {
                switch (e.keyCode) {
                case Event.KEY_ESC:
                     _this.hideBox();
                    break;
                case 78:
                    if(a.fcnN) {a.fcnN.call();}
                    _this.hideBox();
                    break;
                
                case 89:
                    if(a.fcnY) {a.fcnY.call();}
                    _this.hideBox();
                    break;
                }
               });
	    }

	    // Set function calls
	    if(a.fcnY) {    $('lb_buttonY').observe('click',function () {a.fcnY.call(); _this.hideBox();});}// Yes button
	    else {$('lb_buttonY').observe('click',function () {_this.hideBox();});}
	    if(a.fcnN) {    $('lb_buttonN').observe('click',function () {a.fcnN.call(); _this.hideBox();});}// Yes button
	    else {$('lb_buttonN').observe('click',function () {_this.hideBox();});}
	    
	    // Use escape key if cancet
        this.showBoxByID('lb_yn',200,125); // Premade and hidden	
	},
	
	
	setLightboxDimensions : function(width, height) {
		Lustbox.init();
		var windowSize = this.getPageDimensions();
		if(width) {
			if(width < windowSize[0]) {
				$('box').style.width = width + 'px';
			} else {
				$('box').style.width = (windowSize[0] - 50) + 'px';
			}
		}
		if(height) {
			if(height < windowSize[1]) {
				$('box').style.height = height + 'px';
			} else {
				$('box').style.height = (windowSize[1] - 50) + 'px';
			}
		}
	},


	showBox : function() {
		Lustbox.init();
	// if IE5.5+ on win32, then display PNGs with AlphaImageLoader
        var version = parseFloat(navigator.appVersion.split('MSIE')[1]);
        if ((version >= 5.5) && (version < 7) )//&& (document.body.filters))
        {
            // Yeah, mf really dropped the ball here - fixed it!
            hiddenSelectIDs = new Array($$('SELECT').size());
            $$('SELECT').each(function (el,idx) {
                if(el.style.display != 'none') {
                    hiddenSelectIDs[idx]=el; // Store to reveal later
                    el.style.display = 'none';
                    }
                }); // hide all selects
            $('lustbox_overlay').style.backgroundColor = "#000000";             // Just show a black background
        }
	if(this.lightboxModal) {$('lb_close').hide();}
	
		Element.show('lustbox_overlay');
		this.center('box');
		return false;
	},
	
	
	hideBox : function(){
	    // Prevent double call! - Free Listeners
        document.stopObserving('keydown');
        $('lb_buttonY').stopObserving('click');
        $('lb_buttonN').stopObserving('click');
        
	    // if IE5.5+ on win32, then display PNGs with AlphaImageLoader
        var version = parseFloat(navigator.appVersion.split('MSIE')[1]);
        if ((version >= 5.5) && (version < 7) )//&& (document.body.filters))
        {                 
            hiddenSelectIDs.each(function (el) {
                el.style.display = 'block';
                }); // hide all selects
        }
		var contents = $('boxContents');
		if(this.lightboxType == 'id') {
			var body = document.getElementsByTagName("body").item(0);
			Element.hide(this.lightboxCurrentContentID);
			body.appendChild($(this.lightboxCurrentContentID));
		}
		contents.innerHTML = '';
		$('box').style.width = null;
		$('box').style.height = null;
		Element.hide('box');
		Element.hide('lustbox_overlay');
		return false;
	},
	
	// taken from lightbox js, modified argument return order
	getPageDimensions : function(){
		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 windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			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;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight) 
		return arrayPageSize;
	},
	
	center : function(element){
		try{
			element = document.getElementById(element);
		}catch(e){
			return;
		}
		var windowSize = this.getPageDimensions();
		var window_width  = windowSize[0];
		var window_height = windowSize[1];
		
		$('lustbox_overlay').style.height = windowSize[3] + 'px';
		
		element.style.position = 'absolute';
		element.style.zIndex   = 99;
	
		var scrollY = 0;
	
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
	
		var elementDimensions = Element.getDimensions(element);
		var setX = ( window_width  - elementDimensions.width  ) / 2;
		var setY = ( window_height - elementDimensions.height ) / 2 + scrollY;
	
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
	
		element.style.left = setX + "px";
		element.style.top  = setY + "px";
		Element.show(element);
	},
	
	// Hooks removed. If LB is called before DOM insertion of page, error b/c not yet extant. Plus, save needless DOM render time
	init : function() {
		if(!this.initialized)
		{
		    // Another place where mf stunk. This header would scroll and be lost if div too big.
		    var _this = this; // Closure ptr
		    // Also had no keybindings	  
			var lustb_text = '<div id="lustbox_overlay" style="display:none"></div>';
			lustb_text += '<div id="box" style="display:none">';
			lustb_text += '<div id="lb_header"><div id="lb_close"><img src="images/cancel_icon.png" onClick="cancelNotify();" alt="Cancel" title="Cancel" /></div></div>';
			lustb_text += '<div id="boxContents"></div>';
			lustb_text += '</div>';
			lustb_text += '<div id="lb_yn" style="display:none;">';
			lustb_text += '<div id="lb_msg"></div>';
			lustb_text += '<div id="lb_buttons">';
			lustb_text += '<button id="lb_buttonY"></button>&nbsp;&nbsp;';
			lustb_text += '<button id="lb_buttonN"></button>';
	        lustb_text += '</div></div>';
			Insertion.Bottom(document.body,lustb_text);
		}
		this.initialized = true;//track
	}
}



