/**
 * @fileOverview Code for generating a div popup
 * @author Wouter Bos
 * @since 1.0 - 2010-04-7
 * @version 1.0 - 2010-04-7
 */



/**
 * @requires jQuery
 * @see ESDN for related HTML and CSS
 * @class
 * @since 1.0 - 2010-02-23
 * @version 1.0 - 2010-02-23
 * @constructor
 * @param {Object} [newConfig] Configuration object.
 * @param {String} [newConfig.popupCookieName] The name of the cookie that determines if a popup should be shown or not
 * @param {Date} [newConfig.popupCookieExpire] The date and time  on which the cookie will expire
 * @param {Boolean} [newConfig.popupDeactivated] The value of the cookie that determines if a popup should be shown or not
 * @param {Boolean} [newConfig.scrollToWindow] If true, the page will be scrolled to the popup window
 * @param {Number} [newConfig.animationSpeed] Sets the animation speed in milliseconds
 * @param {String} [newConfig.closeText] Sets the text in the close button
 * @param {String} [newConfig.windowSelector] The selector of the popup div
 * @param {String} [newConfig.backgroundSelector] The selector of the background div
 * @param {Number} [newConfig.backgroundOpacity] Opacity level of the layer below the popup that obscures the website
 * @example
 * var popupConfig = {
 *     animationSpeed: 250
 * }
 * var popup = new Estate.Popup(popupConfig);
 * popup.Open();
 */
Estate.Popup = function(newConfig) {
	var error
	error = Estate.Check.ArgumentsCount(arguments.length, [0, 1]);
	if (error != "") throw new Error(error);



	var isIE6 = /MSIE 6/i.test(navigator.userAgent)
	var popCounter = 0
	var config = {
		popupCookieName: "deactivated",
		popupCookieExpire: new Date(2015, 12, 31, 23, 59, 59, 0),
		popupDeactivated: false,
		scrollToWindow: false,
		animationSpeed: 250,
		closeText: "x",
		windowSelector: "#DivPopupWindow",
		backgroundSelector: "#DivPopupBackground",
		backgroundOpacity: 0.5
	}

	function popupIsDisabled() {
		if (Estate.Cookies.Get(config.popupCookieName) == null) {
			config.popupDeactivated = false;
		} else {
			config.popupDeactivated = true;
		}
	}

	function scrollToPopupWindow() {
		if (config.scrollToWindow == true) {
			jQuery('html, body').animate({ scrollTop: jQuery(config.windowSelector).offset().top }, 500)
		}
	}

	function setConfig(newConfig) {
		var error
		error = Estate.Check.ArgumentsCount(arguments.length, 1);
		if (error != "") throw new Error(error);
		error = Estate.Check.LiteralUpdatable(config, newConfig);
		if (error != "") throw new Error(error);

		if (newConfig != null) {
			Estate.Check.UpdateLiteral(config, newConfig)
		}
	}

	function close() {
		jQuery(config.windowSelector).slideUp(config.animationSpeed);
		jQuery(config.backgroundSelector).animate({ opacity: 0 }, config.animationSpeed, function() { jQuery(config.backgroundSelector).hide() });
		if (jQuery(config.windowSelector).find("object, embed").size() > 0) {
			jQuery(config.windowSelector).find("div.movieContainer").html("<div id='movieContainer'></div>")
		}
	}

	if (arguments.length > 0) {
		setConfig(newConfig)
	}

	/* Start public */
	/**
	 * Shows popup
	 */
	this.Open = function() {
		popupIsDisabled()

		if (config.popupDeactivated == false) {
			jQuery(config.windowSelector).find("div.close").text(config.closeText);
			jQuery(config.windowSelector).slideDown(config.animationSpeed, scrollToPopupWindow);
			jQuery(config.backgroundSelector).css("opacity", 0)
			jQuery(config.backgroundSelector).animate({ opacity: config.backgroundOpacity }, config.animationSpeed);
			jQuery(config.backgroundSelector).show()
			if (isIE6 == true) {
				jQuery(config.backgroundSelector).css("height", jQuery("body").height() + "px")
			}
			if (popCounter == 0) {
				jQuery(config.backgroundSelector + ", " + config.windowSelector + " div.close").click(function() {
					close()
				})
				jQuery(config.windowSelector + " input.tag_deactivate").change(function() {
					if (jQuery(this).is(':checked')) {
						Estate.Cookies.Set(config.popupCookieName, "true", config.popupCookieExpire, "/", "", false);
					} else {
						Estate.Cookies.Delete(config.popupCookieName);
					}
				})
			}
			popCounter++
		}
	}
	
	/**
	 * Hides popup
	 */
	this.Close = function() {
		close();
	}
	/* End public */
};

