// Copyright (c) 2003, 2008 Four J's Development Tools.
// All Rights Reserved.
// Note that the modification of this file is not supported.
// For customizing with your own javascript and cascading style sheets
// please read the product documentation.
// 
// $Id: gwccomponents.js 41544 2008-09-08 14:12:34Z ff $

gwc.ComponentSelector = function( component ) {
	var style = component[3];
	var componentName = component[0] + (style?'_'+style:'');
	return gwc.componentSet[componentName] || gwc.componentSet[component[0]];
}


/////////////////////////////////////////////////////////////////////////////

gwc.componentSet = {};

/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.application = {

	title:function( polarity, eid, cid, component, data ) {
		
		if ( polarity )
			window.document.title = data[1];
	},
	
	launch:function( polarity, eid, cid, component, data ) {
		
		if ( polarity ) {
			
			var url = data[1];
			var target = data[2] || '_blank';
			var options = data[3] || '';
			
				var w = window.open( url, target, options );
				if(w) {
					w.gwcChildWindow = true;
				} else {
					alert('Unable to open a new window. Check your popup blocker settings.');
				}

		}
	},
	
	interactive:function( polarity, eid, cid, component, data ) {
	},
		
	processing:function( polarity, eid, cid, component, data ) {
		
		if ( polarity ) {
			
			component.processingTimeout && gwc.tk.ClearTimeout( component.processingTimeout );
			component.processingTimeout = gwc.tk.SetTimeout( function() { gwc.capi.Rest() }, gwc.cfg.processingRefreshInterval );
		} else {

			component.processingTimeout && gwc.tk.ClearTimeout( component.processingTimeout );
			delete component.processingTimeout;
		}
	},

	end:function( polarity, eid, cid, component, data ) {

		gwc.tk.Defer( function() { if (window.gwcChildWindow) window.close(); } ); 
	},
	
	style:function( polarity, eid, cid, component, data ) {
		
		gwc.tk.SetStyleElementText( gwc.tk.IdToElement('gStyleList'), data[1] );
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Window = {
	
	modal:function( polarity, eid, cid, component ) {
	
		if ( polarity && !component.popup ) {
			
			var popupElt = gwc.tk.IdToElement(eid);
			component.layer = new gwc.tk.FrontLayer(gwc.tk.GetBody());
			component.layer.Alpha(0).Visible(true);
			component.layer.Fade(gwc.cfg.backgroundLayerFadingLevel);

			component.popup = new gwc.tk.Popup(popupElt);
			component.popup.Align('center', 'top');
			component.popup.Visible(true);

			
			var moveHandler = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid(cid, 'moveHandler'));
			
			function onMove(pos) {
				
				if ( pos.y <= 0 )
					pos.y = 0;
				if ( pos.x <= 0 )
					pos.x = 0;
			}
			
			component.movable = gwc.tk.Movable(popupElt, onMove, moveHandler ); 

			popupElt.style.visibility = 'visible';
		}
		
		if ( !polarity && component.popup ) {

			component.movable();
			component.layer.Visible(false);
			component.layer.Remove();
			component.popup.Remove();
			delete component.popup;
		}
	}	
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Action = 
gwc.componentSet.ToolBarItem =
gwc.componentSet.StartMenuCommand =
gwc.componentSet.TopMenuCommand =
gwc.componentSet.MenuAction = {

	onClick:function( event, elt, did ) {

		gwc.capi.Action(did);
		gwc.tk.PreventDefault(event);
	},

	clickable:function( polarity, eid, cid, component ) {

		var did = gwc.core.state.MarkData( cid, 'did', eid )[1];
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onClick, did );
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Menu_dialog = {

	dialog:function( polarity, eid, cid, component ) {
		
		if ( polarity && !component.popup ) {
			
			var popupElt = gwc.tk.IdToElement(eid);
			component.layer = new gwc.tk.FrontLayer(gwc.tk.GetBody());
			component.layer.Alpha(0).Visible(true);
			component.layer.Fade(gwc.cfg.backgroundLayerFadingLevel);
			component.popup = new gwc.tk.Popup(popupElt);
			component.popup.Align('center', 'center');
			component.popup.Visible(true);
			gwc.tk.Movable(popupElt);
		}
		
		if ( !polarity && component.popup ) {

			component.layer.Visible(false);
			component.layer.Remove();
			component.popup.Remove();
		}
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.StartMenuGroup = {
	
	toggleGroup:function(event, elt) {
	
		gwc.tk.ToggleClass( elt.parentNode, ['gStartMenuOpenedGroup', 'gStartMenuClosedGroup'] );
	},
	
	group:function( polarity, eid, cid, component ) {

		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.toggleGroup );
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.TopMenu = {

	click: function(event, elt, component, topMenuElt) {
	
		if ( !component.open && gwc.tk.EventTarget(event).parentNode.parentNode == topMenuElt ) {
			
			component.open = true;
			gwc.componentSet.TopMenu.hover(event, elt, component, topMenuElt);
		}
	},
	
	hover: function(event, elt, component, topMenuElt) {
		
		if ( !component.open )
			return;
		var target = gwc.tk.EventTarget(event);
		if ( target.nodeName.toLowerCase() == 'a' ) {
			
			gwc.componentSet.TopMenu.closeUpTo(component, target); 
			component.tbc.push(target);
			var next = gwc.tk.NextElement(target);
			if ( next )
				next.style.display = 'block';
			gwc.tk.CancelEvent(event);
		}
	},

	closeUpTo: function( component, keep ) {
		
		while ( component.tbc.length ) {
		
			var elt = component.tbc.pop();
	 		var next = gwc.tk.NextElement(elt);
			if ( next && keep && gwc.tk.Contains( next, keep ) )
				return component.tbc.push(elt);
			if ( next )
				next.style.display = '';
		}
	},
	
	closeMenu: function(event, elt, component, topMenuElt) {

		component.open = false;

		gwc.tk.Defer( function() { 

			gwc.componentSet.TopMenu.closeUpTo(component, topMenuElt); 
		});	
	},
	
	topmenu: function( polarity, eid, cid, component ) {

		component.tbc = [];
		component.open = false;
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'mouseover', this.hover, [component, elt] );
		gwc.tk.SetEventEx( polarity, elt, 'mouseup', this.click, [component, elt] );
		gwc.tk.SetEventEx( polarity, document.documentElement, 'mouseup', this.closeMenu, [component, elt] );
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Button = {

	onClick:gwc.componentSet.Action.onClick,
	
	clickable:gwc.componentSet.Action.clickable,
	
	currentField:function( polarity, component ) {
		
		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});

		var eid = gwc.core.state.FirstMarkEid( component.cid, 'button' );
		var elt = gwc.tk.IdToElement(eid);
		if ( polarity )
			gwc.tk.SafeFocus(elt);
		else
			gwc.tk.SafeBlur(elt);
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Label = {
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Edit = {
	


	onLocalAccelerators: function(ev, elt, localAcceleratorsValue) {

		var keycode = gwc.tk.EventKeyCode(ev);
		var keyModifiers = gwc.tk.EventKeyModifiers(ev);
		switch ( keycode ) {
			case gwc.tk.acceleratorToKeycode.END:
			case gwc.tk.acceleratorToKeycode.HOME:
			case gwc.tk.acceleratorToKeycode.LEFT:
			case gwc.tk.acceleratorToKeycode.RIGHT:
				if ( keyModifiers != 0 )
					break;
			case gwc.tk.acceleratorToKeycode.DELETE:
			case gwc.tk.acceleratorToKeycode.BACKSPACE:
				if ( keyModifiers != 0 && keyModifiers != 2 )
					break;

				if ( localAcceleratorsValue == 'yes' )
					gwc.tk.StopEventPropagation(ev);
				else
					if ( gwc.core.keyManager.HasListener(keycode, keyModifiers ) )
						gwc.tk.PreventDefault(ev);
				break;
		}		
	},

	localAccelerators: function( polarity, eid, cid, component, data ) {

		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'keydown', this.onLocalAccelerators, [data[1]] );
	},
	
	onTyping:function(event, elt, cid) {

		gwc.capi.Typing( cid );
		gwc.tk.CancelEvent(event);
	},

	onFieldClick:function(event, elt, cid) {
		
		gwc.capi.Focus( cid );
	},
	
	modifiable:function( polarity, eid, cid, component ) {
		
		var elt = gwc.tk.IdToElement(eid);
		var picture = gwc.core.state.DialogInfoByCid(cid).picture;
		var autonext = gwc.core.state.DialogInfoByCid(cid).autonext;
		
		if ( picture && polarity )
			component.removePicture = gwc.tk.fglPicture( elt, picture, function() { autonext && gwc.capi.NextField() } );
			
		if ( !polarity && component.removePicture ) {
			
			component.removePicture();
			delete component.removePicture;
		}

		gwc.tk.SetEventEx( polarity, elt, 'click', this.onFieldClick, [cid] );
	},
	
	currentField:function( polarity, component ) {

		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});
		
		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		if (eid != undefined) {
		
			var elt = gwc.tk.IdToElement(eid);
			elt.readOnly = !polarity;


			gwc.tk.SetEventEx( polarity, elt, 'keyup', this.onTyping, [component.cid] );


			gwc.tk.SetEvent( polarity, elt, 'dblclick', gwc.tk.StopEventPropagation ); 
	
			polarity && gwc.tk.SetHTMLElementAttribute(elt,'autocomplete','off'); 
			
			if ( polarity ) {
			
				gwc.tk.SafeFocus(elt);
				gwc.tk.SafeSelect(elt);
			} else {
			
				gwc.tk.UnselectField(elt);
			}
		}
	},

	GetValue:function( component ) { 

		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		return eid == undefined ? undefined : gwc.tk.IdToElement(eid).value;
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

gwc.componentSet.Edit_FileUpload = {

	onFieldClick:function(event, elt, cid) {

		gwc.capi.Focus( cid );
	},

	modifiable:function( polarity, eid, cid, component ) {

		var elt = gwc.tk.IdToElement(eid);

		gwc.tk.SetEventEx( polarity, elt, 'click', this.onFieldClick, [cid] );
	},

	field:function( polarity, eid, cid, component ) {

		var elt = gwc.tk.IdToElement(eid);

		gwc.tk.SetEventEx( polarity, elt, 'change', this.submitFileQuestion, [component]);    

		component.onSubmitFile = this.onSubmitFile;
		component.onNoSubmitFile = this.onNoSubmitFile;
		component.onFileUploaded = this.onFileUploaded;
	},

	onSubmitFile:function(event, elt, component) {

		gwc.tk.SetEventEx( false, elt, 'click');

		var eid = gwc.core.state.FirstMarkEid( component.cid, 'submit' );

		if (eid != undefined) {

			var formElt = gwc.tk.IdToElement(eid);
			formElt.submit();
		}
	},

	onNoSubmitFile:function(event, elt, component) {

		gwc.tk.SetEventEx( false, elt, 'click');

		component.movableFinalize();

		component.layer.Visible(false);
		component.layer.Remove();
		delete component.layer;
		
		component.popup.Remove();
		component.popup.Visible(false);
		delete component.popup;
	},
	
	onFileUploaded:function(event, elt, component) {

		gwc.tk.SetEventEx( false, elt, 'load');

		component.movableFinalize();

		component.layer.Visible(false);
		component.layer.Remove();
		delete component.layer;

		component.popup.Remove();
		component.popup.Visible(false);
		delete component.popup;
	},

	submitFileQuestion:function(event, elt, component) {

		var modalEid = gwc.core.state.FirstMarkEid( component.cid, 'popup' );

		if (modalEid != undefined) {    

			component.layer = new gwc.tk.FrontLayer(gwc.tk.GetBody());
			component.layer.Alpha(0).Visible(true);
			component.layer.Fade(0.9);   

			var popupElt = gwc.tk.IdToElement(modalEid);
			component.popup = new gwc.tk.Popup(popupElt);
			component.popup.Align('center', 'center');
			component.popup.Visible(true);
			component.popup.Resize(300, 80);
			gwc.tk.Movable(popupElt);

			var moveHandlerEid = gwc.core.state.FirstMarkEid( component.cid, 'moveHandler');
			if( moveHandlerEid != undefined ) {

				var moveHandlerElt = gwc.tk.IdToElement(moveHandlerEid);
				component.movableFinalize = gwc.tk.Movable(popupElt, undefined, moveHandlerElt );
			}

			var submitEid = gwc.core.state.FirstMarkEid( component.cid, 'submitFile' );
			if (submitEid != undefined) {

				var submitElt = gwc.tk.IdToElement(submitEid);
				gwc.tk.SetEventEx( true, submitElt, 'click', component.onSubmitFile, [component]);
			}

			var noSubmitEid = gwc.core.state.FirstMarkEid( component.cid, 'noSubmitFile' );
			if (noSubmitEid != undefined) {

				var noSubmitElt = gwc.tk.IdToElement(noSubmitEid);
				gwc.tk.SetEventEx( true, noSubmitElt, 'click', component.onNoSubmitFile, [component]);
			}

			var iframeEid = gwc.core.state.FirstMarkEid( component.cid, 'fileUploaded' );
			if (iframeEid != undefined) {

				var iframeElt = gwc.tk.IdToElement(iframeEid);
				gwc.tk.SetEventEx( true, iframeElt, 'load', component.onFileUploaded, [component]);
			}    
		}
	},

	currentField:function( polarity, component ) {

		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle',
			function(eid, data) {

			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});

		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		if (eid != undefined) {

			var elt = gwc.tk.IdToElement(eid);
			elt.readOnly = !polarity;

			if ( polarity ) {

				gwc.tk.SafeFocus(elt);
				gwc.tk.SafeSelect(elt);    
			} else {

				//gwc.tk.UnselectField(elt);
			}    
		}
	},

	GetValue:function( component ) { 

		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		return eid == undefined ? undefined : gwc.tk.IdToElement(eid).value;
	}
};

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.TextEdit = {
	

	onLocalAccelerators: function(ev, elt, localAcceleratorsValue) {

		var keycode = gwc.tk.EventKeyCode(ev);
		var keyModifiers = gwc.tk.EventKeyModifiers(ev);
		switch ( keycode ) {
			case gwc.tk.acceleratorToKeycode.LEFT:
			case gwc.tk.acceleratorToKeycode.RIGHT:
			case gwc.tk.acceleratorToKeycode.UP:
			case gwc.tk.acceleratorToKeycode.DOWN:
				if ( keyModifiers != 0 )
					break;
			case gwc.tk.acceleratorToKeycode.DELETE:
			case gwc.tk.acceleratorToKeycode.BACKSPACE:
			case gwc.tk.acceleratorToKeycode.END:
			case gwc.tk.acceleratorToKeycode.HOME:
				if ( keyModifiers != 0 && keyModifiers != 2 )
					break;

				if ( localAcceleratorsValue == 'yes' )
					gwc.tk.StopEventPropagation(ev);
				else
					if ( gwc.core.keyManager.HasListener(keycode, keyModifiers ) )
						gwc.tk.PreventDefault(ev);
				break;
		}		
	},

	localAccelerators: function( polarity, eid, cid, component, data ) {

		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'keydown', this.onLocalAccelerators, [data[1]] );
	},
	
	currentField:function( polarity, component ) {

		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});
		
		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		var elt = gwc.tk.IdToElement(eid);
		elt.readOnly = !polarity;
		
		if ( polarity )
			gwc.tk.SafeFocus(elt);
		else
			gwc.tk.UnselectField(elt);
	},
	
	onFieldClick:gwc.componentSet.Edit.onFieldClick,
	
	onSpecialChar:function(event, elt, cid) {
		
		var keyCode = gwc.tk.EventKeyCode(event);
		var dialogInfo = gwc.core.state.DialogInfoByCid(cid);
		switch (keyCode) {
		case gwc.tk.acceleratorToKeycode.TAB:
			if ( dialogInfo.wanttabs && gwc.tk.EventKeyModifiers(event) == 0 ) {
				
				gwc.tk.PasteText( elt, '\t' );
				gwc.tk.CancelEvent(event);
			}
			break;
		case gwc.tk.acceleratorToKeycode.RETURN:
			if ( dialogInfo.wantreturns && gwc.tk.EventKeyModifiers(event) == 0 )
				gwc.tk.StopEventPropagation(event);
			break;








		}
	},

	modifiable:function(polarity, eid, cid, component) {
	
		gwc.componentSet.Edit.modifiable.apply(this, arguments);
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'keydown', this.onSpecialChar, [cid] );
	},
	GetValue:gwc.componentSet.Edit.GetValue
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.DateEdit = {

	currentField:gwc.componentSet.Edit.currentField,
	onTyping:gwc.componentSet.Edit.onTyping,
	onFieldClick:gwc.componentSet.Edit.onFieldClick,
	GetValue:gwc.componentSet.Edit.GetValue,
	
	onLocalAccelerators: gwc.componentSet.Edit.onLocalAccelerators,
	localAccelerators: gwc.componentSet.Edit.localAccelerators,


	closeCalendar:function( cid ) {
		
		window._gwcCalendar && window._gwcCalendar.popup.Visible(false);
	},
	
	openCalendar:function( cid ) {
	
		if ( gwc.core.state.ComponentHasFocus(cid) ) {
		
			var eid = gwc.core.state.FirstMarkEid( cid, 'field' );
			var elt = gwc.tk.IdToElement(eid);
			var date = elt.value;
			var dateInfo = gwc.core.state.FirstMarkData( cid, 'dateInfo' );

			var dbdate = dateInfo[1];
			var format = dateInfo[2];
			

			if ( !window._gwcCalendar ) { 

				var calendarElt = gwc.tk.GetBody().appendChild(gwc.tk.CreateElement('div'));
				gwc.tk.Movable(calendarElt);
				var popup = new gwc.tk.Popup(calendarElt);
				var calendar = new gwc.tk.Calendar( calendarElt, gwc.tk.LocalDateStrings(), gwc.tk.LocalFirstDayOfTheWeek() );
				calendar.popup = popup; 
				window._gwcCalendar = calendar;
			} else {

				calendar = window._gwcCalendar;
			}
		
			calendar.onEvent = function(date, event) {
				
				if ( event == 'dblclick' )
					calendar.popup.Visible(false);
				elt.value = format ? gwc.tk.DateToFglFormat(date, format) : gwc.tk.DateToFglDbdate(date, dbdate);
				calendar.Paint();
			}
			

			calendar.SetDate( format ? gwc.tk.FglFormatToDate(elt.value, format) : gwc.tk.FglDbdateToDate(elt.value, dbdate) );
			calendar.popup.Move( gwc.tk.AbsoluteX(elt), gwc.tk.AbsoluteY(elt) + elt.clientHeight );
			calendar.popup.Visible(true);
		}		
	},
	
	onCalendarClick:function( event, elt, cid ) {
	
		gwc.capi.Focus( cid );
		gwc.capi.Defer( cid, 'openCalendar' );
	},

	modifiable:function( polarity, eid, cid, component ) { 
		
		gwc.componentSet.Edit.modifiable(polarity, eid, cid, component);
		polarity || this.closeCalendar();
		var eid = gwc.core.state.FirstMarkEid( component.cid, 'button' );
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onCalendarClick, [cid] );
		gwc.tk.SetEvent( polarity, document.documentElement, 'mousedown', this.closeCalendar );
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

gwc.componentSet.TimeEdit = {

	currentField:gwc.componentSet.Edit.currentField,
	onTyping:gwc.componentSet.Edit.onTyping,
	GetValue:gwc.componentSet.Edit.GetValue,
	onFieldClick:gwc.componentSet.Edit.onFieldClick,

	onLocalAccelerators: gwc.componentSet.Edit.onLocalAccelerators,
	localAccelerators: gwc.componentSet.Edit.localAccelerators,
	
	modifiable:function( polarity, eid, cid, component ) {
		
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'click', this.onFieldClick, [cid] );
	},
	
	cancelChanging:function( event, elt, cid ) {

		var component = gwc.core.state.ComponentByCid(cid);
		if ( component._timeout ) {
			
			window.clearTimeout(component._timeout)
			delete component._timeout;
		}
	},

	changing:function( component, elt, step, delay ) {

		var TS = ':'; 
		
		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		var elt = gwc.tk.IdToElement(eid);

		var pos = gwc.tk.GetCaret(elt).start;
		var block = elt.value.indexOf(TS,pos) == -1 ? 2 : elt.value.lastIndexOf(TS,pos-1) == -1 ? 0 : 1;
		var time = elt.value.split(TS);
		
		var date = new Date(0);
		date.setHours(time[0]);
		date.setMinutes(time[1]);
		date.setSeconds(time[2]);
				
		if ( block == 0 )
			date.setHours( date.getHours() + step );
		else if ( block == 1 )
			date.setMinutes( date.getMinutes() + step );
		else if ( block == 2 )
			date.setSeconds( date.getSeconds() + step );

		var h = date.getHours();
		var m = date.getMinutes();
		var s = date.getSeconds();
		
		elt.value = (h>9?h:'0'+h) + TS + (m>9?m:'0'+m) + TS + (s>9?s:'0'+s);
		
		gwc.tk.Defer( function() { gwc.tk.SetCaret(elt, block*3, block*3+2 ) } );
		
		gwc.tk.SafeFocus(elt);

		var arg = arguments;
		delay /= 2;
		component._timeout = window.setTimeout( function() { arg.callee.apply(null,arg) }, delay + 50 );
	},

	changeValue:function( event, elt, cid, step ) {

		if ( !gwc.core.state.ComponentHasFocus(cid) )
			return;
		var eid = gwc.core.state.FirstMarkEid( cid, 'field' );
		var elt = gwc.tk.IdToElement(eid);
		var component = gwc.core.state.ComponentByCid(cid);
		this.changing( component, elt, step, 1000 );
	},


	decrease:function( polarity, eid, cid, component ) {
	
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx.call( this, polarity, elt, 'mousedown', this.changeValue, [cid, -1] );
		gwc.tk.SetEventEx( polarity, elt, 'mouseup', this.cancelChanging, [cid] );
		gwc.tk.SetEventEx( polarity, elt, 'mouseout', this.cancelChanging, [cid] );
	},
	
	increase:function( polarity, eid, cid, component ) {
	
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx.call( this, polarity, elt, 'mousedown', this.changeValue, [cid, 1] );
		gwc.tk.SetEventEx( polarity, elt, 'mouseup', this.cancelChanging, [cid] );
		gwc.tk.SetEventEx( polarity, elt, 'mouseout', this.cancelChanging, [cid] );
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////




gwc.componentSet.SpinEdit = {

	currentField:gwc.componentSet.Edit.currentField,
	onTyping:gwc.componentSet.Edit.onTyping,
	GetValue:gwc.componentSet.Edit.GetValue,
	onFieldClick:gwc.componentSet.Edit.onFieldClick,

	onLocalAccelerators: gwc.componentSet.Edit.onLocalAccelerators,
	localAccelerators: gwc.componentSet.Edit.localAccelerators,
	
	modifiable:function( polarity, eid, cid, component ) {
		
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'click', this.onFieldClick, [cid] );
	},

	changeValue:function( cid, step ) {

		var eid = gwc.core.state.FirstMarkEid( cid, 'field' );
		var elt = gwc.tk.IdToElement(eid);

		var valint = parseInt(elt.value, 10);
		if(!valint)
		  valint = 0;
		elt.value = valint + step;
		gwc.core.state.DialogInfoByCid(cid).onChange && gwc.capi.Commit(); 
	},

	onChangeValue:function( event, elt, cid, step ) {

		gwc.capi.Focus( cid );
		gwc.capi.Defer( cid, 'changeValue', [step] );
	},

	decrease:function( polarity, eid, cid, component, data ) {
	
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onChangeValue, [cid, data[1]] );
	},
	
	increase:function( polarity, eid, cid, component, data ) {
	
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onChangeValue, [cid, data[1]] );
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Slider = {

	onSliderMove: function(component, val) {

		component.cursorElt.title = val;
	},

	currentField:function( polarity, component ) {
	
		var _sliderComponent = this;

		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});

		if ( polarity ) {

			component.CancelMovable = gwc.tk.Movable(component.cursorElt, function(pos) {

				var offsetX = -4;
				pos.y = undefined;
				var width = component.cursorElt.parentNode.parentNode.offsetWidth;
				if (pos.x < offsetX)
					pos.x = offsetX;
				if ( pos.x > width + offsetX )
					pos.x = width + offsetX;
				var val = (component.valueMax-component.valueMin) * pos.x / width + component.valueMin;
				val = Math.floor(val / component.valueStep + 1) * component.valueStep;
				if (component.value != val)
					_sliderComponent.onSliderMove(component, val);
				component.value = val;
			});
		} else {

			component.CancelMovable && component.CancelMovable();
		}
	},
	
	value:function( polarity, eid, cid, component, data ) {
	
		if (polarity) {

			var cid = component.cid;
	
			component.cursorElt = gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( cid, 'cursor' ) );

			var cursorData = gwc.core.state.FirstMarkData( cid, 'cursor' );			
			component.value = parseInt(data[1]);
			component.valueMin = parseInt(cursorData[1]);
			component.valueMax = parseInt(cursorData[2]);
			component.valueStep = parseInt(cursorData[3]);
			
			var val = (component.value - component.valueMin) / (component.valueMax - component.valueMin);
	
			gwc.tk.Defer(function(){
	
				component.cursorElt.style.left = (val * component.cursorElt.parentNode.parentNode.offsetWidth) + 'px'; 
			});
		}
	},
	
	onCursorClick:function( event, elt, component ) {
	
		gwc.capi.Focus( component.cid );

		gwc.tk.SetEventEx( true, document.documentElement, 'mouseup', gwc.componentSet.Slider.onCursorRelease, [component] );
	},
	
	onCursorRelease:function( event, elt, component ) {

		gwc.tk.SetEventEx( false, document.documentElement, 'mouseup', gwc.componentSet.Slider.onCursorRelease, [component] );

		var val = (component.value - component.valueMin) / (component.valueMax - component.valueMin);
		component.cursorElt.style.left = (val * component.cursorElt.parentNode.parentNode.offsetWidth) + 'px';
		
		gwc.core.state.DialogInfoByCid(component.cid).onChange && gwc.capi.Commit(); 
	},
	
	modifiable:function( polarity, eid, cid, component, data ) {

		//var elt = gwc.tk.IdToElement(eid);
		var elt = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( component.cid, 'cursor' ));
		
		gwc.tk.SetEventEx( polarity, elt, 'mousedown', this.onCursorClick, [component] );
	},
	
	GetValue:function( component ) { 
		
		return component.value;
	}
}	



/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.ButtonEdit = {

	currentField:gwc.componentSet.Edit.currentField,
	onTyping:gwc.componentSet.Edit.onTyping,
	onFieldClick:gwc.componentSet.Edit.onFieldClick,
	modifiable:gwc.componentSet.Edit.modifiable,
	GetValue:gwc.componentSet.Edit.GetValue,

	onActionClick:function( event, elt ) {
				
		gwc.capi.Action( elt.name ); 
		gwc.tk.CancelEvent(event);
	},

	clickable:function( polarity, eid, cid, component ) {
	
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onActionClick );
	}
}

//gwc.tk.AddObj(gwc.componentSet.ButtonEdit, gwc.componentSet.Edit);


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.CheckBox = {

	currentField:function( polarity, component ) {
		
		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});

		var eid = gwc.core.state.FirstMarkEid( component.cid, 'field' );

		var elt = eid ? gwc.tk.IdToElement(eid) : undefined;

		elt && gwc.tk.SetEvent( polarity, elt, 'dblclick', gwc.tk.StopEventPropagation ); 

		polarity && elt && gwc.tk.SafeFocus(elt);
	},

	onToggle:function(event, elt, cid, eid) { 
		
		if ( event.type == 'keydown' && gwc.tk.EventKeyCode(event) != 32 ) 
			return;
		gwc.capi.Focus( cid );
		gwc.capi.Defer( cid, 'toggleState', [eid] );
	},

	modifiable:function( polarity, eid, cid, component ) {

		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'click', this.onToggle, [cid, eid] ); 
		gwc.tk.SetEventEx( polarity, elt, 'keydown', this.onToggle, [cid, eid] );
	},
	
	setState:function(component, eid) {
		
		var data = gwc.core.state.MarkData( component.cid, 'valueStyle', eid );
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.ClearClasses(elt, data); 
		gwc.tk.SetClass(elt, true, data[component.state + 1]);
	},
	
	value:function( polarity, eid, cid, component, data ) {
		
		if ( !polarity )
			return;
		var value = data[1];
		var info = gwc.core.state.MarkData( cid, 'field', eid );
		component.state = value==info[2] ? 0 : value==info[1] ? 1 : 2;
		this.setState(component, eid);
	},

	toggleState:function(cid, eid) {

		if ( !gwc.core.state.ComponentHasFocus(cid) )
			return;
			
		var notNull = gwc.core.state.MarkData( cid, 'field', eid )[3];

		
		var component = gwc.core.state.ComponentByCid(cid);

		if(notNull)
		  component.state = (component.state+1) % 2; //check -> uncheck -> ...
		else
		  component.state = (component.state+2) % 3; //check -> uncheck -> null -> ...

		this.setState(component, eid);
		





		gwc.core.state.DialogInfoByCid(cid).onChange && gwc.capi.Commit(); 

	},

	GetValue:function( component ) {

		var info = gwc.core.state.FirstMarkData( component.cid, 'field' );
		return component.state==0 ? info[2] : component.state==1 ? info[1] : '';
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.RadioGroup = {

	onNavKey:function( keycode, keymodifier, cid ) {

		if( keycode==40 || keycode==39) { //Down or right arrow

			var firstelt;
			var firsteid;	
			var stopnext;
			var found = gwc.core.state.ForeachMarkEid( cid, 'radio', function(eid, data) {

				var elt = gwc.tk.IdToElement(eid);
				if(!firsteid) {

					firsteid=eid;
					firstelt=elt;
				}
				if(stopnext) {

				    gwc.tk.SafeFocus(elt);
		            gwc.capi.Defer( cid, 'selectValue', [eid] );
					return true;
				}
				if ( elt.checked )
					stopnext=true;
			});
			if(!found && firsteid ) {

				gwc.tk.SafeFocus(firstelt);
				gwc.capi.Defer( cid, 'selectValue', [firsteid] );
			}
		}
		else if( keycode==37 || keycode==38) { //Up or left arrow

			var eidprev;
			var eltprev;
			var found = gwc.core.state.ForeachMarkEid( cid, 'radio', function(eid, data) {

				elt = gwc.tk.IdToElement(eid);
				if ( elt.checked && eidprev ) {

				    gwc.tk.SafeFocus(eltprev);
		            gwc.capi.Defer( cid, 'selectValue', [eidprev] );
					return true;
				}
				eidprev=eid;
				eltprev=elt;
			});
			if(!found && eidprev) {

				gwc.tk.SafeFocus(eltprev);
				gwc.capi.Defer( cid, 'selectValue', [eidprev] );
			}
		}
	},

	currentField:function( polarity, component ) {

		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});

		if ( polarity ) {
			
			var first;
			var found = gwc.core.state.ForeachMarkEid( component.cid, 'radio', function(eid, data) {

				var elt = gwc.tk.IdToElement(eid);
				if (!first)
					first = elt;
				
				if ( elt.checked ) {
				
					gwc.tk.SafeFocus(elt);
					return true;
				}
			});
			
			if (first && !found)
				gwc.tk.SafeFocus(first); 

			for(var i=37; i<=40; i++)
				gwc.core.keyManager.AddListener( i, 0, this.onNavKey, component.cid );
		}
		else {

			for(var i=37; i<=40; i++)
				gwc.core.keyManager.RemoveListener( i, 0, this.onNavKey);
		}
	},
	
	updateValue:function( component, value ) {
		
		gwc.core.state.ForeachMarkEid( component.cid, 'radio', function(eid, data) {
			
			var elt = gwc.tk.IdToElement(eid);
			gwc.tk.Defer( function() {	elt.checked = ( ('|'+value+'|').indexOf('|'+data[1]+'|') != -1 ) } ); 
		});
		component.value = value;
		gwc.core.state.DialogInfoByCid(component.cid).onChange && gwc.capi.Commit(); 

	},
	
	value:function( polarity, eid, cid, component, data ) {
		
		if ( polarity )
			component.value = data[1];
		else
			delete component.value;
			

	},

	selectValue:function(cid, eid) {

		if ( gwc.core.state.ComponentHasFocus(cid) ) {

			var component = gwc.core.state.ComponentByCid(cid);
			var radioItemValue = gwc.core.state.MarkData( cid, 'radio', eid )[1];
			var notNull = gwc.core.state.FirstMarkData( cid, 'field' )[1];
			var isQuery = gwc.core.state.DialogInfoByCid(cid).query;
			if ( !isQuery ) 
				this.updateValue( component, ( component.value == radioItemValue && !notNull ) ? '' : radioItemValue );
			else {
			
				var radioItemValue = gwc.core.state.MarkData( cid, 'radio', eid )[1];

				var values = component.value.split('|');
				values[0] == '' && values.shift();
				lookForValue:{
					for ( var i = 0; i < values.length; i++ )
						if ( values[i] == radioItemValue ) {

							values.splice(i, 1);
							break lookForValue;
						}
					values.push(radioItemValue); 
				}
				this.updateValue( component, values.join('|') );

			}
		}
	},

	onClick:function(event, elt, cid, eid) { 
	
		var component = gwc.core.state.ComponentByCid(cid);
		var radioItemValue = gwc.core.state.MarkData( cid, 'radio', eid )[1];
		var currentEltState = elt.checked;
		
		if ( !gwc.tk.isKHTML ) 
			gwc.tk.Defer( function() {	elt.checked = currentEltState } ); 
			
		gwc.capi.Focus( cid );
		gwc.capi.Defer( cid, 'selectValue', [eid] );

	},

	modifiable:function( polarity, eid, cid, component, data ) {
		
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', gwc.componentSet.RadioGroup.onClick, polarity&&[cid, eid]);
	},

	GetValue:function( component ) {
		
		return component.value;
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.ComboBox = {

	onKeyPress: function(event, elt, component) {

		if ( !gwc.core.state.FirstMarkData( component.cid, 'searchable' )[1] )
			return;

		var charcode = gwc.tk.EventChar(event);
		if ( !('searchState' in component) )
			component.searchState = '';
		if ( component.searchTimeout )
			gwc.tk.ClearTimeout(component.searchTimeout);
		component.searchTimeout = gwc.tk.SetTimeout( function(){ component.searchState = '' }, 500 );
		component.searchState += charcode.toLowerCase();

		if (!gwc.core.state.ForeachMarkEid( component.cid, 'itemName', function(eid, data) {
			
			var text = gwc.tk.IdToElement(eid).firstChild.data;
			if ( text.toLowerCase().indexOf(component.searchState) == 0 ) { 
				
				gwc.tk.IdToElement( gwc.core.state.FirstMarkEid(component.cid, 'field') ).value = text;
				component.value = data[1]; 
				return true;
			}
		})) {
			
			component.searchState = '';
		}
	},

	currentField:function( polarity, component ) {
		
		gwc.core.state.ForeachMarkEid( component.cid, 'currentFieldStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[1]);
		});
		
		var fieldEid = gwc.core.state.FirstMarkEid( component.cid, 'field' );
		var fieldElt = fieldEid ? gwc.tk.IdToElement(fieldEid) : undefined;
		
		fieldElt && gwc.tk.SetEvent( polarity, fieldElt, 'dblclick', gwc.tk.StopEventPropagation ); 

		gwc.tk.SetEventEx( polarity, document, 'keypress', gwc.componentSet.ComboBox.onKeyPress, [component] );

		if ( !polarity )
			gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( component.cid, 'list' ) ).style.display = ''; 
		else
			gwc.tk.SafeFocus( gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( component.cid, 'field' ) ) ); 
	},

	flipFlopList:function( cid ) { 

		var list = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( cid, 'list' ));
		if(list.style.display=='block')
			list.style.display='';
		else
			gwc.capi.Defer( cid, 'showList' );
	},

	onClick:function(event, elt, buttonClicked, cid, eid) { 

		var isQuery = gwc.core.state.DialogInfoByCid(cid).query;
		gwc.capi.Focus( cid );
		if ( buttonClicked || !isQuery )
			gwc.componentSet.ComboBox.flipFlopList(cid);

		gwc.tk.CancelEvent(event); 
		if ( isQuery )
			gwc.tk.SafeFocus( gwc.tk.IdToElement(eid) ); 
	},
	
	onListClick:function(event, elt, cid) {

		var listItemElt = gwc.tk.EventTarget(event);
		var component = gwc.core.state.ComponentByCid(cid);
		var markData = gwc.core.state.MarkData( cid, 'itemName', listItemElt.id );
		if ( markData ) {
			
			component.value = markData[1];

			var field = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( cid, 'field' ));
			field.value = listItemElt.firstChild.data;
		
			var list = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( cid, 'list' ));
			list.style.display = '';
			gwc.core.state.DialogInfoByCid(cid).onChange && gwc.capi.Commit(); 
		}
		gwc.tk.CancelEvent(event);

	},
	
	showList:function(cid) {
	
		if ( gwc.core.state.ComponentHasFocus(cid) ) {

			var component = gwc.core.state.ComponentByCid(cid);
			var field = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( cid, 'field' ));
			var list = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( cid, 'list' ));
			list.style.display = 'block';
			list.style.visibility = 'hidden';
			list.style.height = 'auto'; 
			if ( field.offsetWidth > list.offsetWidth )
				list.style.width = field.offsetWidth + ( list.offsetWidth - list.clientWidth ) + 'px';
			var listHeight = gwc.tk.MinMax( list.offsetHeight, 5, 150 );
			list.style.height = listHeight + 'px';
			list.style.visibility = '';
		}
	},


	onKeyDown:function(event, elt, cid, component, eid) {

		var keyCode = gwc.tk.EventKeyCode(event);

		 if (event.altKey) { 

			if(keyCode==38||keyCode==40) //open close the list
				gwc.componentSet.ComboBox.flipFlopList(cid);
			gwc.tk.CancelEvent(event);
		}        
		else if((keyCode>=37&&keyCode<=40) && gwc.core.state.FirstMarkData(cid,'field')[1] && component.value=="") {
			
			
			var firstmark = gwc.core.state.FirstMarkData( cid, 'itemName' );
			component.value = firstmark[1];
			elt.value = gwc.tk.IdToElement(gwc.core.state.FirstMarkEid(cid,'itemName')).firstChild.data;
			gwc.tk.CancelEvent(event);
			gwc.core.state.DialogInfoByCid(cid).onChange && gwc.capi.Commit();
		}
		else if(keyCode==38 || keyCode==37) { 

			var valuePrev;
			var valueTextPrev;
			var currentValue;
			var currentTextValue;
			gwc.core.state.ForeachMarkEid( cid, 'itemName', function(eid, data) {
                
				valuePrev = currentValue;
				valueTextPrev = currentTextValue;
				currentTextValue = gwc.tk.IdToElement(eid).firstChild.data;
				currentValue = data[1];

				if(component.value == currentValue) {
					
					if(!valuePrev) 
						return true;
					component.value = valuePrev;
					elt.value = valueTextPrev;
				}
			});

			gwc.tk.CancelEvent(event);
			gwc.core.state.DialogInfoByCid(cid).onChange && gwc.capi.Commit();
		}
		else if(keyCode==40 || keyCode==39) { 

			var stopnext = false;
			gwc.core.state.ForeachMarkEid( cid, 'itemName', function(eid, data) {

				if(stopnext) {
					component.value = data[1];
					elt.value = gwc.tk.IdToElement(eid).firstChild.data;
					return true;
				}
				if(component.value == data[1])
					stopnext = true;
			});

			gwc.tk.CancelEvent(event);
			gwc.core.state.DialogInfoByCid(cid).onChange && gwc.capi.Commit();
		}
	},
	
	
	modifiable:function( polarity, eid, cid, component, data ) {

		var elt = gwc.tk.IdToElement(eid);

		gwc.tk.SetEventEx( polarity, elt, 'mousedown', this.onClick, polarity&&[false, cid, eid] );
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(gwc.core.state.FirstMarkEid( cid, 'button' )), 'mousedown', this.onClick, polarity&&[true, cid, eid] );
		var listElt = gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( cid, 'list' ) );

		if ( polarity )
			component.closeListEvent = function(ev) {
				
				if ( !gwc.tk.EventTarget(ev) || !gwc.tk.Contains( listElt, gwc.tk.EventTarget(ev) ) )
					listElt.style.display = '';
			}
		gwc.tk.SetEvent( polarity, document.documentElement, 'mousedown', component.closeListEvent );
		gwc.tk.SetEvent( polarity, window, 'blur', component.closeListEvent );
		gwc.tk.SetEventEx( polarity, listElt, 'mouseup', this.onListClick, polarity&&[cid, eid] );
		gwc.tk.SetEventEx( polarity, elt, 'keydown', this.onKeyDown, [cid, component, eid] );

	},
	
	value:function( polarity, eid, cid, component, data ) {
		

		
		var newValue = data[1];
		var found = false;
		gwc.core.state.ForeachMarkEid( cid, 'itemName', function(eid, data) {
			
			if ( data[1] == newValue ) {
			
				gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, 'gComboBoxCurrentItem');
				found = true;
				return true;
			}
		});
		
		if ( !found )
			gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( component.cid, 'field' ) ).value = ''; //newValue;
		
		component.value = newValue;
	},

	GetValue:function( component ) {

		var isQuery = gwc.core.state.DialogInfoByCid(component.cid).query;

		if ( !isQuery ) {
		
			return component.value;
		} else {
		
			var value = gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( component.cid, 'field' ) ).value;
			gwc.core.state.ForeachMarkEid( component.cid, 'itemName', function(eid, data) {
			
				if ( value == gwc.tk.IdToElement(eid).firstChild.data ) {

					value = data[1];
					return true;
				}
			});	
			return value;
		}
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.FormField = {

	currentCell:function( polarity, component ) {
		

		gwc.core.state.ForeachMarkEid( component.cid, 'currentRowStyle', function(eid, data) {
			
			gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[2]);

		});


	},






	onSelect:function( event, elt, component, index ) {
	
		gwc.capi.SelectMatrixItem( component.cid, index );
	},
	
	selectable:function( polarity, eid, cid, component, data ) {
		
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onSelect, [ component, data[1] ] );
	}
}



/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Table = {








	
	currentRow:function( polarity, component, currentRowIndex ) {
		
		gwc.core.state.ForeachMarkEid( component.cid, 'row', function(eid, data) {
			
			if ( data[1] == currentRowIndex )
				gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, component.currentRowStyle);
		});
	},
	
	onColumnHeaderClick:function(event, elt, did) { 
				
		gwc.capi.SortColumn( did );
		gwc.tk.CancelEvent(event);
	},

	sortColumn:function( polarity, eid, cid, component, data ) {

		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onColumnHeaderClick, [data[1]] );
	},
	
	sendClick: function(cid, eid) {

		var markData = gwc.core.state.MarkData( cid, 'row', eid );
		markData && gwc.capi.SelectTableRow( gwc.core.state.IdrefByCid(cid), markData[1] );
	},
	
	
	onRowClick:function(event, elt, cid, eid) { 

		gwc.capi.Defer( cid, 'sendClick', [eid] );

	},

	sendDblClick: function(cid) {

		var action = gwc.core.state.DialogInfoByCid(cid).action;
		action && gwc.capi.Action( action );
	},

	onRowDblClick:function(event, elt, cid) { 
		

		gwc.capi.Defer( cid, 'sendDblClick' );
	},

	activeRow:function( polarity, eid, cid, component, data ) {
		
		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'click', this.onRowClick, [cid, eid] );
		gwc.tk.SetEventEx( polarity, elt, 'dblclick', this.onRowDblClick, [cid] );
	},
	
	modifiableRow:function( polarity, eid, cid ) {

		var elt = gwc.tk.IdToElement(eid);
		gwc.tk.SetEventEx( polarity, elt, 'dblclick', this.onRowDblClick, [cid] );
	},
	
	scrollable:function( polarity, eid, cid, component, data ) {
		
		if ( polarity )
			component.scrollbar && component.scrollbar.Disable( !data[1] );
	},
	
	currentRowStyle:function( polarity, eid, cid, component, data ) {
		
		component.currentRowStyle = data[1];
	},
	
	table:function( polarity, eid, cid, component, data ) {
		
		if ( !polarity )
			return;
		component.tableId = data[1];
		component.offset = data[2];
		component.pageSize = data[3];
		component.size = data[4];
		
		if ( component.scrollbar ) {

			component.scrollbar.Size(component.size);
			component.scrollbar.PageSize(component.pageSize);
			component.scrollbar.Offset(component.offset);
			component.scrollbar.Update();
		}		
	},
	
	scrollbar:function( polarity, eid, cid, component, data ) {
	
		if ( polarity && !component.scrollbar ) {
			
			var wheelElt = gwc.tk.IdToElement( gwc.core.state.FirstMarkEid( cid, 'table' ) );
			var elt = gwc.tk.IdToElement(eid);
			component.scrollbar = new gwc.tk.ScrollBar( elt, wheelElt, function(offset) {
				
				gwc.capi.Offset( component.tableId, offset );
			});

			component.scrollbar.Disable( !gwc.core.state.HasMark( component.cid, 'scrollable' ) );

			var data = gwc.core.state.FirstMarkData( cid, 'table' );

			component.scrollbar.Size(data[4]);
			component.scrollbar.PageSize(data[3]);
			component.scrollbar.Offset(data[2]);
			component.scrollbar.Update();

			var data = gwc.core.state.FirstMarkData( cid, 'scrollable' );
			component.scrollbar.Disable( !data[1] );

		}

		if ( !polarity && component.scrollbar ) {

			component.scrollbar.Remove();
			delete component.scrollbar;
		}

	},

	onChangeOffset:function(event, elt, component, scrollBy ) { 

		gwc.capi.Offset( component.tableId, gwc.tk.MinMax( component.offset + scrollBy, 0, component.size - component.pageSize ) );
		gwc.tk.CancelEvent(event);
	},
	
	scrollBy:function( polarity, eid, cid, component, data ) {
	
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'mousedown', this.onChangeOffset, [ component, data[1] ] );
	}
}


/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.ScrollGrid = {
	
	currentRow:function( polarity, component, currentRowIndex ) {
		
		gwc.core.state.ForeachMarkEid( component.cid, 'currentRowStyle', function(eid, data) {
		
			if ( currentRowIndex == data[1] )
				gwc.tk.SetClass(gwc.tk.IdToElement(eid), polarity, data[2]);
		});
	},
	
	onRowClick:function(event, elt, cid, eid) { 

		var markData = gwc.core.state.MarkData( cid, 'row', eid );
		gwc.capi.SelectTableRow( gwc.core.state.IdrefByCid(cid), markData[1] );
	},

	onRowDblClick:function(event, elt) { 
		
		gwc.capi.Action( gwc.core.state.ActionDidByName('accept') );
	},

	modifiable:function( polarity, eid, cid, component, data ) {

		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onRowClick, [cid, eid] );
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'dblclick', this.onRowDblClick );
	},
	
	scrollGrid:function( polarity, eid, cid, component, data ) {
		
		component.scrollGridId = data[1];
		component.offset = data[2];
		component.pageSize = data[3];
		component.size = data[4];
	},

	onChangeOffset:function(event, elt, component, scrollBy ) { 

		gwc.capi.Offset( component.scrollGridId, gwc.tk.MinMax( component.offset + scrollBy, 0, component.size - component.pageSize ) );
		gwc.tk.CancelEvent(event);
	},
	
	scrollBy:function( polarity, eid, cid, component, data ) {
	
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'mousedown', this.onChangeOffset, [ component, data[1] ] );
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
gwc.componentSet.Folder = {

	onClick:function( event, elt ) {

		gwc.capi.Action( elt.name ); 
		gwc.tk.CancelEvent(event);
	},

	clickable:function( polarity, eid, cid, component ) {
		
		gwc.tk.SetEventEx( polarity, gwc.tk.IdToElement(eid), 'click', this.onClick );
	}
}


/////////////////////////////////////////////////////////////////////////////


