/**
	This class is used to control the KnockOutMap  
	
	$Log: knockOutMap.js,v $
	Revision 1.8  2010/11/10 10:28:44  cvsosir
	DAX / XDAX fix
	
	Revision 1.7  2009/10/01 09:26:04  cvsosir
	*** empty log message ***
	
	Revision 1.6  2009/10/01 08:24:07  cvsosir
	*** empty log message ***
	
	Revision 1.5  2009/08/07 15:54:58  cvsosir
	*** empty log message ***
	
	Revision 1.4  2009/07/22 05:39:06  cvsosir
	disclaimer
	
	Revision 1.3  2009/06/18 09:11:53  cvsosir
	Added iOptionType for preselecting the optiontype.
	
	Revision 1.2  2009/05/25 15:31:31  cvsosir
	*** empty log message ***
	
	Revision 1.1  2009/05/25 15:27:05  cvsosir
	*** empty log message ***
	
*/
/**
 * @param iOptiontype (opt) For preselecting the call/put (1=only calls, 2 = only puts, 0 = both)
*/
function KnockOutMap (iOptiontype)
{	
	//default for filter params
	var sCategories=";OpenEndTurbo;MiniCert;SmartMiniCert;TurboOs;XEndlosTurbos;XTurbos;DayTurbo;";
	var sUnderlying="DAX®";
	var sPut= 0;
	var sCall= 0;
	
	//both checked when not defined...
	if( typeof(iOptiontype) == 'undefined' || iOptiontype == 0)
	{
		sPut=1;
		sCall=1;
	}
	else
	{
		if(iOptiontype == 1)
		{
			sCall=1;
		}
		if(iOptiontype ==2)
		{
			sPut=1;
		}
	}
	
	var sUnderlyingISIN="DE0008469008";
	var sExpire="";
	var dDistBarUp=15;
	var dDistBarLow=-15;
	var sExpire="";
	//step soze when using the KO Slider
	var dStepBar=5;
	var url="!GetKnockOutMapData";
	//to indicate if the filter distance to barrier should be used
	var bNoDistBar=false;
	
	
	
	
	var chartWidth=850;	
	var chart = new SWFObject("/flash/amxy/amxy.swf", "chart", chartWidth, "500", "8", "#FFFFFF");
	//chartObj can be used to maipulate the swf chart by js
	var chartObj =null;

	//keeps the underlyings the user can choose
	var underlyingStore = new Ext.data.Store ({
	reader: new Ext.data.JsonReader({
		fields: ['id', 'underlying', 'underlyingisin'],
		root: 'underlyings'
		}),
	proxy: new Ext.data.HttpProxy({
			url:'!GetKnockOutMapUnderlyings' 
		})
	});
	
	/* ==== Forms ====*/
	var frmCategories = new Ext.form.FormPanel({
		renderTo:'frmCategories',
		frame: false,
		border:false,
		bodyBorder:false,
		width: 200,
		items: [
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_DayTurbo.gif" /> Day Turbos',
			name: 'cbDayTurbo',
			category:'DayTurbo',
			listeners: {check: handleCheckEvent}
		},
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_EndlosTurbos.gif" /> Open End-Turbos',
			name: 'cbEndlosTurbos',
			category:'OpenEndTurbo',
			listeners: {check: handleCheckEvent}
		},
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_Mini.gif" /> Mini Future Zertifikate',
			name: 'cbMiniFutureZertifikate',
			category:'MiniCert',
			listeners: {check: handleCheckEvent}
		},
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_SmartMini.gif" /> Smart-Mini Future ',
			name: 'cbSmartMiniFuture',
			category:'SmartMiniCert',
			listeners: {check: handleCheckEvent}
		},
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_TurboOs.gif" /> Turbo-Optionsscheine',
			name: 'cbTurboOptionsscheine',
			category:'TurboOs',
			listeners: {check: handleCheckEvent}
		},
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_XEndlos.gif" /> X-Open End-Turbos',
			name: 'cbXEndlosTurbos',
			category:'XEndlosTurbos',
			listeners: {check: handleCheckEvent}
		},
		{
			xtype: 'checkbox',
			checked: true,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_square_XTurbos.gif" /> X-Turbos',
			name: 'cbXTurbos',
			category:'XTurbos',
			listeners: {check: handleCheckEvent}
		}]
		});
		
	var frmUnderlying = new Ext.form.FormPanel({
		renderTo:'frmUnderlying',
		frame: false,
		border:false,
		bodyBorder:false,
		width: 200,   
		items: [
		{
			xtype: 'combo',
			emptyText: 'DAX® / XDAX® ',
			listeners: {select:  handleUnderlyingSelection},
			name: ' Basiswert',
			hideLabel: true,
			mode: 'local',
			store: underlyingStore,
			displayField: 'underlying',
			width: 180,
			maxHeight : 120,
			typeAhead: true,
			triggerAction: 'all',
			forceSelection:true
		}]
		});


	var cbCall= new Ext.form.Checkbox({
			xtype: 'checkbox',
			checked: sCall,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_triangle_up.gif" /> Call / Long',
			name: 'cbCall',
			id: 'cbCallId',
			listeners: {check: handleCheckEventPutCall}	
	});
	
	var cbPut= new Ext.form.Checkbox({
			xtype: 'checkbox',
			checked: sPut,
			hideLabel:true,
			boxLabel:'<img class="icon" src="images/icons/icon_triangle_down.gif" /> Put / Short',
			name: 'cbPut',
			listeners: {check: handleCheckEventPutCall}	
		});
		
	var frmPutCall = new Ext.form.FormPanel({
		renderTo:'frmPutCall',
		frame: false,
		border:false,
		bodyBorder:false,
		width: 200,  
		items: [
		cbCall,
		cbPut
		]
		});

	/**
	* This function initializes the KnockOutMap
	*/
	function init()
	{

		//retreiving the underlyings
		underlyingStore.load();		
		//initializing the flash chart
		chart.addVariable("path", "flash/amxy/");
		chart.addParam("wmode","opaque");
		chart.addVariable("loading_settings", "Lade Konfiguration...");
		chart.addVariable("chart_id", "chart"); 
		//Reading the settings file
		chart.addVariable("settings_file", encodeURIComponent("flash/amxy/amxy_settings.xml"));
		//Initial load of data with default filters
		chart.addVariable("data_file", encodeURIComponent(url+ '?sessionId=' + GLOBALSESSIONID + '&sCategory=' + sCategories +'&sUnderlying='+ sUnderlying +'&sUnderlyingISIN=' + sUnderlyingISIN + '&sPut=' + sPut + '&sCall=' + sCall + '&sExpire=' + sExpire + '&dDistBarUp=' + dDistBarUp +'&dDistBarLow='+ dDistBarLow));
		chart.write("chartContainer");
		//chartObj can be used to manipulate the chart with JS
		chartObj = document.getElementById('chart');
	}
	/**
	* Is triggered when a category checkbox is getting checked.
	* @param checkkbox The checkbox as obj
	* @param checked 
	*/

	function handleCheckEvent(checkbox, checked)
	{
		if(checkbox.checked == true)
		{
			sCategories=sCategories.concat(checkbox.category + ';');
		}
		else
		{
			sCategories=sCategories.replace(checkbox.category + ';',"");
		}
		
		loadChart();
	}
	/**
	* Listener for checking the Put/Call checkboxes
	* @param checkkbox The checkbox as obj
	* @param checked 
	*/
	function handleCheckEventPutCall(checkbox, checked)
	{
		if(checkbox.checked == true)
		{
			sValue="1";
		}
		else
		{
			sValue="0";
		}
		
		if(checkbox.name== 'cbPut')
		{
			sPut=sValue;
		}
		else
		{
			sCall=sValue;
		}
		loadChart();
	}	
	
	/**
	* Listeren for selecting something from underlying combobox.<b> 
	* @param frmComoBox The ComboBox as obj
	* @param record The dataset that was chosen
	* @param index The index within the combobox
	*/
	function handleUnderlyingSelection(frmComboBox, record, index)
	{
		sUnderlying=record.get('underlying');
		sUnderlyingISIN= record.get('underlyingisin');
		 
		//dependent on the underlying the distances to barriers are set
		//DAX=15%, Currencies 10 %, others all 	
		switch (sUnderlyingISIN)
		{
			//DAX
			case "DE0008469008": dDistBarUp=15;
								 dDistBarLow=-15;
								 bNoDistBar=false;
								break;
			//WÄHREUNGEN ==>
			case "EU0009652759":
								dDistBarUp=10;
								dDistBarLow=-10;
								bNoDistBar=false;
								break;
			case "EU0009653088":
								dDistBarUp=10;
								dDistBarLow=-10;
								bNoDistBar=false;
								break;
			case "EU0009652627":
								dDistBarUp=10;
								dDistBarLow=-10;
								bNoDistBar=false;
								break;
			default: 
								//to indicate that the distance to barrier should not be used...
								bNoDistBar=true;
								dDistBarUp=50;
								dDistBarLow=-50;
		}
		//noisin is the value of the dividing rule within the underlying combobox. nothing should happen when it got choosed
		if(sUnderlyingISIN != 'noisin') 
		{
			loadChart();
		}
	}
	/**
	* Is triggerd when the user clicked upon the slider buttons.
	* The distance to barrier should go up / down.
	* @param up true when up was pressed false when down. 
	*/
	this.handleScrollButtonClick = function(up)
	{
		
		if(bNoDistBar==true)
		{
			bNoDistBar=false;
		}
		if(up==true)
		{
			dDistBarUp=dDistBarUp+dStepBar;
			dDistBarLow=dDistBarLow+dStepBar;
		}
		else
		{
			//DistBarLow should never exceed -100
			if(dDistBarLow > -100)
			{
				dDistBarUp=dDistBarUp-dStepBar;
				dDistBarLow=dDistBarLow-dStepBar;
			}	
		}	
		loadChart();
	}

	/**
	* Triggerd by the Fälligkeits slider.
	* @param value The value of the slider format: 0/2009-07-13|-1,   (slider id/date min | date max) -1 when all (open end) 
	*/
	this.setSliderValue= function (value)
	{	
		var sliderExpire=value;
		//replacing unused information for morpheus
		sliderExpire=sliderExpire.replace("0/","");
		sliderExpire=sliderExpire.replace(",","");
		
		sExpire=sliderExpire;
		loadChart();
	}
	/**
	* Reloads the chart with filter settings
	*/
	function loadChart()
	{
		var sDistBarUp ='';
		var sDistBarLow ='';
		var sTextSchwelle='';
		var sTextAbstandSchwelle='Angezeigt werden Produkte mit einem Abstand der KO-Schwelle zum aktuellen Kurs des Basiswerts von ';
		
		//when both zero, sDistBarUP should be empty. Morpheus will send all data.
		if(bNoDistBar==true)
		{
			sTextSchwelle='Es werden <b>alle</b> Produkte angezeigt.';
		}
		else
		{
			if(dDistBarUp<0) 
			{
				sDistBarUp=dDistBarUp;
			}
			else
			{
				sDistBarUp='+' + dDistBarUp;
			}
			
			if(dDistBarUp == 0)
			{
				sDistBarUp=dDistBarUp;
			}
			
			if(dDistBarLow<0) 
			{
				sDistBarLow=dDistBarLow;
			}
			else
			{
				sDistBarLow='+' + dDistBarLow;
			}
			
			
			if(dDistBarLow == 0)
			{
				sDistBarLow=dDistBarLow;
			}
			
			//Ext.fly('txtSchwelle').update(dDistBarLow + '% bis '+ dDistBarUp + '%' );	
			sTextSchwelle=sTextAbstandSchwelle +'<b>' + sDistBarLow + '% bis '+ sDistBarUp + '%</b>';
		}
		Ext.fly('txtAbstandSchwelle').update(sTextSchwelle);

		Ext.Ajax.request({
			url: url + '?sessionId=' + GLOBALSESSIONID + '&sCategory=' + sCategories +'&sUnderlying='+ sUnderlying + '&sUnderlyingISIN=' + sUnderlyingISIN + '&sPut=' + sPut + '&sCall=' + sCall + '&sExpire=' + sExpire + '&dDistBarUp=' + sDistBarUp + '&dDistBarLow='+ sDistBarLow,
			success: chartDataLoaded,
			failure: chartDataFailure
	})
	}
	/**
	* Is called when chart data was received from server
	* @param response The response obj
	*/
	function chartDataLoaded(response)
	{	
		var sResponseText=response.responseText;
		//reads the response and adjustes the chart settings...
		checkResponse(sResponseText)
		//feeding data into the flash chart
		chartObj.setData(sResponseText);
	}

	/**
	* Adjustes the chart settings dependent on the response of the server
	* @param responseText The response as string
	*/
	function checkResponse(responseText)
	{
		//reading the flag if the data contains only endlos products
		var onlyEndlos=responseText.substr(56, 1);
		//reading the flag if the data contains no endlos products
		var keinEndlos=responseText.substr(71, 1); 
		
		//the space (in procent) that is claimed by non infinite products. is used for placing the label open end 
		var posProcent=responseText.indexOf("prozent=");
		var procent=responseText.substr(posProcent + 9 , 3);
		procent=parseInt(procent,10);
				 
		//if there are only infinite products...
		if(onlyEndlos==1)
		{
		  //the label open end is moved to center of the chart
		  chartObj.setParam('labels.label[3].x',405);
		  //the measuring unit disapears from the x-axes
		  chartObj.setParam('values.x.enabled',false);
		}
		else
		{
			//when there are no infinite products 
			if(keinEndlos == 0)
			{
				//this is for handling the position of the label openend...
				
				//when less than 70 % of space is used by no infinite products
				if(procent < 70)
				{
					//... hide the measurment units
					chartObj.setParam('values.x.enabled',false);
				}
				else
				{
					//show the measurement units
					chartObj.setParam('values.x.enabled',true);
				}
				//calculation the position to place the label
				var posLabelOpenEnd=(800*procent/100)-20;
				//when open end products are displayed... disable the last measurment unit
				chartObj.setParam('values.x.skip_last',1);
				chartObj.setParam('labels.label[3].x',posLabelOpenEnd);	
			}
			else
			{
				//when there are infinite products
				chartObj.setParam('values.x.enabled',true);
				chartObj.setParam('labels.label[3].x',1000);
				chartObj.setParam('values.x.skip_last',0);
			}
		}
	}
	/**
	* Triggered when ajax call failed
	* @response As obj
	*/
	function chartDataFailure(response)
	{}
	init();
}
/*
Those functions are called by the flash tool. check the doc of amCharts for further information. 
function amReturnSettings(chart_id, settings)
{} 
function amChartInited(chart_id)
{}
*/
