﻿Type.registerNamespace("Infragistics.Web.UI");

/****************************************** Behavior **********************************************/
$IG.Behavior = function(element)
{
	$IG.Behavior.initializeBase(this, [element]);
	this._props = [];
}

$IG.Behavior.prototype =
{
	initialize:function()
	{
		$IG.Behavior.callBaseMethod(this, 'initialize');
		if(this._addHandlers)
			this._addHandlers();
	},

	get_owner:function(){return this._owner;},
	set_owner:function(value){this._owner = value;},
	
	dispose:function()
	{
		ig_ui_timer(this, true);
		var elem = this.get_element();
		if(elem)
			$clearHandlers(elem);
		$IG.Behavior.callBaseMethod(this, 'dispose');
	},

	/* main control where behavior belongs to (not a member object like Header in WebDialogWindow, but WebDialogWindow) */
	get_control:function(){return this._control;},
	set_control:function(value){this._control = value;},

	/* prefix for some event dependant on control-owner (like ResizerMouseOver, HeaderMouseDown) */
	_prefix:'',

	/* VS all below came from igExtenderBase.js */
	getTargetElement: function()
	{
		var e = this._targetElem;
		if(!e)
			e = this._targetElem = this.get_element();
		return e;
	},
	get_targetHtmlElementID: function(){ return this._elemID; },
	set_targetHtmlElementID: function(val)
	{
		if(this._elemID)
			return;
		this._elemID = val;
		this._targetElem = document.getElementById(val);
	},
	get_stateID: function(){ return this._stateID; },
	set_stateID: function(val)
	{
		if(!this._stateID)
			this._stateID = val;
	},
	get_stateValue: function(){ return this._stateValue; },
	set_stateValue: function(val, save)
	{
		this._stateValue = val;
		if(!this._stateID || !save)
			return;
		var e = document.getElementById(this._stateID);
		if(e)
			e.value = val;
	},
	_get_: function(i)
	{
		return this._props[i];
	},
	_set_: function(i, val, evtName)
	{
		this._props[i] = val;
		if(evtName)
			$util.addClientEvent(this, evtName, val);
	},
	/* 1st param - name of event to fire */
	/* 2nd param - null, or Xxx prefix of XxxEventArgs, or instance of EventArgs */
	/* 3rd param - original event of browser */
	/* additional params to match with order of XxxEventArgs._props array */
	/* return: null or instance of XxxEventArgs */
	/* Examples: this._raiseClientEvent('click', null, e); will raise "click" event and args will be: new Infragistics.Web.UI.CancelEventArgs(); and e will define value returned by _args.get_browserEvent() */
	/* this._raiseClientEvent('resizing', 'Resize', e, null, 1, 2, 3, 4); will raise "resizing" event and args will be: new Infragistics.Web.UI.ResizeEventArgs(); and 1 will define value returned by _args.get_height(), and 2: _args.get_height() */
	_raiseClientEvent: function()
	{
		if(this._control)
			return this._control._raiseClientEvent(arguments);
		var args = arguments[1];
		var fnc = this.get_events().getHandler(arguments[0]);/* arguments[0] - name of event */
		var str = args && args.substring;
		if(!fnc)/* if 2nd param is EvtArgs object, then return that object as it is */
			return str ? null : args;
		if(str)
			eval('try{args = new Infragistics.Web.UI.' + args + 'EventArgs();}catch(ex){args = null;}');
		var i = 1, len = arguments.length;
		if(!args)/* if EventArgs is missing or invalid, then create default args */
			args = (len < 3) ? new Sys.EventArgs() : new $IG.EventArgs();
		/* if it is our EventArgs object, then initialize its properties */
		if(args._props) while(++i < len)
			args._props[i - 2] = arguments[i];
		/* fire event (notify listeners) */
		fnc(this, args);
		if(args._props)/* if it is our EventArgs object, then delete reference to browser event */
			delete args._props[0];
		return args;
	}
}
$IG.Behavior.registerClass('Infragistics.Web.UI.Behavior', Sys.UI.Behavior);
/****************************************** End bBehavior OBJECT ***********************************************************/

/****************************************** Move Behavior Object   ***********************************************************/
$IG.MoveBehavior = function(element)
{
	$IG.MoveBehavior.initializeBase(this, [element]);
}

$IG.MoveBehavior.prototype =
{
	
}
$IG.MoveBehavior.registerClass('Infragistics.Web.UI.MoveBehavior', $IG.Behavior);
/****************************************** End MoveBehavior Object *************************************/


/****************************************** UI BEHAVIORS OBJECT *************************************/
$IG.UIBehaviorsObject = function(control, collection)
{
    this._control = control;
    this._collection = collection;
    this._selectedItems = [];
    this._handlers = {"mousedown":this.handleEvent, "mouseup":this.handleEvent, "mousemove":this.handleEvent,
                     "keydown":this.handleEvent, "mouseover":this.handleEvent, "mouseout":this.handleEvent};
    $addHandlers(control.get_element(), this._handlers , this);
}

$IG.UIBehaviorsObject.prototype = 
{	
    handleEvent:function(e)
    {
        var elem = e.target;
        var adr = null;
        while(elem)
        {
            if(elem.getAttribute)
                adr = elem.getAttribute("adr");
            if(adr != null)
                break;
            elem = elem.parentNode;    
        }
        if(elem != null)
        {
            var item = elem._object;
            if(item == null)
            {   
                item = this._collection._createObject(adr, elem);
                if(item == null)
                    item = elem._object;
                if(item == null)
                    return;
            }
            /* SJZ 9/11/07 - If a control is a templated control, then this event can fire for both
             * the parent control, and the child control. So we need to make sure that we're firing
             * the event for the correct control*/
            if(item._owner != this._control)
                return; 
            //if(e.type == "mousemove")
            //    this.drag(item, e);
            else if(e.type == "mouseover")
                this.hover(item, e);
            else if(e.type == "mouseout")
                this.unhover(item, e);
            else if(e.type == "mousedown")
                this.select(item, e);
            //else if(e.type == "mouseup")
            //    this.drop(item,e);
            else if(e.type == "keydown")
                this.kbNav(item, e);
        }
    },
    
	select:function(item, e)
	{
	    var itemFlags = item._getFlags();
	    var selectable = itemFlags.getSelectable(this._control);
	    var enabled = itemFlags.getEnabled(this._control);
	    
	    if(selectable && enabled)
	    {
	        if(this._control._shouldSelect != null && e)
	            if(!this._control._shouldSelect(item, e))
	                return;
            var old = this._selectedItems[0];
            if(old != item)
            {
                if(old)
                {
                    old._getFlags().setSelected(false);
                    this._control._selectItem(old, false);
                }
                this._selectedItems[0] = item;
                itemFlags.setSelected(true);
                this._control._selectItem(item, true);
            }
	    }
	    if(e)
	    {
	        this._mouseDown = true;
	        $util.cancelEvent(e);
	    }
	},
	
	unSelectAll:function()
	{
	    if(this._selectedItems != null && this._selectedItems.length > 0)
	    {
	        for(var i in this._selectedItems)
            {
                var item = this._selectedItems[i];
                item._getFlags().setSelected(false);
                this._control._selectItem(item, false);
            }
	    }
	    this._selectedItems = [];
	},
	
	getSelectedItems:function()
	{
	    return this._selectedItems;
	},
	
	hover:function(item, e)
	{
        if(this._control._shouldHover != null && e)
            if(!this._control._shouldHover(item, e))
                return;
	    this._hover(item, true);   
	},
	
	unhover:function(item, e)
	{
       if(this._control._shouldHover != null && e)
            if(!this._control._shouldHover(item, e))
                return;
	   this._hover(item, false);
	},
	
	kbNav:function(item,evnt)
	{
        //this._control._keyPressed(this, evnt.keyCode, item._element, item, evnt);
	},
	
	focus:function(item,select)
	{
	    if(item)
	    {
	        if(!element)
	            element = item.get_element();
	        element.tabIndex = 0; 
	        element.focus();
	        this.select(item);
	    }
	}, 
	
	drag:function(item, e)
	{
	   
	},
	
	dragStop:function(e)
	{
	   
	},
	
	drop:function(item, e)
	{
	   
	},
	
	blur:function(item)
	{
	
	},
	
	_hover:function(item,val)    
	{
        var itemFlags = item._getFlags();
	    var hoverable = itemFlags.getHoverable(this._control);
	    var enabled = itemFlags.getEnabled(this._control);
	        
	    if(hoverable == $IG.DefaultableBoolean.True && enabled == $IG.DefaultableBoolean.True)
	    {
                this._control._hoverItem(item, val);
                itemFlags.setHovered(val);
        }
	},
	
	dispose:function()
	{
	    
	}
	
	
};
$IG.UIBehaviorsObject.registerClass("Infragistics.Web.UI.UIBehaviorsObject");
/******************************************END UI BEHAVIORS OBJECT*********************************/


/******************************************UIBehaviorsType ENUM************************************/
$IG.UIBehaviorsType = function () {}
$IG.UIBehaviorsType.prototype = 
{
   Select:0,
   Hover:1,
   HoverOut:2,
   KBNav:3,
   Drag:4,
   Drop:5
};
$IG.UIBehaviorsType.registerEnum("Infragistics.Web.UI.UIBehaviorsType");
/******************************************END UIBehaviorsType ENUM********************************/
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();