/* overflow.js

	Purpose:
		
	Description:
		
	History:
		Aug 9, 2013 Created by Pao Wang

Copyright (C) 2013 Potix Corporation. All Rights Reserved.
*/

(function () {
	// constant
	var NUM_CELL = 0;
	var STR_CELL = 1;
	var FORMULA_CELL = 2;
	var BLANK_CELL = 3;
	var BOOLEAN_CELL = 4;
	var ERROR_CELL = 5;
	
	// backup before overrode
	var zssoverflow$overrode = {};
	
	zk.override(zss.Cell.prototype, zssoverflow$overrode, {
		
		_processOverflow: function() {
			//ZSS-944: 90 degree text will not do overflow
			var rotate90 = this.rotate == 90 || this.rotate == -90; //ZSS-1020
			if (rotate90) return;
			
			var col = this.c;
			var rBorder = this.rborder;
			var $n = jq(this.$n());
			
			var cave = this.$n('cave');
			var $cave = jq(cave);
			var tn = this.getTextNode();
			var $tn = jq(tn);
			
			// clean pervious width setting for measuring correctly
			// Note. For IE6/IE7 need to set cave's width (set text node width won't work)
			if (zk.ie6_ || zk.ie7_) {
				$cave.css('width', '');
			} else {
				$tn.css('width', '');
			}
			
			var sheet = this.sheet;
			var custColWidth = sheet.custColWidth;
			var wd = custColWidth.getSize(col); // overlap-able width, initial to current cell's width
			var notxtwd = (this._txtwd == undefined || this._txtwd < 0);  //ZSS-1171
			var sw = notxtwd ? tn.scrollWidth : this._txtwd; // heavy duty (it's actually a function call from ZK widget), ZSS-1171
			var cellPad = sheet.cellPad;
			
			//ZSS-1171
			if (notxtwd) {
				this._txtwd = sw;
				sheet._sendOnTextWidth(this.r, col, sw);
			}
			
			// skip to process overflow if current cell's width is enough
			// right grid line won't be removed if it's css has no zscell-overflow class
			if(sw <= custColWidth.getSize(this.c) - cellPad) {
				$n.removeClass("zscell-overflow");
				$n.removeClass("zscell-overflow-b");
				return;
			}
			
			// process overflow
			// count right sibling's width until width is enough or sibling isn't blank
			var cell = this.nextSibling;
			while(cell && cell.cellType === BLANK_CELL && wd < sw) {
				wd += custColWidth.getSize(cell.c);
				cell = cell.nextSibling;
			}
			wd -= cellPad; // cell padding
			
			// apply overflow CSS and adjust cell width
			$n.removeClass(rBorder ? "zscell-overflow" : "zscell-overflow-b")
				.addClass(rBorder ? "zscell-overflow-b" : "zscell-overflow");
			// Note. For IE6/IE7 need to set cave's width (set text node width won't work)
			if (zk.ie6_ || zk.ie7_) {
				$cave.css('width', jq.px0(wd));
			} else {
				$tn.css('width', jq.px0(wd));
			}
		}
	});
	
})(); // end of topmost function
