﻿/**
* jQuery.vinter.overlay()
* Version 0.2
* Updated 15 jan 2008
*
*	jQuery.vinter.overlay() is (c) 2008 Lars Huring, Olov Nilzén and Vinter (www.vinterwebb.se) and is released under the MIT License:
*	http://www.opensource.org/licenses/mit-license.php
*
* VERSION HISTORY:
* 0.1
* - trigger overlay on every <a> with class "v-overlay"
* - load <a href="someurl.htm"> into overlay
* - remove overlay on close
* - custom event: createOverlay
* - custom event: showOverlay
* - custom event: removeOverlay
* 
* 0.2
* - changed creation of default settings to correct way
* - small code cleanups
* - Tiny bit of documentation
*
* 0.3
* - Added remote-call functionality
* - Use iframe in overlay to enable remote calls
*
* Usage (simple):
* $(document).ready(function() { $.overlay(); })
* <a href="someurl.aspx" class="v-overlay">load someurl into overlay</a>
*
* On trigger (needs 'this' to get the event and href):
* <a href="someurl.aspx" onclick='return $.overlay({show: true}, this)'>init on trigger</a>
*/

(function(jQuery) {

	jQuery.overlay = function(opt, ev)
	{	
		// Default options
		var defaults = {
			show: false,
			overlayclass: ".vinteroverlay",
			overlayId: "V-overlay",
			overlayWindowId : "V-overlay-window",
			overlayIframeId: "v-overlay-iframe",
			overlayWidth: "800",
			overlayHeight: "520",
			trigger: "v-overlay",
			remoteCall: false
		};
		
		// Create options object from defaults and user configured
		var options = jQuery.extend(defaults, opt);
		
		function positionOverlay()
		{
			// position the overlay on the screen
			jQuery("#" + options.overlayWindowId).css({
				width: options.overlayWidth + "px",
				height: options.overlayHeight + "px",
				marginLeft: "-" + parseInt((options.overlayWidth / 2), 10) + "px",
				marginTop: "-" + parseInt((options.overlayHeight / 2), 10) + "px"
			});
		}
		
		// Show the overlay
		function showOverlay(e) 
		{
			
			var url;
			
			if (e.target)
				url = e.target.href
			else
			{
				url = e.href;
				// Unbind manually
				$(e).unbind();
			}
			
			
			jQuery("#" + options.overlayWindowId).append(close);
			jQuery(document).trigger("showOverlay", [this]);
			
			jQuery("#" + options.overlayIframeId)
				.attr("src", url)
				.attr("frameborder", "0");
			
		
			// Place the div
			positionOverlay();
			
			// Add click event to remove overlay on click.
			jQuery("#" + options.overlayId)
				.click(removeOverlay)
				.css({
					display: "block"
				});
		
			jQuery("#" + options.overlayWindowId).css({
				display: "block",
				width: options.overlayWidth,
				height: options.overlayHeight
			});
			
			return false;
		}
		
		// Hide overlay
		function removeOverlay()
		{
			// Hide overlay & unbind events
			jQuery("#" + options.overlayId + ",#" + options.overlayWindowId).fadeOut("fast", function()
			{
				jQuery("#" + options.overlayId + ",#" + options.overlayWindowId + ",#V-hideselect").trigger("unload").unbind().remove();
				jQuery("#V-overlay-close-btn").unbind().remove();
			});
			
			// Check IE6
			if (jQuery.browser.msie && (parseInt(jQuery.browser.version) === 6))
			{
				jQuery("body","html").css(
				{
					height: "auto", 
					width: "auto"
				});
				
				jQuery("html").css("overflow","");
			}
			
			jQuery(document).trigger("removeOverlay");
			
		}
		
		// Create the overlay div
		function createOverlay(e)
		{		
		
		
			if (jQuery("#" + options.overlayId).length > 0)
			{
				showOverlay(e);
			}
			else
			{
			
				// IE6 check
				if (jQuery.browser.msie && jQuery.browser.version < 7)
				{
					jQuery("body", "html").css({
						height: "100%",
						width: "100%"
					});
					
					jQuery("html").css({ overflow: "hidden" });
					
					if (jQuery("#V-hideselect").length == 0)
						jQuery("body").append("<iframe id='V-hideselect'></iframe>");
					
				} 
				
				var o = jQuery("<div />")
							.attr("id", options.overlayId);
				
				var ow = jQuery("<div />")
					.attr("id", options.overlayWindowId)
					.appendTo("body");
					
				jQuery("<a/>")
					.attr("id", "V-overlay-close-btn")
					.bind("click", removeOverlay)
					.html("<span>[x] stäng</span>")
					.css({
						cursor: "pointer"
					})
					.appendTo(ow);
					
				jQuery("<iframe frameborder='0' />")
					.attr("id", options.overlayIframeId)
					.css("border", "0px solid #FFF")
					.attr("frameborder", "0")
					.appendTo(ow);
					
				o.appendTo("body");
				
				
				// jQuery("body").append("<div id='" + options.overlayId + "'></div><div id='" + options.overlayWindowId + "'>" + close +  "<iframe id='" + options.overlayIframeId + "'></iframe></div>");
				
				// Needs Safari PNG fix
				jQuery("#" + options.overlayId).addClass("V-overlayBg");
				
				jQuery(document).trigger("createOverlay", jQuery("#" + options.overlayId));
				showOverlay(e);
						
			}
			
			return false;
		}
		
		// Get all the links that triggers overlay
		jQuery("." + options.trigger).each(function(i, item) {
			jQuery(item).click(createOverlay);
		});
		
		if (options.show === true) createOverlay(ev);
			
		
		return false;
		
	}
	
})(jQuery);