﻿var catDoc = this;
$(function () {

    $('.members-email').toggleVal();
    $('.success').hide();
    if ($.cookie('submit-member-form') == '1') {
        $('#showForm-pre').hide();
        $('.success').show();
        $('.member-hide').hide();
        $('.members-only-wrapper').css({ 'padding': '0px' });
        $.cookie('submit-member-form', null, { path: '/' });
        $.cookie('submit-member-form', '2', { path: '/' });
    }
    else if ($.cookie('submit-member-form') == '2') {
        $('#LeftMembersOnlyForm').hide();
    }
    else {
        $('#showForm-pre').show();
        $('.success').hide();
    }

    var useFireClick = (typeof (fc_click) != "undefined" && fc_click != null);
    var useOmniture = (typeof (s_gi) != "undefined" && s_gi != null);

    // Immediately after the page loads, we're going to tick the view type:    
    var isListView = $("TABLE[id$='gvListView']").length > 0;
    var showingViewType = isListView ? "LV_showinglist" : "LV_showinggallery";
    if (useOmniture || window.location.href.indexOf("www.petfooddirect.com") == -1) {
        if (typeof (console) != "undefined") { console.log("TRIGGERED: s.tl(this, 'o', \"" + showingViewType + "\");") }
        if (typeof (s_gi) != "undefined") {
            var s = s_gi("s_account");
            if (typeof (s) != "undefined" && typeof (s.tl) != "undefined") {
                s.tl(catDoc, 'o', showingViewType);
            }
        }
    }
    if (useFireClick || window.location.href.indexOf("www.petfooddirect.com") == -1) {

        if (typeof (console) != "undefined") {
            console.log("TRIGGERED: fc_click('" + showingViewType + "', 'Ad');")
        }

        if (typeof (fc_click) != "undefined") { fc_click(showingViewType, "Ad"); }
    }

    // When the List or Gallery buttons are pressed, log the event to Omniture/Fireclick
    $("A[id$='btnListViewSelect'],A[id$='btnGridViewSelect']").click(function () {
        var isLV = $(this).attr("id").indexOf("ListView") > -1;
        var val = isLV ? "LV_listview" : "LV_galleryview";
        if (useOmniture || window.location.href.indexOf("www.petfooddirect.com") == -1) {
            if (typeof (console) != "undefined") { console.log("TRIGGERED: s.tl(this, 'o', " + val + ");") }
            if (typeof (s_gi) != "undefined") {
                var s = s_gi("s_account");
                if (typeof (s) != "undefined" && typeof (s.tl) != "undefined") {
                    s.tl(catDoc, 'o', val);
                }
            }
        }
        if (useFireClick || window.location.href.indexOf("www.petfooddirect.com") == -1) {
            if (typeof (console) != "undefined") {
                console.log("TRIGGERED: fc_click('" + val + "', 'Ad');")
            }
            if (typeof (fc_click) != "undefined") { fc_click(val, "Ad"); }
        }
    });

    var prodIds = [];
    $("DIV.variant-row LABEL.label_radio").each(function () {
        if (prodIds.length < 3) { prodIds.push($(this).attr("id").split("radio-")[1]); }
    });

    $("DIV.GridViewImage IMG.GridViewProductImage").each(function () {
        var h = $(this).attr("src").split("_lg.jpg")[0].split("\/");
        if (prodIds.length < 3) { prodIds.push(h[h.length - 1]); }
    });

    // Load the new endeca content
    if (prodIds.length > 0) {
        $("DIV.bottomEndeca").show();
        $.ajax({
            type: "POST",
            url: "/ClientSideOperations.asmx/LoadCertonaResultsForProducts",
            data: JSON.stringify({ "productIds": prodIds }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                var d = $("<div></div>");
                d.html(msg.d);
                var f = d.find("form");
                f.find("DIV:has(INPUT[name='__VIEWSTATE'])").remove();
                $("DIV.bottomEndecaContent").html(f.html());
            }
        });
    }
    else {
        $("DIV.bottomEndeca").hide();
    }
});

function showBasketPopup() {
    if (location.href.toLowerCase().indexOf("cart.aspx") > 0) {
        window.location = location.href;
    }
    else {
        var screenH = $(document).height();
        var screenW = $(window).width();
        var winH = $(window).height();
        var winW = $(window).width();
        newtop = 0;
        newleft = 0;
        newtop = winH / 2 - $('#BasketDisplay').height() / 2;
        newleft = winW / 2 - $('#BasketDisplay').width() / 2;

        $('#BasketDisplay').css("top", newtop);
        $('#BasketDisplay').css("left", newleft);
        $('#BasketDisplay').fadeIn('fast');
        $('#bkgPopup').show();
    }
}

function closeCSPopup() {
    $('#CSDisplay').hide();
    $('#BasketDisplay').hide();
    $('#bkgPopup').hide();
}



