/* 
 * 	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  */
/** JP 20/07/09*/
/**   JS Linted*/
/**   Checked and no issues. */
/** JP END*/
 
// topWin is a global variable which gives you the top window in your domain name.
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) {
			$('select').css('visibility','hidden');
			$('html').css('overflow','hidden');
		}

		var mask = document.createElement('div'),
			eDiv = document.createElement('div');
		
		$(document.body).append(mask).append(eDiv);
		$(mask).addClass('_simpleModalMask').
		css({
			'position':'absolute',
			'top':'0px',
			'left':'0px',
			'width':$(document).width() + 'px',
			'height':$(document).height() + 'px',
			'margin':'0px',
			
			'padding':'0px',
			'background-color':'#000000',
			'opacity':0.8,
			'overflow':'hidden'
		});
		
		$(eDiv).addClass('_simpleModal').
		css({
			'height':height + 'px',
			'width':width + 'px',
			'padding':'0px',
			'margin':'0px',
			'z-index':'300',
			'border':'1px solid #ccc',
			'position':'absolute',
			'background-color':'#FFF',
			'left': (($(window).width() / 2) - (width / 2)) + $(document).scrollLeft() + 'px',
			'top': (($(window).height() / 2) - (height / 2)) + $(document).scrollTop() + 'px'
		}).
		append("<iframe frameborder='0' scrolling='no'></iframe>");
		
		$('iframe:first', eDiv).attr('src',url).
		css({
			'height':'100%',
			'width':'100%',
			'background-color':'#FFF',
			'border':'none',
			'overflow':'hidden'
		});
		
		$(window).scroll(function() {
			$('div._simpleModal').css({
				'left': (($(window).width() / 2) - (width / 2)) + $(document).scrollLeft() + 'px',
				'top': (($(window).height() / 2) - (height / 2)) + $(document).scrollTop() + 'px'
			});
		});
		
		if (typeof callback === 'function') {
			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;
		}
	
		$('div._simpleModal:last').remove();
		$('div._simpleModalMask:last').remove();
		
		if ($('div._simpleModal').length < 1) {
			// sort out ie bugs
			if (jQuery.browser.msie) {
				$('select').css('visibility','visible');
				$('html').css('overflow','auto');
			}
		}
		
		if (that.callbacks.length > 0) {
			var fn = that.callbacks.pop();
			fn(); // doesn't add in unless it's a function
		}
	};
	
	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;
	}
}());