/*
 *
 * Copyright (c) 2009 Doubleclique (dev [at] doubleclique [dot] com)
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license
 *
 */

/**
 *
 * jquery.dql.overlay
 *
 * @name     jquery.dql.overlay
 * @version  0.1
 * @author   Kyle Beattie (kyle [at] doubleclique [dot] com)
 * @requires jQuery
 *
 */
/*global jQuery */
"use strict";
(function ($) {
	$.fn.overlay = function (options) {
		var $this = $(this), priv = {}, publ = {};

		options = $.extend(
			{
				color : "#fff",
				opacity : 0.5,
				fadeInSpeed : 500,
				windowWidth : 278,
				windowHeight : 420,
				windowPosX : 0,
				windowPosY : 0,
				orientation : 'left',
				overlayWindowClass : 'overlayWindow',
				load : false,
				canClose : true,
				getContentFrom : null,
				mask : $('#Overlay'),
				onLoadCallback : null,
				addExtraMarkup : {
					placement : 'before', //before || after
					html : '' // e.g <p>Some text here</p>
				}
			},
			options
		);

		priv.init = function () {
			
			priv.$triggers = $this;
			
			priv.$link = $this.attr('href');

			if (options.mask.length) {

				priv.$mask = options.mask;

				priv.dynamicMask = false;

			} else {

				priv.$mask = $('<div></div>');

				priv.dynamicMask = true;

			}
			
			priv.$overlayWindow = $('<div></div>').addClass(options.overlayWindowClass);

			priv.$modalWindow = null;

			priv.$close = $('<a></a>').attr('href','#').addClass('close');

			priv.canClose = options.canClose;

			priv.content = [];
			
			priv.addEventHandlers();
			
		};

		publ.load = function () {

			priv.createMask(priv.dynamicMask);
			
		};

		priv.addEventHandlers = function () {

			priv.$triggers.each(function (index) {

				$(this).click(function (event) {

					event.preventDefault();

					priv.createMask(priv.dynamicMask);
					
				});

			});

			if (priv.canClose) {

				priv.$mask.bind('click', function () {

					priv.closeOverlay();

				});

				priv.$close.bind('click', function (event) {

					event.preventDefault();
					
					priv.closeOverlay();
					
				});

			} else {

				priv.$mask.unbind('click');
				
			}

			$(window).resize(function () {
				priv.$mask.css({
					width : $(window).width(),
					height : $(window).height()
				});
			});

		};

		priv.closeOverlay = function () {
			
			priv.$close.hide();

			priv.$mask.animate({
				opacity : 0
			}, options.fadeInSpeed, "linear", function () {
				priv.$mask.hide();
			});

			priv.$overlayWindow.animate({
				opacity : 0
			}, options.fadeInSpeed, "linear", function () {
				priv.$overlayWindow.hide();
			});
		};

		priv.newModalWindow = function () {
		
			if (priv.$modalWindow == null) {

				$('body').append(priv.$overlayWindow);

				priv.$modalWindow = priv.$overlayWindow;

				if (options.orientation == 'left') {

					priv.$modalWindow.css('left', options.windowPosX);

				} else {

					priv.$modalWindow.css('right', options.windowPosX);

				}

				var pos = (options.canClose) ? 'absolute' : 'fixed';

				priv.$modalWindow.css({
					width : options.windowWidth+'px',
					top : options.windowPosY/2,
					opacity : 0,
					display : 'block',
					position : pos
				}).animate({
					top : options.windowPosY,
					opacity : 1
				}, options.fadeInSpeed/2, "linear", function () {
					priv.$close.show();
				});

				if (priv.getContent().length > 1) {

					priv.getContent().each(function (index) {

						var $wrapper = $('<div></div>').addClass('overlayContentSection');

						$wrapper.html($(this).clone().contents());
						
						//$(this).empty();
						
						priv.$modalWindow.prepend($wrapper);

					});

					priv.$modalWindow.append($('<div></div>').addClass('clear'));

				} else {

					priv.$modalWindow.prepend(priv.getContent().clone().contents());

					priv.getContent().empty();
					
				}

				if (typeof options.addExtraMarkup == 'object') {

					if (options.addExtraMarkup.html) {

						if ( options.addExtraMarkup.placement == 'before') {

							priv.$modalWindow.prepend(options.addExtraMarkup.html);

						} else if (options.addExtraMarkup.placement == 'after') {

							priv.$modalWindow.append(options.addExtraMarkup.html);

						}

					}
					
				}

				priv.$modalWindow.find('h2').each(function () {

					$(this).text($(this).text());

				});

				Cufon.refresh();

			} else {
				priv.$modalWindow.css({
					width : options.windowWidth+'px',
					top : options.windowPosY/2,
					opacity : 0,
					display : 'block',
					position : pos
				}).animate({
					top : options.windowPosY,
					opacity : 1
				}, options.fadeInSpeed/2, "linear", function () {
					priv.$close.show();
				});
			}

			priv.$modalWindow.find('.overlayContentSection:even').addClass('overlayContentSectionEven');

			if (priv.canClose) {

				priv.$close.css({
					top : options.windowPosY-6
				}).hide();

				priv.$overlayWindow.after(priv.$close);

				priv.$close.css({
					left : (priv.$overlayWindow.offset().left + options.windowWidth) - (priv.$close.width() / 2)
				});
			}
			
			priv.$close.fadeIn(options.fadeInSpeed*2);

			if (typeof options.onLoadCallback == 'function') {

				options.onLoadCallback(priv.$overlayWindow);

			}

		};

		priv.getContent = function () {
			
			if (options.getContentFrom != null) {

				priv.content = options.getContentFrom;
				
			}

			return priv.content;

		};

		priv.createMask = function (isDynamic) {

			if (isDynamic) {

				priv.$mask.css({
					background : options.color,
					opacity : 0,
					display : 'none',
					position : 'absolute',
					top : '0',
					left : '0',
					zIndex : 9,
					width : $(document).width(),
					height : $(document).height()
				});

				$('body').append(priv.$mask);

			} else {

				priv.$mask.css({
					opacity : 0,
					width : $(document).width(),
					height : $(document).height()
				});
				
			}

			priv.$mask.show().animate({
				opacity : options.opacity
			}, options.fadeInSpeed, "linear");

			priv.newModalWindow();

		};

		priv.init();

		return publ;

	}
}(jQuery));
