// JavaScript Document
window.addEvent("domready", function(){
  if($$('.chart').length > 0) initiateCharts();																 
});


var charts = Array();

function initiateCharts(){
	$$('.chart').each(function(el){ 
	  charts.push(new MegaBuyteChart(el));				 
	});
}


var MegaBuyteChart = new Class({
    initialize: function(element){
			var self = this;
			this.container = element;
			this.type      = element.className.split(" ")[1];
			this.chart     = null;
			
			this.categories = [];
			$$('.'+this.type+'_category').each(function(el){
			  self.categories.push(el.get("html"));																						
      });
			
			this.options = {
        chart: {
          renderTo: this.container,
          defaultSeriesType: 'line',
					/* margin: [60, 120, 70, 50], */
					margin: [60, 30, 50, 30],
					borderColor: '#EEEDE1',
					borderWidth: 1,
					backgroundColor: '#FBFAEB',
					style: {
						fontFamily: '"Trebuchet MS",Arial,Helvetica,sans-serif',
	          color: '#E31351',
	          fontSize: '11px'
          },
					inverted: false
        },
				colors: [
				  '#E31351',
					'#415860',
					'#4b80c9',
					'#e65a7c',
					'#c33b1a',
					'#f6cc5d',
					'#650b25'
				],
				legend: {
					enabled: false,
					backgroundColor: '#F0EEDD',
					borderColor: '#EEEDE1',
					align: 'right',
          verticalAlign: 'top',
          x: -10,
          y: 60,
					layout: 'vertical',
					labelFormatter: function() {
	          if(this.name.length < 7) return this.name
						else return this.name.substr(0, 6)+"..";
          }
					/* itemWidth: 90 */
				},
				title: {
          text: 'Financials',
					y: 30,
					style: {
						fontFamily: '"Trebuchet MS",Arial,Helvetica,sans-serif',
	          color: '#E31351',
	          fontSize: '16px'
          }
        },
				xAxis: {
					categories: this.categories,
					lineColor: '#DCD9BE',
					tickColor: '#EEEDE1',
					labels: {
						style: {
						  fontFamily: '"Trebuchet MS",Arial,Helvetica,sans-serif',
	            color: '#455660',
	            fontSize: '11px'
            },
						rotation: -45,
						y: 22
					}
				},
				yAxis: [{
					title: '',
					labels: {
						style: {
						  fontFamily: '"Trebuchet MS",Arial,Helvetica,sans-serif',
	            color: '#455660',
	            fontSize: '11px'
            }
					},
					gridLineColor: '#EEEDE1'
				},{
				  opposite: true
				}],
				credits : {
          enabled: false
        },
				tooltip: {
					formatter: function(){
						return "<strong>"+this.series.name+"</strong><br>"+this.x+": "+this.y;
					}
				},
				plotOptions: {
					area: {
						fillOpacity: 0.7
					},
					series: {
						borderColor: '#FFFFFF',
						shadow: false,
						borderWidth: 0,
            stacking: null
					}
				},
				series: []
      };
			
			this.names = $$('.'+this.type+'_header');
			
			
			if(this.type == "financial_summary"){
				this.rows  = $$('.'+this.type+'_data');
				
				var data  = Array();
				var datakey = 1;
			  this.rows.each(function(el){
				   data.push(parseFloat(el.getElements("td")[datakey].get("html")));
        });
				this.add('Revenue', 'column', data, 0);
				
				var data  = Array();
				var datakey = 2;
			  this.rows.each(function(el){
				   data.push(parseFloat(el.getElements("td")[datakey].get("html")));
        });
				this.add('PBT', 'line', data, 1);	
				
			}
			else if(this.type == "financial_table"){
				if(data_groups){
				  for(var i = 0; i < data_groups.length; i++){
				    this.options.series.push(data_groups[i]);
					  //this.render();
				  }
				}
			}
			
			if(this.container.getParent("div").getElement("div.expandable_drag")){
				
				this.container.makeResizable({
          modifiers: {x: false, y: 'height'},
          snap: 0,
          handle: this.container.getParent("div").getElement("div.expandable_drag"),
          limit: {y: [280, 600]},
          preventDefault: true,
          onComplete: function(el){ if(self.chart){ self.render(); } },
          onSnap: function(el){ if(self.chart) { self.chart.destroy(); } }
        });
				
			}
			
			this.render();
			
		},
		add: function(_name, _type, _data, _y_axis){
			this.options.series.push({
        name: _name,
				type: _type,
        data: _data,
				y_axis: _y_axis
      });
		},
		render: function(){
			this.chart = new Highcharts.Chart(this.options);
		}
		
});


