Ext.define('TradePortal.proxy.JSONAjaxProxy', {
	alias: 'proxy.jsonajax',
	extend: 'Ext.data.proxy.Ajax',

	noCache: false,

	reader : {
		type : 'json'
	},

	doRequest: function(operation, callback, scope) {
		var me = this,
			writer  = me.getWriter(),
			request = me.buildRequest(operation),
			method  = me.getMethod(request);

		if (operation.allowWrite()) {
			request = writer.write(request);
		}

		Ext.apply(request, {
			binary        : me.binary,
			headers       : me.headers,
			timeout       : me.timeout,
			scope         : me,
			callback      : me.createRequestCallback(request, operation, callback, scope),
			method        : method,
			disableCaching: false // explicitly set it to false, ServerProxy handles caching
		});

		// We need to copy the request here, because the params object is attached to
		// the request and may be accessed by other callers, since it doesn't really care how the
		// information is encoded, so we can't mutate it here.
		request = Ext.apply({
			jsonData: request.params
		}, request);

		request.params = {};
		delete request.jsonData.ipContextId;

		Ext.Ajax.request(request);

		return request;
	},

	getMethod: function(request) {
		return 'POST';
	}
});

Ext.define('TradePortal.Application',
	{
		name: 'TradePortal',

		extend: 'Ext.app.Application',

		requires: ['TradePortal.view.Viewport'],

		controllers: ['Login', 'Main', 'Home', 'Account',
			'Administration', 'Invoice', 'Statements', 'Orders',
			'Products', 'DrillDown', 'Review', 'Transaction',
			'Downloads', 'Customlists', 'Payaccount', 'PriceBook'],

		launch: function () {
			var me = this;

			Ext.create('TradePortal.view.Viewport');

			Ext.Ajax.useDefaultXhrHeader = false;

			Ext.customParam = {};

			// custom
			Ext.override(Ext.data.Store, {
				setExtraParam: function (name, value) {
					this.proxy.extraParams       = this.proxy.extraParams || {};
					this.proxy.extraParams[name] = value;
					this.proxy.applyEncoding(this.proxy.extraParams);
				}
			});
			Ext.override(Ext.data.TreeStore, {
				setExtraParam: function (name, value) {
					this.proxy.extraParams       = this.proxy.extraParams || {};
					this.proxy.extraParams[name] = value;
					this.proxy.applyEncoding(this.proxy.extraParams);
				}
			});
			Ext.override(Ext.data.TreeStore, {
				load: function (options) {
					options        = options || {};
					options.params = options.params || {};

					var me = this, node = options.node
						|| me.tree.getRootNode(), root;

					// If there is not a node it means the user hasnt
					// defined a rootnode yet. In this case lets just
					// create one for them.
					if (!node) {
						node = me.setRootNode({
							expanded: true
						});
					}

					if (me.clearOnLoad) {
						// this is what we changed. added false
						node.removeAll(false);
					}

					Ext.applyIf(options, {
						node: node
					});
					options.params[me.nodeParam] = node ? node.getId()
						: 'root';

					if (node) {
						node.set('loading', true);
					}

					return me.callParent([options]);
				}
			});

			Ext.QuickTips.init();

			Ext.EventManager.on(window, 'keydown',
				function (e, t) {
					if (e.getKey() == e.BACKSPACE
						&& (!/^textarea$/i.test(t.tagName)
						|| t.disabled || t.readOnly)
						&& (!/^input$/i.test(t.tagName)
						|| t.disabled || t.readOnly)) {
						e.stopEvent();
					}
				});
		}
	});

function goToHome() {
	"use strict";
	Ext.getCmp('main_panel').items.items[1].down('panel').items.each(function (panel) {
		panel.hide();
	});
	Ext.getCmp('home_panel').show();
}

function goToProducts() {
	"use strict";
	Ext.getCmp('main_panel').items.items[1].down('panel').items.each(function (panel) {
		panel.hide();
	});
	Ext.getCmp('products_panel').show();
	Ext.getCmp('products_list').removeAll();
	Ext.getCmp('products_grid').store.removeAll();
	Ext.getCmp('products_left_menu').items.each(function (panel) {
		panel.hide();
	});
	if (Ext.getCmp('products_class_attributes') != undefined) {
		Ext.getCmp('products_class_attributes').destroy();
	}
	Ext.getCmp('prod_breadcrumbs').update('<small class="breadcrumb_text"><span class="breadcrumbsSub" onclick="goToHome()">' + Ext.getStore('Config').data.last().data.breadcrumb_text + '</span> > Products</small>');
}

function goToProduct(id) {
	"use strict";
	if (Ext.getCmp(id) != undefined) {
		Ext.getCmp('products_top_menu').topMenuClick(Ext.getCmp(id));
	}
	else {
		Ext.getCmp('products_left_menu').leftMenuClick(Ext.getCmp(id));
	}
}
