/**
 * @author bobringer
 */
Ext.ns("L3.V4");

L3.V4.App = Ext.extend(Ext.util.Observable, {
	
	init: function(){
		/*
 		 * We provide a lot of info in our quicktips, set the default quicktip object to keep tips on the screen for awhile
 		 * The test for the existance of a quicktip object is only necessary until Longana 1.0 supports it
 		 */
		if (!Ext.QuickTips.getQuickTip()) {Ext.QuickTips.init();}
		Ext.QuickTips.getQuickTip().dismissDelay = 30000;
		
		
		/*
		 * Set the document title
		 */
		var title = "NHL";
		title += (this.config.siteTitle) ? (" >> " + this.config.siteTitle) : "";
		title += (this.config.appTitle) ? (" >> " + this.config.appTitle) : "";
		document.title = title;
		
		
		/*
		 * Do we have a page or a viewport?
		 */
		this.appType = (this.config.type || "page");

		
		/*
		 * Set up our header images
		 */
		var img = this.config.siteTitleImage || "/link3/sections/link3/resources/link-header.gif";
		this.titleImage = "<img src='" + img + "' />";
		
		if (this.config.appTitleImage) {
			this.subTitleImage = "<img src='" + this.config.appTitleImage + "' />";
		} else {
			if (this.config.appTitle) {
				this.subTitleImage = this.config.appTitle;
			}
		}

		
		
		/*
		 * Set up the sections of our application body
		 */
		this.bodyItems = [];
		
		if (this.config.westSidebar) {
			this.westSidebarConfig = Ext.applyIf(this.config.westSidebar, {
				id		: "link-west-sidebar",
				region	: "west",
				width	: 308,
				style	: "padding-right:6px",
				bodyStyle: "background:transparent",
				border	: false		
			});
			
			this.bodyItems.push(this.westSidebarConfig);
		}
		
		this.appBodyConfig = Ext.applyIf(this.config.appBody || {}, {
			id			: 'link-app-body',
			region		: "center",
			border		: false,
			autoScroll	: true,
			bodyStyle	: "background:transparent",
			columnWidth	: 1
		});
		this.bodyItems.push(this.appBodyConfig);
		
		if (this.config.eastSidebar) {
			this.eastSidebarConfig = Ext.applyIf(this.config.eastSidebar, {
				id		: "link-east-sidebar",
				region	: "east",
				width	: 308,
				style	: "padding-left:6px",
				bodyStyle: "background:transparent",
				border	: false
			});
			
			this.bodyItems.push(this.eastSidebarConfig);
		}

		
		
		/*
		 * Build layout
		 */
		
		if (this.appType == "page") {
			this.layoutPage();
		} else {
			this.layoutViewport();
		}


		this.westSidebar 	= Ext.getCmp("link-west-sidebar");
		this.eastSidebar 	= Ext.getCmp("link-east-sidebar");
		this.appBody 		= Ext.getCmp("link-app-body");
		
		if (!this.config.hideLinkHeader) {
			this.appHeader = Ext.getCmp("link3-v4-appheader");
		}

		var tbpan = Ext.getCmp("link3-v4-tbarpanel");
		if (tbpan) {
			this.tbar = tbpan.getTopToolbar();
		}
		
		
		if (this.initApp) {
			this.initApp();
		}
	},
	
	
	
	layoutPage	: function() {
		
		var elems = [];
		elems.push({tag: 'div', id: 'link3-v4-teampicker'});

		if (!this.config.hideLinkHeader) {
			elems.push(
				{tag: 'div', id: "link3-v4-header", style:"position:relative", children: [
					{tag: 'div', style: "position:absolute; top:10px; left:200px", html:this.titleImage},
					{tag: 'div', style: "position:absolute; top:16px; right:15px", html:this.userDetails},
					{tag: 'div', style: "position:absolute; top:60px; left:205px; font-weight:bold; font-family:Arial; font-size:24px; color:#e4e4e4; font-style:italic; width:600", 
						html:this.subTitleImage
					},
					{tag: 'div', style: "position:absolute; top:61px; right:15px; text-align:right; width:125px", align:"right", id:"link3-employee-search-container"}					
				]}
			);
		}
		
		
		if (this.config.tbar || this.config.appHeader) {
			elems.push({tag: 'div', id: "link3-v4-appheader"});
		}
		
		elems.push({tag: 'div', id: "link3-v4-body"});
		
		
		/*
		 * Add the elements to our page
		 */
		var c = Ext.DomHelper.append(Ext.getBody(), {
			id:'link3-v4-container', tag:'div', children:elems
		});
		
		/*
		 * Add the team picker to our page
		 */
		if (!this.config.hideLinkHeader) {
			swfobject.embedSWF("/link3/resources/v3/NHLLogoNav.swf", "link3-v4-teampicker", "990", "40", "9.0.0", "expressInstall.swf", false, {bgcolor:"#000000"}, false);	
		}
		
		
		/*
		 * If we have a tbar or appheader, add it to the container here
		 */
		if (this.config.appHeader) {
			this.config.appHeader.id = "link3-v4-appheader";
			var ah = new Ext.Panel(this.config.appHeader);
			ah.render("link3-v4-appheader");
		}
		
		
		/*
		 * If we have a tbar or appheader, add it to the container here
		 */
		if (this.config.tbar) {
			var tb = new Ext.Panel({
				id		: "link3-v4-tbarpanel",
				border	: false,
				height	: 0,
				style	: "padding-top:2px; padding-bottom:2px; background:transparent",
				tbar	: this.config.tbar
			});
			tb.render("link3-v4-appheader");
		}
		
		
		/*
	 	 * Add the application elements to the page
	 	 */
		var p = new Ext.Panel({
			layout		: "column",
			id			: "link-app-panel",
			bodyStyle	: "padding:6px; background:transparent",
			style		: "background-color:#e4e4e4; padding:6px; margin-bottom:12px",
			border		: false,
			bodyStyle	: "background:transparent",
			items		: this.bodyItems
		});
		p.render("link3-v4-body");
	},
	
	
	layoutViewport	: function() {
			
			/*
			 * Get the HTML for our north region
			 */
			var vpItems;
			if (!this.config.hideLinkHeader) {
					var h = "<div id='link3-v4-header' style='position:relative'>" +
					"<div style='position:absolute; top:10px; left:200px;'>" + this.titleImage + "</div>" +
					"<div style='position:absolute; top:57px; left:205px; font-weight:bold; font-family:Arial; font-size:24px; color:#e4e4e4; font-style:italic; width:600'>" + this.subTitleImage + "</div>" +
					"</div></div>";
				
				var hp;
				if (this.config.appHeader || this.config.tbar) {
					var itms = [{
						border	: false,
						html	: h
					}];
					
					if (this.config.appHeader) {
						this.config.appHeader.id = "link3-v4-appheader";
						itms.push(this.config.appHeader);
					}
				
					if (this.config.tbar) {
						itms.push({
							id		: "link3-v4-tbarpanel",
							border	: false,
							style	: "padding-top:2px; padding-bottom:2px; background:#121212",
							bodyStyle	: "height:0px; display:none",
							tbar	: this.config.tbar
						});
					}
					
					hp = {
						region		: "north",
						border		: false,
						autoHeight	: true,
						items		: itms
					};
				} else {
					hp = {
						region		: "north",
						border		: false,
						autoHeight	: true,
						html		: h
					};
				}
				
				vpItems = [hp, this.appBodyConfig];
			} else {
				vpItems = [this.appBodyConfig];
			}

			
			if (this.config.eastSidebar) {
				vpItems.push(this.eastSidebarConfig);
			}
			
			if (this.config.westSidebar) {
				vpItems.push(this.westSidebarConfig);
			}
			
			var vp = new Ext.Viewport({
				layout	: "border",
				border	: false,
				items	: vpItems
			});
			vp.doLayout();
	}
});
