// Copyright 2008 ProjectWizards GmbH. All rights reserved.

//console.log("GanttRulerView.js");

var GanttRulerView = Base.extend({
	constructor: function(ganttView, container, ruler, environment)
	{
		this.environment = environment;
		this.ganttScrollView = ganttView;
		this.container = container;
		this.ruler = ruler;
		this.rulerRowsHeight = 0;
		this.lastRowsCode = 0;	
		this.refreshRuler();
		this.tableWidth = 0;
        this.lastStatusDateTime = 0;
	},

    projectStatusDateTime: function()
    {
		var project = this.ruler.project;
  	    if(project){
            var statusDate = project.valueForKey('statusDate');
            return statusDate ? statusDate.getTime() : 0;
		}
		return 0;
    },

	refreshRuler: function()
	{	
		this.rulerRowsHeight = this.ruler.rows.length * this.rulerRowHeight();
		var rowsCode = this.ruler.rowsCode();
        
		if(rowsCode && (this.lastRowsCode != rowsCode || this.lastStatusDateTime != this.projectStatusDateTime()) && this.ruler) {			
			this.tableWidth = this.ruler.dateToPos(this.ruler.endDate);
			while(this.container.lastChild)
				this.container.removeChild(this.container.lastChild);			
			this.tables = [];
			var bottom = 0;
			var height = this.rulerRowHeight();
			for(var rowIndex=0; rowIndex<this.ruler.rows.length; rowIndex++){		
				var table = $(document.createElement("div"));
				table.style.position = "absolute";
				table.style.overflowX = "visible";
				table.style.width = "1px";
				table.style.height = height+"px";
				table.style.left = "0px";
				if(rowIndex != 0)
					bottom+=1;
				table.style.bottom = bottom+"px";
				table.rulerRow = this.ruler.rows[rowIndex];
				table.rowIndex = rowIndex;
				table.cells = [];
				bottom+=height;
				this.tables.push(table);
				this.container.appendChild(table);
			}
			this.gradientContainer = this.inPrintWindow ? null : PWUtils.drawHeaderGradient(this.container, this.environment.images.grayGradient);
			this.drawRulerFromStartDateToEndDate(this.ganttScrollView.visibleStartDate, this.ganttScrollView.visibleEndDate);
			this.ganttScrollView.rulerChanged(); // Hot Spot
		}	
	},
	
	drawCell: function(table, left, width, height, text, highlight)
	{
		var cell = $(document.createElement("div"));
		cell.style.position = "absolute";
		cell.style.borderRight = "1px solid #aaaaaa";
		cell.style.borderBottom = "1px solid #aaaaaa";
		cell.style.color = "#000000";
		cell.style.textAlign = "center";
		cell.style.overflow = "hidden";
		cell.style.whiteSpace ="nowrap";
		cell.style.padding= "0px";
		cell.style.margin = "0px";
		cell.style.top = '0px';
		cell.style.left = left +'px';
		cell.style.width = Math.max(width, 0) +'px';
		cell.style.height = (height - (table.rowIndex == 0 ? 1 : 0)) +'px';
        if(highlight)
        {
            var highlightCell = $(document.createElement("div"));
            highlightCell.style.backgroundColor = "#D3DFF5";            
            highlightCell.style.position = "absolute";
            highlightCell.style.top = "0px";
            highlightCell.style.left = "1px";
            highlightCell.style.width = Math.max(width-1, 0) +'px';
            highlightCell.style.height = (height - (table.rowIndex == 0 ? 1 : 0)) +'px';
            cell.appendChild(highlightCell);    
        }
		var text = SPAN({style:'left:3px;top:0px;height:'+height+'px;width:'+(width-6)+'px; overflow:hidden; position:absolute;'}, document.createTextNode(text));		
		cell.appendChild(text);
		table.appendChild(cell);	
	},
	
	drawTableContentFromStartDateToEndDate: function(table, start, end, highlightStatusDate)
	{		
		if(start && end){
			var row = table.rulerRow;
			var startTime = Math.max(start.getTime(), this.ruler.startDate.getTime());
			var endTime = Math.min(end.getTime(), this.ruler.endDate.getTime());
			var startDate = new Date(startTime);
			var endDate = new Date(endTime);	
			var unitStart = row.startOfUnitContainingDateClipAtDate(startDate, this.ruler.startDate);
			var height = this.rulerRowHeight();
			var width;
			var lastStartTime;
            var statusDateTime = this.projectStatusDateTime();
            this.lastStatusDateTime = statusDateTime;
            
			while(unitStart.getTime() < endDate.getTime()){
				var unitEnd = row.endOfUnitWithStartDateClipAtDate(unitStart, this.ruler.endDate);
				var unitStartX = this.ruler.dateToPos(unitStart);
				var unitEndX = this.ruler.dateToPos(unitEnd);
				var text = table.rulerRow.descriptionAtDateWithLocale(unitStart, null);
				width = unitEndX - unitStartX;
				if(width<0)
					width = 0;				
				var key = unitStartX;
				if(!table.cells[key]){
					table.cells[key] = 1;
                    var highlight = highlightStatusDate && statusDateTime && statusDateTime >= unitStart.getTime() && statusDateTime <= unitEnd.getTime()
					this.drawCell(table, unitStartX, width, height, text, highlight);
				}
				unitStart = unitEnd;
				var time = unitStart.getTime();
				if(time == lastStartTime)
					break;
				lastStartTime = time;
			}	
		}
	},
	
	drawRulerFromStartDateToEndDate: function(startDate, endDate)
	{
		this.rulerRowsHeight = this.ruler.rows.length * this.rulerRowHeight();
		for(var i=0; i<this.tables.length; i++)
			this.drawTableContentFromStartDateToEndDate(this.tables[i], startDate, endDate, i==0);
	},

	visibleWidthChanged: function()
	{
		var newStart = this.ganttScrollView.visibleStartDate;
		var newEnd = this.ganttScrollView.visibleEndDate;
		if(!this.visibleStartDate || !this.visibleEndDate || !this.visibleStartDate.isEqual(newStart) || !this.visibleEndDate.isEqual(newEnd)){
			this.drawRulerFromStartDateToEndDate(newStart, newEnd);
			this.visibleStartDate = newStart;
			this.visibleEndDate = newEnd;
		}
	},

	setRuler: function(newRuler)
	{
		ruler = newRuler;
		if(ruler)
			this.refreshRuler();
	},

	rulerRowHeight: function() 
	{
		return 18;
	},

	heightOfAllRows: function()
	{
		return this.rulerRowsHeight;
	},
	
	refreshBackground: function()
	{
		if(this.gradientContainer)
			this.gradientContainer.style.width = Math.max(this.width(), this.container.cachedOffsetWidth) + "px";	
	},

	width: function()
	{
		return this.tableWidth;
	}
});