/*
 * jqPopupBox - jQuery Plugin
 * Really simple "popups"
 *
 * Examples and documentation at: http://redvulpine.com/blog/
 * 
 * Copyright (c) 2011 Fábio Rogerio
 * That said, it is hardly a one-person project. Many people have submitted bugs, code, and offered their advice freely. Their support is greatly appreciated.
 * 
 * Version: 0.1.1 (11/10/2011)
 * Requires: jQuery v1.4+
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

(function ($) {
	$.popupBox = function (options) {
		var defaults = {
			containerColor: '#FFF',
			color:			'#777',
			opacity:		.8,
			width:			400,
			height:			270,
			closeText:		'Kapat',
			close:			null,
			load:			null,
			useFrame:		false,
			content:		''
		}
		
		var options = $.extend(defaults, options);
		
		// Begin
		var r				= $(document.createElement('DIV'));
		var t				= $(document.createElement('A'));
		var overflow_state	= $(document.body).css('overflow') || 'auto';
		var height_state	= $(document.body).css('overflow') || 'auto';
		var body			= $(document.body);
		var win				= $(window);
		
		options.content = options.content.toString();
		
		if(options.content.match(/^http/i)) {
			if(options.useFrame) {
				var c = $(document.createElement('IFRAME'));
	
				c.attr('src', options.content)
				 .css('border', 'none');			
			} else {
				var c = $(document.createElement('DIV'));
				
				c.load(options.content);			
			}
		} else {
			var c = $(document.createElement('DIV'));
			
			c.html(options.content);
		}

		r.css('background-color', options.color)
		 .css('opacity', options.opacity)
		 .css('position', 'fixed')
		 .css('top', 0)
		 .css('left', 0)
		 .addClass('jqpopupbox-region');

		c.css('position', 'fixed')
		 .css('background-color', defaults.containerColor)
		 .addClass('jqpopupbox-container');
		
		t.css('position', 'fixed')
		 .addClass('jqpopupbox-close')
		 .html('&times; ' + options.closeText)
		 .css('cursor', 'pointer');

		body.append(r)
			.append(c)
			.append(t)
			.css('overflow', 'hidden')
			.css('height', '100%');

		function resize_callback() {
			r.css('width', win.width())
			 .css('height', win.height());
			
			c.css('width', options.width)
			 .css('height', options.height)
			 .css('left', (win.width() / 2) - (options.width / 2))
			 .css('top', (win.height() / 2) - (options.height / 2));
			
			t.css('top', c.offset().top - (t.height()))
			 .css('left', c.offset().left + c.width() - (t.width()))
		}
		
		function close_callback() {
			body.css('overflow', overflow_state)
				.css('height', height_state);
				
			win.unbind('resize', resize_callback);
			
			function remove() {
				c.remove();
				r.remove();
			}

			t.remove();
			
			// call the callback (hmmm redundancy? lol)
			if(options.close instanceof Function) {
				options.close.apply({remove: remove}, [r, c]);
			} else {
				remove();
			}
		}		
		
		// binds =D
		win.bind('resize', resize_callback);
		r.bind('click', close_callback);
		t.bind('click', close_callback);
		
		resize_callback();

		if(options.load instanceof Function) {
			options.load.apply(null, [r, c]);
		}
	}
})(jQuery);
