/******************************************
 * The Base Modal Class
 ******************************************/

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

onepica.Modal = Class.create({
	overlayId: 'overlay-modal',
	options: {
		width: 438,
		height: 600,
		resizable: false,
		minimizable: false,
		maximizable: false,
		draggable: false,
		destroyOnClose: true,
		showEffectOptions: { duration: 0.5 },
		hideEffectOptions: { duration: 0.5 },
		useOverlay: false
	},

	loadOptions: {
		width: 150,
		height: 150,
		resizable: false,
		minimizable: false,
		maximizable: false,
		draggable: false,
		destroyOnClose: true,
		showEffectOptions: { duration: 0.2 },
		hideEffectOptions: { duration: 0.2 }
	},
	
	/**
	 * Constructor.
	 * Not called by child classes, so don't put stuff here.
	 */
	initialize: function() {},

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

	/**
	 * Shows the modal
	 */
	showWindow: function() {
		this.window.showCenter();
		if (this.useOverlay) {
			$(this.overlayId).observe('click', this.overlayHideWindow.bindAsEventListener(this));
		}
	},

	/**
	 * 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);
		this.enableScreen();
	},

	/**
	 * 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 onepica.Ajax.Request(this.url, options);
	},

	/**
	 * Create (loading) callback.
	 */	
	loadCreate: function (transport) {
		this.disableScreen();
		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();
		}
		this.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/lightview/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());
		}
	},
	
	/**
	 * Enables the overlay to disable the screen content.
	 */
	disableScreen: function () {
    	if (this.useOverlay) {
			WindowUtilities.disableScreen(this.window.options.className, this.overlayId, this.window.overlayOpacity, this.window.getId(), this.window.options.parent);
			Windows.maxZIndex++;
		}
	},

	/**
	 * Hides the overlay to enable the screen.
	 */
	enableScreen: function () {
		if (this.useOverlay) {
			WindowUtilities.enableScreen(this.overlayId);
		}
	}
});