
Clockwork.Live.Lightbox = { };
Clockwork.Live.Lightbox.lightboxes = new Array( );
Clockwork.Live.Lightbox.active_lightbox;

Clockwork.Live.Lightbox.lightbox = Class.create();

Clockwork.Live.Lightbox.lightbox.prototype = {

	mode: 'url',
	yPos: 0,
	xPos: 0,
	display_mode:'iframe',
	width: 600,
	height: 364,
	wrapperTop: "",
	wrapperBottom: "",
	show_closebox: true,

	initialize: function( mode, options ) {

		// keep track of this lightbox
		Clockwork.Live.Lightbox.lightboxes.push( this );
		
		if ( ! Object.isUndefined( options ) ) {

			// the location of the content spcified in the href
			this.content = options.url;
			
			// set the display mode based on the class
			if ( options.inline ) {
				this.display_mode = 'inline';
			}
			else {
				this.display_mode = 'iframe';
			}
			
		}
	},

	// activate an iframe with this url
	activate_with_url: function(url){
		this.content = url;
		this.activate();
	},

	// Turn everything on - mainly the IE fixes
	activate: function(){
		
		// do not activate if another lightbox is active
		if ( Clockwork.Live.Lightbox.active_lightbox ) {
			return false;
		}
		
		this.displayLightbox("block");
		
		Clockwork.Live.Lightbox.active_lightbox = this;
		
	},
	
	prepareParent: function(height, overflow){
		
		var bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
  	
		var htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow;
		
	},
	
	displayLightbox: function(display){
		
		if(display != 'none') {
			$('overlay').style.display	= display;
			$('lightbox').style.display	= display;
			this.loadInfo();
		}
		else {
			$('overlay').style.display = 'none';
			$('lightbox').style.display = 'none';
		}
		
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		
		if ( this.display_mode == 'iframe' ) {
			this.load_iframe_content( );
		}
		else if ( this.display_mode == 'inline' ) {
			this.load_inline_content( );
		}

		Event.observe(window, 'resize', this.onresize.bind(this));
		this.onresize( );
	},

	onresize: function( ) {

		window_height = document.viewport.getHeight();
		$('lightbox').setStyle( {'height': window_height + 'px'} );
		var topmargin = 50;
		var bottommargin = 130;

		var iframe_height = window_height - topmargin - bottommargin;

		// no smaller than 300px
		iframe_height = ( iframe_height < 300 ) ? 300 : iframe_height;
		
		if ( $('lightbox_container') != null ) {
			$('lightbox_container').setStyle( {'height': iframe_height + 'px'} );

			lightbox_window = this.get_iframe_by_name( 'lightbox_container' );

			if ( this.display_mode == 'iframe' ) {

				lightbox_window.parent_size = { h : iframe_height }; // explicitly set a window var in the iframe for initial load sizing

				if ( lightbox_window.parent_resize ) {  // 
					lightbox_window.parent_resize( { h : iframe_height } );
				}

			}
		}

	},



	/*
	* This is necessary because window.frames['name'] does not work when multiple iframes of the same name are created, destroyed and recreated
	*/
	get_iframe_by_name: function( name ) {

		for ( var i=0; i<window.frames.length; i++ ) {
			if ( window.frames[i].name == name ) {
				return window.frames[i]
			}
		}

		return null;

	},
	
	load_iframe_content: function ( ) {
		
		var info = '';
		
		if ( this.show_closebox === true ) {
			var info = "<a id='lightbox_close' href='javascript:parent.Clockwork.Live.Lightbox.active_lightbox.deactivate( );' class='no_onbeforeunload'>Close</a>";	
		}
		
		var info = info + this.wrapperTop + "<div id='lightbox_content'></div>" + this.wrapperBottom;
		
		new Insertion.Before($('lbInsertionPoint'), info);

		$('lightbox_content').update( 
				'<iframe id="lightbox_container" frameborder="0" scrolling="no"' +
				'name="lightbox_container" style="overflow-y:auto;" ' +
				'width="'+this.width+' " ' +
				'height="'+ this.height +' ' +
				'"src="'+this.content+'"></iframe>' 
			);
		this.processInfo( );
		
	},
	
	load_inline_content: function ( ) {
		
		new Ajax.Request(
	        this.content,
		        {
					method: 'get',
					parameters: "",
					evalScripts: true,
					onSuccess: function ( response ) { 
						var info = this.wrapperTop + "<div id='lightbox_content'>" + response.responseText + "</div>" + this.wrapperBottom;
						new Insertion.Before($('lbInsertionPoint'), info); 
					},
					onComplete: this.processInfo.bindAsEventListener(this)
				}
		);
		
	},
	
	// Display Ajax response
	processInfo: function(response){
		$('lightbox').className = "done";	
		this.actions();			
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		var lightbox_actions = $$('.lightbox_action');

		for( var i = 0; i < lightbox_actions.length; i++) {
			Event.observe(lightbox_actions[i], 'click', this[lightbox_actions[i].rel].bindAsEventListener(this), false);
			lightbox_actions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	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)}
	   );
	 
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		
		this.displayLightbox("none");
		
		// completly remove the contents of the deactivate lightbox
		Element.remove($('lightbox_content'));
		
		if ( this.show_closebox === true ) {
			Element.remove($('lightbox_close'));
		}
		
		Clockwork.Live.Lightbox.active_lightbox = null;
		
	}
}

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


// Onload, make all links that need to trigger a lightbox active
Clockwork.Live.Lightbox.setup = function ( ) {
	
	Clockwork.Live.Lightbox.addLightboxMarkup();
	
}


// 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.
Clockwork.Live.Lightbox.addLightboxMarkup = function ( ) {
	
	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="lbInsertionPoint"></div>';
	
	bod.appendChild(overlay);
	bod.appendChild( lb );
	
}


Clockwork.Live.Lightbox.lightbox_callback_exists = function ( callback ) {
	
	if ( ! parent ) {
		Logger.log( 'Unable to find the pop-up\'s parent.', CW_LOG_ERROR );
		return false;
	}

	return ( parent[callback] ? true : false );
	
}


Clockwork.Live.Lightbox.lightbox_callback_post_process = function ( ) {
	
	if ( deactivate_lightbox == null ) { deactivate_lightbox = false; }
	
	if ( deactivate_lightbox == true ) {
		Clockwork.Live.Lightbox.active_lightbox.deactivate( );
		deactivate_lightbox = false;
	}
	
}


Clockwork.Live.Lightbox.perform_lightbox_callback = function ( callback, callback_data ) {
	
	if ( ! Clockwork.Live.Lightbox.lightbox_callback_exists( callback ) ) {
		return;
	}

	var p               =  parent;
	var bound_callback  =  p[callback].bind( p, callback_data );

	p.setTimeout( bound_callback, 0 );
	
	return;
	
}

Event.observe(window, 'load', Clockwork.Live.Lightbox.setup, false);


