﻿/**
 * Copyright (c) 2010 eSolutionsGroup
 * All rights reserved.
 * 
 * Author: Timothy Grant Vogelsang <tvogelsang@esolutionsgroup.ca>
 */

Ext.ns('Suite');

Date.patterns = {
	ShortDate: "n/j/Y"
};

Suite = function() {
	
	var Current =
	{
		Confirmation: false
	};
	
	return {
	
		/**
		 * @private
		 * Handle Initialization.
		 */
		Initialization: function() {
			Ext.MessageBox.ACCEPT = 'ext-mb-accept';
		},
		
		/**
		 * Retrieve Confirmation.
		 */
		GetConfirmation: function() {
			return Current.Confirmation;
		},
		
		/**
		 * Retrieve Id.
		 */
		GetId: function() {
			return Current.Id;
		},
		
		/**
		 * Set Confirmation.
		 */
		SetConfirmation: function(confirmation) {
			Current.Confirmation = confirmation;
		},
		
		/**
		 * Set Id.
		 */
		SetId: function(id) {
			Current.Id = id;
		},
		
		/**
		 * Open a partial view.
		 * @param config {Object} The config object.
		 */
		OpenPartial: function(config) {
			config = Ext.applyIf(config, {
				callback: function() {
					if (config.maskControl) {
						setTimeout(function() {
							config.maskControl.body.unmask();
						} );
					}
				},
				disableCaching: true,
				failure: function(response) {
					new Ext.Window( {
						height: 500,
						items: [ {
							xtype: 'panel',
							autoScroll: true,
							html: response.responseText
						} ],
						layout: 'fit',
						width: 600,
						title: 'Server Error'
					} ).show();
				},
				maskControl: Ext.getCmp('Center'),
				method: 'GET',
				scripts: true,
				success: function(response) {
					try
					{
						eval(response.responseText)
					}
					catch(err)
					{
						new Ext.Window( {
							height: 400,
							html: response.responseText,
							width: 500,
							title: 'Error'
						} ).show();
					}
				}
			} );
			
			if (config.maskControl) {
				config.maskControl.body.mask('Loading ...', 'x-mask-loading');
			}
			
			config.control.removeAll();
			
			Ext.Ajax.request(config);
		},
		
		/**
		 * Open a partial window.
		 * @param config {Object} The config object.
		 * @param windowConfig {Object} The window config object.
		 */
		OpenPartialWindow: function(config, windowConfig) {
			var win = Suite.OpenWindow(windowConfig);
			
			win.show();
			
			config = Ext.apply(config, {
				control: win,
				maskControl: win
			} );
			
			Suite.OpenPartial(config);
		},
		
		/**
		 * Open a window.
		 * @param config {Object} The config object.
		 */
		OpenWindow: function(config) {
			config = Ext.applyIf(config, {
				bodyStyle: 'background-color: White;',
				closable: false,
				collapsible: false,
				draggable: false,
				height: 500,
				id: 'Window',
				layout: 'fit',
				modal: true,
				resizable: false,
				width: 600
			} );
			
			return new Ext.Window(config);
		},

		/**
		 * Show confirmation message.
		 */
		ShowConfirm: function(message, fn) {
			Ext.MessageBox.show( {
				closable: false,
				buttons: Ext.MessageBox.YESNO,
				fn: fn,
				icon: Ext.MessageBox.QUESTION,
				modal: true,
				msg: message
			} );
		},
		
		/**
		 * Show failure message.
		 */
		ShowFailure: function(message) {
			Ext.MessageBox.show( {
				closable: false,
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.WARNING,
				modal: true,
				msg: Ext.isEmpty(message) ? 'There was a complication processing your request -- please contact administration' : message
			} );
		},
		
		/**
		 * Show info message.
		 */
		ShowInfo: function(message) {
			Ext.MessageBox.show( {
				closable: false,
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.INFO,
				modal: true,
				msg: message
			} );
		},
		
		/**
		 * Show success message.
		 */
		ShowSuccess: function(message) {
			Ext.MessageBox.show( {
				closable: false,
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.ACCEPT,
				modal: true,
				msg: message
			} );
			
			setTimeout(function() {
				Ext.MessageBox.hide();
			}, 2000);
		},
		
		/**
		 * Show warning message.
		 */
		ShowWarning: function(message) {
			Ext.MessageBox.show( {
				closable: false,
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.WARNING,
				modal: true,
				msg: message
			} );
		},
		
		/**
		 * Show validation errors.
		 */
		ShowValidationErrors: function(form) {
			var result = 'The following errors require your attention:<br><br><ul>';
			
			form.cascade(function(item) {
				if (item.isValid && item.getActiveError) {
					if (item.xtype != 'compositefield') {
						if (item.isValid() == false) {
							result += '<li>' + item.getActiveError() + '</li>';
						}
					} else {
						if (item.isValid() == false) {
							item.items.each(function(temp) {
								if (temp.isValid() == false) {
									result += '<li>' + temp.getActiveError() + '</li>';
								}
							} );
						}
					}
				}
			} );
			
			result += '</ul>';
			
			Suite.ShowWarning(result);
		},
		
		/**
		 * Perform validation.
		 */
		Validate: function(form) {
			if (form.isValid() == false) {
				Suite.ShowValidationErrors(form);
				
				return false;
			}
			
			return true;
		},
		
		/**
		 * Listeners.
		 */
		Listeners: {
		
			Store: {

				/**
				 * Handles the Exception event.
				 */
				Exception: function(response, e) {
					var result = Ext.util.JSON.decode(response.responseText);
					
					if (result && result.message) {
						Suite.ShowFailure(result.message);
					}
					else {
						Suite.ShowFailure(e && e.message ? e.message : response.message || response.statusText);
					}
				},

				/**
				 * Handles the Save event.
				 */
				Save: function(message) {
					Suite.ShowSuccess(message);
				}

			}

		}
	}
} ();

Ext.onReady(Suite.Initialization, Suite);

