﻿Amchart = new Ext.util.Observable();

Ext.apply(Amchart, {
	chart : null,

	init: function(chart_id) {
		Amchart.chart = document.getElementById(chart_id);
	},
	
    // SET DATA //////////////////////////////////////////////////////////////////////////////
    // flashMovie.setData(data)
    // This function can be used for setting the chart's data on the fly. 
    // Data must be in CSV or XML form, as per the <data_type> setting.
    setData: function(data) {
      this.chart.setData(data);
    },
    
    // APPEND DATA ///////////////////////////////////////////////////////////////////////////
    // flashMovie.appendData(data[, remove_count])
    // This function can be used to append new data to the existing dataset. 
    // The data must be in CSV or XML form, as per the <data_type> setting. 
    // The remove_count variable is optional and sets the number of data points
    // that should be removed from the beginning of dataset.
    appendData: function(data) {
      this.chart.appendData(data, 0);
    },
    
    // SET SETTINGS //////////////////////////////////////////////////////////////////////////
    // flashMove.setSettings(settings[, rebuild])
    // This function can be used to set some part or all the settings. The settings should be
    // in XML format, inside <settings></settings>. The "rebuild" parameter is optional and 
    // can be "true" or "false". "false" means that the new settings will not be applied right
    // after this function is called. They will can be applied using flashMovie.rebuild()
    // function or by adding some more setings with the "rebuild" set to "true". The default
    // value of "rebuild" is "true"  
    setSettings: function(settings) {
      this.chart.setSettings(settings, true);
    },
    
    // REBUILD CHART /////////////////////////////////////////////////////////////////////////
    // flashMovie.rebuild();
    // This function might be used to rebuild the chart after several portions of settings were
    // set using setSettings(settings, rebuild) function, with the rebuild set to false
    rebuild: function() {
      this.chart.rebuild();
    },
    
    // RELOAD DATA ///////////////////////////////////////////////////////////////////////////
    // flashMove.reloadData([file_name])
    // This function will reload the data. The file_name variable is optional, if you do not
    // set it here, data from the original file will be reloaded.       
    reloadData: function() {
      this.chart.reloadData();
    },
    
    // RELOAD SETTINGS ///////////////////////////////////////////////////////////////////////
    // flashMovie.reloadSettings([file_name])
    // This function will reload the settings. The file_name variable is optional, if you do
    // not set it here, settings from the original file will be reloaded. 
    reloadSettings: function() {
      this.chart.reloadSettings();
    },
    
    // RELOAD ALL ////////////////////////////////////////////////////////////////////////////
    // flashMovie.reloadAll([data_file_name][,settings_file_name])
    // This function will reload both data and settings. The names of the files are optional. 
    // If you do not set them, the original file names will be used.
    reloadAll: function() {
      this.chart.reloadAll();
    },
    
    // SET PARAM /////////////////////////////////////////////////////////////////////////////
    // flashMovie.setParam(param, value)
    // This function lets you change a single setting. The parameter names are formed using 
    // the section name and the parameter name, separated with a period. For example: 
    // background.alpha or labels.label[1].text 
    setParam: function(param, value) {
      this.chart.setParam(param, value);
    },
    
    // GET PARAM /////////////////////////////////////////////////////////////////////////////
    // flashMovie.getParam(param)
    // This function will ask Flash to return the value of a setting. The parameter name is
    // formed in the same way as the setParam function (described above). When you call this
    // function to return the setting value, Flash will return the value by calling the 
    // amReturnParam(chart_id, param_value, param_name) function
    getParam: function(param) {
      this.chart.getParam(param);
    },
    
    // GET DATA //////////////////////////////////////////////////////////////////////////////
    // flashMovie.getData()
    // This function will ask Flash to return the whole data. When you call this function to
    // return the data, Flash will call the amReturnData(chart_id, data) function.
    getData: function() {
      this.chart.getData();
    },
    
    // GET SETTINGS //////////////////////////////////////////////////////////////////////////
    // flashMovie.getSettings()
    // This function will ask Flash to return the whole settings XML. When you call this 
    // function to return the settings, Flash will call the 
    // amReturnSettings(chart_id, settings) function. 
    getSettings: function() {
      this.chart.getSettings();
    },
    
    // EXPORT AS IMAGE ///////////////////////////////////////////////////////////////////////
    // flashMovie.exportImage([file_name]) 
    // This function will start the process of exporting the chart as an image. The file_name
    // is a name of a file to which image data will be posted (files provided in the download 
    // package are export.php and export.aspx). The file_name is optional and can be set in 
    // the <export_as_image><file> setting.
    exportImage: function(file_name) {
      this.chart.exportImage(file_name);  
    },
    
    // PRINT /////////////////////////////////////////////////////////////////////////////////
    // flashMovie.print()
    // This function will print the chart. Use this print function if you don't have any
    // values rotated by 90 degrees, also if you don't have a custom bitmap background.
    print: function(){
      this.chart.print();
    },
    
    // PRINT AS BITMAP ///////////////////////////////////////////////////////////////////////
    // flashMovie.printAsBitmap()
    // Use it if you have values rotated by 90 degrees and/or a custom bitmap background.
    printAsBitmap: function(){
      this.chart.printAsBitmap();
    },
    
    // CLICK SLICE ///////////////////////////////////////////////////////////////////////////
    // flashMovie.clickSlice(index)
    // This function mimics a viewer clicking on a slice. Index is the sequential number of
    // the slice, counting from 0.
    clickSlice: function(index) {
      this.chart.clickSlice(index);
    },
    
    // ROLL OVER SLICE ///////////////////////////////////////////////////////////////////////
    // flashMovie.rollOverSlice(index)
    // This function mimics a viewer rolling over a slice (showing a balloon if it is enabled)
    // Index is the sequential number of the slice, counting from 0.
    rollOverSlice: function(index){
      this.chart.rollOverSlice(index);
    },
    
    // ROLL OUT OF THE SLICE /////////////////////////////////////////////////////////////////
    // rollOutSlice()
    // This function mimics a viewer rolling away from a slice
    rollOutSlice: function() {
      this.chart.rollOutSlice();
    }
});

// Subscribe to chart events
Amchart.subscribe('amchartinited', Amchart.init);

//////////////////////////////////////////////////////////////////////////////////////////
// Functions that are called by the chart ////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
      
// CHART INITED //////////////////////////////////////////////////////////////////////////    
// amChartInited(chart_id)
// This function is called when the chart is fully loaded and initialized.
function amChartInited(chart_id) {
	Amchart.publish('amchartinited', chart_id);
}      

// PROCESS COMPLETED //////////////////////////////////////////////////////////////////////////    
// amProcessCompleted(chart_id, process_name)
// This function is called when the chart finishes doing some task triggered by another 
// JavaScript function 
function amProcessCompleted(chart_id, process_name) {
	Amchart.publish('amprocesscompleted', chart_id, process_name);
}
      
// RETURN DATA ///////////////////////////////////////////////////////////////////////////
// amReturnData(chart_id, data)
// This function is called when you request data from a chart 
//  by calling the flashMove.getData() function.
function amReturnData(chart_id, data) {
	Amchart.publish('amreturndata', chart_id, data);
}

// RETURN PARAM //////////////////////////////////////////////////////////////////////////
// amReturnParam(chart_id, value, param_name)
// This function is called when you request a setting from a chart  
// by calling the flashMovie.getParam(param) function.
function amReturnParam(chart_id, value, param_name) {
    Amchart.publish('amreturnparam', chart_id, value, param_name);
}

// RETURN SETTINGS ///////////////////////////////////////////////////////////////////////
// amReturnSettings(chart_id, settings)
// This function is called when you request settings from a chart 
// by calling flashMovie.getSettings() function.  
function amReturnSettings(chart_id, settings) {
    Amchart.publish('amreturnsettings', chart_id, settings);
}

// RETURN IMAGE DATA /////////////////////////////////////////////////////////////////////
// amReturnImageData(chart_id, data)
// This function is called when the export to image process is finished and might be used
// as alternative way to get image data (instead of posting it to some file)
function amReturnImageData(chart_id, data){
    Amchart.publish('amreturnimagedata', chart_id, data);
}

// ERROR /////////////////////////////////////////////////////////////////////////////////
// amError(chart_id, message)
// This function is called when an error occurs, such as no data, or file not found.
function amError(chart_id, message) {
	Amchart.publish('amerror', chart_id, message);
}    

// FIND OUT WHICH SLICE WAS CLICKED //////////////////////////////////////////////////////
// amSliceClick(chart_id, index, title, value, percents, color, description)
// This function is called when the viewer clicks on the slice. It returns chart_id, 
// the sequential number of the slice (index), the title, value, percent value, 
// color and description.
function amSliceClick(chart_id, index, title, value, percents, color, description) {
    Amchart.publish('amsliceclick', chart_id, index, title, value, percents, color, description);
} 

// FIND OUT WHICH SLICE WAS HOVERED //////////////////////////////////////////////////////
// amSliceOver(chart_id, index, title, value, percents, color, description)
// This function is called when the viewer rolls over the slice. It returns chart_id, the 
// sequential number of the slice (index), the title, value, percent value, 
// color and description.
function amSliceOver(chart_id, index, title, value, percents, color, description) {
    Amchart.publish('amsliceover', chart_id, index, title, value, percents, color, description);
}

// FIND OUT WHEN THE MOUSE WAS MOVED AWAY FROM A SLICE ///////////////////////////////////
// amSliceOut(chart_id)
// This function is called when the viewer rolls away from the slice.
function amSliceOut(chart_id) {
    Amchart.publish('amsliceout', chart_id);
}