﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="../jquery-1.2.6-vsdoc.js"/>

// Business

Type.registerNamespace('SCOTT.CSC.Tools.GroceryList');

// Grocery List Objects

// Aisle (ID, Name, SortOrder, Created, Modified)
// Item (ID, AisleID, Name, IsShared, Created, Modified)
// List (ID, UserID, Name, Created, Modified)
// ListItem (ID, ItemID, ListID, SortOrder, Note, Created, Modified)

// !!! Important Note !!!
//
// To ensure compatiblity with the server-side objects via the web services
// it is necessary to have the __type property defined and matching the type
// on the server, especially for collections. While moving objects back and
// forth the __type property is often dropped so the Convert functions below
// ensure the objects are reconstructed properly.

// Aisle (ID, Name, SortOrder, Created, Modified)
SCOTT.CSC.Tools.GroceryList.Aisle = function(data) 
{
    if (data)
    {
        this.ID = { Value : data.ID.Value };
        this.Name = data.Name;
        this.SortOrder = data.SortOrder;
        this.Created = data.Created;
        this.Modified = data.Modified;
    }
    else
    {
        this.ID = { Value : 0 };
        this.Name = '';
        this.SortOrder = 0;
        this.Created = new Date();
        this.Modified = new Date();
    }
};

SCOTT.CSC.Tools.GroceryList.Aisle.ConvertAll = function(data)
{
    var aisles = [];
    var arr = jQuery.makeArray(data);
    var i = 0;
    for (i=0;i<arr.length;i++)
    {
        aisles[i] = new SCOTT.CSC.Tools.GroceryList.Aisle(data[i]);
    }
    return aisles;
};

SCOTT.CSC.Tools.GroceryList.Aisle.prototype =
{
    __type : 'SCOTT.CSC.Model.Tools.GroceryList.Aisle'
};

SCOTT.CSC.Extend(SCOTT.CSC.Tools.GroceryList.Aisle, SCOTT.CSC.BusinessObject);

// Item (ID, AisleID, Name, IsShared, Created, Modified)
SCOTT.CSC.Tools.GroceryList.Item = function(data) 
{
    if (data)
    {
        this.ID = { Value : data.ID.Value };
        this.AisleID = data.AisleID;
        this.Name = data.Name;
        this.IsShared = data.IsShared;
        this.Created = data.Created;
        this.Modified = data.Modified;
    }
    else
    {
        this.ID = { Value : 0 };
        this.AisleID = 0;
        this.Name = '';
        this.IsShared  = false;
        this.Created = new Date();
        this.Modified = new Date();
    }
};

SCOTT.CSC.Tools.GroceryList.Item.ConvertAll = function(data)
{
    var items = [];
    var arr = jQuery.makeArray(data);
    var i = 0;
    for (i=0;i<arr.length;i++)
    {
        items[i] = new SCOTT.CSC.Tools.GroceryList.Item(data[i]);
    }
    return items;
};

SCOTT.CSC.Tools.GroceryList.Item.prototype =
{
    __type : 'SCOTT.CSC.Model.Tools.GroceryList.Item'
};

SCOTT.CSC.Extend(SCOTT.CSC.Tools.GroceryList.Item, SCOTT.CSC.BusinessObject);

// List (ID, UserID, Name, Created, Modified)
SCOTT.CSC.Tools.GroceryList.List = function(data) 
{
    if (data)
    {
        this.ID = { Value : data.ID.Value };
        this.UserID = data.UserID;
        this.Name = data.Name;
        this.IsDeleted = data.IsDeleted;
        this.Created = data.Created;
        this.Modified = data.Modified;
        this.Items = [];
        var i;
        for (i=0;i<data.Items.length;i++)
        {
            this.Items[i] = new SCOTT.CSC.Tools.GroceryList.ListItem(data.Items[i]);
        }
    }
    else
    {
        this.ID = { Value : 0 };
        this.UserID = 0;
        this.Name = 'Untitled';
        this.IsDeleted = false;
        this.Created = new Date();
        this.Modified = new Date();
        this.Items = [];
    }
};

SCOTT.CSC.Tools.GroceryList.List.Convert = function(list)
{
    return new SCOTT.CSC.Tools.GroceryList.List(list);
};

SCOTT.CSC.Tools.GroceryList.List.prototype =
{
    __type : 'SCOTT.CSC.Model.Tools.GroceryList.List',
    AddItem : function(item)
    {
        var listItem = new SCOTT.CSC.Tools.GroceryList.ListItem();
        listItem.ItemID = item.ID.Value;
        listItem.ListID = this.ID.Value;
        this.Items[this.Items.length] = listItem;
        return listItem;
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.Tools.GroceryList.List, SCOTT.CSC.BusinessObject);

// ListItem (ID, ItemID, ListID, AisleID, SortOrder, Note, Created, Modified)
SCOTT.CSC.Tools.GroceryList.ListItem = function(data) 
{
    if (data)
    {
        this.ID = { Value : data.ID.Value };
        this.ItemID = data.ItemID;
        this.ListID = data.ListID;
        this.SortOrder = data.SortOrder;
        this.Note = data.Note;
        this.Created = data.Created;
        this.Modified = data.Modified;
    }
    else
    {
        this.ID = { Value : 0 };
        this.ItemID = 0;
        this.ListID = 0;
        this.SortOrder = 0;
        this.Note = '';
        this.Created = new Date();
        this.Modified = new Date();
    }
};

SCOTT.CSC.Tools.GroceryList.ListItem.Convert = function(listItem)
{
    var newItem = new SCOTT.CSC.Tools.GroceryList.ListItem(listItem);
    return newItem;
};

SCOTT.CSC.Tools.GroceryList.ListItem.prototype =
{
    __type : 'SCOTT.CSC.Model.Tools.GroceryList.ListItem'
};

SCOTT.CSC.Extend(SCOTT.CSC.Tools.GroceryList.ListItem, SCOTT.CSC.BusinessObject);
