/*
history:
05/30/09, monjay, initial created, no longder competes with lightbox.js
*/

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

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

function getBrowserInfo() { if (checkIt('msie')) browser = "Internet Explorer"; else browser = "not ie";};
function checkIt(string) {place = detect.indexOf(string) + 1;thestring = string;return place;};

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl){
		this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},

	prepareIE: function(height, overflow){
		//var bod = $$('body')[0]; bod.style.height = height; bod.style.overflow = overflow;
		//var htm = $$('html')[0]; htm.style.height = height; htm.style.overflow = overflow; 
	},
	
	hideSelects: function(visibility){
		var selects = $$('select');
		for(i = 0; i < selects.length; i++){
			selects[i].style.visibility = visibility;
		}
	},

	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){
		//$('lightbox-iframe-overlay').style.display = display;
		$('lightbox-iframe').style.display = display;
		if (display != 'none') this.loadInfo();
	},

	loadInfo: function(){
		if ($('lightbox-content')) { $('lightbox-content').remove(); }
		var url = this.content;
		var html = "<div id=\"lightbox-content\">" + 
			      "<a href=\"javascript:void(0);\" id=\"lightbox-iframe-close\" rel=\"deactivate\">" +
			         "<img src=\"/lightbox/images/closelabel.gif\" width=\"66\" alt=\"close this box\"/></a>" +
			   "<iframe style=\"background-color:#fff;\" id=\"lightbox-iframe-iframe\" scrolling=\"no\" frameborder=\"0\" " +
			      "width=\"504\" height=\"438\" src=\""+url+"\"</iframe>" + 
			   "</div>";
		new Insertion.Before($('lbLoadMessage'), html)
		$('lightbox-iframe').className = "done";	
		this.actions();				
	},
	
	actions: function(){
		var lclose = $('lightbox-iframe-close');
		Event.observe(lclose, 'click', this[lclose.rel].bindAsEventListener(this), false);
		lclose.onclick = function(){return false;};	
	},
	
	insert: function(e){
	   var link = Event.element(e).parentNode;
	   Element.remove($('lightbox-content'));
	   var myAjax = new Ajax.Request( link.href,{method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	deactivate: function(){
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		this.displayLightbox("none");
	}
};

function addLightboxMarkup(){
/* depcated
	var css = '/lightbox-iframe/lightbox-iframe.css'
	var oLink = document.createElement("link")
	    oLink.href = css;
	    oLink.rel = "stylesheet";
	    oLink.type = "text/css";
	    //$$('head')[0].appendChild(oLink); //msie 6 doesn't like this...
	    document.getElementsByTagName('head')[0].appendChild(oLink);

	var css = '/lightbox-iframe/lightbox-iframe-ie.css'
	var oLink1 = document.createElement("link")
	    oLink1.href = css;
	    oLink1.rel = "stylesheet";
	    oLink1.type = "text/css";
	    //$$('head')[0].appendChild(oLink); //msie 6 doesn't like this...
	    document.getElementsByTagName('head')[0].appendChild(oLink1);
*/

	var overlay = document.createElement('div');
	    overlay.id = 'lightbox-iframe-overlay';

	var lb = document.createElement('div');
	    lb.id = 'lightbox-iframe';
	    lb.className = 'loading';
	    lb.innerHTML= '<div id="lbLoadMessage">' +'loading' +'</div>';

	var bod = $$('body')[0];
	    bod.appendChild(overlay);
	    bod.appendChild(lb);
};

function initialize(){
	addLightboxMarkup();
	var lbox = $$('.lightboxIFRAME');
	    lbox.each( function(e) { e.style.display = 'inline'; valid = new lightbox(e) })

/* MSIE 6 doesn't seem to like this stuff
	addLightboxMarkup();
	var lbox = document.getElementById('SuggestLink')
            lbox.style.display = 'inline';
	    new lightbox(lbox)
*/
};

//if (navigator.userAgent.match('MSIE')) { initialize(); } /* for msie 6  but may fire for ie 7 */
