﻿function GenerateItemListing(itemObj, searchResultsObj, isEmbedded) {
    itemHtml = "";
    if (itemObj.SmallImg != '') {
        smallImage = itemObj.SmallImg;
    }

    // Swap out portions of linking when search result is embedded
    itemHtml += '<tr class="result"><td class="mfProductImage">';
    if (isEmbedded) {
        // Swap production link for print link for image
        itemHtml += '<span id="spHypItemImageLink" style="cursor:pointer;" onclick="GetPrintCSS(\'' +
                        itemObj.ItemType + '\',\'' + itemObj.ItemCode + '\')" >' + smallImage + '</span> <br />';

        // Swap production link for print link for sku
        itemHtml += '</td><td class="mfProductContent"><span id="spHypUrl" class="mfSku" style="cursor:pointer;"onclick="GetPrintCSS(\'' +
                        itemObj.ItemType + '\',\'' + itemObj.ItemCode + '\')" >' + itemObj.ItemCode + '</span>';
    }
    else {
        // Image with production link
        itemHtml += '<span id="spHypItemImageLink" style="cursor:pointer;" >' + itemObj.ItemImageLink + '</span> <br />';

        // Swap series vs. production compare method call
        $('#pnlCompareProducts').show();
        if (itemObj.ItemType == "Series") {
            itemHtml += '<input id="chkCompare" type="checkbox" onclick="addSeriesToCompareList(' + itemObj.ItemCode + ',' + $('#chkCompare') + ',' + smallImage + 
                        ')" /><label id="lblChkCompare" >Compare</label>'
        }
        else {
            itemHtml += '<input id="chkCompare" type="checkbox" onclick="addProductToCompareList('+ itemObj.ItemCode + ',' + $('#chkCompare') + ',' + smallImage +
                        ')" /><label id="lblChkCompare" >Compare</label>'
        }

        // Sku product link
        itemHtml += '</td><td class="mfProductContent"><span id="spHypUrl" class="mfSku" style="cursor:pointer;">' + itemObj.ItemURL + '</span>';
    }

    // Title and Description        
    itemHtml += '<h2 id="hTitle"><label id="lblTitle">' + itemObj.ItemTitle + '</label></h2>' +
                    '<p><label id="lblDescription">' + itemObj.ItemDescription + '</label></p>';

    // If requiring a bracket, add in bracket link and warning message
    if (itemObj.BracketProductCode != null) {
        itemHtml += '<div id="divBracketNote"><div class="calloutBox"><span class="red">Note:</span>' +
                        'When ordering this product you must also order <a ID="hypBracketLink" runat="server" href="/Products/'
                        + itemObj.BracketProductCode + '"> ' + itemObj.BracketProductCode + '</a>.</div></div>'
    }

    // If embedded display product link instead of print link
    itemHtml += '</td><td class="mfProductOptions"><ul>'
    if (isEmbedded) {
        itemHtml += '<li id="liProdLnk"><img src="/favicon.ico" alt="Chief" style="margin-right:5px;" />' + itemObj.ItemURL.replace('">', '" target="_blank">') + '</li>';
    }
    else {
        itemHtml += '<li id="liPrint"><a id="linkPrint" class="iconPrint" onclick="GetPrintCSS(\'' + itemObj.ItemType + '\',\'' + itemObj.ItemCode + '\')">Print</a></li>';
    }

    // Generate Download lists                                              
    if (searchResultsObj.Downloads != null) {
        itemHtml += GenerateDownloads(searchResultsObj.Downloads, itemObj.ItemID);
    }

    // Generate Video lists  
    if (searchResultsObj.Videos != null) {
        GenerateVideos(searchResultsObj.Videos, itemObj.ItemID);
    }

    // Finish off row
    itemHtml += '</ul></td></tr>';
    return itemHtml;
};

function GenerateDownloads(downloads, searchItemID) {
    var downloadsStr = '<li id="liDownloads" onmousedown="$(this).children().next().show();" onmouseout="$(this).children().next().hide();">' +
                            '<a class="iconDownloads" >Downloads</a>' +
                            '<div onmouseover="$(this).show();" class="downloadsEmbeddedDropDownPopup"></div>' +
                            '<div id="divDownloadsPopup" class="downloadsEmbeddedDropDownPopup" onmouseover="$(this).show();">' +
                                '<ul id="tblDownloads">';

    $.each(downloads, function () {
        if (this.ItemID == searchItemID) {
            downloadsStr += '<li>' + this.DownloadHtml + '</li>';
        }
    });

    downloadsStr += '</ul>' +
                '</div>' +
            '</li>';
    return downloadsStr;
};

function GenerateVideos(videos, searchItemID) {
    var videosStr = '<li><ul id="ulVideos">';
    $.each(videos, function () {
        if (this.ItemID == searchItemID) {
            videosStr += '<li>' + this.Videohtml + '</li>';
        }
    });

    videosStr += '</ul></li>';
    return videosStr;
};
