function amChart( params ) {


	this.chart_content_id = params[ 'chart_content_id' ];
	this.root_path = params[ 'root_path' ];
	this.view = params[ 'view' ] != undefined ? 1 : 0;

	this.zoom =  params[ 'zoom' ] != undefined ?  params[ 'zoom' ]  : 'ytd';

	this.cfgfile = this.zoom == 'ytd' ? 'be_settings.xml' : 'be_settings_max.xml' 

	this.startdate =  params[ 'startdate' ]; 

	this.chart_id = 'id'+this.chart_content_id;

	this.width = params[ 'width' ];
	this.heigth = params[ 'heigth' ];

	this.swf = undefined;

	this.tts_id_selected = new Array();
	this.stocks= new Hash();

	this.max_cmp=3;

	this.sessionid = undefined;
	this.savesession = 1;

	this.chartcolors = new Array('#A33013','#2F4C85','#178A32','#B49524','#A11167');

	this.chartId = undefined;

	this.init = function( id, count) {

		if( this.view == 0 ){
			this.createCompanyFields(id, count);
		}

		var so = new SWFObject( this.root_path+"amstock/amstock.swf" , this.chart_id, this.width, this.heigth, "8", "#FFFFFF");
		so.addVariable("chart_id", this.chart_id );
		so.addVariable("path", this.root_path+"amcharts");
		so.addVariable("settings_file", encodeURIComponent(this.root_path+"amstock/"+this.cfgfile));
		so.write( this.chart_content_id );
	}

	this.chartFunction = function( fhash ) {

		var func='this.swf.'+fhash[ 'function' ]+'(';
		if( fhash[ 'params' ] != undefined ){
			func+='\''+fhash[ 'params' ].join("','")+'\'';
		}
		func+=')';

		try {
			eval( func );
		} catch ( e ) {

		}
	}

	this.setSessionId = function( sid ) {
		//alert( 'received sid: '+ sid );
		this.sessionid = sid;
	}

	this.saveSessionData = function() {

		if( this.savesession == 0) {
			return 1;
		}

		var url='/amcharts/ssd';

		var ttsids = new Array();
		ttsids.push(this.tts_id_selected[ 0 ][ 'tts_id' ]);
		ttsids.push(this.tts_id_selected[ 1 ][ 'tts_id' ]);
		ttsids.push(this.tts_id_selected[ 2 ][ 'tts_id' ]);
		ttsids.push(this.tts_id_selected[ 3 ][ 'tts_id' ]);

		new Ajax.Request(url, {
			method: 'get',
			parameters : {
				sid		: this.sessionid,
				object : this.chart_content_id,
				tts_ids	: ttsids
			},
			onSuccess: function(transport) {
				var response=transport.responseText.evalJSON();
				var resp=response[ 'json_response' ];
				//eval( resp[ 'object' ]+'.setSessionId(\''+resp[ 'sid' ]+'\')' );
				bechart.setSessionId( resp[ 'sid' ] );
			}
		});
	}

	this.setParam = function ( param, value ) {

		this.swf.setParam(param, value);
	}

	this.setChart = function( chart_id ) {

		if( this.chartId == undefined ) {

			this.chartId = chart_id;

			this.swf = $(''+chart_id);

			if( this.tts_id_selected[0][ 'tts_id' ] == undefined ) {

				this.setStocksOnLoad();

			} else if ( this.tts_id_selected.length > 0 ) {

				this.savesession = 0;
				this.setSettings();
			}
		}
	}

	this.setStock = function( data ) {

		this.stocks[ data[ 'tts_id' ] ] = new Array( data[ 'sname' ],data[ 'lname' ] );
	}

	this.setStocksOnLoad = function( ) {

		var url=window.location.href;

		var result=url.split("?");

		if( result.length > 0 ) {

			var TTSIDS=result[1].split('+');

			if( TTSIDS.length > 0) {
				//alert('set savesession = 0');
				this.savesession = 0;
			}

			for( var i=0; i < TTSIDS.length ; i++) {

				if( TTSIDS[ i ].match(/^tts/) ) {
					//alert( 'tts_id: '+TTSIDS[ i ] );
					this.setCompany( TTSIDS[ i ] );
				} else {
					if( TTSIDS[ i ].match(/^[A-Za-z]+$/) ) {
						this.stocks.each( function(pair) {

							if( pair.value[ 0 ] == TTSIDS[ i ] || pair.value[ 1 ] == TTSIDS[ i ]) {
								bechart.setCompany( pair.key );
							}
						});
					}
				}
			}
		}
	}

	this.selectDataSet = function( datasetid ) {

		var id = datasetid == 'object' ? datasetid.value :  datasetid;
		this.swf.selectDataSet( id );
	}

	this.setCompany = function( tts_id ) {

		$( 'stock_selector' ).style.display='none';

		for(var i=0; i < this.max_cmp ; i++) {

			$( 'comp'+ ( i + 1 ) ).removeClassName('cmp_disabled');

			if( this.tts_id_selected[ i ][ 'tts_id' ] == undefined ) {
				this.tts_id_selected[ i ][ 'tts_id' ] = tts_id;
				this.tts_id_selected[ i ][ 'sname' ] = this.stocks[ tts_id ][ 1 ];
				this.tts_id_selected[ i ][ 'lname' ] = this.stocks[ tts_id ][ 0 ];

				$( 'comp'+ ( i + 1 ) ).innerHTML = this.stocks[ tts_id ][ 0 ];

				this.setSettings(  );
				return 1;
			}
		}
	}

	this.setStockSelected= function( tts_id ) {

		var cnt=this.tts_id_selected.length;

		this.tts_id_selected[ cnt ]= new Hash();

		this.tts_id_selected[ cnt ]['tts_id'] = tts_id;
		this.tts_id_selected[ cnt ]['sname'] =  this.stocks[ tts_id ][ 1 ];
		this.tts_id_selected[ cnt ]['lname'] = this.stocks[ tts_id ][ 0 ];
		this.tts_id_selected[ cnt ]['visible'] = 1;

		this.max_cmp=this.tts_id_selected.length;
	}

	this.compareDataSet = function ( datasetid ) {

		var id= typeof( datasetid ) == 'object' ? datasetid.value : datasetid;

		if( this.tts_id_selected[ id ][ 'tts_id' ] != undefined ) {

			this.swf.compareDataSet( id );
		}
	}

	this.toggleDataSet = function ( id ) {

		if( this.tts_id_selected[ id ][ 'tts_id' ] != undefined  && id > 0 ) {

			if (this.tts_id_selected[ id ][ 'visible' ] == 1) {

				this.swf.uncompareDataSet( id );
				this.tts_id_selected[ id ][ 'visible' ] = 0;
				$( 'comp'+ (id + 1) ).addClassName('cmp_disabled');
			} else {

				this.swf.compareDataSet( id );
				this.tts_id_selected[ id ][ 'visible' ] = 1;
				$( 'comp'+ (id + 1) ).removeClassName('cmp_disabled');
			}
		} else if ( this.tts_id_selected[ id ][ 'tts_id' ] != undefined  && id == 0 ) {

			this.resetCompanys();
			$( 'stock_selector' ).style.display='block';
		}
		else {
			$( 'stock_selector' ).style.display='block';
		}
	}

	this.setSettings = function(  ) {

		var settings='<settings>';
		settings+='<margins>0</margins>';
		settings+='<text_size>10</text_size>';
		settings+='<number_format><letters>';
		settings+='<letter number="1000">K</letter>';
		settings+='<letter number="1000000">M</letter>';
		settings+='<letter number="1000000000">B</letter></letters>';
		settings+='</number_format>';
		settings+='<data_sets>';

		for(var i=0; i < this.max_cmp ; i++) {

			if( this.tts_id_selected[ i ][ 'tts_id' ] != undefined ) {

				if( this.tts_id_selected[ i ][ 'sname' ] == ''){

					this.tts_id_selected[ i ][ 'sname' ]=this.tts_id_selected[ i ][ 'lname' ];
				}

				settings+='<data_set did="';
				settings+=i;
				settings+='">';
				settings+='<title>'
				settings+=this.tts_id_selected[ i ][ 'lname' ];
				settings+='. company</title>';
				settings+='<short>';
				settings+=this.tts_id_selected[ i ][ 'sname' ];
				settings+='</short>';
				settings+='<file_name>/amstock/';
				settings+=this.tts_id_selected[ i ][ 'tts_id' ];

				if( this.startdate ) {
					settings+='/';
					settings+=this.startdate;
				}
				settings+='</file_name>';

				if( i == 0){
					settings+='<main_drop_down selected="true"></main_drop_down>';
				} else {
					settings+='<compare_list_box selected="true"></compare_list_box>';
				}

				settings+='<csv>';
				settings+='<reverse>true</reverse>';
				settings+='<separator>,</separator>';
				settings+='<date_format>YYYY-MM-DD</date_format>';
				settings+='<decimal_separator>.</decimal_separator>';
				settings+='<columns>';
				settings+='<column>date</column>';
				settings+='<column>close</column>';
				settings+='</columns>';
				settings+='</csv>';
				settings+='</data_set>';
			}
		}

		settings+='</data_sets>';
		settings+='<charts><chart cid="0"><bg_color>f5f5f5,ffffff</bg_color><border_color>#CCCCCC</border_color><border_alpha>100</border_alpha><grid><x><dashed>true</dashed></x><y_right><color>cccccc</color><alpha>100</alpha><dashed>true</dashed></y_right></grid><legend><show_date>true</show_date></legend><graphs><graph gid="0"><axis>right</axis><type>line</type><data_sources><close>close</close></data_sources><compare_source>close</compare_source><legend><date key="true" title="true"><![CDATA[<b>{close}</b>]]></date><period key="true" title="true"><![CDATA[ low:<b>{low}</b> high:<b>{high}</b> ]]></period><date_comparing key="true" title="true"><![CDATA[{close.percents}]]></date_comparing><period_comparing key="true" title="true"><![CDATA[{close.percents}]]></period_comparing></legend></graph></graphs></chart></charts><data_set_selector><enabled>false</enabled></data_set_selector><period_selector><button><bg_color_hover>b81d1b</bg_color_hover><bg_color_selected>b81d1b</bg_color_selected><text_color_hover>ffffff</text_color_hover><text_color_selected>ffffff</text_color_selected></button><periods><period type="DD" count="10">10D</period><period type="MM" count="1">1M</period><period type="MM" count="3">3M</period><period type="YYYY" count="1">1Y</period><period type="YYYY" count="3">3Y</period>';
//		settings+='<period type="YTD" count="0">YTD</period>';
		settings+='<period selected="true" type="MAX">MAX</period>';
		settings+='</periods><periods_title>Zoom:</periods_title><custom_period_title>Custom period:</custom_period_title></period_selector><header><enabled>false</enabled><text><![CDATA[<b>{title}</b> ({short}) {description}]]></text><text_size>12</text_size></header><plot_area><border_color>cccccc</border_color></plot_area><scroller><enabled>true</enabled><height>50</height><graph_data_source>close</graph_data_source><bg_color>f5f5f5,ffffff</bg_color><resize_button_color>b81d1b</resize_button_color></scroller>';
		settings+='</settings>';

		this.saveSessionData();
		this.swf.setSettings(settings);
	}

	this.resetCompanys = function () {

		for( var i=0; i < this.max_cmp ; i++ ) {

			this.tts_id_selected[ i ]['tts_id'] = 'data1.csv';
			this.tts_id_selected[ i ]['sname'] = '';
			this.tts_id_selected[ i ]['lname'] = '';
		}

		this.sessionid=undefined;
		this.savesession=1;
		this.setSettings();
		this._clearTTSArray();
	}

	this._clearTTSArray = function() {

		for( var i=0; i < this.max_cmp ; i++ ) {

			this.tts_id_selected[ i ]['tts_id'] = undefined;
			this.tts_id_selected[ i ]['sname'] = undefined;
			this.tts_id_selected[ i ]['lname'] = undefined;

			$( 'comp'+ ( i + 1 ) ).innerHTML='Aktie ausw&auml;hlen ...';
			$( 'comp'+ ( i + 1 ) ).addClassName('cmp_disabled');
		}
	}

	this.createCompanyFields = function( id, count ) {

		var cnt= count ? count : this.max_cmp;
		this.max_cmp = cnt;

		this.cmpfield = id ? id : this.cmpfield;

		for(var i=0; i < cnt ; i++) {

			this.tts_id_selected.push( {
				'tts_id'	: undefined,
				'sname'		: undefined,
				'lname'		: undefined,
				'visible'	: 1
			} );
		}

		var html='';

		for( var i=1; i <= count; i++) {

			html+='<div class="cmp cmp_disabled" id="comp';
			html+= i;
			html+='" style="color: ';
			html+=this.chartcolors[ i-1 ];
			html+='" onclick="'
			html+=this.chart_content_id;
			html+='.toggleDataSet(';
			html+= i-1;
			html+= ')">';
			html+='Aktie ausw&auml;hlen ...</div>';
		}

		$( this.cmpfield ).innerHTML = html;

		$('comp1').style.borderColor='red';
	}

	this.setZoomYTD = function (from, to) {
		this.swf.setZoom('2008-01-01','2008-10-10');
	}

	this.setZoom = function (from, to) {
		this.swf.setZoom( from , to );
	}

	this.saveImage = function() {

		var stock_counter=0;

		var params="?";

		for( var i=0; i < this.max_cmp ; i++ ) {

			if( this.tts_id_selected[ i ]['tts_id'] != undefined ){
				stock_counter++;
				params+='sname=';
				params+=this.tts_id_selected[ i ]['sname'];
				params+='&'
			} else {
				break;
			}
		}

		this.swf.exportImage('/amcharts/save_image/'+params);
	}

	this.setZoom = function(from, to) {
		this.from = from;
		this.to = to;
	}

	this.getZoom = function() {
		return 'sepp';
	}	
}

/* amChart events */
function amChartInited( chart_id ) {

	var chart=chart_id.substr(2);

	eval( chart+'.setChart(\''+chart_id+'\')' );

	try{
		$( 'reset' ).style.display='block';
		$( 'save' ).style.display='block';
	} catch( e ){}

}

function amError( chart_id, message ) {
}

function amReturnSettings( chart_id, settings) {
	$( 'besettings' ).value=unescape(settings);
}

function amGetZoom( chart_id, from, to ){

	var chart=chart_id.substr(2);
	var zoom =  eval( chart+'.getZoom()' );

}

function amProcessCompleted(chart_id, process_name){
}	


