function Piechart() {

	this.seriesData = [];

	this.setTarget = function(target) {
		this.target = target;
	};

	this.setTitle = function(title) {
		this.title = title;
	};

	this.setSubTitle = function(title) {
		this.subTitle = title;
	};

	this.getTitle = function() {
		if (this.title === undefined) {
			return "";
		} else {
			return this.title;
		}
	};

	this.addSeriesData = function(name, value) {
		var temp = [];
		temp.push(name);
		temp.push(value);
		this.seriesData.push(temp);
	};

	this.setMaxQuantity = function(num) {
		this.maxQuantity = num;
	};

	this.getSeriesData = function() {
		
		var total = this.getTotalAmount();
		var data = [];
		var sum = 0;
		$(this.seriesData).each(function () {
            if((this[1]/total)>0.02) {
                data.push(this);
                
            } else {
                sum += this[1];
            }
		});
		
		if(sum >0){
		    data.push(['Övrigt', sum]);
		}
		return data;
	};
	
	this.getCategoriesDataLabels = function () {
	       var categories = [];
	       
	       var max = this.maxQuantity;
   		   if (this.seriesData.length < max) {
   			   	$(this.seriesData).each(function() {
          				  categories.push(this[0]);
          			});
       	       return categories;
   		   } else {
   		    
   		    var part1 = this.seriesData.slice(0, max)
   		    $(part1).each(function() {
      				  categories.push(this[0]);
      		});
      		categories.push("Övrigt");
   		    return categories;   
   		   }  
	       
  		
	};
	
	this.getCategoriesData = function () {
	       var categories = [];
	       
	        var max = this.maxQuantity;
      		   if (this.seriesData.length < max) {
      		       $(this.seriesData).each(function() {
          				  categories.push(this[1]);
          			});

       	       return categories;      		       
  		   } else {
  		        var part1 = this.seriesData.slice(0, max);
    			var part2 = this.seriesData.slice(max);
    			var sum = 0;
    			$(part2).each(function() {
    				sum += this[1];
    			});
    			
    			$(part1).each(function() {
          		    categories.push(this[1]);
          		});
                
                categories.push(sum);
                
  		       return categories;
  		   }
	       
	       return categories;
	};
	

	this.getTotalAmount = function() {
		var sum = 0;
		$(this.seriesData).each(function() {
			sum += this[1];

		});

		return sum;
	};

    this.renderBarChart = function () {
        chart = new Highcharts.Chart({
        				chart: {
        					renderTo: this.target,
        					defaultSeriesType: 'column',
        					margin: [ 50, 50, 100, 80]
        				},
        				title: {
        					text: this.getTitle()
        				},
        				xAxis: {
        					categories: this.getCategoriesDataLabels(),
        					labels: {
        						rotation: -45,
        						align: 'right',
        						style: {
        							 font: 'normal 13px "Myriad Pro", verdana'
        						}
        					}
        				},
        				yAxis: {
        					min: 0,
        					title: {
        						text: ''
        					}
        				},
        				legend: {
        					enabled: false
        				},
        				credits: {
            				enabled: false
            			},
        				tooltip: {
        					formatter: function() {
        						return '<b>'+ this.x +'</b><br/>'+
        							 ''+ Highcharts.numberFormat(this.y, 1) +
        							 'SEK';
        					}
        				},
        			        series: [{
        					name: 'Dont know',
        					data: this.getCategoriesData(),
        					dataLabels: {
        						enabled: true,
        						rotation: -90,
        						color: '#FFFFFF',
        						align: 'right',
        						x: -3,
        						y: 10,
        						formatter: function() {
        							return this.y;
        						},
        						style: {
        							font: 'normal 13px Verdana, sans-serif'
        						}
        					}			
        				}]
        			});
        
        
           
    };

	this.render = function() {

		chart = new Highcharts.Chart({
			chart: {
				renderTo: this.target,
				margin: [10, 140, 10, 10],
				height: 500
			},
			title: {
				text: this.getTitle()
			},
			plotArea: {
				shadow: null,
				borderWidth: null,
				backgroundColor: null
			},
			tooltip: {
				formatter: function() {
					return '<b>' + this.point.name + '</b>: ' + this.y + ' kr ' + parseInt(this.percentage, 10) + '%';
				}
			},
			plotOptions: {
				pie: {
					allowPointSelect: true,
					dataLabels: {
						enabled: true,
						formatter: function() {
							if (this.y > 5) {
								return this.point.name;
							}
						},
						color: 'white',
						style: {
							font: '13px'
						}
					}
				}
			},
			legend: {
				layout: 'vertical',
				style: {
					left: 'auto',
					bottom: 'auto',
					right: '10px',
					top: '10px'
				},
				symbolPadding: 5
			},
			credits: {
				enabled: false
			},
			series: [{
				type: 'pie',
				name: 'dont know',
				data: this.getSeriesData()
			}]
		});
	};
}
