
/**
*  This supports UserCommentDialog.html
*/

function MUserCommentDialogContext()
{
    this._messageTextArea = document.getElementById('userCommentMessage');
    selectTextField(this._messageTextArea);
    this._suggestionShelf = document.getElementById('suggestionShelf');
    this._suggestionShelfWrapper = document.getElementById('suggestionShelfWrapper');
    this._scratchDiv = document.createElement('div');
    this._suggestedProducts = this.initSuggestedProducts();
    this.adjustShelfHeight();
}

MUserCommentDialogContext.prototype.initSuggestedProducts = function()
{
    var suggestionsHiddenField = document.getElementById('suggestions');
    var suggestedUberIds = suggestionsHiddenField.value.trim();
    var suggestedProducts = null;
    if (suggestedUberIds.length > 0) {
        suggestedProducts = suggestedUberIds.split(',');
        // Bug 904: Safarirequires adjustShelfHeight() be called before adding prods to the shelf.
        // would rather not init _suggestedProducts but need to for adjustShelfHeight().  Could use
        // some cleanup, but be careful and check validation error reporting on all browsers after touching this.
        this._suggestedProducts = suggestedProducts;
        this.adjustShelfHeight();
        var suggestedProductsCount = suggestedProducts.length;
        for (var index = 0; index < suggestedProductsCount; index++) {
            var suggestedProductUberId = suggestedProducts[index];
            var productDiv = document.getElementById(suggestedProductUberId);
            productDiv.style.display = 'none';
            this.addToShelf(productDiv);
        }
    }
    else {
        suggestedProducts = new Array();
        this._suggestedProducts = suggestedProducts;
    }
    suggestedProducts.size = suggestedProducts.length;
    return suggestedProducts;
}

MUserCommentDialogContext.prototype.handleOKClicked = function()
{
    trimTextArea('userCommentMessage', 1000);
    var suggestionsHiddenField = document.getElementById('suggestions');
    suggestionsHiddenField.value = this._suggestedProducts;
    openDialogForForm('userCommentForm');
}

MUserCommentDialogContext.prototype.handleFormKeyPress = function(event)
{
    if (event.keyCode == Event.KEY_RETURN && !this.formIgnoreCR) {
        this.handleOKClicked(event);
        Event.stop(event);
    }
    this.formIgnoreCR = false;
}

MUserCommentDialogContext.prototype.handleTextAreaKeyPress = function(event)
{
    var eventKeyCode = event.keyCode;
    if (eventKeyCode == Event.KEY_TAB) {
        Event.stop(event);
    }
    else if (eventKeyCode == Event.KEY_RETURN) {
        this.formIgnoreCR = true;
    }
}

MUserCommentDialogContext.prototype.handleMouseOverProduct = function(productDiv,event)
{
    productDiv.style.border = '1px solid #b9b9b9';
}

MUserCommentDialogContext.prototype.handleMouseOutProduct = function(productDiv,event)
{
    productDiv.style.border = '1px solid white';
}

MUserCommentDialogContext.prototype.handleProductClicked = function(productDiv,event)
{
    // hide the item clicked
    productDiv.style.border = '1px solid white';
    productDiv.style.display = 'none';
    // record the item clicked
    this._suggestedProducts.push(productDiv.id);
    this._suggestedProducts.size = this._suggestedProducts.size + 1;
    this.adjustShelfHeight();
    this.addToShelf(productDiv)
}

MUserCommentDialogContext.prototype.adjustShelfHeight = function()
{
    var imagesPerRow = 6;
    var shelfRowHeight = 46;
    var rowCount = 1 + Math.floor((this._suggestedProducts.size - 1) / imagesPerRow);
    var maxRowCount = 2;
    if (rowCount <= maxRowCount) {
        this._suggestionShelf.style.overflow = '';
    }
    else {
        rowCount = maxRowCount;
        this._suggestionShelf.style.overflow = 'auto';
    }
    var shelfHeight = rowCount * shelfRowHeight;
    if (shelfHeight != parseInt(this._suggestionShelf.style.height)) {
        this._suggestionShelf.style.height = shelfHeight + "px";
        this._suggestionShelfWrapper.style.height = shelfHeight + "px"
        this._messageTextArea.style.height = (170 - shelfHeight) + "px";
    }
}

MUserCommentDialogContext.prototype.addToShelf = function(productDiv)
{
    // display the shelf version of the item clicked
    var productImg = findChildTagWithId(productDiv, 'IMG', 'productImg');
    var productImgUrl = productImg.src;
    var shelfImgUrl = productImgUrl.replace(/_smallmed.jpg/, '_small.jpg');
    this._scratchDiv.innerHTML = '' +
            '<div style="float:left;height:40px;width:32px;margin:4px">' +
                '<div style="position:relative;top:0px;left:0px;" ' +
                            'onmouseover="document.userCommentDialogContext.handleMouseOverShelf(this,event)" ' + 
                            'onmouseout="document.userCommentDialogContext.handleMouseOutShelf(this,event)" ' + 
                            'onclick="document.userCommentDialogContext.handleMouseClickShelf(this,event)">' + 
                    // close image container
                    '<span id="shelfClose" style="display:none;position:absolute;top:0px;left:20px;width:0px;height:0px;"><img src="/im/delete-box.gif" border="0"></span>' +
                    // image table
                    '<div style="top:0px;left:0px;">' +
                        '<table border="0">' + 
                            '<tr>' + 
                                 '<td valign="middle" height="40px" width="32px">' + 
                                      '<img src="' + shelfImgUrl + '">' + 
                                 '</td>' + 
                            '</tr>' + 
                         '</table>' + 
                    '</div>' + 
                    '</div>' +
                 '</div>' +
             '</div>';
    var imgWrapper = findFirstNontextChild(this._scratchDiv);
    imgWrapper.id = productDiv.id;
    this._suggestionShelf.appendChild(imgWrapper);
    // Scroll div to bottom
    if (this._suggestionShelf.style.overflow == 'auto') {
        this._suggestionShelf.scrollTop = imgWrapper.offsetTop + imgWrapper.clientHeight - this._suggestionShelf.clientHeight;
    }
}

MUserCommentDialogContext.prototype.handleMouseOverShelf = function(mouseHandlerDiv,event)
{
    var shelfCloseSpan = findChildTagWithId(mouseHandlerDiv, 'span', 'shelfClose');
    shelfCloseSpan.style.display = '';
}

MUserCommentDialogContext.prototype.handleMouseOutShelf = function(mouseHandlerDiv,event)
{
    var shelfCloseSpan = findChildTagWithId(mouseHandlerDiv, 'span', 'shelfClose');
    shelfCloseSpan.style.display = 'none';
}

MUserCommentDialogContext.prototype.handleMouseClickShelf = function(mouseHandlerDiv,event)
{
    var imgWrapper = mouseHandlerDiv.parentNode;
    var productUberId = imgWrapper.id;
    var index = this._suggestedProducts.indexOf(productUberId);
    delete this._suggestedProducts[index];
    this._suggestedProducts.size = this._suggestedProducts.size - 1;
    this.adjustShelfHeight();
    this._suggestionShelf.removeChild(imgWrapper);
    var productDiv = document.getElementById(productUberId);
    productDiv.style.display = '';
    productDiv.style.border = '1px solid white';
}

function usercomment_rawComments(commentTableId, url)
{
    var handleAjaxResponseFunction = function(xmlhttp) {
        var dialogContents = getResponseText(xmlhttp);
        if (handleAjaxExceptionPage(dialogContents)) {
            return;
        }
        var windowScrollTop = tx_windowScrollTop();
        var commentTableDiv = document.getElementById(commentTableId);
        var commentTableHeight1 = store_elementHeight(commentTableDiv);
        var tempDiv = document.createElement("div");
        tempDiv.innerHTML = dialogContents;
        commentTableDiv.parentNode.replaceChild(tempDiv.firstChild, commentTableDiv);
        var commentTableHeight2 = store_elementHeight(document.getElementById(commentTableId));
        window.scrollTo(0, windowScrollTop - (commentTableHeight1 - commentTableHeight2));
    };
    var optionsDict = {
            method: 'get',
            onSuccess: handleAjaxResponseFunction
    };
    new Ajax.Request(url, optionsDict);
    return false;
}
