﻿function showDescOverlay(item, id)
{
    var overlay = $("descOverlay_" + id);
    overlay.__closeDescOverlay = false;

    overlay.style.display = "";
    overlay.style.top = (topPosition(item) - overlay.offsetHeight + 17) + "px";
    overlay.style.left = (leftPosition(item) - 30) + "px";
    
    overlay.onmouseover = function() { this.__closeDescOverlay = false; }
    overlay.onmouseout = function() { hideDescOverlay(id); }
}

function hideDescOverlay(id)
{
    var overlay = $("descOverlay_" + id);
    overlay.__closeDescOverlay = true;

    function close()
    {
        if(overlay.__closeDescOverlay)
        {
            overlay.style.display = "none";
        }
    }
    //setTimeout(close, 50);
    close();
}

function saveItem(ctrl, id)
{
    var img = $$(ctrl, "img")[0];
    
    if(img.src.indexOf("Star.png") > 0)
    {
        img.src = img.src.replace("Star.png", "StarActive.png");
        YAHOO.util.Connect.asyncRequest("POST", "../Search/Results/Ajax/Favorite/Add.aspx", null, "id=" + id);
    }
    else if(img.src.indexOf("StarActive.png") > 0)
    {
        img.src = img.src.replace("StarActive.png", "Star.png");
        YAHOO.util.Connect.asyncRequest("POST", "../Search/Results/Ajax/Favorite/Remove.aspx", null, "id=" + id);
    }
    
    listFavorites();
}

function listFavorites()
{
    var callback =
    {
        success : function(o)
        {
            if(o && o.responseXML)
            {
                var doc = o.responseXML;
                var panel = $("favoriteStash");

                if(doc.documentElement.getAttribute("count") != "0")
                {
                    var html = "<table cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:85%;\">";
                    var nodes = $$(doc.documentElement, "item");
                    
                    for(var i=0; i<nodes.length; i++)
                    {
                        var itemId = nodes[i].getAttribute("itemId");
                        var storeId = nodes[i].getAttribute("storeId");
                        var itemName = nodes[i].getAttribute("itemName");
                        var storeName = nodes[i].getAttribute("storeName");
                        var price = nodes[i].getAttribute("newPrice");
                        
                        html += "<tr>" +
                                    "<td style=\"vertical-align:top;padding:5px 5px 0px 0px;\">" +
                                        "<a href=\"" + SystemInfo.RootPath + "Rd.aspx?i=" + itemId + "&amp;s=" + storeId + "\" rel=\"nofollow\"><img src=\"" + SystemInfo.RootPath + "ProductImages/" + storeId + "/" + itemId + ".png\" alt=\"\" style=\"width:24px;height:24px;\" /></a>" +
                                    "</td>" +
                                    "<td style=\"vertical-align:top;padding-top:5px;\">" +
                                        "<div style=\"width:100%;height:1em;overflow:hidden;text-transform:capitalize;\"><a href=\"" + SystemInfo.RootPath + "Rd.aspx?i=" + itemId + "&amp;s=" + storeId + "\" rel=\"nofollow\">" + itemName.htmlEncode() + "</a></div>" +
                                        "<div style=\"width:100%;height:1em;overflow:hidden;\">" + price + " @ " + storeName.htmlEncode() + "</div>" +
                                    "</td>" +
                                "</tr>";
                    }

                    html += "</table>";
                    html += "<div style=\"padding-top:5px;color:#999;font-size:95%;\"><a href=\"" + SystemInfo.RootPath + "FavoriteStash/" + getCookie("SessionKey") + "/1.aspx\" rel=\"nofollow\">see all " + doc.documentElement.getAttribute("count") + " items</a></div>";
                    
                    panel.innerHTML = html;
                }
                else
                {
                    panel.innerHTML = "<div style=\"font-size:95%;padding-top:5px;font-style:italic;\">Click on the star to save an item for later, or share it with friends.</div>";
                }
            }
        }
    }
    
    YAHOO.util.Connect.asyncRequest("POST", "../Search/Results/Ajax/Favorite/List.aspx", callback, null);
}
