﻿var ItemControl = {
    CookieName: "wsw_item",
    CookieData: {
        OtherItemsPage: 1,
        Seller: 0
    },
    Disable: false,
    ID: 0,
    Seller: 0,
    ShowDialog: function(msg) {
        $(".dialog-content").html(msg);
        $("#dialog").dialog("option", "title", "Sorry!");
        $("#dialog").dialog("open");

    },
    Buy: function() {
        if (!this.Disable) {
            window.location.href = App.baseURL + "/checkout.aspx?item=" + this.ID;
        } else {
            ItemControl.ShowDialog("<b>This item is not available for purchase.</b>");
            return false;
        }
    },
    Favorite: function() {
        if (!this.Disable) {
            var url = App.baseURL + "/dialog/item-favorite.aspx?item=" + this.ID + "&KeepThis=true&TB_iframe=true&height=300&width=500&modal=true";
            tb_show("Favorite", url, null);
        } else {
            ItemControl.ShowDialog("<b>This feature is not available for this item.</b>");
            return false;
        }
    },
    Flag: function() {
        if (!this.Disable) {
            var url = App.baseURL + "/dialog/item-flag.aspx?item=" + this.ID + "&KeepThis=true&TB_iframe=true&height=300&width=500&modal=true";
            tb_show("Favorite", url, null);
        } else {
            ItemControl.ShowDialog("<b>This feature is not available for this item.</b>");
            return false;
        }
    },
    Share: function() {
        var url = App.baseURL + "/dialog/item-share.aspx?item=" + this.ID + "&KeepThis=true&TB_iframe=true&height=310&width=500&modal=true";
        tb_show("Favorite", url, null);
    },
    UpdateCookie: function() {
        $.cookie(this.CookieName, JSON.stringify(this.CookieData));
    },
    SwapPhoto: function(photo) {
        $(".x-product-photo img").attr("src", photo);
    },
    Trade: function() {
        if (!this.Disable) {
            var url = App.baseURL + "/dialog/item-trade.aspx?item=" + this.ID + "&KeepThis=true&TB_iframe=true&height=450&width=600&modal=true";
            tb_show("Trade", url, null);
        } else {
            ItemControl.ShowDialog("<b>This item is not available for trade.</b>");
            return false;
        }
    },
    Cancel: function() {
        var params = { item: this.ID };
        $.ajax({
            type: "POST",
            url: "/services/Listings.asmx/Cancel",
            data: JSON.stringify(params),
            success: function(msg) {
                window.location.href = App.baseURL + "/post";
            }
        });
    },
    Init: function(id, seller) {

        this.ID = id;
        this.Seller = seller;


        $("button#buy-btn").click(function() {
            ItemControl.Buy();
        });

        // share menu items
        $("a#favorite-btn").click(function() {
            ItemControl.Favorite();
        });

        $("a#share-btn").click(function() {
            ItemControl.Share();
        });

        $("a#flag-btn").click(function() {
            ItemControl.Flag();
        });


        if (this.Disable) {
            $("#publish-btn").click(function() {
                $("#previewListing").append("<input type=\"hidden\" name=\"confirm\" value=\"Y\" />");
                $("#previewListing").submit();
            });

            $("#cancel-btn").click(function() {
                ItemControl.Cancel();
            });
        }

        // Cookie settings
        var prefs;
        var wsw = $.cookie(this.CookieName);

        // Create Cookie
        if (wsw == null) {
            this.CookieData.Seller = this.Seller;
            this.CookieData.OtherItemsPage = 1;
            // persist our cookie data
            this.UpdateCookie();

        } else {
            var c = JSON.parse($.cookie(this.CookieName));
            this.CookieData.OtherItemsPage = c.OtherItemsPage;
            this.CookieData.Seller = c.Seller;
            // if same seller as the last
            if ((this.Seller == this.CookieData.Seller) && (this.CookieData.OtherItemsPage > 1)) {
                SellerOtherItems.Page = this.CookieData.OtherItemsPage;
                SellerOtherItems.Offset = (SellerOtherItems.Page - 1) * SellerOtherItems.MaxRows;
            } else {
                // reset page number
                this.CookieData.OtherItemsPage = 1;
            }
        }



    }
}


var SellerOtherItems = {
    Items: new Array(),
    Page: 1,
    MaxRows: 2,
    Total: 11,
    Offset: 0,
    CreateElement: function(item) {
        var element = '\
        <div class="x-thumb-wrap"> \
            <div class="x-thumb-photo">\
            <a href="' + itemLink(item.ID) + '"><img src="' + item.Photo + '" /></a></div> \
        </div>';
        return element;
    },
    CreateFiller: function() {
        var element = '\
        <div class="x-thumb-wrap"> \
            <div class="x-thumb-photo">\
                <img src="/common/images/clear.gif" />\
            </div>\
        </div>';
        return element;
    },
    RefreshUI: function() {
        var text;
        var div = $("#x-other-items");

        if (this.Items.length > 0) { $("#x-seller-items").show(); }

        $(div).empty(); // clear out the loading image

        /* If we find items, we'll add two at a time */
        var count = 0;

        $.each(this.Items, function() {
            $(div).append(SellerOtherItems.CreateElement(this));
            if (count == 1) { return false; }
            count++;
        });

        // Add other a blank filler item
        if (this.Items.length == 1) {
            $(div).append(SellerOtherItems.CreateFiller());
        }


        // Need to subtract 1 from the count of other items
        if (this.Total > 0) { this.Total = this.Total - 1; }
        text = (this.Total > 1) ? " items" : " item";
        $("span#count a").html("(" + this.Total + text + ")");
    },
    GetItems: function() {
        // setup loading image
        $("#x-other-items").html("<div class=\"x-thumb-wrap\" style=\"background-color: #fafafa; border: none; width: 275px;\"><img src=\"/common/images/big-rotate.gif\" style=\"margin-top: 25px;height:32px; width: 32px;\" /></div>");
        var params = { seller: ItemControl.Seller, offset: this.Offset, maxrows: this.MaxRows, listing: ItemControl.ID };
        $.ajax({
            url: "/services/Profile.asmx/OtherListings",
            data: JSON.stringify(params),
            success: function(msg) {
                SellerOtherItems.Items = msg.d.Items;
                SellerOtherItems.Total = msg.d.Total;
                SellerOtherItems.RefreshUI();
            }
        });

        // update our cookie
        ItemControl.CookieData.OtherItemsPage = this.Page;
        ItemControl.UpdateCookie();

    },
    Previous: function() {
        if (this.Page > 1) {
            this.Page--;
            this.Offset = (this.Page - 1) * this.MaxRows;
            this.GetItems();
        }
    },
    Next: function() {
        var numPages = Math.ceil(this.Total / this.MaxRows);
        if (this.Page < numPages) {
            this.Page++;
            this.Offset = (this.Page - 1) * this.MaxRows;
            this.GetItems();
        }

    }
}


$(document).ready(function() {
    $("#dialog").dialog({
        autoOpen: false,
        modal: true,
        width: 350,
        resizable: false,
        buttons: {
            "OK": function() { $(this).dialog("close"); }
        }
    });   

});