SQ4.QualitySummaryWidget = function(cfg){
	var me = this;
	this.config = SQ4.Utils.extend(cfg, {design:{
		backgroundColor:'white',
		linesColor:'#dddddd',
		font:'12px Arial',
		captionWidth:60,
		captionColor:'black',
		problemColor:'#ff0000',
		noProblemColor:'green',
		padding:5
	}});
	
	SQ4.Utils.setLogConfig(this.config.logLevel,'EquityChart');
	
	this.canvasWrapper = document.getElementById(this.config.canvasId);
	if (!this.canvasWrapper){
		SQ4.Utils.log('No element with id:'+this.config.canvasId+' was found. Cannot continue.', SQ4.Consts.LOG_ERROR);
		return;		
	}
	
	var canvases = this.canvasWrapper.getElementsByTagName('canvas');
	if (canvases.length!=1){
		SQ4.Utils.log('No unique canvas was found. Cannot continue.', SQ4.Consts.LOG_ERROR);
		return;		
	}
	this.canvas = canvases[0];	
	
	this.canvasHelper = new SQ4.CanvasHelper(this.canvas);
	initialize();
		
	function initialize() {
		SQ4.Utils.log('Initializing quality widget', SQ4.Consts.LOG_DEBUG);
		
		me.canvasWrapper.style.position='relative';
	};	

	this.initSizes = function(count){
		var percents = false;
		if (me.config.width){		
			me.widthInfo = SQ4.Utils.parseSize(me.config.width);
		}
		
		var height = count*me.lineHeight+2*me.config.design.padding;
		me.heightInfo = SQ4.Utils.parseSize(height+'px');	
		me.updateCanvasSize();
	}
	
	this.updateCanvasSize = function(){
		return me.canvasHelper.updateCanvasSize(me.canvas, me.canvasWrapper, me.widthInfo, me.heightInfo);
	}	
		
	this.setData = function(data, minHour, maxHour, opacity){
		SQ4.Utils.log('Handling loaded data', SQ4.Consts.LOG_DEBUG);
		
		if (!data || data==null){
			return;
		}
		
		this.opacity = typeof opacity=='undefined' ? 'ff' : opacity;
		this.minHour = typeof minHour=='undefined' ? 0 : minHour;
		this.maxHour = typeof maxHour=='undefined' || maxHour==0 ? 23 : maxHour;
		this.lineHeight = this.maxHour-this.minHour+1;
		this.hourHeight = 1;
		
		if (this.lineHeight<6){
			this.lineHeight *= 5;
			this.hourHeight = 5;	
		}else if (this.lineHeight<10){
			this.lineHeight *= 3;
			this.hourHeight = 3;
		} else if (this.lineHeight<14){
			this.lineHeight *= 2;
			this.hourHeight = 2;	
		}
		
		this.draw(data);
	}
	
	this.getWidth = function(){
		return me.canvasHelper.getWidth();
	}
	
	this.getContentWidth = function(){
		return me.getWidth() - 2*me.config.design.padding;
	}
	
	this.getHeight = function(){
		return me.canvasHelper.getHeight();
	}
	
	this.getContentHeight = function(){
		return me.getHeight() - 2*me.config.design.padding;
	}	
	
	this.normalizeDates = function(data){
		var bestStartDay=-1;
		var bestEndDay = -1;
		data.forEach(function(yearData){
			yearData.start = new Date(yearData.start);
			yearData.end = new Date(yearData.end);
			
			var startDay = SQ4.Utils.getDayOfYear(yearData.start);
			var endDay = SQ4.Utils.getDayOfYear(yearData.end);
			
			if (bestStartDay==-1 || bestStartDay>startDay){
				bestStartDay = startDay;
			}
			if (bestEndDay==-1 || bestEndDay<endDay){
				bestEndDay = endDay;
			}			
			
			yearData.problems.forEach(function(problem){
				problem.from = new Date(problem.from);
				problem.to = new Date(problem.to);
			});
		});
		
		return {min:bestStartDay,max:bestEndDay};
	}
	
	this.draw = function(data){
		var design = me.config.design;
		var size = data.length;
		me.initSizes(size);
		this.canvasHelper.clear(design.backgroundColor);		
		
		var contentWidth = me.getContentWidth();
		var contentHeight = me.getContentHeight();
		
		var top = design.padding;
		if (data.length==0){
			return;
		}
		
		me.canvasHelper.drawLine(design.padding,top,design.padding+contentWidth, top, design.linesColor);
		
		var barAreaWidth = contentWidth-design.captionWidth;
		var daysInYear = this.normalizeDates(data);
		var totalDaysInYear = daysInYear.max-daysInYear.min+1;
		var dayWidth = barAreaWidth/totalDaysInYear;
		var hourHeight = this.hourHeight;
		
		var beginDraw = design.captionWidth+design.padding;
		
		var linesY = [];
		data.forEach(function(yearData){			
			var areaCaption = {x:design.padding,y:top+3,width:design.captionWidth,height:me.lineHeight};
			me.canvasHelper.centerText(yearData.year+"", design.captionColor, design.font,areaCaption);
			
			var beginDay = SQ4.Utils.getDayOfYear(yearData.start)-daysInYear.min;
			var endDay = SQ4.Utils.getDayOfYear(yearData.end)-daysInYear.min;
			
			var startX = beginDay*dayWidth+beginDraw;
			var barWidth = (endDay-beginDay+1)*dayWidth;
			me.canvasHelper.drawFilledRectangle(startX, top, barWidth, me.lineHeight,design.noProblemColor,{antialias:true})
			
			var bottom = top + me.lineHeight;
			yearData.problems.forEach(function(problem){
				var problemBeginDay = SQ4.Utils.getDayOfYear(problem.from)-daysInYear.min;
				var problemEndDay = SQ4.Utils.getDayOfYear(problem.to)-daysInYear.min;
				
				var problemBeginHour = SQ4.Utils.getHourOfDay(problem.from)-me.minHour;
				var problemEndHour = SQ4.Utils.getHourOfDay(problem.to)-me.minHour;
				
				for(var day=problemBeginDay;day<=problemEndDay;day++){
					var problemStartX = day*dayWidth+beginDraw;
					var problemBarWidth = (problemEndDay-day)*dayWidth;
					
					if (problemBarWidth==0){
						problemBarWidth=dayWidth;
					}
					
					var problemTop,problemHeight;
					
					if (day==problemBeginDay){
						//hours in first problematic day
						problemTop = top + problemBeginHour*hourHeight;	
						if (problemBeginDay==problemEndDay){
							problemHeight = (problemEndHour-problemBeginHour+1)*hourHeight;	
						}else{
							problemHeight = bottom-problemTop;
						}
					}else if (day==problemEndDay){
						//hours in the laast problematic day
						problemTop = top;
						problemHeight = problemEndHour*hourHeight;
					}else{
						//hour between problematic days
						problemTop = top;
						problemHeight = me.lineHeight;
					}
				
					if (problemTop+problemHeight>bottom){
						problemHeight = bottom-problemTop;
						
						if (problemHeight<=0){
							problemTop = bottom-1;
							problemHeight=1;
						}
					}
					me.canvasHelper.drawFilledRectangle(problemStartX, problemTop, problemBarWidth, problemHeight,design.problemColor+me.opacity,{antialias:false});	
				}
			});
			
			top += me.lineHeight;
			linesY.push(top);			
		});
		
		linesY.forEach(function(y){
			me.canvasHelper.drawLine(design.padding,y,design.padding+contentWidth, y, design.linesColor);
		});
		
		me.canvasHelper.drawLine(design.padding+design.captionWidth,design.padding,design.padding+design.captionWidth, top, design.linesColor);		
		me.canvasHelper.drawLine(design.padding,design.padding,design.padding, top, design.linesColor);
		me.canvasHelper.drawLine(design.padding+contentWidth,design.padding,design.padding+contentWidth, top, design.linesColor);
	}
}