/*
 * popupbox (for jQuery)
 * version: 1.0 (12/19/2007)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/popupbox/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007 Chris Wanstrath [ chris@ozmm.org ]
 *
 * Usage:
 *
 *  jQuery(document).ready(function() {
 *    jQuery('a[@rel*=popupbox]').popupbox()
 *  })
 *
 *  <a href="#terms" rel="popupbox">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="popupbox">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="popupbox">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 *
 *    jQuery.popupbox('some html')
 *
 *  This will open a popupbox with "some html" as the content.
 *
 *    jQuery.popupbox(function() { ajaxes })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */
(function($) {
  $.popupbox = function(data)
  {
    //$.popupbox.init();
    $.isFunction(data) ? data.call() : $.popupbox.reveal(data);
  }

  /**
   * Closes the popup and removes
   * event handlers.
   */
  $.popupbox.close = function()
  {
    $(document).unbind("keydown");
    $.jqDnR.stop();

    // Fade overlay and popup, then remove the overlay class and popup style.
    $("#popup-overlay").fadeOut("fast", function()
    {
        $("#popup-overlay").remove();
    });

    $(".popup")
      .hide()
      .removeClass(".popup");
//      .fadeOut("fast", function()
//	    {
//	        $(this).removeClass("popup");
//	    });

    return false;
  }

  /**
   * Attaches click handlers to open a popup to
   * the selected elements.
   */
  $.fn.popupbox = function()
  {
    function click_handler() {
      var targetId = this.href.split('#')[1];

      $.popupbox.reveal(targetId);

      return false;
    }

    // Attach click handler.
    this.click(click_handler)
    return this
  }

  /**
   * Opens the popup box and sets up
   * event handler.s
   */
  $.popupbox.reveal = function(popupId)
  {
    popupId = "#" + popupId;

    //console.log("$.popupbox.reveal pid = " + popupId +" this = "+this);

    // Add overlay div and popup CSS class, centre popup.
    $(popupId)
        .before($('<div id="popup-overlay" style="display:none;"></div>').css("opacity", 0.3))
        .addClass("popup")
        .jqDrag('.jqDrag');

    $.popupbox.centre(popupId);

    // If window was resized, calculate the new overlay dimensions
		$(window).resize(function() {
			// Get page sizes

			var arrPageSizes = $.getPageSize();
			// Style overlay and show it
			//$('#popup-overlay').css({
				//width:		arrPageSizes.pageWidth,
			//	height:		arrPageSizes.pageHeight
			//});
			// Get page scroll
			var arrPageScroll = $.getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
		$('.popup').css({
				top:	arrPageScroll.top + (arrPageSizes.windowHeight / 50),
				left:	arrPageScroll.left
			});
		});

  	// Bind Escape key and close button to close method.
    $(document).keydown(function(e) {
      if (e.keyCode == 27) $.popupbox.close()
    });
    $('.popup .close').click($.popupbox.close);
    $('.popup .submit').click($.popupbox.close);
    //ensure transparent layer has opacity 0.3 as IE initialises it to 1 first time round; causing black background 
    $("#popup-overlay").css('opacity',0.3);
    // Fade controls in.
    $(".popup, #popup-overlay").show();
  }

  /**
   * Centres the popup.
   */
  $.popupbox.centre = function(popupId)
  {
		var pageSizes = $.getPageSize();
		var pageScroll = $.getPageScroll();

		var setX = (pageSizes.pageWidth - $(popupId).width()) / 2;
		setX = (setX < 0) ? 0 : setX;

		$(".popup").css({
		    "left"         : setX+"px",
		    "top"          : pageScroll.top + (pageSizes.windowHeight / 10)
		});
  }

  /**
   * Shows a popup frame in the centre of the page with default
   * options set so that clicking outside the frame closes it.
   */
  $.fn.popupframe = function(options)
  {
  	var el = $(this);
  	var options = $.extend({
  		closeButton: null,
  		closeOnEscape: true,
  		closeOnOutsideClick: true
  	}, options || {});

  	function closeFrame()
  	{
  		$(document)
  		  .unbind("click", closeFrame)
  		  .unbind("keydown");

  		el
  		  .removeClass("popup")
  		  .hide()
  		  .prev()
  		  .remove();

  		return false;
  	}

    // Click outside frame to close it.
    if (options.closeOnOutsideClick)
    {
		  $(document).mouseup(function()
		  {
		  	$(document)
		  	  .unbind("mouseup")
		  	  .bind("click", closeFrame);

		    return false;
		  });

	    // So that clicking inside the frame doesn't close it.
		  el.click(function() { return false; });
    }

    // Close on Escape key hit.
    if (options.closeOnEscape)
    {
	    $(document).keydown(function(e)
	    {
	      if (e.keyCode == 27)
	        closeFrame();
	    });
    }

    if (options.closeButton)
    {
    	el
    	  .find(options.closeButton)
    	    .click(closeFrame);
    }

	  el
      .before(
        $('<div></div>')
          .css({
          	"opacity": 0.3,
          	"z-index": 10,
          	"background-color": "#000"
          })
          .fullScreen())
	    .addClass("popup")
	    .show();
	    //.centre();		
  	return this;
  }

  $(document).ready(function() {
    if(!location.href.match('podcasts')) 
        $("a[@rel=popupbox]").popupbox();
  });

})(jQuery);
