// Copyright 2008 ProjectWizards GmbH. All rights reserved.

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

var disableGantt = true;

var GanttScrollView = Base.extend({
	constructor: function(ganttContainer, outlineView, enableInteraction, ruler, environment, delegate){
		this.delegate = delegate;
		this.environment = environment;
		this.ruler = ruler;
		this.gantt = ganttContainer;
		this.outlineView = outlineView;
		this.drawShadows = !this.outlineView.showPrintView;
		this.enableInteraction = enableInteraction;
		this.dependendView = outlineView;
		this.hasFillView = false;	
		this.createGanttAndRulerView();
		var me = this;
		setTimeout(function(){
			me.containerSizeChanged();
		}, 200);
	},
	
	project: function(){
		if(this.outlineView.rows && this.outlineView.rows.length)
			return this.outlineView.rows[0];
		return null;
	},

	setStyleList: function(styleList) {
		this.styleList = styleList;
		if(this.ganttView)
			this.ganttView.setStyleList(styleList);
	},
	
	setViewOptions: function(viewOptions) {
		this.viewOptions = viewOptions;
		if(this.ganttView)
			this.ganttView.setViewOptions(viewOptions);
	},

	hasVerticalScrollbar: function() {
		var ganttHeight = this.ganttView.height();
		var containerHeight = this.ganttContainer.cachedOffsetHeight;	// Hot Spot
		var isOverflowYHidden = this.ganttContainer.style.overflowY != "hidden";
		var result = (ganttHeight > containerHeight) && isOverflowYHidden;
		return result;
	},

	hasHorizontalScrollbar: function() {
		return (this.ganttRulerView.width() > this.ganttContainer.cachedOffsetWidth);
	},

	rulerDescription: function() {
		return this.ruler.rulerDescription();;
	},

	rulerHeight: function()	{
		var result = this.ganttRulerView.heightOfAllRows();
		return result;
	},

	createGanttAndRulerView: function()	{
		// Ruler

		this.rulerContainer = $(document.createElement("div"));
		this.rulerContainer.className = "rulerContainer"
		this.rulerContainer.style.overflow = "hidden";
		this.rulerContainer.style.top = "0px";
		this.rulerContainer.style.left = "0px";
		this.rulerContainer.style.right= "0px";
		this.rulerContainer.style.position = "absolute";
		this.gantt.appendChild(this.rulerContainer);	
		this.ganttRulerView = new GanttRulerView(this, this.rulerContainer, this.ruler, this.environment);
		this.ganttRulerView.inPrintWindow = !this.enableInteraction;
		
		// Gantt
		
		this.ganttContainer = $(document.createElement("div"));
		this.ganttContainer.className = "ganttContainer";
		this.ganttContainer.id = "GanttContainer";
		this.ganttContainer.style.top = this.rulerHeight()+"px";
		this.ganttContainer.style.left = "0px";
		this.ganttContainer.style.right= "0px";
		this.ganttContainer.style.bottom= "0px";
		this.ganttContainer.style.position = "absolute";
		this.ganttContainer.cachedScrollLeft = 0;		

		PWUtils.setSelectionEnabled(this.rulerContainer, false);
		PWUtils.setSelectionEnabled(this.ganttContainer, false);

		this.gantt.appendChild(this.ganttContainer);


		if(1){	// Enable GanttView
			if (navigator.appVersion.match(/\bMSIE\b/)){	// IE
				this.ganttView = new GanttVMLView(this, this.ganttContainer, this.ruler, this.environment, this.drawShadows);
			}else
				this.ganttView = new GanttSVGView(this, this.ganttContainer, this.ruler, this.environment, this.drawShadows);
		}
        
//		this.gantt.appendChild(this.ganttContainer);    //huhu
        
            
                	
		this.setEnableScrollBars(true)
		if(this.enableInteraction) {
			this.doscrollEventListener = this.doscroll.bindAsEventListener(this);
			PWUtils.observe(this.ganttContainer, "scroll", this.doscrollEventListener, true);		
		}
		this.outlineView.setDependendView(this);		
		
		this.outlineView.setHeaderHeight(this.rulerHeight());
		if(this.ganttView)
			this.ganttView.blockRefresh = 1;
		this.containerSizeChanged();	
		this.rulerChanged();
		this.outlineViewChanged();
		if(this.ganttView)
			this.ganttView.blockRefresh = 0;
		
		if(this.enableInteraction) {
			this.domousewheelEventListener = this.domousewheel.bindAsEventListener(this);
			PWUtils.observe(this.ganttContainer, "mousewheel", this.domousewheelEventListener, true);// IE + Safari
			PWUtils.observe(this.ganttContainer, "DOMMouseScroll", this.domousewheelEventListener, true);// FF
		}		
	},

	setEnableScrollBars: function(flag) {
		this.ganttContainer.style.overflowY = flag ? "auto" : "hidden";	
		this.ganttContainer.style.overflowX = flag ? "scroll" : "hidden";
		if(!flag)
			this.removeRulerFillView();
	},

	setDependendView: function(dependentContainer) {
		this.dependendView = dependentContainer;
	},

	setScrollTop: function(scrollTop) {
		if(this.ganttContainer.scrollTop != scrollTop)
			this.ganttContainer.scrollTop = scrollTop;	

	},

	calculateVisibleWidth: function() {
		var hdiff = this.ganttContainer.cachedOffsetWidth;
		var scrollLeft = this.ganttContainer.cachedScrollLeft;		
		this.visibleStartDate = this.ruler.posToDate(scrollLeft-hdiff);
		this.visibleEndDate = this.ruler.posToDate(scrollLeft+hdiff);
		this.ganttRulerView.visibleWidthChanged();			
		if(this.delegate)
			this.delegate.visibleGanttChanged(this.visibleStartDate, this.visibleEndDate);
	},

	visibleRows: function(expansionPixels) {
		return this.outlineView.visibleRows(expansionPixels);
	},
	
	doscroll: function(event) {	
		event = event || window.event;
		this.ganttContainer.cachedScrollLeft = this.ganttContainer.scrollLeft;
		if(this.rulerContainer.scrollLeft!=this.ganttContainer.cachedScrollLeft)
			this.rulerContainer.scrollLeft=this.ganttContainer.cachedScrollLeft;
		if(this.dependendView)
			this.dependendView.setScrollTop(this.ganttContainer.scrollTop);
		Event.stop(event);
		this.calculateVisibleWidth();
	},

	domousewheel: function(event) {	
		var delta = mouseWheelDeltaFromEvent(event);
		this.ganttContainer.scrollTop -= delta.y;
		this.ganttContainer.scrollLeft -= delta.x;
		this.doscroll(event);
		return;
	},

	addRulerFillView: function() {
		if(!this.outlineView.showPrintView) {	
			if(!this.rulerFillView) {
				if(!this.outlineView.showPrintView)
					this.rulerContainer.style.right = PWUtils.scrollbarWidth()+"px";		
				this.rulerFillView = $(document.createElement("div"));
				this.rulerFillView.className = "headerFillView";
				this.rulerFillView.style.position = "absolute";
				this.rulerFillView.style.top = "0px";
				this.rulerFillView.style.right = "0px";
				this.rulerFillView.style.height = parseInt(this.rulerContainer.style.height)+"px";
				this.rulerFillView.style.width = PWUtils.scrollbarWidth()-1+"px";
				if(this.enableInteraction)	
					PWUtils.drawHeaderGradient(this.rulerFillView, this.environment.images.grayGradient);
				this.gantt.appendChild(this.rulerFillView);
			}
		}
	},

	removeRulerFillView: function()	{
		if(!this.outlineView.showPrintView) {	
			if(this.rulerFillView) {
				this.gantt.removeChild(this.rulerFillView);
				this.rulerFillView = null;
				this.rulerContainer.style.right = "0px";	
			}
		}
	},

	zoomIn: function() {
		this.ganttView.initialRows = this.outlineView.visibleRows();
		this.ruler.zoomIn();
		this.ganttRulerView.refreshRuler();
		this.containerSizeChanged();		
	},

	zoomOut: function()	{
		this.ganttView.initialRows = this.outlineView.visibleRows();
		this.ruler.zoomOut();
		this.ganttRulerView.refreshRuler();
		this.containerSizeChanged();
	},

	rulerChanged: function() {
		if(this.ganttRulerView)	{
			this.rulerContainer.style.height = this.rulerHeight()+"px";
			var rulerWidth = this.ganttRulerView.width();
			if(this.ganttView)
				this.ganttView.setWidth(rulerWidth);
			this.calculateVisibleWidth();
			PWUtils.notificationCenter.sendMessage("rulerChanged", this.ruler);
		}
	},

	outlineViewChanged: function(rows) {
		if(this.ganttView) {
			this.ganttView.initialRows = rows;
			var outlineHeight = this.outlineView.height();
			var ganttViewHeight = this.ganttView.height();
			this.ganttView.setHeight(outlineHeight);
			this.calculateVisibleWidth();
		}			
		this.ganttRulerView.refreshBackground();
	},

	outlineRows: function() {
		return this.outlineView.rows();
	},


	refreshRulerFillView: function() {
		if(this.ganttView) {
			var ganttHeight = this.ganttView.height();
			var containerHeight = this.ganttContainer.cachedOffsetHeight - PWUtils.scrollbarWidth();	
			if(ganttHeight > containerHeight)
				this.addRulerFillView();
			else
				this.removeRulerFillView();					
			this.ganttRulerView.refreshBackground();	
		}
	},

	containerSizeChanged: function() {
		this.ganttContainer.cachedOffsetHeight = this.ganttContainer.offsetHeight ? this.ganttContainer.offsetHeight : 1000;
		this.ganttContainer.cachedOffsetWidth = this.ganttContainer.offsetWidth;
		this.rulerContainer.cachedOffsetWidth = this.rulerContainer.offsetWidth;
		this.calculateVisibleWidth();
		if(this.ganttView)
			this.ganttView.rulerWidthChanged(this.ganttRulerView.width());	
		this.refreshRulerFillView();
	},

	fullHeight: function() {
		return this.ganttView.height() + this.ganttRulerView.heightOfAllRows();;
	},

	width: function() {
		return this.ganttRulerView.width();
	},
	
	refreshSelection: function(objects) {
		this.ganttView.refreshSelection(objects);
	},

	setBlockRefresh: function(flag) {
		if(this.ganttView)
			this.ganttView.blockRefresh = flag;
	},

	refresh: function() {
		this.outlineViewChanged();		
	},
	
	handleClickInRowAtPosition: function(event, y) {
		var rows = this.outlineView.rows();
		var row;
		for(var i=0; i<rows.length; i++){
			row = rows[i];
			if(row && y >= row.cachedOffsetTop && y <= row.cachedOffsetTop+row.heightInPixel){
				this.outlineView.handleClickInRow(event, row);	
				break;
			}
		}	
	},
	
	handleClickInRow: function(event, row) {
		this.outlineView.handleClickInRow(event, row);
	},

	outlineDidCreateRows: function(outline, rows) {
		this.ganttView.outlineDidCreateRows(outline, rows);
	},
	
	outlineRowsBecameVisible: function(outline, rows) {
		this.ganttView.outlineRowsBecameVisible(outline, rows);
	}

	
});