/* autoheight.js

	Purpose:
		
	Description:
		
	History:
		Mar 18, 2015 Created by JerryChen

Copyright (C) 2015 Potix Corporation. All Rights Reserved.
*/
//ZSS-958
(function () {
	var zssautoheightCell$overrode = {},
		zssautoheightRow$overrode = {},
		zssautoheightSSheetCtrl$overrode = {};
	
	zk.override(zss.Cell.prototype, zssautoheightCell$overrode, {
		_getTextHeight0: function () {
			var cell = jq(this.$n()),
				rotate = this.rotate,
				textNode = this.getTextNode();
			
			//ZSS-1151: optimize
			if (textNode) {
				if (!textNode.textContent || textNode.textContent.trim().length == 0) {
					return 0;
				}
			}
			
			var	real = jq(textNode.cloneNode(true)).css({
						'position': 'absolute',
						'left': '-10000px',
						'top': '-10000px',
						'height': '',
						'max-height': '999999px',
						'width': textNode.offsetWidth
					}).appendTo(this.cave),
				height = (rotate == 90 || rotate == -90) ? this._textWidth() : real.height(); //ZSS-1020

			//ZSS-1151: IE9 seems add the offset into height for an empty text
			if (zk.ie && zk.ie < 10 && height >= 10000) {
				height -= 10000; //because we set left:-10000, top:-10000
			}
			
			real.remove();
			return height;
		},
		_saveNoneDefaultHeightCell: function () {
			var custRowHeight = this.sheet.custRowHeight,
				row = this.r;
			if (!custRowHeight.isDefaultSize(row) && !custRowHeight.isCustomSize(row)) {
				this.parent.addAutoHeightCell(this);
			}
		}
	});
	
	zk.override(zss.Row.prototype, zssautoheightRow$overrode, {
		$init: function	(evt) {
			zssautoheightRow$overrode.$init.apply(this, arguments);
			this.autoHeightCells = [];
		},
		updateRowAutoHeight: function () {
			if (this.sheet.custRowHeight.isCustomSize(this.r)) {
				return;
			}

			var autoHeight = this.sheet.custRowHeight.getDefaultSize(),
				autoHeightCells = this.autoHeightCells;
			
			if (autoHeightCells.length > 0) {
				var largestAutoHeight = this._largestAutoHeight || -1;
				
				if(largestAutoHeight == -1) {
					//enlarge height upon largest text height among wrapped cells
					var i = autoHeightCells.length;
					while (i--) {
						if (autoHeightCells[i]) {
							autoHeight = Math.max(autoHeight,  autoHeightCells[i].getTextHeight());
						}
					}
					
					this._largestAutoHeight = autoHeight;
				} else {
					// use cache
					autoHeight = Math.max(autoHeight, largestAutoHeight);
				}
			}
			
			if (jq(this.$n()).height() == autoHeight)
				return;//equals to current row height, no need to change
			var CUSTOM = false;
			this.sheet._setRowHeight(this.r, autoHeight, true, false, false, this.zsh, CUSTOM);
		},
		updateAutoHeightDirty: function (oldCellHeight, newCellHeight) {
			var autoHeight = this._largestAutoHeight || this.sheet.custRowHeight.getDefaultSize();
			if(newCellHeight > autoHeight)
				this._largestAutoHeight = newCellHeight;
			else if(oldCellHeight == autoHeight) /* could be the previous highest one */
				this._largestAutoHeight = -1; // -1 means dirty
		},
		processCellAutoHeight: function (cell) {
			if (!this.sheet.custRowHeight.isCustomSize(this.r)) {
				this.addAutoHeightCell(cell);
				this.sheet.triggerAutoHeight(this.r);
			}
		},
		addAutoHeightCell: function (cell) {
			var autoHeightCells = this.autoHeightCells;
			if (!autoHeightCells.$contains(cell))
				autoHeightCells.push(cell);
		},
		removeAutoHeightCell: function (cell) {
			var autoHeightCells = this.autoHeightCells;
			if (autoHeightCells.$contains(cell))
				autoHeightCells.$remove(cell);
		}
	});
	
	zk.override(zss.SSheetCtrl.prototype, zssautoheightSSheetCtrl$overrode, {
		fireProcessAutoHeight_: function() {
			if (this._wgt.isSheetCSSReady()){
				var r = this._autoHeightRange,
					block = this.activeBlock, //ZSS-1116
					ar = block.range; //ZSS-1116
				if (r != undefined) {
					var custRowHeight = this.custRowHeight,
						top = Math.max(r.tRow, ar.top),  //ZSS-1116
						bottom = Math.min(r.bRow, ar.bottom); //ZSS-1116
					for(var i = top; i <= bottom; i++) {
						if(!custRowHeight.isHidden.apply(custRowHeight, [i])) {
							var row = block.getRow(i);
							row.updateRowAutoHeight.apply(row);
						}
					}
					delete this._autoHeightRange;
				}
			}else{
				var self = this; 
				setTimeout(function(){
					self.fireProcessAutoHeight_();
					},25);
			}
		},
		triggerAutoHeight: function (row) {
			var r = this._autoHeightRange = this._autoHeightRange || {};
			typeof r.tRow == 'undefined' ? r.tRow = row : r.tRow = Math.min(r.tRow, row);
			typeof r.bRow == 'undefined' ? r.bRow = row : r.bRow = Math.max(r.bRow, row);
		},
	});
	
})();
