assert(web.layout)
namespace web.layout.event;

GROUPS = {
	/*intellisense(web.layout.event.GROUPS)*/ 
	HANDLE_INITIALIZATION = 0x0000;
	HANDLE_MOUSE = 0x0001;
	HANDLE_KEY = 0x0002;
	HANDLE_FOCUS = 0x0004;
	HANDLE_SCROLL = 0x0008;
	HANDLE_TIMER = 0x0010;
	HANDLE_SIZE = 0x0020;
	HANDLE_DRAW = 0x0040;
	HANDLE_DATA_ARRIVED = 0x080;
	HANDLE_BEHAVIOR_EVENT = 0x0100;
	HANDLE_METHOD_CALL = 0x0200;
	HANDLE_EXCHANGE = 0x1000;
	HANDLE_GESTURE = 0x2000;
	HANDLE_ALL = 0xFFFF;
	DISABLE_INITIALIZATION = 0x80000000;
	/*end intellisense*/
}

namespace INITIALIZATION {
	BEHAVIOR_DETACH = 0;
	BEHAVIOR_ATTACH = 1;
	mapProcName = {
		[BEHAVIOR_DETACH] = "onBehaviorDetach";
		[BEHAVIOR_ATTACH] = "onBehaviorAttach"; 
	}
}
	
namespace MOUSE {
	/*intellisense(web.layout.event.MOUSE)*/ 
	MOUSE_ENTER = 0;
	MOUSE_LEAVE = 1;
	MOUSE_MOVE  = 2;
	MOUSE_UP    = 3;
	MOUSE_DOWN  = 4;
	MOUSE_DCLICK = 5;
	MOUSE_WHEEL = 6; 
	MOUSE_TICK  = 7;
	MOUSE_IDLE  = 8;
	DROP        = 9;
	DRAG_ENTER  = 0xA;
	DRAG_LEAVE  = 0xB;
	DRAG_REQUEST = 0xC;
	MOUSE_CLICK = 0xFF;
	DRAGGING = 0x100;
	/*end intellisense*/
	
	mapProcName = {
		[MOUSE_ENTER] = "onMouseEnter";
		[MOUSE_LEAVE] = "onMouseLeave";
		[MOUSE_MOVE] = "onMouseMove";
		[MOUSE_UP] = "onMouseUp";
		[MOUSE_DOWN] = "onMouseDown";
		[MOUSE_DCLICK] = "onMouseDblClick";
		[MOUSE_WHEEL] = "onMouseWheel";
		[MOUSE_TICK] = "onMouseTick";
		[MOUSE_IDLE] = "onMouseIdle";
		[DROP] = "onDrop";
		[DRAG_ENTER] = "onDragEnter";
		[DRAG_LEAVE] = "onDragLeave";
		[DRAG_REQUEST] = "onDragRequest";
		[MOUSE_CLICK] = "onMouseClick";
		[DRAGGING] = "onDragging";
	}
}

namespace KEY {
	KEY_DOWN = 0;
	KEY_UP = 1;
	KEY_CHAR = 2;
	mapProcName = {
		[KEY_DOWN] = "onKeyDown";
		[KEY_UP] = "onKeyUp";
		[KEY_CHAR] = "onKeyChar";
	}
}
 
namespace FOCUS {
	FOCUS_LOST = 0;
	FOCUS_GOT = 1;
	mapProcName = {
		[FOCUS_LOST] = "onFocusLost";
		[FOCUS_GOT] = "onFocusGot"; 
	}
}

namespace SCROLL {
	SCROLL_HOME = 0;
	SCROLL_END = 1;
	SCROLL_STEP_PLUS = 2;
	SCROLL_STEP_MINUS = 3;
	SCROLL_PAGE_PLUS = 4;
	SCROLL_PAGE_MINUS = 5;
	SCROLL_POS = 6;
	SCROLL_SLIDER_RELEASED = 7;
	mapProcName = {
		[SCROLL_HOME] = "onScrollHome";
		[SCROLL_END] = "onScrollEnd";
		[SCROLL_STEP_PLUS] = "onScrollStepPlus";
		[SCROLL_STEP_MINUS] = "onScrollStepMinus";
		[SCROLL_PAGE_PLUS] = "onScrollPagePlus";
		[SCROLL_PAGE_MINUS] = "onScrollPageMinus";
		[SCROLL_POS] = "onScrollPos";
		[SCROLL_SLIDER_RELEASED] = "onSliderRelease";
	}
}

namespace BEHAVIOR{  
	/*intellisense(web.layout.event.BEHAVIOR)*/ 
	BUTTON_CLICK = 0;
	BUTTON_PRESS = 1;
	BUTTON_STATE_CHANGED = 2;
	EDIT_VALUE_CHANGING = 3;
	EDIT_VALUE_CHANGED = 4;
	SELECT_SELECTION_CHANGED = 5;
	SELECT_STATE_CHANGED = 6;
	POPUP_REQUEST   = 7;
	POPUP_READY     = 8;
	POPUP_DISMISSED = 9;
	MENU_ITEM_ACTIVE = 0xA;
	MENU_ITEM_CLICK = 0xB;
	CONTEXT_MENU_SETUP   = 0xF;
	CONTEXT_MENU_REQUEST = 0x10;
	VISIUAL_STATUS_CHANGED = 0x11;
	DISABLED_STATUS_CHANGED = 0x12;
	POPUP_DISMISSING = 0x13;
	HYPERLINK_CLICK = 0x80;
	TABLE_HEADER_CLICK = 0x81;
	TABLE_ROW_CLICK = 0x82;
	TABLE_ROW_DBL_CLICK = 0x83;
	ELEMENT_COLLAPSED = 0x90;
	ELEMENT_EXPANDED = 0x91;
	ACTIVATE_CHILD = 0x92;
	DO_SWITCH_TAB = 0x93;
	ACTIVATE_CHILD = 0x93;
	INIT_DATA_VIEW = 0x94;
	ROWS_DATA_REQUEST = 0x95;
	UI_STATE_CHANGED = 0x96;
	FORM_SUBMIT = 0x97;
	FORM_RESET = 0x98;
	DOCUMENT_COMPLETE = 0x99;
	HISTORY_PUSH = 0x9A;
	HISTORY_DROP = 0x9B;
	HISTORY_PRIOR = 0x9C;
	HISTORY_NEXT = 0x9D;
	HISTORY_STATE_CHANGED = 0x9E;
	CLOSE_POPUP = 0x9F;
	REQUEST_TOOLTIP = 0xA0;
	ANIMATION = 0xA0;
	FIRST_APPLICATION_EVENT_CODE = 0x100;
	/*end intellisense*/
	
	mapProcName = {  
		[BUTTON_CLICK] = "onButtonClick";
		[BUTTON_PRESS] = "onButtonPress";
		[BUTTON_STATE_CHANGED] = "onButtonStateChanged";
		[EDIT_VALUE_CHANGING] = "onEditValueChanging";
		[EDIT_VALUE_CHANGED] = "onEditValueChanged";
		[SELECT_SELECTION_CHANGED] = "onSelectSelectionChanged";
		[SELECT_STATE_CHANGED] = "onSelectStateChanged";
		[POPUP_REQUEST] = "onPopupRequest";
		[POPUP_READY] = "onPopupReady";
		[POPUP_DISMISSED] = "onPopupDismissed";
		[MENU_ITEM_ACTIVE] = "onMenuItemActive";
		[MENU_ITEM_CLICK] = "onMenuItemClick";
		[CONTEXT_MENU_SETUP] = "onContextMenuSetup";
		[CONTEXT_MENU_REQUEST] = "onContextMenuRequest";
		[VISIUAL_STATUS_CHANGED] = "onVisualStateChanged";
		[DISABLED_STATUS_CHANGED] = "onDisabledStateChanged";
		[POPUP_DISMISSING] = "onPopupDismissing";
		[HYPERLINK_CLICK] = "onHyperlinkClick";
		[TABLE_HEADER_CLICK] = "onTableHeaderClick";
		[TABLE_ROW_CLICK] = "onTableRowClick";
		[TABLE_ROW_DBL_CLICK] = "onTableRowDblClick";
		[ELEMENT_COLLAPSED] = "onElementCollapsed";
		[ELEMENT_EXPANDED] = "onElementExpanded";
		[ACTIVATE_CHILD] = "onActiveChild";
		[INIT_DATA_VIEW] = "onInitDataView";
		[ROWS_DATA_REQUEST] = "onRowsDataRequest";
		[UI_STATE_CHANGED] = "onUIStateChanged";
		[FORM_SUBMIT] = "onFormSubmit";
		[FORM_RESET] = "onFormReset";
		[DOCUMENT_COMPLETE] = "onDocumentComplete";
		[CLOSE_POPUP] = "onClosePopup";
		[REQUEST_TOOLTIP] = "onRequestTooltip";
		[ANIMATION] = "onAnimation"; 
	} 
}

INITIALIZATION_PARAMS = class {
	INT cmd;
}

MOUSE_PARAMS = class {
	INT cmd;
	pointer target;
	struct pos = ::POINT();
	struct pos_document = ::POINT();
	INT button_state;
	INT alt_state;
	INT cursor_type;
	bool is_on_icon;
	int dragging;
	INT dragging_mode;
}

KEY_PARAMS = class {
	INT cmd;
	pointer target;
	INT key_code;
	INT alt_state;
}

FOCUS_PARAMS = class {
	INT cmd;
	pointer target;
	bool by_mouse_click;
	bool cancel;
}

SCROLL_PARAMS = class {
	INT cmd;
	pointer target;
	int pos;
	bool vertical;
}

BEHAVIOR_EVENT_PARAMS = class {
	INT cmd;
	pointer heTarget;
	pointer he;
	INT reason;
	pointer data;
} 

var raw_convert = ..raw.convert;
var element = ..web.layout.element;

var callEvent = function(this,handler,he,...){
	var t = type(handler) 
	if( t == "function" ){  
		var ele = element(he);
		invoke(handler,this,ele,... );
	} 
	elseif( t == "table" ){   
		var ele = element(he);
		var id = ele.id || ele.name;
		var func = id ? handler[id];
		if( func ){
			invoke(func,handler,ele,... );
		}
	}
} 
_defaultHandler = {
	[ GROUPS.HANDLE_INITIALIZATION ] = function(this,tag, he, evtg, prms){
		var initParams = raw_convert(prms, INITIALIZATION_PARAMS());
		var funcName = INITIALIZATION.mapProcName[initParams.cmd]  
		var handler = this[funcName];
		if( handler ){
			invoke(handler,this,initParams );
		} 
	}
	[ GROUPS.HANDLE_MOUSE ] = function(this,tag, he, evtg, prms){
		var mouseParams = raw_convert(prms, MOUSE_PARAMS()); 
		var funcName = MOUSE.mapProcName[mouseParams.cmd] 
		var handler = this[funcName]; 
		if(handler) callEvent( this,handler,mouseParams.target,mouseParams.pos.x,mouseParams.pos.y, mouseParams); 
	 
	}  
	[ GROUPS.HANDLE_KEY ] = function(this,tag, he, evtg, prms){
		var keyParams = raw_convert(prms, KEY_PARAMS()); 
		var funcName = KEY.mapProcName[keyParams.cmd] 
		var handler = this[funcName];
		if(handler) callEvent( this,handler,keyParams.target,keyParams.key_code,keyParams.alt_state, keyParams);  
	}  
	[ GROUPS.HANDLE_FOCUS ] = function(this,tag, he, evtg, prms){
		var focusParams = raw_convert(prms, FOCUS_PARAMS()); 
		var funcName = FOCUS.mapProcName[focusParams.cmd] 
		var handler = this[funcName];
		if(handler) callEvent( this,handler,focusParams.target,focusParams ); 
	} 
	[ GROUPS.HANDLE_SCROLL ] = function(this,tag, he, evtg, prms){
		var scrollParams = raw_convert(prms, SCROLL_PARAMS()); 
		var pos = scrollParams.pos; 
		var funcName = SCROLL.mapProcName[scrollParams.cmd]  
		var handler = this[funcName];
		if(handler) callEvent( this,handler,scrollParams.target,pos,scrollParams );
		
	} 
	[ GROUPS.HANDLE_BEHAVIOR_EVENT ] = function(this,tag, he, evtg, prms){ 
		var behaviorParams = raw_convert(prms, BEHAVIOR_EVENT_PARAMS()); 
		var funcName = BEHAVIOR.mapProcName[behaviorParams.cmd]; 
		var handler = this[funcName];
		if(handler) callEvent( this,handler,behaviorParams.heTarget,reason,behaviorParams );
	} 
} 
 
_handler = function (tag, he, evtg, prms) { 
	  
	if (owner.domEventsHandler) {
		var re = owner.domEventsHandler(tag, he, evtg, prms);
		if (re !== null) return re;
	}
	
	var handle = _defaultHandler[evtg] 
	if ( handle) { 
		handle(owner,tag, he, evtg, prms );
	}; 
	return 0;
}
 
/**intellisense()
!weblayout.onBehaviorDetach = @.onBehaviorDetach = function (initParams) {\n	__\n}
!weblayout.onBehaviorAttach = @.onBehaviorAttach = function (initParams) {\n	__\n}

!weblayout.onMouseEnter = @.onMouseEnter = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseLeave = @.onMouseLeave = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseMove = @.onMouseMove = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseUp = @.onMouseUp = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseDown = @.onMouseDown = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseDblClick = @.onMouseDblClick = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseWheel = @.onMouseWheel = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseTick = @.onMouseTick = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseIdle = @.onMouseIdle = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onDrop = @.onDrop = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onDragEnter = @.onDragEnter = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onDragLeave = @.onDragLeave = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onDragRequest = @.onDragRequest = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onMouseClick = @.onMouseClick = function (layoutEle, x,y,mouseParams) {\n	__\n}
!weblayout.onDragging = @.onDragging = function (layoutEle, x,y,mouseParams) {\n	__\n}

!weblayout.onKeyDown = @.onKeyDown = function (layoutEle, keyCode, altState, keyParams) {\n	__\n}
!weblayout.onKeyUp = @.onKeyUp = function (layoutEle, keyCode, altState, keyParams) {\n	__\n}
!weblayout.onKeyChar = @.onKeyChar = function (layoutEle, keyCode, altState, keyParams) {\n	__\n}

!weblayout.onFocusLost = @.onFocusLost = function (layoutEle,focusParams) {\n	__\n}
!weblayout.onFocusGot = @.onFocusGot = function (layoutEle,focusParams) {\n	__\n}

!weblayout.onScrollHome = @.onScrollHome = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onScrollEnd = @.onScrollEnd = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onScrollStepPlus = @.onScrollStepPlus = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onScrollStepMinus = @.onScrollStepMinus = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onScrollPagePlus = @.onScrollPagePlus = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onScrollPageMinus = @.onScrollPageMinus = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onScrollPos = @.onScrollPos = function (layoutEle, pos, scrollParams) {\n	__\n}
!weblayout.onSliderRelease = @.onSliderRelease = function (layoutEle, pos, scrollParams) {\n	__\n}

!weblayout.onButtonClick = @.onButtonClick = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onButtonPress = @.onButtonPress = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onButtonStateChanged = @.onButtonStateChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onEditValueChanging = @.onEditValueChanging = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onEditValueChanged = @.onEditValueChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onSelectSelectionChanged = @.onSelectSelectionChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onSelectStateChanged = @.onSelectStateChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onPopupRequest = @.onPopupRequest = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onPopupReady = @.onPopupReady = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onPopupDismissed = @.onPopupDismissed = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onMenuItemActive = @.onMenuItemActive = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onMenuItemClick = @.onMenuItemClick = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onContextMenuSetup = @.onContextMenuSetup = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onContextMenuRequest = @.onContextMenuRequest = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onVisualStateChanged = @.onVisualStateChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onDisabledStateChanged = @.onDisabledStateChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onPopupDismissing = @.onPopupDismissing = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onHyperlinkClick = @.onHyperlinkClick = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onTableHeaderClick = @.onTableHeaderClick = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onTableRowClick = @.onTableRowClick = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onTableRowDblClick = @.onTableRowDblClick = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onElementCollapsed = @.onElementCollapsed = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onElementExpanded = @.onElementExpanded = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onActiveChild = @.onActiveChild = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onInitDataView = @.onInitDataView = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onRowsDataRequest = @.onRowsDataRequest = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onUIStateChanged = @.onUIStateChanged = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onFormSubmit = @.onFormSubmit = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onFormReset = @.onFormReset = function (layoutEle,reason,behaviorParams) {\n	__\n} 
!weblayout.onClosePopup = @.onClosePopup = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onRequestTooltip = @.onRequestTooltip = function (layoutEle,reason,behaviorParams) {\n	__\n}
!weblayout.onAnimation = @.onAnimation = function (layoutEle,reason,behaviorParams) {\n	__\n}
end intellisense**/
