/*
 * Copyright (c) 2008 Tom Coote (http://www.tomcoote.co.uk)
 * This is licensed under GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/** 
 * 	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.
 *
 */

/*jslint  eqeqeq: true, browser: true */
/*global $ */
 
var SimpleModal = {
	open: function(url, height, width) {
		if (!window.SimpleModal.isController) {
			if (typeof window.top.SimpleModal !== 'object') {
				alert('Simple modals need the JavaScript available in the top window.');
			} else {
				window.top.SimpleModal.open(url, height, width);
			}
			return;
		}
	
		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':'#FFFFFF',
			'opacity':0.8
		});
		
		$(eDiv).addClass('_simpleModal').
		css({
			'height':height + 'px',
			'width':width + 'px',
			'padding':'0px',
			'margin':'0px',
			'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.top).scroll(function() {
			$('div._simpleModal').css({
				'left': (($(window).width() / 2) - (width / 2)) + $(document).scrollLeft() + 'px',
				'top': (($(window).height() / 2) - (height / 2)) + $(document).scrollTop() + 'px'
			});
		});
	},
	close:function() {
		$('div._simpleModalMask:last', window.top.document).remove();
		$('div._simpleModal:last', window.top.document).remove();
	},
	isController:false
};

if (typeof window.top.SimpleModal === 'object') {
	window.top.SimpleModal.isController = true;
}