/*

Modificado por Rene Lopez Caballero para que en vez de unas las clase
tenga que agregar onclick="return lightbox.show( this )" a la url que quiero mostrar como lightbox
y para cerrar uso onclick="return lightbox.close()"

*/

/*
Created By: Chris Campbell, http://particletree.com, 2/1/2006
Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

var lightbox = {
	yPos : 0,
	xPos : 0,
	postData : '',
	closeCallback : null,
	// Turn everything on - mainly the IE fixes
	showURL: function( ctrl, callback ){
		this.url = ctrl.href || ctrl;
		this.callback = callback || null;
		this.showLB();	
		this.loadInfo();
		$( 'lightbox' ).style.background = '#fff';
		$( 'lightbox' ).style.width = '500px';
		$( 'lightbox' ).style.height = '400px';
		$( 'lightbox' ).style.padding = '10px';
		$( 'lightbox' ).style.margin = '-220px 0 0 -250px';
		
		var width=500;
		var height=400;
		$('lightboxFooter').style.display='block';
		$('lightboxFooter').style.margin='-' + ( height/2 ) + 'px 0 0 -' + ( width/2 ) + 'px';
		$('lightboxFooter').style.zIndex='9999';
		$('lightboxFooter2').style.width= (width+10) + 'px';
		$('lightboxFooter2').style.top= (height+10) + 'px';
		$('closeLBImage').style.display='none';
		$('rightFooterLB').innerHTML='<a href="javascript:void(0)" onclick="return lightbox.close();">Close <img src="images/icon_close_small.gif" alt="Close" title="Close" align="top"></a>';
		return false;
	},
	setPostData: function( data ){
		this.postData = data;
	},
	showImg: function( ctrl, title ){
		this.url = ctrl.href || ctrl;
		this.title = title;
		this.showLB();
		
		var imgPreloader = new Image();
		$( 'lightbox' ).style.background = '#eee';
		// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			var width = parseInt( this.width );
			var height = parseInt( this.height ) + 40;
			$( 'lightbox' ).style.width = width + 'px';
			$( 'lightbox' ).style.height = height + 'px';
			$( 'lightbox' ).style.padding = '10px';
			$( 'lightbox' ).style.margin = '-' + ( height/2 ) + 'px 0 0 -' + ( width/2 ) + 'px';
			$('closeLBImage').style.display='block';
			lightbox.changeContent( '<img src="' + lightbox.url + '" alt="Contact Image" title="" />' + ( lightbox.title ? '<div id="imageTitleLB">' + lightbox.title + '</div>': '' ) );
		}
		imgPreloader.src = this.url;
		return false;
	},
	showIframe: function( ctrl, options ) {
		options = options || {};
		options.type = 'iframe';
		return this.show(ctrl, options);
	},
	showHTML: function( html, callback ) {
		return this.show(html, {'type':'html','callback':callback||null });
	},
	show: function( ctrl, options ){
		options = options || {};
		this.url = ctrl.href || ctrl;
		this.callback = options.callback || null;
		var type = options.type || 'url';
		var width = options.width || 500;
		var height = options.height || 400;
		var showFooter = options.showFooter || true;
		var caption = ctrl.title || options.caption || '';
		var rightFooter = options.rightFooter || '<a href="javascript:void(0)" onclick="return lightbox.close();">Close <img src="images/icon_close_small.gif" alt="Close" title="Close" align="top"></a>';
		
		if( type=='url' )
			this.loadInfo();
		this.showLB();
		
		if( showFooter ) {
			$('lightboxFooter').style.display='block';
			$('lightboxFooter').style.margin='-' + ( height/2 ) + 'px 0 0 -' + ( width/2 ) + 'px';
			$('lightboxFooter').style.zIndex='9999';
			$('lightboxFooter2').style.width= (width-10) + 'px';
			$('lightboxFooter2').style.top= (height+7) + 'px';
			$('captionLB').style.display='inline';
			$('captionLB').innerHTML=caption;
			$('closeLBImage').style.display='none';
			$('rightFooterLB').innerHTML=rightFooter;
		} else {
			$('closeLBImage').style.display='block';
		}

		$( 'lightbox' ).style.width = width + 'px';
		$( 'lightbox' ).style.height = height + 'px';
		$( 'lightbox' ).style.padding = '0';
		$( 'lightbox' ).style.margin = '-' + ( height/2 ) + 'px 0 0 -' + ( width/2 ) + 'px';
		if( type=='iframe' )
			lightbox.changeContent( '<iframe name="lightboxIF" id="lightboxIF" src="' + lightbox.url + '" width="' + width + '" height="' + height + '" frameborder="0" scrolling="no"></iframe>' );
		else if( type=='html' )
			lightbox.changeContent( this.url );
		return false;
	},
	showLB: function(){
		$('lightbox').className = "loading";
		//if (this.isIE() == '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( var 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;
		$('lightbox').style.display = display;
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		new Ajax.Request( this.url, { postBody: this.postData, onComplete : this.processInfo.bindAsEventListener(this) });
		this.postData='';
	},
	
	// Display Ajax response
	processInfo: function(response){
		this.changeContent( response.responseText );
	},
			
	changeContent:function( html ){
		try{ Element.remove($('lbContent')); } catch( e ){} // i remove old contect if exsit
		var info = "<div id='lbContent'>" + html + "</div>";
		new Insertion.Before($('lbLoadMessage'), info)
		$('lightbox').className = "done";
		if( this.callback )
			setTimeout(function(){this.callback();}.bind(this), 10);
	},
	
	// Example of creating your own functionality once lightbox is initiated
	close: function(){
		try{ Element.remove($('lbContent')); } catch( e ) {}
		try{ this.closeCallback(); this.closeCallback=null; } catch( e ) {}
		//if (this.isIE() == "Internet Explorer"){
			this.prepareIE("auto", "auto");
			this.setScroll(0,this.yPos);
			this.hideSelects("visible");
		//}
		$('lightboxFooter').style.display='none';
		$('captionLB').style.display='none';
		this.displayLightbox("none");
		return false;
	},
	
	isIE: function() {
		return navigator.userAgent.toLowerCase().indexOf('msie') + 1;
	}
}

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

// 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 initialize(){
	var bod = document.getElementsByTagName('body')[0];
	var overlay = document.createElement('div');
	overlay.id = 'overlay';
	var lb = document.createElement('div');
	lb.id = 'lightbox';
	lb.className = 'loading';
	lb.innerHTML = '<div id="lbLoadMessage"><p><img src="images/loadingAnimation.gif" width="100" height="100" alt="Ajax Working" id="ajaxIcon" /><br /><br />Loading...</p></div><a id="closeLBImage" href="javascript:void(0)" onclick="return lightbox.close();"><img src="images/close.gif" alt="Close" title="Close"></a>';                
	bod.appendChild(overlay);
	bod.appendChild(lb);
	// i create the footer
	var footer = document.createElement('div');
	footer.id = 'lightboxFooter';
	footer.innerHTML = '<div id="lightboxFooter2"><span id="captionLB"></span><span id="rightFooterLB"></span></div>';
	bod.appendChild(footer);
}
function JJkeyboardAction(event) {
        var keycode = event.keyCode;
        var escapeKey;
        if (event.DOM_VK_ESCAPE)  
            escapeKey = event.DOM_VK_ESCAPE; // mozilla
        else
            escapeKey = 27; // ie
        var key = String.fromCharCode(keycode).toLowerCase();
        // close lightbox
	if( keycode == escapeKey )
            lightbox.close();
}

document.observe('keydown', JJkeyboardAction);
document.observe('dom:loaded', initialize );
