﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="../jquery-1.2.6-vsdoc.js"/>

// Utility

SCOTT.CSC.Tools.GroceryList.Utility = function(){};

SCOTT.CSC.Tools.GroceryList.Utility.prototype =
{
    GetItemByElementID: function(elementId)
    {        
        var id = parseInt(elementId.slice('Item-'.length), 10);
                
        var items = this.GetItems();
        if (items !== undefined)
        {
            var i;
            for (i=0;i<items.length;i++)
            {
                if (items[i].ID.Value === id)
                {
                    return items[i];
                }
            }
        }
        return undefined;
    },
    GetDisplayItemByElementID: function(elementId, displayItems)
    {
        var index = parseInt(elementId.slice('ListItem-'.length), 10);
                
        if (displayItems !== undefined)
        {
            var i;
            for (i=0;i<displayItems.length;i++)
            {
                if (displayItems[i].Index === index)
                {
                    return displayItems[i];
                }
            }
        }
        return undefined;
    },
    GetItems : function()
    {
        if (this.SelectedAisle && this.SelectedAisle.Items)
        {
            return this.SelectedAisle.Items;
        }
        else
        {
            return undefined;
        }
    },
    GetAisleByID : function(aisleId)
    {
        if (!this.Aisles)
        {
            return undefined;
        }
        
        var i;
        for (i=0;i<this.Aisles.length;i++)
        {
            var aisle = this.Aisles[i];
            if (aisle.ID.Value === itemId)
            {
                return aisle;
            }
        }
        
        return undefined;
    },
    GetItemByID : function(itemId)
    {
        if (!this.Items)
        {
            return undefined;
        }
        
        var i;
        for (i=0;i<this.Items.length;i++)
        {
            var item = this.Items[i];
            if (item.ID.Value === itemId)
            {
                return item;
            }
        }
        
        return undefined;
    },
    ResetTimer : function()
    {
        this.StartTime = undefined;
    },
    GetTime : function()
    {
        // Usage:
        // this.ResetTimer();
        // console.log('start - ' + this.GetTime());
        // console.log('end - ' + this.GetTime());
        
        // support for measuring performance
        var time = new Date().getTime();
        if (this.StartTime === undefined)
        {
            this.StartTime = time;
        }
        return time - this.StartTime;
    }
};

// Add Utility functions to Application
SCOTT.CSC.Extend(SCOTT.CSC.Tools.GroceryList.Application, 
    SCOTT.CSC.Tools.GroceryList.Utility);
