/*
 * Copyright 2002-2006 Jahia Ltd
 *
 * Licensed under the JAHIA COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (JCDDL), 
 * Version 1.0 (the "License"), or (at your option) any later version; you may 
 * not use this file except in compliance with the License. You should have 
 * received a copy of the License along with this program; if not, you may obtain 
 * a copy of the License at 
 *
 *  http://www.jahia.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
/**
 * Subclass of DwtListView that is used to display the page information
 *
 * @param parent    The DwtComposite nesting this ListView
 * @param headers   An Array of Arrays containing all the data for the column headers
 * @param pattern   The pattern to use to display the data
 */
function PageInfoListView(parent, headers, pattern, props) {
    this._tableWidth = 0;
    this._headers = this._getHeaderList(headers);
    this._columns = headers.length;
    this._pattern = pattern;
    this._props = props;
    this.currentHTML = "";

    var cssClass = props.CURRENT_STYLE == ComplexTreeProperties.FILEMANAGER_STYLE ? 'DwtListView scrollable' : 'DwtListView';
    DwtListView.call(this, parent, cssClass, DwtControl.ABSOLUTE_STYLE, this._headers, true);
}

PageInfoListView.prototype = new DwtListView;
PageInfoListView.prototype.constructor = PageInfoListView;

PageInfoListView.COLUMN_BETWEEN_SPACING = 4;

PageInfoListView.prototype.toString =
function() {
    return "PageInfoListView";
}

PageInfoListView.prototype.clear =
function() {
    this._resetList();
    this.removeAll(true);
    this._list = null;

    this._tableWidth = null;
    this._headers = null;
    this._columns = null;
    this._pattern = null;
    this._props = null;
    this.currentHTML = null;
    DwtComposite.prototype.dispose.call(this);
}

/**
 * Returns the number of columns
 */
PageInfoListView.prototype.getColumnCount =
function() {
    return this._columns;
}

/**
 * Returns the minimal width allowed for this ListView depending on the number of columns and their size.
 */
PageInfoListView.prototype.getMinTableWidth =
function() {
    return this._tableWidth;
}

/**
 * Called by DwtListView to draw a row representing a single item.
 */
PageInfoListView.prototype._createItemHtml =
function(item, now, itemIndex) {
    var div = document.createElement("div");
    var base = "Row";
    if (this._pattern == ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN ||
        this._pattern == ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN) {
        base = "RowIcon";
    }
    div._styleClass = base;
    div._selectedStyleClass = [base, DwtCssStyle.SELECTED].join("-");
    // Row-selected

    this.associateItemWithElement(item, div, DwtListView.TYPE_LIST_ITEM);
    div.className = div._styleClass;

    if (item.isHidden()) {
        div.className += "-hidden";
        return div;
    }

    var htmlArr = new Array();
    var idx = 0;
    var listViewWidth = this.getSize().x - 30;
    if ((this._pattern == ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN && (itemIndex - 1) % 3 == 0) ||
        (this._pattern == ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN && (itemIndex - 1) % 3 == 0)) { // 3 items per row
        // Table
        htmlArr[idx++] = "<table cellpadding='0' cellspacing='0' border='0' ";
        this._noMaximize = true;
        if (this._headers.length == 1) {
            htmlArr[idx++] = " width='" + listViewWidth + "'>";
        }
        // Row
        htmlArr[idx++] = "<tr id='" + item._id + "'>";

    } else if (this._pattern != ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN &&
               this._pattern != ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN) {
        this._noMaximize = true;
        htmlArr[idx++] = "<table cellpadding='0' cellspacing='0' border='0' ";
        htmlArr[idx++] = this._noMaximize ? ">" : " width=100%>";
        // Row
        htmlArr[idx++] = "<tr id='" + item._id + "'>";
    }
    var size = this._pattern.length;
    var putLink = item.dir && (this._pattern == ComplexTreeProperties.FILEMANAGER_LIST_STYLE_PATTERN ||
                               this._pattern == ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN ||
                               this._pattern == ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN);

    // Data
    for (var j = 0; j < this._headers.length; j++) {
        var col = this._headers[j];
        if (!col._visible) continue;

        htmlArr[idx++] = "<td ";
        if (putLink && j == 0) {
            htmlArr[idx++] = "ondblclick='javascript:getFolderWithKey(\"" + item._id + "\", \"" + this._props.TREE_DIV + "\");' ";
        }

        if (this._pattern == ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN ||
            this._pattern == ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN) {
            htmlArr[idx++] = "width='"+ listViewWidth / 3 + "' align='left' style='text-align:center;overflow:hidden;white-space:nowrap;'><div style='overflow:hidden;white-space:nowrap;width:"+listViewWidth/3+"px;'>";
        } else {
            // IE misbehaves w/ the box model so we correct ourselves
            var absolute = col._width + "";
            absolute = absolute.indexOf("%") < 0;
            var width = (AjxEnv.isIE && absolute) ? (col._width + 4) : col._width;
            htmlArr[idx++] = width ? (" width=" + width + ">") : ">";
            // add a div to force clipping (TD's dont obey it)
            htmlArr[idx++] = "<div";
            htmlArr[idx++] = width ? " style='width: " + width + "'>" : ">";
            delete absolute;
            delete width;
        }

        var index = j;
        if (this._pattern == ComplexTreeProperties.WORKFLOW_LIST_STYLE_PATTERN && j > 0) index = 1;
        else if (this._pattern == ComplexTreeProperties.WORKFLOW_LIST_STYLE_PATTERN) index = 0;
        else if (size == 1) index = 0; 
        var value = ComplexTreeProperties.getHtmls(this._pattern[index], item.getValue(col.attrId), item._enabled, this._props);

        htmlArr[idx++] = value + "</div></td>";
        delete col;
        delete index;
        delete value;
    }
    delete putLink;
    delete size;

    if ((this._pattern == ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN && (itemIndex - 1) % 3 == 2) || // 3 items per row
        (this._pattern == ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN && (itemIndex - 1) % 3 == 2) ||
        this._pattern != ComplexTreeProperties.FILEMANAGER_ICONS_STYLE_PATTERN &&
        this._pattern != ComplexTreeProperties.FILEMANAGER_TB_STYLE_PATTERN) {
        htmlArr[idx++] = "</tr></table>";
        div.newRow = true;
    }

    this.currentHTML += htmlArr.join("");
    delete htmlArr;
    delete idx;
    delete base;
    delete listViewWidth;
    return div;
}

/**
 *
 */
PageInfoListView.prototype.addItem =
function(item, index, skipNotify, last) {
	if (!this._list)
		this._list = new AjxVector();

	// clear the "no results" message before adding!
	if (this._list.size() == 0)
		this._resetList();

	this._list.add(item, index);
    var itemIndex;
    if (! index) {
        itemIndex = this.getList().size();
    } else {
        itemIndex = index;
    }
    var div = this._createItemHtml(item, this._now, itemIndex);
    if (div.newRow || last) {
        if (last) {
            var listViewWidth = this.getSize().x - 30;
            for (var i = 0; i < 2 - ((itemIndex - 1) % 3); i++) {
                this.currentHTML += "<td width='"+listViewWidth / 3+"'><div style='width:"+listViewWidth / 3+"px' &nbsp;</td>";
            }
            this.currentHTML += "</tr></table>";
            delete listViewWidth;
        }
        div.innerHTML = this.currentHTML;
        this._addRow(div, index);
        this.currentHTML = "";
    }
    delete itemIndex;
    delete div;
}

/**
 *
 */
PageInfoListView.prototype._renderList =
function (list) {
    if (list instanceof AjxVector && list.size()) {
        var size = list.size();
        for (var i = 0; i < size; i++) {
            var item = list.get(i);
            var div = this._createItemHtml(item, this._now, i);
            if (div) {
                if (div instanceof Array) {
                    for (var j = 0; j < div.length; j++) {
                        this._addRow(div[j]);
                    }
                } else {
                    div.innerHTML = this.currentHTML;
                    this.currentHTML = "";
                    this._addRow(div);
                }
            }
            delete item;
            delete div;
        }
        delete size;
    }
}

/**
 * Returns the headers for this list view. It creates an Array of DwtListHeaderItems.
 */
PageInfoListView.prototype._getHeaderList =
function(fullAttrList) {
    var headerList = new Array();
    for (var i = 0; i < fullAttrList.length; i++) {
        var header = fullAttrList[i];
        var colId = i + "col";
        var displayText;
        if (header[4] && header[1]) {
            displayText = "<a href=" + header[4] + "><img src='" + header[1] + "' alt='" + header[0] +
                          "' border='0' class='flagImg' /></a>";
        } else if (header[4]) {
            displayText = "<a href=" + header[4] + " class='blacklink'>" + header[0] + "</a>";
        } else if (header[1]) {
            displayText = "<img src='" + header[1] + "' alt='" + header[0] + "' border='0' class='flagImg' /></a>";
        } else {
            displayText = header[0];
        }
        headerList[i] = new DwtListHeaderItem(colId, displayText, null, header[2], null, header[3], true);
        headerList[i].attrId = header[0];
        headerList[i]._tfAttr = header;
        headerList[i]._visible = true;
        this._tableWidth += header[2] + PageInfoListView.COLUMN_BETWEEN_SPACING;
        delete header;
        delete colId;
        delete displayText;
    }
    // For the last column
    this._tableWidth += PageInfoListView.COLUMN_BETWEEN_SPACING;
    return headerList;
}

/**
 * Returns the headers in the order they are currently displayed in the browser.
 */
PageInfoListView.prototype.getCurrentHeaders =
function () {
    var result = new Array();
    for (var i = 0; i < this._headers.length; i++) {
        result[i] = [this._headers[i].attrId];
    }
    return result;
}

/**
 * Creates an Array containing the data necessary for a ListItem. It collects the data from the Hashtable.
 */
PageInfoListView.prototype.makeListItemData =
function (pageInfo, ctree) {
    var result = new Array();
    var size = this._pattern.length;
    for (var i = 0; i < this._headers.length; i++) {
        var colId = this._headers[i].attrId;
        var index = i;
        if (this._pattern == ComplexTreeProperties.WORKFLOW_LIST_STYLE_PATTERN && i > 0) index = 1;
        else if (this._pattern == ComplexTreeProperties.WORKFLOW_LIST_STYLE_PATTERN) index = 0;
        else if (size == 1) index = 0;
        var tmp = new Array();
        for (var j = 0; j < this._pattern[index].length; j++) {
            var values = ComplexTreeProperties.getDataFromObjectInfo(pageInfo, this._pattern[index][j], colId, this._props, ctree);
            if (! values) continue;
            for (var k = 0; k < values.length; k++) {
                tmp[tmp.length] = values[k];
            }
            delete values;
        }
        result[i] = tmp;
        delete colId;
        delete index;
        delete tmp;
    }
    delete size;
    return result;
}

/**
 * This method hides the row at the given index if not allready hidden
 */
PageInfoListView.prototype.hideRow =
function (index) {
    var item = this.getList().get(index);
    if (!item || item.isHidden()) return;
    item.hide();
}

/**
 * This method deletes the row at the given index if not allready deleted
 */
PageInfoListView.prototype.deleteRow =
function (index) {
    var item = this.getList().get(index);
    if (item) this.getList().remove(item);
    item = null;
    this.setUI();
}

/**
 * This method shows the row at the given index if not allready visible
 */
PageInfoListView.prototype.showRow =
function (index) {
    var item = this.getList().get(index);
    if (!item || ! item.isHidden()) return;
    item.show();
}

/**
 *
 */
PageInfoListView.prototype.invertCheckBoxValues =
function (colName) {
    var items = this.getList().getArray();
    for (var i = 0; i < items.length; i++) {
        items[0].invertCheckboxValue(colName);
    }
    delete items;
    this.setUI();
}

