/* 
 * 	This is Toms simple modal window... it's simple... thats why I called it SimpleModal.
 *
 *	If you want to make it complex then go make a ComplexModal. 
 *
 * 	NOTE: you need jQuery on the page for it to work!
 *
 * 	Instructions:
 *	call SimpleModal.open('http://www.myurl.com',500,600); to open a modal window 500px heigh by 600px wide.
 *	call SimpleModal.close(); to close the modal window you last opened.
 *
 *	Added 24 March 09
 *	If you need to call a callback type function when the modal you open has been closed then add it at the end
 *	of the parameters when opening;
 *	SimpleModal.open('http://www.myurl.com',500,600, function() {  alert('closed'); });
 */

/*jslint  eqeqeq: true, browser: true */
/*global $, jQuery  */
 
// topWin is a global variable which gives you the top window in your domain name.

jQuery.noConflict();

var topWin = window;
 
var SimpleModal = function() {

	var that = {};

	that.open = function(url, height, width, callback) {	
		if (!window.SimpleModal.isController) {
			if (typeof topWin.SimpleModal !== 'object') {
				alert('Simple modals need the JavaScript available in the top window.');
			} 
			else 
			{
				topWin.SimpleModal.open(url, height, width, callback);
			}
			return;
		}

		// sort out ie bugs
		if (jQuery.browser.msie) {
			jQuery('select').css('visibility','hidden');
			jQuery('html').css('overflow','hidden');
		}

		var mask = document.createElement('div'),
			eDiv = document.createElement('div');
		
		jQuery(document.body).append(mask).append(eDiv);
		jQuery(mask).addClass('_simpleModalMask').
		css({
			'Z-INDEX':'1001',
			'position':'fixed',
			'top':'0px',
			'left':'0px',
			'width':jQuery(document).width() + 'px',
			'DISPLAY':'none', 
			'height':jQuery(document).height() + 'px',
			'margin':'0px',
			'padding':'0px',
			'background-color':'#000',	
			'filter':'alpha(opacity=70)',
			'opacity':'0.7',
			'overflow':'hidden'
		});
		
		jQuery(eDiv).addClass('_simpleModal').
		css({
			'Z-INDEX':'1002',
			'height':height + 'px',
			'width':width + 'px',
			'padding':'0px',
			'margin':'0px',
			'border':'1px solid #ccc',
			'position':'absolute',
			'background-color':'#FFF',
			'left': ((jQuery(window).width() / 2) - (width / 2)) + jQuery(document).scrollLeft() + 'px',
			'top': ((jQuery(window).height() / 2) - (height / 2)) + jQuery(document).scrollTop() + 'px'
//			'top':'0px' 
		}).
		append("<iframe frameborder='0' scrolling='yes'></iframe>");
		
		jQuery('iframe:first', eDiv).attr('src',url).
		css({
			'height':'100%',
			'width':'100%',
			'background-color':'#FFF',
			'border':'none',
			'overflow':'hidden'
		});
		
		jQuery(window).scroll(function() {
			jQuery('div._simpleModal').css({
				'left': ((jQuery(window).width() / 2) - (width / 2)) + jQuery(document).scrollLeft() + 'px',
				'top': ((jQuery(window).height() / 2) - (height / 2)) + jQuery(document).scrollTop() + 'px'
			});
		});
		
		that.callbacks.push(callback);
	};
	
	that.close = function() {
		if (!window.SimpleModal.isController) {
			if (typeof topWin.SimpleModal !== 'object') {
				alert('Simple modals need the JavaScript available in the top window.');
			} else 
			{
				topWin.SimpleModal.close();
			}
			return;
		}
	
		jQuery('div._simpleModal:last').remove();
		jQuery('div._simpleModalMask:last').remove();
		
		if (jQuery('div._simpleModal').length < 1) {
			// sort out ie bugs
			if (jQuery.browser.msie) {
				jQuery('select').css('visibility','visible');
				jQuery('html').css('overflow','auto');
			}
		}
		
		if (that.callbacks.length > 0) {
			var fn = that.callbacks.pop();
			if (typeof fn === 'function') {
				fn();
			}
		}
	};
	
	that.isController = false;
	that.callbacks = [];
	
	return that;
}();

/* Need to find the true top window for the current domain. There are case's
 * where the actual top frame is another domain when a URL is using frame masking.	
 */
(function() { 
	var previousTopWin;

	while (true) {
		var ref;
		
		try {
			ref = topWin.parent.location.href;
		}
		catch(err) {
			ref = undefined;
		}
		
		if (typeof ref === 'undefined' || previousTopWin === topWin) {
			break;
		}
		else {
			previousTopWin = topWin;
			topWin = topWin.parent;
		}
	}

	if (typeof topWin.SimpleModal === 'object') {
		topWin.SimpleModal.isController = true;
	}
}());
