﻿/************************************** Splendid *************************************
* Created By:		Seb Wells ( seb@howsplendid.com )
* Creation Date:	15th December 2010
* Edited ----------------------------------------------------------------------------
*       By:              On:  
* Description -----------------------------------------------------------------------
*       Create a lightbox with an iframe. Based on Steve Doggett's accessible lightbox.
*
*       Dependancies:
*       - JQuery plugin v1.4.1
*       - accessible-lightbox.js
*       - css style .nockBack { background: none repeat scroll 0 0 #000000; left: 0; position: absolute; top: 0; z-index: 400; }
*
*       Usage: 
*        html in host page: <a href="/lightboxcontents.htm" class="signInOverlay">Sign In</a>   
*        script: jQuery(document).ready(function () {
*            // Wire up a click event for the popuplink
*            jQuery('.signInOverlay').click(function () {
*                var lightbox = new iframeLightbox(this.href, 600, 500);
*                lightbox.CreateLightbox();
*                return false;
*            });
*        });
*        html in client page: <a href="javascript:parent.CloseLightbox();">close</a>
*          
**************************************************************************************/

function iframeLightbox(href, width, height) 
{
	this.width = width;
	this.height = height;
	this.href = href;

	this.CreateLightbox =
	function () {
		//alert("CreateLightbox");
		this.RemoveOldLightbox();
	};

	this.RemoveOldLightbox = 
	function ()
	{
		//alert("RemoveOldLightbox");
		if ($('#' + popupID).length == 0) 
		{
			this.PostFadeOut();
			return;
		}

		$('#'+popupID).fadeOut(fadeSpeed, function()
		{
			$('#'+popupID).remove();
			this.PostFadeOut();
		});
	};

	this.PostFadeOut =
	function () 
	{
		//alert("PostFadeOut");
	    $('body').append('<div id="' + popupID + '"><iframe src ="' + this.href + '" scrolling="no" frameborder="0" width="' + this.width + '" height="' + this.height + '" allowtransparency="true" /></div>');

		$('#'+popupID).css(
			{
				display: 'none',
				position: "absolute",
				zIndex: 500
			}
		);

		PositionLightbox();

		this.FadeBackgroundIn();
		$('#'+popupID).fadeIn(fadeSpeed);
	};
	
	this.FadeBackgroundIn =
	function ()
	{
		//alert("FadeBackgroundIn");
		var pageHeight = $(document).height();
		$('body').append('<div class="nockBack" style="opacity:0;"></div>');

	
		$("."+fadeClass).css({
			"height": $(document).height(),
			"z-index'": "5",
			"width": $(window).width()
		}).animate({
			"opacity": 0.7
		}, fadeSpeed);

		// This function is in main-navigation.js and it hides dropdowns, checkboxes and radio buttons in ie6.
		checkForDodgyIE6Elements("hidden");

		// When the fade is clicked, remove it again
		$("."+fadeClass).click(CloseLightbox);	
	
		$(window).resize(this.ResizeEvent);	
	};
	
	this.ResizeEvent =
    function () 
    {
        try {
            $("." + fadeClass).css(
                {
                    "height": $(document).height(),
                    "width": $(window).width()
                }
            );
            PositionLightbox();
        }
        catch (e) { }
		
    };
}

function PositionLightbox ()
{
    if ($('#' + popupID).height() > $(window).height()) {
        $('#' + popupID).css({
            left: ($(window).width() / 2) - ($('#' + popupID).width() / 2) + 'px',
            top: '20px'
        });
        return;
    }
    
    //alert("PositionLightbox" + );
	
    var topOffset = getTopOffset();

	if ($('#' + popupID).height() >= $(window).height()) {
		topOffset = topOffset + 20;
	} else {
		topOffset = topOffset - 15;
	}
	
	// Position the popup in the centre of the browser window and fade it in
	$('#'+popupID).css({
		left: ($(window).width() / 2) - ($('#'+popupID).width() / 2) + 'px',
		top: ((($(window).height() / 2) + topOffset) - ($('#'+popupID).height() / 2)) + 'px'
	});
};

function CloseLightbox ()
{
	//alert("CloseLightbox");
	// Fade out the page div so page is visible as noraml again.
	$("."+fadeClass).fadeOut(fadeSpeed, function()
	{
		$("."+fadeClass).css({
			height: "0px"
		}).unbind("click");
		$("."+fadeClass).remove();

		// This function is in main-navigation.js and it shows dropdowns, checkboxes and radio buttons in ie6.
		checkForDodgyIE6Elements("visible");

		$(window).unbind("resize");
	});

	$('#'+popupID).fadeOut(fadeSpeed, function()
	{
		$('#'+popupID).remove();
	});
}; 
