/**
*	This file holds the logic for visualizing the chart and its JS controls.
*	It works with the Ext JS library, so it must be included in the script path of the current page.
*/
	Ext.onReady(function() {
		
		var indexStore = new Ext.data.Store({
			url: '/faStatistics/getIndicies.php',
			reader: new Ext.data.JsonReader({
				root: 'indicies',
				id: 'code'           	
			}, [
				{name: 'code'},
				{name: 'display'},
				{name: 'flag'}
			])
		});
		
		var indexCombo = new Ext.form.ComboBox({
			id: "indexCombo",
			store: indexStore,
			displayField:'display',
			fieldLabel: 'Select: ',
			disableKeyFilter: true,
			typeAhead: false,
			mode: 'remote',
			editable: false,
			triggerAction: 'all',
			value: "Dow Jones Ind.",
			valueField: 'display',
			emptyText:'Select a name...',
			selectOnFocus: true,
			width:135
		});	
			
		var	twCombo = new Ext.form.ComboBox({
			id: "twCombo",
			store: new Ext.data.Store ({							
				reader: new Ext.data.ArrayReader(
					{idIndex: 0},
					[{name: 'timeWindow', type: 'int'}] 
				),
				data : [[5],[8],[10],[15],[20],[25],[30]]
			}),
			displayField: 'timeWindow',			
			typeAhead: true,
			mode: 'local',
			forceSelection: true,
			triggerAction: 'all',
			emptyText: 'Select MAV',
			value: 10,
			selectOnFocus: true,
			width: 50
		});
		
		var tb = new Ext.Toolbar({
			renderTo: 'chart_toolbar',
			defaults: {
				style: "padding-left: 5px; padding-right: 2px"
			},
			items: [
				{
					text: "Index: ",
					xtype: "label"
				},
				indexCombo,
				{
					text: "Moving Average (MAV) Window: ",
					xtype: "label"
				},
				twCombo, 
				{
					xtype: "button",
					cls: "x-btn-text",
					text: "Pop Out",
					handler: popOut.createDelegate(indexCombo)
			}]			
		});		
				
		indexCombo.on("select", redrawAmstock.createDelegate(indexCombo));		
		twCombo.on("select", redrawAmstock.createDelegate(indexCombo));						
		
		// load the combo items, important!
		indexCombo.getStore().load();
		
		var obsPanel = new Ext.Panel({
			autoLoad: "/faStatistics/observations.php?period=1y",
			renderTo: "chart_footer"
		});
		
		new Ext.Panel({
			autoLoad: "/faStatistics/fullTable.php",
			renderTo: "cognity_table"
		});
		
		function redrawAmstock () {				
			var amStockFrame = document.getElementById("amStockFrame");
			var selectionIndex = (this.selectedIndex == -1) ? 0 : this.selectedIndex; 
			var chart_context =  this.store.data.items[selectionIndex].data.code;							
			var url = "/faChart/amstock/amstock_settings.php?selIndex="+ chart_context +"&timeWindow=" + Ext.get("twCombo").getValue();
			
			var theSWF  = findSWF("amstock");
			if (theSWF != null) {
				theSWF.reloadSettings(url);
			}			
			
			obsPanel.load({
				url: "/faStatistics/observations.php?period=1y&index=" + chart_context,
				//params: chart_context, 
				text: "Loading..."
			});		
		}
		
	}); // end of Ext.onReady
	
function popOut() {		
	  var browserWidth = 0, browserHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		browserWidth = window.innerWidth;
		browserHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		browserWidth = document.documentElement.clientWidth;
		browserHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		browserWidth = document.body.clientWidth;
		browserHeight = document.body.clientHeight;
	  }
	  //window.alert( 'Width = ' + browserWidth );
	  //window.alert( 'Height = ' + browserHeight );
	browserWidth = browserWidth - 100;
	browserHeight = browserHeight - 100;		
	var selectionIndex = (this.selectedIndex == -1) ? 0 : this.selectedIndex; 
	var indx =  this.store.data.items[selectionIndex].data.code;							
	var url = "/faChart/iframeAmstock.php?width="+browserWidth+"&height="+browserHeight+"&selIndex="+ indx + "&timeWindow=" + Ext.get("twCombo").getValue();
	window.open(url, Ext.get("twCombo").getValue() + "_Id", "left=0, top=0, location=no,toolbar=no, status=0, resizable=1, scrollbars=1, width="+browserWidth + ", height=" + browserHeight);
}

function addCopyrightText(cpr) {
	cpr.innerHTML = "<span style='font-size:9px;text-align:center;width:100%'>Copyright &copy; 2008 FinAnalytica Inc. Provided for informational purposes only and not intended for trading or 	advice.</span>";
}

function createButton(btnConf, defConf) {
	Ext.applyIf(btnConf, defConf);
	return btnConf;
}

function findSWF(movieName) {
  return document.getElementById(movieName);
}
