﻿//
// EasyPreviewCollection.class.js
//
// Yvan Rodrigues, Mabel's Labels Inc. http://www.mabel.ca
//
// This javascript class maintains a collection of EasyPreview objects.
//

function EasyPreviewCollection()
{
    // the list's internal storage
    this.previews = new Array();

    // Add an item to the collection
    EasyPreviewCollection.prototype.Add = function(EasyPreview)
    {
        if (this.Contains(EasyPreview))
            return; // ignore it if it's already in the collection.
        this.previews[EasyPreview.ImageId] = EasyPreview;
    }

    // Clear the collection
    EasyPreviewCollection.prototype.Clear = function()
    {
        this.previews = new Array();
    }

    // Associate a control with all previews
    EasyPreviewCollection.prototype.Associate = function(Control)
    {
        for (var a in this.previews)
            this.previews[a].Associate(Control);
    }
	// Remove all controls tied to a preview
    EasyPreviewCollection.prototype.DisassociateAll = function()
    {
        for (var a in this.previews)
            this.previews[a].DisassociateAll();
    }
    // Check if an EasyPreview is contained in the collection
    EasyPreviewCollection.prototype.Contains = function(EasyPreviewId)
    {
        return this.FindById(EasyPreviewId) != null;
    }
    
    // Forces an update of an item in the collection
    // Control (optional) = The control that made the change
    // Token (optional) = The token that should be changed
    EasyPreviewCollection.prototype.Update = function(Control, Token)
    {
        // allow an object or the id of the object
        Control = $(Control);
        ControlId = Control.attr('id');

        for (var a in this.previews)
            for (var b in this.previews[a].Controls)
                if(this.previews[a].Controls[b] == ControlId)
                    this.previews[a].Update(ControlId, Token);
        return true;
    }
 
    // Forces an update of all items in the collection
    EasyPreviewCollection.prototype.UpdateAll = function(Control, Token)
    {
        // allow an object or the id of the object
        Control = $(Control);
        ControlId = Control.attr('id');
        for (a in this.previews)
            this.previews[a].Update(ControlId, Token);
        return true;
    }

    // Gets the EasyPreview in the collection with the specified id,
    // or null if it is not in the collection.
    EasyPreviewCollection.prototype.FindById = function(EasyPreviewId)
    {
        for (a in this.previews)
            if (this.previews[a].ImageId == EasyPreviewId)
                return this.previews[a];
        return null;
    }
}

var previews = window['previews'] = new EasyPreviewCollection();

