// JavaScript Document
/* modded by lance dolan 7/14/2011 to use $13 variable for JQuery version 1.3 */
if (typeof com == "undefined") { 
	com={}; 
}
if (typeof com.digitaria == "undefined") {
	com.digitaria={}; 
}

/**
 * Initialize the lightbox system.
 * @returns {void}
 * @author Nick Davison
 */
com.digitaria.LightBox=function() {
	// If the content div doesn't exist, create it.
	if ($13('#lightbox_content').length==0) {
		$13('body').prepend('<div id="lightbox_content"></div>');	
	}
	
	// If the shadow div doesn't exist, create it.
	if ($13('#lightbox_shadow').length==0) {
		$13('body').prepend('<div id="lightbox_shadow"></div>');
		
		// If we're not in IE6 set bShowShadow to false
		bShowShadow=true;
		if (navigator.appName.indexOf('Microsoft')!=-1) {
			verPos=navigator.appVersion.indexOf('MSIE');
			try {
				ver=parseInt(navigator.appVersion.substring(verPos+5), 10);
			} catch(e) {
				ver=5;
			}
			if (ver<=6) bShowShadow=false;
		}
		
		// If we can safely show PNGs, create the lightbox_shadow div and its inner PNG
		if (bShowShadow) {
			if (navigator.appVersion.toString().toLowerCase().indexOf("webkit")==-1) {
				$13('#lightbox_shadow').append('<img src="/apps/lifetech/docroot/images/backgrounds/modal_dropshadow.png" width="100%" height="100%" />');
			} else {
				document.getElementById('lightbox_content').style.webkitBoxShadow='0 0 40px #000';
			}
		}
	}
	
	// If the bg div doesn't exist, create it.
	if ($13('#lightbox_bg').length==0) {
		$13('body').prepend('<div id="lightbox_bg"></div>');
	}
	
	// Keep copies so we only need to get them once.
	this.bg=$13('#lightbox_bg');
	this.shadow=$13('#lightbox_shadow');
	this.content=$13('#lightbox_content')
	
	// Hide the lightbox until needed
	this.hide();
	
	// Watch for window resizing.
	var _that=this;
	var _callback=function() {
		_that.resizeToBody();
	}
	$13(window).bind('resize', _callback);
	
	// Monitor for content resizing (as this doesn't trigger a DOM event, we have to watch manually)
	this.lastHeight=0;
	this.lastWidth=0;
	this.watchSize();
	
	$13('.close_popup').live('click', function(e) {
		e.preventDefault();
		_that.hide();
	});
}

/**
 * Show the lightbox then resize it as needed to make it and the shadow fit the current dimensions.
 * @returns {void}
 * @see com.digitaria.LightBox.resizeToBody (calls)
 * @author Nick Davison
 */
com.digitaria.LightBox.prototype.show=function() {
	this.content.css('display', 'block');
	this.shadow.css('display', 'block');
	this.bg.css('display', 'block');
	this.resizeToBody();
	
	// Find where on the page the lightbox is.
	var cpos=this.content.position();
		
	// If we're scrolled down beneath it, scroll back up to it.
	if ($13(document).scrollTop()>cpos.top) {
		$13(document).scrollTop(cpos.top-10);
	}
}

/**
 * Hide the lightbox
 * @returns {void}
 * @author Nick Davison
 */
com.digitaria.LightBox.prototype.hide=function() {
	if (this.content.find('#vid_pop_holder').length>0) {
		swfobject.removeSWF("vid_pop_holder");
	}
	this.content.css('display', 'none');
	this.shadow.css('display', 'none');
	this.bg.css('display', 'none');
}

/**
 * Load new content in to the lightbox via AJAX and show it if it's hidden.
 * @returns {void}
 * @param {hash} settings Hash of optional settings
 * @param {string} settings.url The URL to load in.
 * @param {function} settings.success Success callback (same as jQuery AJAX)
 * @param {function} settings.error Error callback (same as jQuery AJAX)
 * @param {function} settings.complete Complete callback (same as jQuery AJAX)
 * @see com.digitaria.LightBox.show (calls)
 * @author Nick Davison
 */
com.digitaria.LightBox.prototype.load=function(settings) {
	settings=jQuery.extend({
		url: "",				   
		success: function() {},
		error: function() {},
		complete: function() {}
	}, settings);
	
	var _that=this;

	$13.ajax({
		url: settings.url,
		success: function(data) {
			$13('#lightbox_content').html(data);
			_that.show();
			settings.success(data, this);
		},
		error: settings.error,
		complete: settings.complete
	});

}

/**
 * Resize and reposition the lightbox as the window resizes.
 * @returns {void}
 * @see com.digitaria.LightBox.resizeShadow (calls)
 * @author Nick Davison
 */
com.digitaria.LightBox.prototype.resizeToBody=function() {
	var bheight=$13('body').height();
	var wheight=$13(window).height();
	if (wheight>bheight) bheight=wheight;
	this.bg.height(bheight);
	newLeft=Math.floor(($13('body').width()-this.content.width())/2);
	this.content.css('left', newLeft);
	this.resizeShadow();
}

/**
 * Resize the shadow to remain 20px larger in every direction than the main content.
 * @returns {void}
 * @see com.digitaria.LightBox.resizeToBody (called by)
 * @author Nick Davison
 */
com.digitaria.LightBox.prototype.resizeShadow=function() {
	if (this.shadow.length>0) {	
		contentOffset=this.content.offset();
			
		this.shadow.css('left', (contentOffset.left-20));
		this.shadow.css('top', (contentOffset.top-20));
		this.shadow.css('width', (this.content.width()+40));
		this.shadow.css('height', (this.content.height()+40));
	}
}

/**
 * Monitor the size of the content window.
 * This has to be done manually as a div resizing 
 * (like when AJAX loads in or an IMG finally loads)
 * does not trigger any specific DOM event to watch.
 * @returns {void}
 * @see com.digitaria.LightBox.resizeToBody (calls)
 * @author Nick Davison
 */
com.digitaria.LightBox.prototype.watchSize=function() {
	// If the content is visible
	if (this.content.css('display')=='block') {
		// Check for size changes
		newHeight=this.content.height();
		newWidth=this.content.width();

		if ((this.lastHeight!=newHeight) || (this.lastWidth!=newWidth) ) {
			// If there was a change, update everything
			this.lastHeight=newHeight;
			this.lastWidth=newWidth;
			this.resizeToBody();	
		} else {
			// Even if there hasn't been a resize, FORCE the shadow to update.
			this.resizeShadow();
		}	
	}
	
	var _that=this;
	var _callback=function() {
		_that.watchSize();
	}
	setTimeout(_callback, 500);
}

var lightbox=null;
$13(document).ready(function() {
	lightbox=new com.digitaria.LightBox();
});
