﻿/********************************* Item Object **********************************************/
$IG.DataMenuItem = function(adr,element, props, owner, csm, collection, parent)
{
	$IG.DataMenuItem.initializeBase(this, [adr,element, props, owner, csm, collection, parent]);
	this.initialize();
}

$IG.DataMenuItem.prototype =
{   
    _isActive:false,
    
    initialize: function()
    {
    	this._initialClass = this.get_element().className;
    	this._isActive = false;
    	this._initialized = true;
    },
    
    _get_level: function()
    {
        ///<summary>
        /// Gets the level of the item on client. Root items are at level 1.
        ///</summary>
        return this._get_address().split(".").length;
    },
    
    _get_idAttribute: function()
    {
        return this._element.getAttribute("id");
    },

    _navigateOnClick: function()
    {
        var url = this.get_navigateUrl();
        var targetFrame = this.get_target();

        if(targetFrame == null || targetFrame == "")
            targetFrame = "_self";

        window.open(url,targetFrame);
    },

    get_isSeparator:function()
    {
        if(this._get_value($IG.DataMenuItemProps.IsSeparator) < 1)
            return false;
        return true;    
    },
    
    get_text:function()
    {
        ///<summary>
        /// Gets the text content of an item within the Menu.
        ///</summary>
        if(this.get_textElement() != null) return this.get_textElement().innerHTML
        return "[Templated Item]";
    },
    
	set_text:function(text)
	{
		///<summary>
	    /// Sets the text content of an item within the Menu.
	    ///</summary>
	    $IG.DataMenuItem.callBaseMethod(this, 'set_text', [text]);
	    if(this.get_textElement() != null) this.get_textElement().innerHTML = text; 
	},
	
    get_textElement:function() 
    {
		///<summary>
	    /// Returns a reference to the Span Html Element that contains the text of the item.
	    ///</summary>
        return this.get_element().getElementsByTagName("SPAN")[0];
    },
    
    get_isActive: function()
    {
        return this._isActive;
    },
    
    set_active: function(state, forceSelection)
    {
        var realForce = true;
        if(forceSelection != null && typeof(forceSelection) != "undefined") realForce = false;
        if(state)
        {
            $util.addCompoundClass(this.get_element(), this.get_stateCssClass("Active"));
            this._isActive = true;
            if(realForce) this.set_selected(true);
        }
        else
        {
            if(!this.get_expanded())$util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Active"));
            this._isActive = false;            
            this.set_selected(false);
        }
        
    },
    
    /* AS 28/4/2009 #16496 Override set_enabled to apply styles */
    set_enabled: function(value)
    {
        ///<summary>
        /// Sets this item's enabled state
        ///</summary>
        this._getFlags().setEnabled(value);
        if (value)
            $util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Disabled"));
        else
            $util.addCompoundClass(this.get_element(), this.get_stateCssClass("Disabled"));
    },    
    
    get_selected: function() 
    {
		///<summary>
	    /// Returns a boolean value which indicates if this item is currently selected.
	    ///</summary>
        return this._getFlags().getSelected(this._owner);
    },
    
	set_selected: function(value)
	{
		///<summary>
	    /// Sets the item to be selected using the SelectedCssClass
	    ///</summary>
	    this._getFlags().setSelected(value);
        if(value) 
        {
            $util.addCompoundClass(this.get_element(), this.get_stateCssClass("Selected"));
        }
        else
        {
            $util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Selected"));
        }
	},
	
	get_cssClass: function()
	{
	    return this._initialClass;
	},
    
    get_stateCssClass: function(state) 
    {
		///<summary>
	    /// Returns the state CssClass used for this item. 
	    /// Possible states are:
	    /// Active
	    /// Hover
	    /// Selected
	    /// Disabled
	    /// "" (blank) - will get default (initial cssClass)
	    ///</summary>    
       var menu = this._get_owner();
       var stateVal = "";
       var cssClass = "";
       if(this.get_orientation() == $IG.Orientation.Vertical)
       {
           if(this._get_level() < 2) { stateVal = "mivRoot"+state; }
            else { stateVal = "miv"+state; }
       }
       else
       {
           if(this._get_level() < 2) { stateVal = "mihRoot"+state;}
            else { stateVal = "mih"+state; }       
       }
       if(state.toString().toLowerCase() == "hover")
       {
             cssClass = this._owner._itemSettings.get_HoverCssClass();
       }
       if(state.toString().toLowerCase() == "selected")
       {
             cssClass = this._owner._itemSettings.get_SelectedCssClass();
       }
       if(cssClass != null && cssClass.length > 1) return cssClass;
        else return menu._get_clientOnlyValue(stateVal);
    },
    
    get_anchorElement: function()
    {
		///<summary>
	    /// Returns a reference to the Anchor Html Element associated with this item.
	    ///</summary>
        if(!this._anchorElement)
            this._anchorElement =  this.get_element().getElementsByTagName("a")[0];
        return this._anchorElement;
    },
    
    get_navigateUrl : function()
    {
        var a = this.get_anchorElement();
        if(a)
            return a.href;
        else
            return null;
    },
    
    get_target : function ()
    {
        var a = this.get_anchorElement();
        if(a)
            return a.target;
        else
            return null;
    },
    
    get_defaults:function()
    {
        var defaults = $IG.DataMenuItem.callBaseMethod(this, 'get_defaults')
        return defaults.concat([0,0]);
    }, 
       
    get_expanded : function() {
		///<summary>
	    /// Returns a boolean value which indicates true if the item is currently expanded, false otherwise.
	    ///</summary>
	    var val = this._get_value( $IG.DataMenuItemProps.Expanded);
	    return (val == 1) ? true : false;
	},
	
    set_hover : function(value)
    {
        if(value)
        {
            $util.addCompoundClass(this.get_element(), this.get_stateCssClass("Hover"));
        }
        else if(!this.get_expanded())
        {
            $util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Hover"));
        }
    },
    
	set_expanded : function(value) {
		///<summary>
	    /// Sets the expanded state of the item to the passed in value.
	    ///</summary>
        var val = this._get_value( $IG.DataMenuItemProps.Expanded);
        if(val == value) /* Already in the indicated state so return */
            return;
	    this._set_value( $IG.DataMenuItemProps.Expanded, value);
	},
	     
	_get_subgroup : function() {
	    var last = this.get_element().lastChild;
	    /*
	    if( last != null && (last.nodeName == "UL" || last.nodeName == "DIV") )
	        return last;
	    return null;
	    */
	    if(last == null) return null;    
	    if(last.nodeName != "UL" && last.firstChild != null)
	        last = last.firstChild;
	    return (last.nodeName == "UL") ? last : null;
	    
	},
	
	get_childItem:function(index, enabled)
	{
	    /* AS 28/4/2009 appeared while working on 16496, so adding new parameter
            to indicate if we want enabled item
        */
		///<summary>
	    /// Returns the child item at the index specified if it exists.  Null is returned otherwise.
	    /// Optional secondary boolean parameter "enabled" may be supplied to indicate that we want
	    /// Enabled item. In this case the children collection will be traveresed for the nearest enabled item,
	    /// if the item at specified index is not enabled
	    ///</summary>
        var item = this.getItems()._getObjectByIndex(index);
        if (item == null) return null;
        var nextItem = item;
        if (enabled != null && typeof (enabled) != "undefined" && enabled)
        {
            var initialIndex = index;
            var collection = this.getItems();
            while (!nextItem._getFlags().getEnabled(this._owner) || nextItem.get_isSeparator())
            {
                index++;
                if (index >= collection._items.length) index = 0;
                nextItem = collection._items[index];
                if (index == initialIndex || nextItem == item) { nextItem = null; break; };
            }
        }
        return nextItem;
	},
	
	hasChildren:function()
	{
		///<summary>
	    /// Returns a boolean value which indicates whether or not this item has any child items beneath it.
	    ///</summary>
	    var child = this.get_childItem(0);
	    return (typeof(child) != "undefined" && child != null);
	},
	
	getChildrenCount:function()
	{
	    ///<summary>
	    /// Returns the total number of children this item has.
	    ///</summary>
        return this.getItems().getChildrenCount();	    
	},
	
	get_parentItem:function()
	{
		///<summary>
	    /// Returns the parend item of this item.   If this item is at the top of the item hierarchy, null is returned.
	    ///</summary>
	    var index = this._address.lastIndexOf(".");
	    if(index == -1)
	        return null;
	    var adr = this._address.substring(0, index);
	    return this.getItems()._getObjectByAdr(adr);
	},
	
	open:function()
	{
	    ///<summary>
	    /// Opens all sub-menus down to this item
	    ///</summary>
	    var parentItems = [];
	    var i = 0;
	    var parentItem = this.get_parentItem();
	    while(parentItem != null)
	    {
	        parentItems[i] = parentItem;
	        i++;
	        parentItem = parentItem.get_parentItem();
	    }    
	    for (var j = i-1; j >=0; j--)
	    {
	        this._get_owner().__showElement(parentItems[j]._get_subgroup(), parentItems[j], false);
	    }
	},
	
	get_nextItem:function()
	{
		///<summary>
	    /// Returns the last sibling item of this item.  If this is the last item in the collection, null is returned.
	    ///</summary>
	    var i = this._address.lastIndexOf(".");
	    var adr = "";
	    var index = "";
	    if(i == -1) {
	        index = parseInt(this._address);
	    }
	    else {
	        adr = this._address.substring(0, i);
	        adr += "."
	        index = this._address.substring(i + 1);
	    }
	    index = parseInt(index);
	    index++;
	    adr += index;
	    return this.getItems()._getObjectByAdr(adr);
	},
	
	get_previousItem:function()
	{
		///<summary>
	    /// Returns the previous sibling item of this item.  If this is the first item in the collection, null is returned.
	    ///</summary>
	    var i = this._address.lastIndexOf(".");
	    var adr = "";
	    var index = "";
	    if(i == -1) {
	        index = parseInt(this._address);
	    }
	    else {
	        adr = this._address.substring(0, i); 
	        adr += "."
	        index = this._address.substring(i + 1);
	    }
	    index = parseInt(index);
	    index--;
	    adr += index;
	    return this.getItems()._getObjectByAdr(adr);
	
	},
	
	_ensureFlags:function()
	{
	    $IG.DataMenuItem.callBaseMethod(this, '_ensureFlags');
	    this._ensureFlag($IG.ClientUIFlags.Selected, false);
	},
	
   get_orientation : function()
   {
		///<summary>
	    /// Returns an integer value which indicates the orientation the item has!
	    /// Not the orientation its sub-items should have!
	    ///</summary>
	    /* do nothing if not initialized /or disposed/ */
	    if(!this.get_isInitialized()) return;
	    if(this._get_level() < 2)
	    {
	        /* root item, return orientation for the menu */
	        return this._get_owner().get_orientation();
	    }
	    if(this.get_prentItemsGroupSettings() != null)
            return this.get_prentItemsGroupSettings().get_orientation();
        return $IG.Orientation.Vertical;    
	},
	
	restore_style:function()
	{
        if(this.get_selected())
            $util.addCompoundClass(this.get_element(), this.get_stateCssClass("Selected"));
        if(this._getFlags().getHovered())
            $util.addCompoundClass(this.get_element(), this.get_stateCssClass("Hover"));
        if(!this.get_selected())
            $util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Selected"));
        if(!this._getFlags().getHovered() && !this.get_expanded())
            $util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Hover"));
        if(!this.get_expanded())
            $util.removeCompoundClass(this.get_element(), this.get_stateCssClass("Active"));    
	},
	
	get_itemsGroupSettings:function()
	{
        return this._owner._get_itemsGroupSettins(this._get_address());
	},
	
	get_prentItemsGroupSettings:function()
	{
	    if(this.get_parentItem() != null)
	        return this._owner._get_itemsGroupSettins(this.get_parentItem()._get_address());
	    return this._owner.get_menuGroupSettings();
	},
	
	isDescendant:function(item)
	{
        var pItem = this.get_parentItem();
        if(pItem == null)
            return false;
        if( this._get_address() == item._get_address() )
        {
            return true;
        }
        return pItem.isDescendant(item);
	},
	
	calcScrolls:function()
	{
	    scrollObj = new Object();
	    scrollObj.Top = 0;
	    scrollObj.Left = 0;
	    
	    var parent = this.get_element().parentNode;
	    while(parent != null && parent.nodeName != "BODY" && parent.style.position != "static")
	    {
	        scrollObj.Top += parent.scrollTop;
	        scrollObj.Left += parent.scrollLeft;
	        parent = parent.parentNode;
	        if(parent.nodeName == "BODY") break;
	    }
	    
	    return scrollObj;
	},
	
	_toggleClicked: function()
	{
		var oldState = this._get_value($IG.DataMenuItemProps.Clicked, true);
		this._set_value($IG.DataMenuItemProps.Clicked, !oldState);
	},
	
	dispose: function()
	{
	    this._initialized = false;
	}
}
$IG.DataMenuItem.registerClass('Infragistics.Web.UI.DataMenuItem', $IG.NavItem);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();