/******************************************
 * The Base Modal Class
 ******************************************/
 if (typeof crumbs == "undefined" || !crumbs) {
    var crumbs = {};
}

Windows.overlayShowEffectOptions = {duration: 2};
Windows.overlayHideEffectOptions = {duration: 2};

crumbs.Modal = Class.create({
	contentWidth: 584,

	options: {
		width: 614,
		height: 600,
		resizable: false,
		minimizable: false,
		maximizable: false,
		draggable: false,
		destroyOnClose: true,
		showEffectOptions: { duration: 0.5 },
		hideEffectOptions: { duration: 0.5 },
		useOverlay: false
	},

	loadOptions: {
		width: 236, 
		height: 63,
		resizable: false,
		minimizable: false,
		maximizable: false,
		draggable: false,
		destroyOnClose: true,
		showEffectOptions: { duration: 0.2 },
		hideEffectOptions: { duration: 0.2 }
	},
	
	/**
	 * Constructor
	 */
	initialize: function() {
	},

	/**
	 * Sets the ajax request url.
	 *
	 * @param string url
	 */
	setUrl: function(url) {
		this.url = url;
	},

	/**
	 * Sets the dimensions of the modal.  This doesn't really do anything.
	 *
	 * @param int width
	 * @param int height

	setDimensions: function (width, height) {
		this.width = width;
		this.height = height;
	},
	*/

	/**
	 * Shows the modal
	 */
	showWindow: function() {
		this.window.showCenter();
		$(this.containerId + "_content").setStyle({width: this.contentWidth + "px"});
	
		if (this.useOverlay) {
			this.overlayHideWindowEvent = this.overlayHideWindow.bindAsEventListener(this);
			Event.observe($('overlay_modal'), 'click', this.overlayHideWindowEvent);
		}
	},

	/**
	 * Hides the modal when user clicks on overlay.
	 * This needs to call window.hide(), since the close button wasn't
	 * actually clicked
	 */
	overlayHideWindow: function() {
		this.hideWindow(true);
	},
	
	/**
	 * Hides the modal
	 *
	 * By default, triggerHide is false because it's triggered when the X button is closed.  
	 * Hiding the window when the X is clicked would double-close the modal and generate an error.
	 *
	 * @param boolean triggerHide
	 */
	hideWindow: function(triggerHide) {
		if (triggerHide) 
			this.window.hide();  	
		var closer = $(this.containerId + "_close");
		Event.stopObserving(closer);
		if (this.useOverlay) {
			WindowUtilities.enableScreen();
		}
		screenManager.getLocation().clearProduct();
		addthis_close();
	},

	/**
	 * Load the modal content via an asynchronous request.
	 *
	 * @param array options
	 */
	load: function (options) {
		if (!Object.isArray(options)) {
			this.eventCreate = this.loadCreate.bindAsEventListener(this);
			this.eventSuccess = this.loadSuccess.bindAsEventListener(this);
			this.eventFailure = this.loadFailure.bindAsEventListener(this);

			var options = {
				method: 'get',
				onCreate: this.eventCreate,
				onSuccess: this.eventSuccess,
				onFailure: this.eventFailure
			};
		}
		
		this.ajaxRequest = new crumbs.Ajax.Request(this.url, options);
	},

	/**
	 * Create (loading) callback.
	 */	
	loadCreate: function (transport) {
		if (this.useOverlay) {
			WindowUtilities.disableScreen(this.window.options.className, 'overlay_modal', this.window.overlayOpacity, this.window.getId(), this.window.options.parent);
			Windows.maxZIndex++;
		}
		this.loadWindow.getContent().innerHTML = this.getLoadingHTML();
		this.loadWindow.showCenter();
	},
	
	/**
	 * Load success callback.
	 */
	loadSuccess: function(transport) {
		this.loadWindow.hide();
		this.window.getContent().innerHTML = transport.responseText;
		this.evalHtml(this.window.getContent());
		this.showWindow();

		// track the page view
		screenManager.trackPageview(this.url);
	},

	/**
	 * Load failure callback.
	 */
	loadFailure: function(transport) {
		if (!Object.isUndefined(this.loadWindow)) {
			this.loadWindow.hide();
		}
		if (this.useOverlay) {
			WindowUtilities.enableScreen();
		}
	},

	/**
	 * Returns an element holding modal loading html content.
	 */
	getLoadingContent: function() {
		var div = document.createElement("div");
		div.className = "modal-loading";
		var loadingImg = document.createElement("img");
		loadingImg.setAttribute("src", TEMPLATE_PATH+"images/loading.gif");
		var textImg = $(document.createElement("img"));
		textImg.setAttribute("src", TEMPLATE_PATH+"images/loading_text.gif");
		textImg.addClassName("loading-text");
	
		div.appendChild(loadingImg);
		div.appendChild(textImg);
	
		return div;
	},
	
	/**
	 * Returns a string of modal loading html content.
	 */
	getLoadingHTML: function () {
		var container = document.createElement("div");
		container.appendChild(this.getLoadingContent());
		return container.innerHTML;
	},

	/**
	 * Executes the javascript in script tags within the element 'el'.
	 */
	evalHtml: function (el)
	{
		var scripts = el.getElementsByTagName("script");
		for (i = 0; i < scripts.length; i++) {
			eval(scripts[i].innerHTML.toString());
		}
	}
});