/* Btn.js

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Sat May 7, 2011 11:31:29 AM , Created by henrichen
}}IS_NOTE

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

/**
 * AutoFilter buttons.
 */
zssex.Btn = zk.$extends(zul.wgt.Div, {
	$init: function (sht, r, c, parent, cls, field, btntype, left) {
		this.$supers(zssex.Btn, '$init', []); //DO NOT pass "arguments" or all fields in sht will be copied into this Object. 
		this.sheet = sht;
		this.loffset = left ? sht.custColWidth.getStartPixel(left) : 0; //ZSS-954: dataPanel's left column might not start from 0
		this.row = r;
		this.col = c;
		this.field = field;
		this.btntype = btntype;
		this.uuid = zk.Widget.nextUuid();
		var ctmp = '<div id="'+this.uuid+'" zs.t="SBtn" class="zsbtn '+ cls +'"></div>';
		this._zclass = 'zsbtn';
		this._sclass = cls;
		jq(parent).append(ctmp); //attach to dom
		this.replaceHTML(ctmp); //force binding
		this.relocate(r,c);
	},
	cleanup: function () {
		var elm = this.$n();
		this.unbind();
		if (elm)
			jq(elm).remove();
	},
	doTooltipOver_: function() {
		//unknown
	},
	doTooltipOut_: function() {
		//unknown
	},
	doClick_: function (evt) {
		//do nothing. eat the event.
	},
	doMouseDown_: function (evt) {
		if ((evt.which) && (evt.which == 1)) //work only if a left mouse down
			this.sheet._doBtndown(evt, this.btntype, evt.domTarget, this); //auto filter
	},
	doMouseUp_: function (evt) {
		this.sheet._doMouseup(evt);
	},
	doRightClick_: function (evt) {
		this.sheet._doMouserightclick(evt);
	},
	doDoubleClick_: function (evt) {
		this.sheet._doMousedblclick(evt);
	},
	doKeyDown_: function (evt) {
		this.sheet.doKeyDown_(evt);
	},
	afterKeyDown_: function (evt) {
		this.sheet._wgt.afterKeyDown_(evt);
	},
	doKeyPress_: function (evt) {
		this.sheet._doKeypress(evt);
	},
	relocate: function (row, col) {
		var sht = this.sheet;
		this.row = row;
		this.col = col;
		
		//ZSS-805
		var colR = col;
		var cell = sht.getCell(row, col);
		if (cell && cell.merr && cell.merr > colR) {
			colR = cell.merr;
		}
		
		var custColWidth = sht.custColWidth,
			custRowHeight = sht.custRowHeight,
			l = custColWidth.getStartPixel(col),
			t = custRowHeight.getStartPixel(row),
			w = custColWidth.getStartPixel(colR + 1) - l,
			h = custRowHeight.getStartPixel(row + 1) - t,
			imgw = 17,
			imgh = 17,
			imgwp = imgw,
			imghp = imgh;
		if (w < imgw) {
			imgw = w;
			imghp = w;
		}
		if (h < imgh) {
			imgh = h;
			imgwp = h;
		}
		if (imghp < imgh) {
			imgh = imghp;
		}
		if (imgwp < imgw) {
			imgw = imgwp;
		}
		//'af' button is at left side while 'dv' button is at right side of right edge
		//ZSS-954, left must minus the offset because column might not start from 0
		this.relocate_(l - this.loffset + w - (this.btntype == 'af' ? imgw : -1), t + h - imgh, imgw, imgh);  
	},
	/**
	 * Sets the button poisition
	 * @param int left position
	 * @param int top position
	 * @param int width
	 * @param int height
	 */
	relocate_: function(l, t, w, h) {
		this.imgwidth = w;
		this.imgheight = h;
		this.imgleft = l - 1;
		this.imgtop = t - 2;
		jq(this.$n()).css({'width': jq.px0(w), 'height': jq.px0(h), 'left': jq.px(this.imgleft)});
		if (zk.ie6_) //ie6 only. Have to setup the top, too
			jq(this.$n()).css({'top': jq.px(this.imgtop)});
	}
});
