if (typeof com == "undefined") { com={}; }
if (typeof com.digitaria == "undefined") { com.digitaria={}; }

com.digitaria.lightbox={
	/* Store the radius we're using */
	shadowRadius: 20,
	/* Store references to the key elements */
	divShadow: null,
	tblShadow: null,
	divWindow: null,
	bScrollNotMove: false,
	
	getScrollY: function() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return scrOfY;
	},
	
	/**
	 * Scroll the browser so the main divWindow comes in to view.
	 * @returns {void}
	 * @author Nick Davison
	 */
	scrollIntoViewScrollBrowser: function() {
		var pos = com.digitaria.lightbox.divWindow.cumulativeOffset();
		if (pos[1]<com.digitaria.lightbox.getScrollY()) {
	    	window.scrollTo(pos[0], pos[1]-20);
		}
	},
	
	/**
	 * Move the main divWindow so it comes in to view in the browser window.
	 * @returns {void}
	 * @author Nick Davison
	 */
	scrollIntoViewMovePane: function() {
		var top=com.digitaria.lightbox.getScrollY();
		var paneHeight=com.digitaria.lightbox.divWindow.getHeight();
		var winHeight=window.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
		if (paneHeight+100<winHeight) {
			com.digitaria.lightbox.divHolder.setStyle({
				top: (top+100)+'px'
			});
		}
	},
	
	/**
	 * Either move the browser to show the div or the div in to view for the browser, depending on the current mode.
	 * @returns {void}
	 * @author Nick Davison
	 */
	scrollIntoView: function() {
		if (com.digitaria.lightbox.bScrollNotMove) {
			com.digitaria.lightbox.scrollIntoViewScrollBrowser();
		} else {
			com.digitaria.lightbox.scrollIntoViewMovePane();
		}
	},
	
	/**
	 * Resize the shadow div to the correct size/position for the content.
	 * @returns {void}
	 * @author Nick Davison
	 */
	resizeShadow: function() {	
		if (com.digitaria.lightbox.divWindow.style.display=="block") {
			com.digitaria.lightbox.divShadow.clonePosition(com.digitaria.lightbox.divWindow, {
				setWidth: false,
				setHeight: false,
				offsetLeft: 0-com.digitaria.lightbox.shadowRadius,
				offsetTop: 0-com.digitaria.lightbox.shadowRadius
			});
			
			var w1=com.digitaria.lightbox.divWindow.getWidth();
			var h1=com.digitaria.lightbox.divWindow.getHeight();
			var w=w1+com.digitaria.lightbox.shadowRadius*2;
			var h=h1+com.digitaria.lightbox.shadowRadius*2;
						
			com.digitaria.lightbox.divShadow.style.width=w+"px";
			com.digitaria.lightbox.divShadow.style.height=h+"px";
									
			if (com.digitaria.lightbox.tblShadow!=null) {
				com.digitaria.lightbox.tblShadow.setAttribute('width', w);
				com.digitaria.lightbox.tblShadow.setAttribute('height', h);
				document.getElementById('lightbox_middle').style.height=h1;
				document.getElementById('lightbox_middle').style.width=w1;
			}
		}
	},
	
	/**
	 * Set up a loop that will keep calling com.digitaria.lightbox.resizeShadow every 500ms.
	 * This lets us catch new content loading in to the window, page resizes, etc.
	 * @returns {void}
	 * @author Nick Davison
	 */
	resizeLoop: function() {
		if (!com.digitaria.lightbox.bScrollNotMove) {
			com.digitaria.lightbox.scrollIntoView();
		}
		com.digitaria.lightbox.resizeShadow();
		setTimeout(com.digitaria.lightbox.resizeLoop, 500);
	},
	
	/**
	 * Show the lightbox.
	 * For good measure, make sure the shadow's correctly positioned and scroll it into view.
	 * @returns {void}
	 * @author Nick Davison
	 */
	show: function() {
		com.digitaria.lightbox.divShadow.style.display="block";
		com.digitaria.lightbox.divWindow.style.display="block";
		com.digitaria.lightbox.resizeShadow();
		com.digitaria.lightbox.scrollIntoView();
	},
	
	/**
	 * Hide the lightbox.
	 * @returns {void}
	 * @author Nick Davison
	 */
	hide: function() {
		com.digitaria.lightbox.divShadow.style.display="none";
		com.digitaria.lightbox.divWindow.style.display="none";
	},
	
	/**
	 * Load content in to the lightbox via AJAX.
	 * Once the content's loaded, show the lightbox.
	 * @returns {void}
	 * @param {hash} params Optional parameters.
	 * @param {string} params.url The url you wish to load in.
	 * @param {function} onSuccess Any callback you want once the content is loaded.
	 * @param {function} onFailure Any callback you want if there's a problem loading the content.
	 * @author Nick Davison
	 */
	load: function(params) {
		var params = Object.extend({
			url: "",
            method: 'get',
			onSuccess: function() {},
			onFailure: function() {}
		}, params || { });
		
		new Ajax.Request(params.url, {
            method: params.method,
			onSuccess: function(data) {
				com.digitaria.lightbox.divWindow.innerHTML=data.responseText;
                // See if there is any JS in the response
                try {
                    var scripts = com.digitaria.lightbox.divWindow.getElementsByTagName('script');
					if ((typeof scripts!="undefined") && (scripts!=null) && (scripts.length!=null)) {
	                    for (var i = 0; scripts[i]; i++) {
    	                    eval(scripts[i].innerHTML);
        	            }
					}
                } catch(err) {
                    alert('There was an error evaluating js in the lightbox: ' + err.description);
                }
				com.digitaria.lightbox.show();
				params.onSuccess(data);
			},
			onFailure: function() {
				com.digitaria.lightbox.hide();
				params.onFailure();
			}
		});
	},
	
	/**
	 * Set up the shadow best suited to the browser.
	 * If it's IE<7 - Don't use a shadow.
	 * If it's Safari - Use the boxShadow from CSS3.
	 * Everything else, use a whole bunch of PNGs.
	 * @returns {void}
	 * @author Nick Davison.
	 */
	buildShadow: function() {
		var imagePath="/images/modal/";
		var radius=20;
		// If we're in IE6 or less, bail without creating the shadow
		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) return; // Bail if we're in an old version of IE
		}
		if (navigator.appVersion.toString().toLowerCase().indexOf("webkit")!=-1) {
			// If we're in safari and can show boxShadows...
			document.getElementById('lightbox_window').style.webkitBoxShadow='0 0 '+radius+'px #000';
		} else {
			// Otherwise, let's build a mess of PNGs to create a scalable, distortable shadow
			var content=new Array();
			content.push('<table id="lightbox_shadow_table" border="0" cellspacing="0" cellpadding="0">');
			content.push('   <tr>');
			content.push('      <td id="lightbox_top_left" width="20" height="20">&nbsp;</td>');
			content.push('      <td id="lightbox_top" height="20">&nbsp;</td>');
			content.push('      <td id="lightbox_top_right" width="20" height="20">&nbsp;</td>');
			content.push('   </tr>');
			content.push('   <tr>');
			content.push('      <td id="lightbox_left" width="20">&nbsp;</td>');
			content.push('      <td id="lightbox_middle">&nbsp;</td>');
			content.push('      <td id="lightbox_right" width="20">&nbsp;</td>');
			content.push('   </tr>');
			content.push('   <tr>');
			content.push('      <td id="lightbox_bottom_left" width="20" height="20">&nbsp;</td>');
			content.push('      <td id="lightbox_bottom" height="20">&nbsp;</td>');
			content.push('      <td id="lightbox_bottom_right" width="20" height="20">&nbsp;</td>');
			content.push('   </tr>');
			content.push('</table>');
			com.digitaria.lightbox.divShadow.innerHTML=content.join("\n");
			com.digitaria.lightbox.tblShadow=$('lightbox_shadow_table');
			com.digitaria.lightbox.resizeShadow();
		}
		
	},
	
	/**
	 * Initialize the lightbox system, creating all of the base HTML and starting the resizeLoop.
	 * @returns {void}
	 * @author Nick Davison
	 */
	init: function() {
		var bdy=$$('body');
		bdy[0].insert('<div id="lightbox_holder"><div id="lightbox_shadow"></div><div id="lightbox_window"></div></div>');
		
		// Determine which type of scrolling to use
		if (document.location.pathname.toString().indexOf('/videos/')==0) {
			com.digitaria.lightbox.bScrollNotMove=true;
		} else {
			com.digitaria.lightbox.bScrollNotMove=false;
		}
		
		com.digitaria.lightbox.divHolder=$('lightbox_holder');
		com.digitaria.lightbox.divShadow=$('lightbox_shadow');
		com.digitaria.lightbox.divWindow=$('lightbox_window');
		
		// Build the shadow
		com.digitaria.lightbox.buildShadow();

		// Make sure everything's hidden
		com.digitaria.lightbox.hide();
		
		com.digitaria.lightbox.resizeLoop();
	}
}
