﻿/*--------------------------------------------- 
    Stéphane Forget
    Centre Canadien d'architecture
    mai 2009
------------------------------------------------*/

var selectedGroup = null;

function GetTmsGroupChilds(parentID) {
    var parentElement = $get('groupTBody' + parentID);
    var parentContainer = parentElement.insertRow(-1).insertCell(-1);
    var containers = new ObjectsTable(parentID);
    parentContainer.colSpan = 2;
    parentContainer.className = 'dataCellGroupDetail'
    containers.Parent = parentContainer;
    containers.Parent = parentContainer;
    if (!containers.Exists()) {

        parentContainer.id = 'toto';
        parentElement.className = 'GroupDetailDetailOpe n';
        containers.IsFromArchive = true;
        containers.Create();
        containers.SetClassName('GroupChildsTable');
        containers.AddGroupChilds(parentID);
    } else {
        if (!containers.IsFull()) {
            parentContainer.id = 'tata';
            containers.IsFromArchive = true;
            containers.Create();
            containers.SetClassName('GroupChildsTable');
            containers.AddGroupChilds(parentID);
        } else {
            var groupTbody = new TmsGroupDisplay(parentElement, parentID);
            groupTbody.Toggle();
            groupTbody = null;
        }
    }
}


function ObjectsTable(containerID) {
    this.ID = containerID;
    this.Name = 'Containers' + this.ID;
    this.Parent = null;
    this.ClassName = null;
    this.Containers = null;
    this.DisplayObjects = null;
    this.IsLoaded = false;
    this.Currentdisplay = null;
    this.IsFromArchive = false;
    this.Length = 0;
    this.Exists = function() {
        this.Containers = $get(this.Name);
        if (this.Containers) { return true; } else { return false; }
    };

    this.IsFull = function() {
        if (this.Exists()) {
            if ($get('groupTBody' + this.ID).rows.length > 0) {
                return true;
            } else {
                return false;
            }
        } else { return false; }

    }

    this.FillWithObject = function(tmsObject) {
        this.Containers = this.Parent;
        this.AddGroupObject(tmsObject);
    }

    this.Create = function() {
        if (!this.Exists()) {
            this.Containers = document.createElement('div');
            this.Containers.className = 'ObjectsContainers';
            this.Containers.id = this.Name;
            this.Parent.appendChild(this.Containers);
        }
        return this.Exists();
    }

    this.SetClassName = function(className) {
        this.ClassName = className;
        this.Containers.className = this.ClassName;
    }

    this.GetNewContainer = function() {
        //return this.Containers.insertRow(-1).insertCell(-1);
    }

    this.AddGroupObject = function(tmsObject) {
        this.DisplayObjects = new Array();
        this.DisplayObjects[0] = new Objectdisplay(this.Containers, tmsObject);
        this.DisplayObjects[0].IsFromArchive = this.IsFromArchive;
        //this.DisplayObjects[0].TMSObject.HasChild = 1;
        this.DisplayObjects[0].Create();
        this.IsLoaded = false;
    }

    this.loadTmsObjects = function(tmsObjects) {
        if (!selectedContainers.IsLoaded) {
            selectedContainers.DisplayObjects = new Array();
            for (var key in tmsObjects) {
                var container = selectedContainers.Containers; //.GetNewContainer();
                  selectedContainers.DisplayObjects[key] = new Objectdisplay(container, tmsObjects[key]);
                 selectedContainers.DisplayObjects[key].IsFromArchive = selectedContainers.IsFromArchive;
                 selectedContainers.DisplayObjects[key].Create();
            }
            var loadingGif = new LoadingIndicator(selectedContainers);
            loadingGif.Get('loadiingdiv' + selectedContainers.ID);
            if (loadingGif.Exists == true) {
                loadingGif.Toggle();
            }

            selectedContainers.IsLoaded = true;
        }
    }


    var selectedContainers = null;
    this.AddGroupChilds = function(parentID) {
        selectedContainers = this;
        var loadingGif = new LoadingIndicator(selectedContainers);
        loadingGif.Get('loadiingdiv' + this.ID);
        if (loadingGif.Exists == true) {
            loadingGif.Toggle();
        }
        Gel.CollectionObject.GetGroupChilds(parentID, this.loadTmsObjects, onFail);
    }

}




function Objectdisplay(parentContainer, tmsObject) {
    this.ParentContainer = parentContainer;
    this.ObjectContainer = null;
    this.TMSObject = new TmsObject(tmsObject);
    this.TopTBody = null;
    this.DetailTBody = null;
    this.GroupDisplay = null;
    this.IsFromArchive = false;
    this.ToolBar = null;

    this.Create = function() {
        this.ObjectContainer = document.createElement('table');
        this.ObjectContainer.id = 'objectTable' + this.TMSObject.ObjectID;
        this.ObjectContainer.className = 'objectHeaderTBody';
        this.ParentContainer.appendChild(this.ObjectContainer);
        this.TopTBody = new TmsTopDisplay(this.ObjectContainer, this.TMSObject);
        this.TopTBody.IsFromArchive = this.IsFromArchive;
        this.TopTBody.Create();
        this.ToolBar = new ActionBar(this.ObjectContainer, this.TMSObject);
        this.ToolBar.IsFromArchive = this.IsFromArchive;
        this.ToolBar.Create();
        this.DetailTBody = new TmsDetailDisplay(this.ObjectContainer, this.TMSObject);
        this.DetailTBody.Create();
        this.GroupDisplay = new TmsGroupDisplay(this.ObjectContainer, this.TMSObject.ObjectID);
        this.GroupDisplay.Create();
    }
}


function Action(parentContainer) {
    this.ParentContainer = parentContainer;
    this.ID = null;
    this.ObjectID = null;
    this.Container = null;
    this.OnClique = null;
    this.ClassName = null;
    this.Image = null;
    this.Label = null;
    this.HasArrow = false;
    this.IsActive = true;

    this.Create = function(element) {
        this.Container = document.createElement(element);


        var innerHtml = '<' + element;
        if (this.ID) {
            innerHtml += ' id=' + this.ID;
        }
        if (this.IsActive == true) {
            innerHtml += ' class="' + this.ClassName;
            innerHtml += '" onclick="' + this.OnClique + '"';
            innerHtml += ' onmouseover = "this.style.color=' + "'#2be62b';" + '"';
            innerHtml += ' onmouseout = "this.style.color=' + "'#363636';";
        } else {
            innerHtml += ' class="inactiveAction"';
        }
        innerHtml += '">' + this.Label + '</' + element + '>';
        this.Container.innerHTML = innerHtml
        this.ParentContainer.appendChild(this.Container);
        if (this.HasArrow) {
            var arrow = document.createElement('img');
            arrow.id = 'arrow_' + this.ObjectID;
            arrow.src = "./Images/readMore_arrow_grey.png";
            arrow.className = 'objectDetailImg';
            this.ParentContainer.appendChild(arrow);
        }
    }

}

function LoadingIndicator(parentContainer) {
    this.SRC = 'Images/loading4.gif';
    this.Parent = parentContainer;
    this.alt = 'loading';
    this.Container = null;
    this.ClassName = 'loadingGif2';
    this.Name = null;
    this.IndicatorHeight = '20px';
    this.IndicatorWidth = '20px';
    this.Exists = false;

    this.Get = function(name) {
        this.Container = $get(name);
        if (this.Container) { this.Exists =  true; }
    }

    this.Create = function(name) {
        this.Container = document.createElement('div');
        this.Container.className = this.ClassName;
        this.Name = name;
        this.Container.id = this.Name;
        this.Parent.appendChild(this.Container);
        var img = document.createElement('img');
        img.src = this.SRC;
        img.alt = this.alt;
        img.style.height = this.IndicatorHeight;
        //img.style.paddingLeft = '20px';
        img.style.width = this.IndicatorWidth;
        this.Container.appendChild(img);
        this.Container.style.display = 'none';
        
    }

    this.Toggle = function() {
        if (this.Container) {
            if (this.Container.style.display == 'none') {
                this.Container.style.display = 'block';
            } else {
                this.Container.style.display = 'none';
            }
        }
    }
}


function ActionBar(parentContainer, tmsObject) {
    this.Actions = new Array();
    this.MainContainer = null;
    this.ParentContainer = parentContainer;
    this.TMSObject = tmsObject;
    this.IsFromArchive = false;
    this.LoadgingGif = null;

    this.CreateSelectionContainer = function() {
        var actionCell = this.ParentContainer.insertRow(-1).insertCell(-1);
        actionCell.colSpan = 2;
        var actionDiv = document.createElement('div');
        actionDiv.className = 'objectDetailAction';
        actionDiv.id = 'actionDiv' + this.TMSObject.ObjectID;
        var action = new Action(actionDiv);
        action.ClassName = 'objectDetailLink';
        action.ID = 'contentActionSpan' + this.TMSObject.ObjectID;
        action.Label = 'Envoyer';
        action.OnClique = "var selection = new SelectionsContainer(); selection.Delete(" + tmsObject.ObjectID + "); selection = null";
        action.HasArrow = false;
        this.Actions[this.Actions.length] = action;
        actionCell.appendChild(actionDiv);
        for (var i = 0; i < this.Actions.length; i++) {
            if (i > 0) {
                var sep = document.createElement('span');
                sep.className = 'objectDetailSep';
                sep.innerHTML = '|';
                actionDiv.appendChild(sep);
            }
            this.Actions[i].ObjectID = this.TMSObject.ObjectID;
            this.Actions[i].Create('span');
        }
    }

    this.CreateSelection = function() {
        var actionCell = this.ParentContainer.insertRow(-1).insertCell(-1);
        actionCell.colSpan = 2;
        var actionDiv = document.createElement('div');
        actionDiv.className = 'objectDetailAction';
        actionDiv.id = 'actionDiv' + this.TMSObject.ObjectID;
        var action = new Action(actionDiv);
        action.ClassName = 'objectDetailLink';
        action.ID = 'contentActionSpan' + this.TMSObject.ObjectID;
        action.Label = 'effacer';
        action.OnClique = "var selection = new SelectionsContainer(); selection.Delete(" + tmsObject.ObjectID + ");";
        action.HasArrow = false;
        this.Actions[this.Actions.length] = action;
        actionCell.appendChild(actionDiv);
        for (var i = 0; i < this.Actions.length; i++) {
            if (i > 0) {
                var sep = document.createElement('span');
                sep.className = 'objectDetailSep';
                sep.innerHTML = '|';
                actionDiv.appendChild(sep);
            }
            this.Actions[i].ObjectID = this.TMSObject.ObjectID;
            this.Actions[i].Create('span');
        }
    }

    this.Create = function() {
        var actionCell = this.ParentContainer.insertRow(-1).insertCell(-1);
        actionCell.colSpan = 2;
        var actionDiv = document.createElement('div');
        actionDiv.className = 'objectDetailAction';
        actionDiv.id = 'actionDiv' + this.TMSObject.ObjectID;
        actionCell.appendChild(actionDiv);

        var ShowContentaction = new Action(actionDiv);
        ShowContentaction.Label = labels.__lblContains;
        ShowContentaction.ID = 'contentActionSpan' + this.TMSObject.ObjectID;
        ShowContentaction.HasArrow = true;
        ShowContentaction.IsActive = false;

        if (this.TMSObject.HasChild == true) {
            if ((this.TMSObject.ClassificationID == 4) | (this.TMSObject.ClassificationID == 2) | (this.TMSObject.Classification == "série") | (this.TMSObject.Classification == "sous-série")
            //  | (this.TMSObject.ClassificationID == 12) | (this.TMSObject.Classification == "dossier") | (this.TMSObject.Classification == "sous-dossier")
                | (this.TMSObject.Classification == "volume") | (this.TMSObject.Classification == "groupe logique") | (this.TMSObject.Classification == "sous-groupe") | (this.TMSObject.Classification == "groupe") | (this.TMSObject.Classification == "groupe logique")) {
                ShowContentaction.ClassName = 'objectDetailLink';
                ShowContentaction.OnClique = "GetTmsGroupChilds(" + this.TMSObject.ObjectID + ")";
                ShowContentaction.IsActive = true;
            }
        }
        this.Actions[this.Actions.length] = ShowContentaction;

        var expandAction = new Action(actionDiv);
        expandAction.ID = 'actionSpan' + this.TMSObject.ObjectID;
        expandAction.Label = labels.__lblMore;
        expandAction.HasArrow = true;
        expandAction.IsActive = false;

        if (this.TMSObject.CanExpand == true) {
            if ((this.TMSObject.Title.length > 0) | (this.TMSObject.DisplayName.length > 0)) {
                expandAction.ClassName = 'objectDetailLink';
                expandAction.OnClique = "GetObjectDetail('" + this.TMSObject.ObjectID + "','DetailTBody" + this.TMSObject.ObjectID + "'," + 1 + ')';
                expandAction.IsActive = true;
            }
        }
        this.Actions[this.Actions.length] = expandAction;


        var action = new Action(actionDiv);
        action.ClassName = 'objectDetailAddHist';
        action.ID = 'addToSelection' + this.TMSObject.ObjectID;
        action.Label = labels.__lblAddToList + ' +';
        //       action.OnClique = "AddToSelection_(" + this.TMSObject.ObjectID + ",'" + this.TMSObject.Title.replace("'", "&#39;") + "','" + this.TMSObject.ImagePath + "','" + this.TMSObject.ObjectNumber + "','selectionTable')";
        action.HasArrow = false;
        action.IsActive = false;

        this.Actions[this.Actions.length] = action;



        if (!this.IsFromArchive) {
            if (this.TMSObject.ArchiveName) {
                var archivesPaths = this.TMSObject.ArchivePath.split("^");
                if (archivesPaths) {
                    if (archivesPaths.length > 1) {
                        var firstPath = ''
                        var j = archivesPaths.length - 1;
                        while ((firstPath.length < 2) & (j >= 0)) {
                            firstPath = archivesPaths[j].split('$');
                            j -= 1;
                            var firstPathNumber = firstPath[0];
                            var firstObjectID = firstPath[2];
                        }
                        var sArchive = 'GetArchivesParents2(' + firstObjectID + ')';
                        var action = new Action(actionDiv);
                        action.ClassName = 'objectDetailAddHist';
                        action.ID = 'actionSpan' + this.TMSObject.ArchiveName;
                        action.Label = this.TMSObject.ArchiveName;
                        action.IsActive = this.TMSObject.ArchiveStatus == 0;
                        action.OnClique = sArchive;
                        action.HasArrow = false;
                        this.Actions[this.Actions.length] = action;
                    }
                }
            }
        }
        
        
        
        for (var i = 0; i < this.Actions.length; i++) {
            if (i > 0) {
                var sep = document.createElement('span');
                sep.className = 'objectDetailSep';
                sep.innerHTML = '|';
                actionDiv.appendChild(sep);
            }
            this.Actions[i].ObjectID = this.TMSObject.ObjectID;
            this.Actions[i].Create('span');
        }

        this.LoadgingGif = new LoadingIndicator(actionDiv);
        this.LoadgingGif.Create('loadiingdiv' + this.TMSObject.ObjectID);

    }
    
}





function TmsTopDisplay(parentContainer,tmsObject) {
    this.id = "TopTBody" + tmsObject.ObjectID;
    this.TopTBody;
    this.Data = null;
    this.className = 'objectHeaderTBody';
    this.ParentContainer = parentContainer;
    this.IsFromArchive = false;
    this.Thumbnail;
    this.Container=null;
    this.TMSObject = tmsObject;
    this.Create = function() {
        this.TopTBody = document.createElement('tbody');
        this.TopTBody.id = this.id;
        this.TopTBody.className = this.className;
        this.ParentContainer.appendChild(this.TopTBody);
        this.Container = this.TopTBody.insertRow(-1);
        this.Thumbnail = new TmsThumnail(this.Container, this.TMSObject);
        this.Thumbnail.SetImage();
        this.Data = new TmsTopData(this.TMSObject, this.Container);
        this.Data.IsFromArchive = this.IsFromArchive;
        this.Data.SetData(this.Thumbnail.Exists);
    }
}

function TmsTopData(tmsObject, parentContainer) {
    this.ParentContainer = parentContainer;
    this.IsFromArchive = false;
    this.TMSObject = tmsObject;
    this.Container = null;
    this.SetSelection = function(HasImage) {
        this.Container = this.ParentContainer.insertCell(-1);
        this.Container.className = "dataCell";
        if (!HasImage) {
            this.Container.colSpan = 2;
        }
        if (this.TMSObject.Title) {
            var par = getNewParagraph(this.TMSObject.Title, 'SelectionP');
            par.onclick = function() { getCcaObjectByIDWS(tmsObject.ObjectID); }
            this.Container.appendChild(par);
            par = null;
        }
    }


    this.SetData = function(HasImage) {
        this.Container = this.ParentContainer.insertCell(-1);
        this.Container.className = "dataCell";
        if (!HasImage) {
            this.Container.colSpan = 2;
        }

        if (this.IsFromArchive == true) {
            this.Container.appendChild(getNewParagraph(this.TMSObject.ObjectCount + ' ' + this.TMSObject.ObjectName, 'detailP'));
            this.Container.appendChild(getNewParagraph(this.TMSObject.DisplayName, 'detailP'));
            if (this.TMSObject.Title) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.Title, 'detailP'));
            }
            if (this.TMSObject.DisplayDate) {
                if (this.TMSObject.DisplayDate.length > 0) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.DisplayDate, 'detailP'));
                }
            } else {
                if (this.TMSObject.Date) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.Date, 'detailP'));
                }
            }
            if (this.TMSObject.ObjectNumber) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.ObjectNumber, 'detailP'));
            }
            if (this.TMSObject.Description) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.Description, 'detailP'));
            }
            if (this.TMSObject.Medium) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.Medium, 'detailP'));
            }
            if (this.TMSObject.Dimensions) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.Dimensions, 'detailP'));
            }
            if (this.TMSObject.Inscribed) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.Inscribed, 'detailP'));
            }
            if (this.TMSObject.Historique) {
         //       this.Container.appendChild(getNewParagraph(this.TMSObject.Historique, 'detailP'));
            }
            if (this.TMSObject.CurContainer) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.CurContainer, 'detailP'));
            }
            if (this.TMSObject.ColNumber) {
                if (this.TMSObject.ColNumber.length > 0) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.ColNumber, 'detailP'));
                }
            }

        } else {
            this.Container.appendChild(getNewParagraph(this.TMSObject.ObjectCount + ' ' + this.TMSObject.ObjectName, 'detailP'));
            this.Container.appendChild(getNewParagraph(this.TMSObject.DisplayName, 'DisplayName'));
            if (this.TMSObject.Title) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.Title, 'detailP'));
            }
            //'DisplayName'));
            if (this.TMSObject.DisplayDate) {
                if (this.TMSObject.DisplayDate.length > 0) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.DisplayDate, 'detailP'));
                }
            } else {
                if (this.TMSObject.Date) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.Date, 'detailP'));
                }
            }
            if (this.TMSObject.ObjectNumber) {
                this.Container.appendChild(getNewParagraph(this.TMSObject.ObjectNumber, 'detailP'));
            }
            if (this.TMSObject.Description) {
                if (CheckIfIsArchive(this.TMSObject.ClassificationID)) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.Description, 'detailP'));
                }
            }
            if (this.TMSObject.ColNumber) {
                if (this.TMSObject.ColNumber.length > 0) {
                    this.Container.appendChild(getNewParagraph(this.TMSObject.ColNumber, 'detailP'));
                }
            }
        }
    }
}


function TmsThumnail(parentRow, tmsObject) {
    this.Exists = false;
    if (tmsObject.ImagePath) {
        this.Exists = tmsObject.ImagePath.length > 0;
    } 
    this.Container = true;
    this.ParentContainer = parentRow;
    this.className = "objectCell";

    this.SetSelectionImage = function() {
        if (this.Exists) {
            this.Container = this.ParentContainer.insertCell(-1);
            var thumbnail = document.createElement('img');
            thumbnail.className = 'SelectionthumbNailImg';
            thumbnail.onclick = function() {
                window.open("http://svrdam.cca.qc.ca/ZooMI/Default.aspx?obj=" + tmsObject.ObjectNumber, "ZoomImage", "scrollbars=no,menubar=no,toolbar=0,width=1000,height=620");
            };
            thumbnail.alt = tmsObject.ObjectNumber;
            thumbnail.src = tmsObject.ImagePath;
            this.Container.appendChild(thumbnail);
        }
    }

    this.SetImage = function() {
        if (this.Exists) {
            this.Container = this.ParentContainer.insertCell(-1);
            this.Container.className = "thumbNailCell";
            var thumbnail = document.createElement('img');
            thumbnail.className = 'thumbNailImg';
            var thumbNailDiv = document.createElement('div');
            thumbNailDiv.className = 'objectThumbnailCell';
            thumbNailDiv.appendChild(thumbnail);
            thumbnail.onclick = function() {
            window.open("http://svrdam.cca.qc.ca/ZooMI/Default.aspx?obj=" + tmsObject.ObjectNumber, "ZoomImage", "scrollbars=no,menubar=no,toolbar=0,width=1000,height=620");
            };
            thumbnail.alt = tmsObject.ObjectNumber;
            thumbnail.src = tmsObject.ImagePath;
            this.Container.appendChild(thumbNailDiv);
        }
    }
}




function TmsDetailDisplay(container, tmsObject) {
    this.id = "DetailTBody" + tmsObject.ObjectID;
    this.DetailTBody;
    this.className = 'objectDetail';
    this.Container = container;
    this.Create = function() {
        this.DetailTBody = document.createElement('tbody');
        this.DetailTBody.id = this.id;
        this.DetailTBody.className = this.className;
        this.Container.appendChild(this.DetailTBody);
    }
    
}



function TmsGroupDisplay(parentContainer,groupID) {
    this.GroupID = groupID;
    this.id = 'groupTBody' + this.GroupID;
    this.className = 'objectDetail';
    this.ParentContainer = parentContainer;
    this.arrayOfTmsObjects = null;
    this.IsLoaded = false;
    this.IsDisplay = false;
    this.GroupTBody = null;

    function Exists() {
        return $get(this.id) != null;
    }
    this.Exists = Exists;
 
    this.IsFull = function (){
        if($get(this.id).rows.length > 0)
        { return true;} 
        else {return false;}
    }
    
    function Create() {
        this.GroupTBody = document.createElement('tbody');
        this.GroupTBody.id = this.id;
        this.GroupTBody.className = this.className;
        this.ParentContainer.appendChild(this.GroupTBody);
    }
    this.Create = Create;

    this.Hide = function () {
        if (this.Exists()) {
            $get(this.id).style.display = 'none';
            this.IsDisplay = false;
        }
    }

    this.Show = function () {
        if (this.Exists()) {
            $get(this.id).style.display = 'block';
            this.IsDisplay = true;
        }
    }

    this.Toggle = function () {
        if ($get(this.id).style.display == 'none') {
            this.Show();
        } else { this.Hide(); }
    }

    function loadTmsObjects(tmsObjects) {
        this.arrayOfTmsObjects = new Array(tmsObjects.length);
        if (!this.IsLoaded) {
            for (var i = 0; i < arrayOfTmsObjects.length; i++) {
                arrayOfTmsObjects[i] = new TmsObject(tmsObjects[i]);
            }
        }
    }
    this.loadTmsObjects = loadTmsObjects;

    function Fill() {
        if (this.Exists()) {
            if (!this.IsLoaded) {
                Gel.CollectionObject.GetGroupChilds(this.GroupID, this.loadTmsObjects, onFail);
            }
        }
    }
    this.Fill = Fill;
}

function TmsObjectDisplay(tmsObject, objectsContainer) {
    
}


function TmsChildGroupDisplay(tmsObject,tableObject) {
    var _TmsObject = new TmsObject(tmsObject);
    var objectCell = tableObject.insertRow(-1).insertCell(-1);
    var objectTable = document.createElement('table');
    objectTable.cellPading = 0;
    objectTable.cellSpacing = 0;
    objectTable.id = 'objectTable' + _TmsObject.ObjectID;
    var objectHeaderTBody = document.createElement('tbody');
    objectTable.appendChild(objectHeaderTBody);
    objectTable.className = 'objectHeaderTBody';
    var objectDetailTBody = document.createElement('tbody');
    objectDetailTBody.id = 'objectDetailTBody' + _TmsObject.ObjectID;
    objectDetailTBody.className = 'objectDetail';
    objectTable.appendChild(objectDetailTBody);
}




function Selection(parentContainer,tmsObject) {
    this.Container = parentContainer;
    this.TMSObject = tmsObject;
    this.Create = function() {
        var row = this.Container.insertRow(-1);
        row.id = 'selection_' + this.TMSObject.ObjectID;
        var thumbNail = new TmsThumnail(row, this.TMSObject);
        thumbNail.SetSelectionImage();
        var topBody = new TmsTopData(this.TMSObject, row);
        topBody.SetSelection(thumbNail.Exists);
        topBody = null;
        thumbNail = null;
        var actionBar = new ActionBar(this.Container, this.TMSObject);
        actionBar.CreateSelection();
    }
}


function AddToSelection_(objectID, objectTitle, imagePath, objectNumber) {
    var TMSObject = new TmsObject(null);
    TMSObject.ObjectID = objectID;
    TMSObject.Title = objectTitle;
    TMSObject.ImagePath = imagePath;
    TMSObject.ObjectNumber = objectNumber;
    var selectionContainer = new SelectionsContainer();
    selectionContainer.Add(TMSObject);
    
    TMSObject = null;
    selectionContainer = null;
}


function SelectionsContainer() {
    this.Get = function() {
        return $get('selectionTable');
    }

    this.Show = function() {
        showLeftPanel($get('selectionTab'), 'selectionDiv');
    }

    this.Add = function(tmsObject) {
        this.Show();
        if (this.Get().rows.length == 0) {
            var actionBar = new ActionBar(this.Get(),tmsObject);
            actionBar.CreateSelectionContainer();
        }
        for (var r in this.Get().rows) {
            if (this.Get().rows[r].id == 'selection_' + tmsObject.ObjectID) {
                return;
            }
        }
        var selection = new Selection(this.Get(), tmsObject);
        selection.Create();
        selection = null;
    }
    
    this.Delete = function(objectID){
        for(var r in this.Get().rows){
            if(this.Get().rows[r].id == 'selection_' + objectID){
                this.Get().deleteRow(r);
                this.Get().deleteRow(r);
                return;
            }
        }
    }

}


function TmsObject(tmsObject){
    if (tmsObject == null) {
        this.ObjectID = null;
        this.DisplayName = null;
        this.Title = null;
        this.DisplayDate = null;
        this.ObjectNumber = null;
        this.ObjectName = null;
        this.ClassificationID = null;
        this.Classification = null;
        this.ImagePath = null;
        this.NbResults = null;
        this.Description = null;
        this.ObjectCount = null;
        this.Dimensions = null;
        this.CopyRight = null;
        this.Inscribed = null;
        this.isArchive = null;
        this.ArchivePath = null;
        this.ArchiveName = null;
        this.ArchiveID = null;
        this.ColNumber = null;
        this.ParentID = null;
        this.CanExpand = null;
        this.HasChild = null;
        this.Medium = null;
        this.CurContainer = null;
        this.Historique = null;
        this.Date = null;
        this.ArchiveStatus = null;
       
    }
    else {
        this.ObjectID = tmsObject.ObjectID;
        this.DisplayName = tmsObject.DisplayName;
        this.Title = tmsObject.Title;
        this.Date = tmsObject.Date;
        this.DisplayDate = tmsObject.DisplayDate;
        this.ObjectNumber = tmsObject.ObjectNumber;
        this.ObjectName = tmsObject.ObjectName;
        this.ClassificationID = tmsObject.ClassificationID;
        this.Classification = tmsObject.Classification;
        this.ImagePath = tmsObject.MainImagePath;
        this.NbResults = tmsObject.NbResults;
        this.Description = tmsObject.Description;
        this.ObjectCount = tmsObject.ObjectCount;
        this.Dimensions = tmsObject.Dimensions;
        this.CopyRight = tmsObject.CopyRight;
        this.Inscribed = tmsObject.Inscribed;
        this.isArchive = tmsObject.isArchive;
        this.ArchivePath = tmsObject.ArchivePath;
        this.ArchiveName = tmsObject.ArchiveName;
        this.ArchiveID = tmsObject.ArchiveID;
        this.ColNumber = tmsObject.ColNumber;
        this.ParentID = tmsObject.ParentID;
        this.CanExpand = tmsObject.CanExpand == true;
        this.HasChild = tmsObject.HasChild == true;
        this.Medium = tmsObject.Medium;
        this.CurContainer = tmsObject.CurContainer;
        this.Historique = tmsObject.Historique;
        this.ArchiveStatus = tmsObject.ArchiveStatus;
        
    }
}



var ADisplay =
{
    show: function(site) {
        _delete_Table();
    },
    _delete_Table: function() {
        var tableName = $get('authorityTable');
        _deleteAllTableRow(tableName);
        return;
    },
    _deleteAllTableRow: function(tableName) {
        for (var i = tableName.rows.length - 1; i >= 0; i--) {
            tableName.deleteRow(i);
        }
        return
    }
}


function fillSit2e(site) {
    var tableName = $get('authorityTable');
    deleteAllTableRow(tableName);
    var dataCell = tableName.insertRow(-1).insertCell(-1);
    dataCell.id = "authorityDataCell";
    $get('seeAllAuth').innerHTML = site.SiteName;
    $get('btnSeeAll').onclick = function() {
        //isAuthority = true;
        GetAuthorityObjects(site.SiteID, 2, site.SiteName, true);
        closeAuthorityDiv();
        $get('TextBoxQuickSearch').value = site.SiteName;
    };
}





/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/



var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 255) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 266) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}
