//======================
// holds the global chart data used on this page
//======================
var g_chartmgr_chart_data = [];

//======================
//======================
function findSWF(movieName) 
{
	if (navigator.appName.indexOf("Microsoft")!= -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

// ======================
// This is a global reserved function that OFC
// calls upon initialization
//======================
function open_flash_chart_data ( id )
{
	if ( g_chartmgr_chart_data[id] == null )
		return "{}";
	else
		return JSON.stringify(g_chartmgr_chart_data[id]['data']);
}

//======================
//======================
function make_color ( col )
{
	return "#"+Math.floor(col[0] / 16).toString(16)+
				(col[0] % 16).toString(16)+
				Math.floor(col[1] / 16).toString(16)+
				(col[1] % 16).toString(16)+
				Math.floor(col[2] / 16).toString(16)+
				(col[2] % 16).toString(16);
}

//======================
//======================
function convert_ofc_to_flot_data_pie ( ofc_data )
{
	var colors =
	[
		[0xd9,0x89,0],[0,0x89,0xd9],[0x89,0,0xd9],[0xd9,0,0x89],[0x89,0xd9,0],[0,0xd9,0x89],
		[0xd9,0xd9,0],[0,0xd9,0xd9],[0xd9,0,0xd9],[0xd9,0,0xd9],[0xd9,0xd9,0],[0,0xd9,0xd9],
		[0,0,255],[165,42,42],[0,255,255],[240,230,140],[0,139,139],[255,215,0],[139,0,139],
		[85,107,47],[255,140,0],[189,183,107],[153,50,204],[233,150,122],[148,0,211],
		[255,0,255],[75,0,130],[173,216,230],[144,238,144],[255,182,193],[0,255,0],[255,0,255],
		[128,128,0],[255,165,0],[255,192,203],[128,0,128],[255,0,0],[192,192,192],[255,255,0]
    ];

	var data = [];
	var cno = 0;
	for ( k=0;k<ofc_data.elements.length;k++ )
	{
		if ( ofc_data.elements[k].type != "pie" )
			continue;

		for ( v=0;v<ofc_data.elements[k].values.length;v++ )
		{
			if ( ofc_data.elements[k].values[v].value != 0 )
			{
				data.push ( {data:ofc_data.elements[k].values[v].value, label: ofc_data.elements[k].values[v].label, color: make_color ( colors[cno] )} );
				cno++;
			}
		}
	}
	return data;
}

//======================
//======================
function convert_ofc_to_flot_data_line ( ofc_data )
{
	var data = [];
	
	for ( k=0;k<ofc_data.elements.length;k++ )
	{
		var cset = { data: [], lines: {show:true, fill:false, fillColor:null}, points: { show:false,fill:false}, color: null};
		switch ( ofc_data.elements[k].type  )
		{
		case 'area_hollow':
		case 'area_line':
			cset.lines.show = true;
			cset.lines.fill = true;
			cset.points.show = true;
			cset.points.fill = true;
			break;
		}
		
		if ( ofc_data.elements[k].colour != null )
			cset.color = ofc_data.elements[k].colour;
		
		if ( ofc_data.elements[k].type == 'scatter_line' )
		{
			for ( v=0;v<ofc_data.elements[k].values.length;v++ )
			{
				cset.data.push ( [ofc_data.elements[k].values[v].x,ofc_data.elements[k].values[v].y] );
			}
		}
		else
		{
			for ( v=0;v<ofc_data.elements[k].values.length;v++ )
			{
				cset.data.push ( [v,ofc_data.elements[k].values[v].value] );
			}
		}
		
		data.push ( cset );
	}
	return data;
}

//======================
//======================
function convert_ofc_to_flot_data ( ofc_data )
{
	if ( is_ofc_pie ( ofc_data ) )
		return convert_ofc_to_flot_data_pie ( ofc_data );
	else 
		return convert_ofc_to_flot_data_line ( ofc_data );
}

//======================
// By default all Y axis are number formatted. If you wish 
// to override, you should send a complete flot_pars object 
//======================
function chartmgr_number_formatter ( v )
{
	if ( v < 1000 )
		return ""+v;
	else if ( v < 1000000 )
		return Math.floor(v/1000)+"K";
	else if ( v < 1000000000 )
		return Math.floor(v/1000000)+"M";
	else
		return Math.floor(v/1000000)+"B";
}

//======================
//======================
function is_ofc_pie ( ofc_data )
{
	// Is this a pie chart?
	var is_pie = false;
	for ( k=0;k<ofc_data.elements.length;k++ )
	{
		if ( ofc_data.elements[k].type == "pie" )
			return true;
	}
	return false;
}

//======================
//======================
function convert_ofc_to_flot_pars ( ofc_data )
{
	// special handling for pie
	if ( is_ofc_pie ( ofc_data ) )
	{
		return {
			pie: { 
				show: true, 
				pieStrokeLineWidth: 0, 
				pieStrokeColor: '#FFF', 
				labelBackgroundOpacity: 0.0, 	// default is 0.85
				labelFormatter: function(serie){ return null; }
			},
			//colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
			legend: {
				show: true, 
				position: "ne", 
				backgroundOpacity: 0
			}
		}
	}
	
	var pars = { xaxis : { ticks:[] }, yaxis : {tickFormatter: chartmgr_number_formatter} };
	// Do the X axis. It may be better to use annotation 
	// style since some grid items may be missing
	if ( ofc_data.x_axis != null && ofc_data.x_axis.labels != null && ofc_data.x_axis.labels.labels != null )
	{
		for ( t=0;t<ofc_data.x_axis.labels.labels.length;t++ )
		{
			if ( ofc_data.x_axis.labels.labels[t].text != "" )
				pars.xaxis.ticks.push ( [t, ofc_data.x_axis.labels.labels[t].text] );
		}
	}
	return pars;
}

//======================
//======================
function embed_chart ( obj_id, use_ofc, ofc_data, chart_wid, chart_ht, chart_id, ofc_swf_url, flot_pars )
{
	D = [];
	D['data'] = ofc_data;
	D['use_ofc'] = use_ofc;
	D['obj_id'] = obj_id;
	D['flot_obj'] = null;
	
	g_chartmgr_chart_data[chart_id] = D;
	
	if ( use_ofc )
	{
		var chart_pars = 
		{ 
			width: chart_wid, 
			height: chart_ht, 
			quality: "high", 
			movie: ofc_swf_url, 
			allowFullScreen: "true", 
			wmode: "transparent" 
		};
		var flash_vars = 
		{
			"id" : chart_id, 
			"save_image_message" : "Copyright Anvato, Inc"
		}; 
		swfobject.embedSWF ( ofc_swf_url, obj_id, chart_wid, chart_ht, "9.0.0", "expressInstall.swf", flash_vars, chart_pars, {} );
	}
	else
	{
		//$("#"+obj_id).width ( chart_wid );
		$("#"+obj_id).height ( chart_ht );

		if ( is_ofc_pie ( ofc_data ) )
			g_chartmgr_chart_data[chart_id]['flot_obj'] = $.pieplot($("#"+obj_id), convert_ofc_to_flot_data(ofc_data), flot_pars==null ? convert_ofc_to_flot_pars(ofc_data) : flot_pars );
		else
			g_chartmgr_chart_data[chart_id]['flot_obj'] = $.plot($("#"+obj_id), convert_ofc_to_flot_data(ofc_data), flot_pars==null ? convert_ofc_to_flot_pars(ofc_data) : flot_pars );
	}
}

//======================
//======================
function load_chart_data ( chart_id, ofc_data )
{
	if ( g_chartmgr_chart_data[chart_id] == null )
		return false;
		
	var use_ofc = g_chartmgr_chart_data[chart_id]['use_ofc'];
	var obj_id = g_chartmgr_chart_data[chart_id]['obj_id'];
	g_chartmgr_chart_data[chart_id]['data'] = ofc_data;
	
	if ( use_ofc )
	{
		chart_obj = findSWF ( obj_id );
		if ( chart_obj != null )
			chart_obj.load ( JSON.stringify(ofc_data) );
	}
	else
	{
		if ( g_chartmgr_chart_data[chart_id]['flot_obj'] == null )
			return false;
		
		g_chartmgr_chart_data[chart_id]['flot_obj'].setData(convert_ofc_to_flot_data(ofc_data));
		g_chartmgr_chart_data[chart_id]['flot_obj'].setupGrid();
		g_chartmgr_chart_data[chart_id]['flot_obj'].draw();
	}
	
	return true;
}

