/**
 * Copyright(c) 2006-2009, FeyaSoft Inc.
 */
Ext.ns('feyaSoft.oilAccount.accountPublic');

/**
 * @class feyaSoft.oilAccount.accountPublic.MainPanel
 * @constructor
 * @param config.userDisplayName:
 *            {String}
 */
feyaSoft.oilAccount.accountPublic.MainPanel = function(config) {

	config = config || {};

	// now add the company search part ...
	this.companyPanel = new feyaSoft.oilAccount.share.search.CompanyPanel({
				loginButtonAppear : 'yes'
			});

	// add product panel
	this.productPanel = new feyaSoft.oilAccount.share.search.ProductPanel({
				loginButtonAppear : 'yes'
			});
	this.homePanel = new feyaSoft.oilAccount.accountPublic.HomeMainPanel({
				id : 10
			});
	this.itemsTab = new Ext.TabPanel({
				activeTab : 0,
				deferredRender : true,
				monitorResize : true,
				defaults : {
					autoScroll : true
				},
				resizeTabs : true,
				tabWidth : 200,
				enableTabScroll : true,
				plugins : new Ext.ux.TabCloseMenu(),
				layoutOnTabChange : true,
				hideMode : 'offsets',
				border : false,
				items : [this.homePanel, {
							title : 'Companies',
							closable : false,
							autoScroll : true,
							border : false,
							layout : 'fit',
							items : this.companyPanel
						}, {
							title : 'Products',
							closable : false,
							autoScroll : true,
							border : false,
							layout : 'fit',
							items : this.productPanel
						}, {
							title : 'About OilAccount',
							closable : false,
							autoScroll : true,
							border : false,
							layout : 'fit',
							items : [{
										xtype : 'iframepanel',
										loadMask : {
											msg : '<center>Loading</center>'
										},
										defaultSrc : "home.html",
										border : true
									}]
						}]
			});

	this.companyPanel.initLoadData();

	feyaSoft.oilAccount.accountPublic.MainPanel.superclass.constructor.call(
			this, {
				id : 'oilaccount-account-mainPanel',
				region : 'center',
				activeTab : 0,
				deferredRender : false,
				monitorResize : true,
				defaults : {
					autoScroll : true
				},
				resizeTabs : true,
				tabWidth : 150,
				minTabWidth : 100,
				enableTabScroll : true,
				plugins : new Ext.ux.TabCloseMenu(),
				layoutOnTabChange : true,
				hideMode : 'offsets',
				border : false,

				// list tab items
				items : [{
							id : 'mainTab-myOilaccount-public',
							title : 'OilAccount.com',
							closable : false,
							autoScroll : true,
							border : false,
							layout : 'fit',
							items : [this.itemsTab]
						}]
			});

	// this will be popup window
	/**
	 * this.on('afterlayout',function(){ new
	 * feyaSoft.oilAccount.share.search.LoginWin(); },this,{single:true});
	 */

};

Ext.extend(feyaSoft.oilAccount.accountPublic.MainPanel, Ext.TabPanel, {

	goToSearch : function() {
		window.location = 'search';
	},
	pageLinkFn : function() {

	},
	// this just load simple page with URL and name
	loadSimpleUrl : function(url, name, uniqueId, isHide, myCompanyId,
			productId) {
		var tab = Ext.getCmp('simpleUrl-' + uniqueId);
		if (tab)
			this.remove(tab);

		this.webBtn = new Ext.Toolbar.Button({
					text : 'Go to WebPage',
					iconCls:'icon_world_link',
					scope : this,
					handler : function() {
						// now jump to new tab of browser
						window.open(url, '_blank', '')
					}
				});
		this.pageLinkBtn = new Ext.Toolbar.Button({
					text : 'Page Link',
					iconCls:'icon_link',
					scope : this,
					handler : function() {
						// now jump to new tab of browser
						tab = new Ext.Panel({
									id : 'simpleUrl_chlid' + uniqueId,
									title : name,
									closable : true,
									autoScroll : true,
									layout : 'fit',
									items : [{
												xtype : 'iframepanel',
												loadMask : {
													msg : '<center>Loading</center>'
												},
												defaultSrc : url,
												border : true,
												closable : false
											}]
								});

						// get page url tab panel
						this.add(tab);
						this.setActiveTab(tab);
					}
				});
		if (myCompanyId) {
			// ok ... need use Ajax call
			Ext.Ajax.request({
				url : 'accountCompany/load?myCompanyId=' + myCompanyId,
				success : function(result, request) {
					var jsonData = Ext.util.JSON.decode(result.responseText);
					if (jsonData.success == 'true') {
						var backResult = Ext.util.JSON
								.decode(result.responseText);

						this.companyPanel = new feyaSoft.oilAccount.share.myCompany.preview.GrouptabsPanel(
								{
									data : backResult.companyData[0],
									productData : backResult.dataProducts,
									productId : productId
								});

						tab = new Ext.Panel({
									id : 'simpleUrl-' + uniqueId,
									title : name,
									closable : true,
									autoScroll : true,
									layout : 'fit',
									items : [{
												items : this.companyPanel,
												layout : 'fit',
												border : true,
												closable : false,
												tbar : [this.webBtn,
														this.pageLinkBtn]
											}]
								});

						// get page url tab panel
						this.add(tab);
						if (!isHide)
							this.setActiveTab(tab);
					}
				},
				failure : function(result, request) {
					// do nothing ....
				},
				scope : this
			});
		} else {
			tab = new Ext.Panel({
						id : 'simpleUrl-' + uniqueId,
						title : name,
						closable : true,
						autoScroll : true,
						layout : 'fit',
						items : [{
									xtype : 'iframepanel',
									loadMask : {
										msg : '<center>Loading</center>'
									},
									defaultSrc : url,
									border : true,
									closable : false,
									tbar : [this.webBtn,this.pageLinkBtn]
								}]
					});

			// get page url tab panel
			this.add(tab);
			if (!isHide)
				this.setActiveTab(tab);

		}
	}
});

