﻿$(document).ready(function() {


    $("a[rel='w-a'], a.smladd").bind("click", addAccessoryToWishlist);
    
    if (!typeof (isWishlist))
        return;

    $("a[rel='r']").bind("click", removeFromWishlist);
    $(".r select").bind("change", updateQty);
    $("a#empty").bind("click", emptyWishlist);
});


function trackBuyonline(dealerId) {
if (typeof (pageTracker) == "undefined")
      return;
   var base = location.href.split("/")[3] + "/";

    pageTracker._trackPageview(base + "buyonline/" + dealerId);
}
function addComboToWishlist(li, btn) {
    btn.attr("href", "javascript:return void(0);");

    if (!chooseIt) {
        var wait = $("#wait").clone(false).removeAttr("id").css({ display: 'block' });
        btn.html(wait);
    }
    if (dealerId > 0 || chooseIt) {
        if (dealerId == 0)
            dealerId = -1;


        if (typeof (redac) == "undefined" || !redac)
            window.location.href = "/wishlist/ajax/addcombo/" + li.attr("id").replace("c_", "") + "/" + dealerId;
        else {
            ajaxJsonPost("/wishlist/ajax/addcombo/" + li.attr("id").replace("c_", ""), null, addToWishlistSuccess, true, { item: li, btn: btn, wait: wait });
        }
    }
    else {
        ajaxJsonPost("/wishlist/ajax/addcombo/" + li.attr("id").replace("c_", ""), null, addToWishlistSuccess, true, { item: li, btn: btn, wait: wait });
    }
       
    
        
    return false;
}

function addAccessoryToWishlist() {

    var wait = $("#wait").clone(false).removeAttr("id").css({ display: 'block' });
    $(this).attr("href", "javascript:return void(0);");
    $(this).find("span").eq(0).html(wait);
    
   
        ajaxJsonPost("/wishlist/ajax/addaccessory/" + $(this).attr("id").replace("p_", ""), null, addToWishlistSuccess, true, { item: null, btn: $(this), wait: wait });
    return false;
}

function addToWishlistSuccess(msg) {
    var item = this.eventdata.item;
    var wait = this.eventdata.wait;
    var btn = this.eventdata.btn;


    if (typeof (dealerId) != "undefined" && dealerId != 0 && typeof (redac) != "undefined" && redac) {
        window.top.location.href = "/buyonline/" + dealerId;
    }
  
    if (item != null) {
        item.css({ backgroundColor: "#ffffcc" });
        item.animate({ backgroundColor: 'rgb(255,255,255)' }, 2500);
    }
    else {
      
    }

    if (wait != null) {
        wait.fadeOut("slow", function() { wait.remove(); btn.hide(); btn.before(unescape(msg.text)); });
    }
    
 
               
    
    if (btn.hasClass("smladd") && dealerId != 0) {
        var p = btn.parent();
        $("#acc").append("<tr><td width=\"25\"><strong>1 x</strong></td><td><strong>" + p.children().eq(1).html() + "</strong><br/><span>" + p.children().eq(2).html() + "</span></td></tr>");
        $("#acc").show();
        btn.fadeOut("slow");
        $("#n").hide();
        $("#a").show();
        return false;
    }
    updateTotals(msg);
    return false;
}

function removeFromWishlist() {
    if (!confirm("Are you sure you want to remove this item?"))
        return false;
    var item = $(this).parent().parent();

    if ($(".wishlist-item tr").length == 1) {
        emptyWishlist();
        return;
    }

    ajaxJsonPost("/wishlist/ajax/removeitem/" + item.attr("id").replace("i_", ""), null, removeFromWishlistSuccess, true, { item: item });
    return false;

}

function removeFromWishlistSuccess(msg) {
    var item = this.eventdata.item;
   
   // item.css({ backgroundColor: "#ffffcc" });
    //item.animate({ backgroundColor: 'rgb(255,255,255)' }, 2500);

    if (msg.cr) {
        var combo = item.parent();
        while (combo.attr("rel") != "c") {
            combo = combo.parent();
        }
        combo.fadeOut("slow", function() { combo.remove(); updateTotals(msg); });
    }
    else
    {
        if(item.attr("rel") == "a" && msg.acount==0)
            $("#acc").fadeOut("slow", function() { item.remove(); updateTotals(msg); });  
        else 
        {
            item.find("td").fadeOut("slow", function() { item.remove(); updateTotals(msg); });
        }
    }

}

function updateQty() {
    var item = $(this).parent().parent();
    ajaxJsonPost("/wishlist/ajax/updateitem/" + item.attr("id").replace("i_", "") + "/" + $(this).val(), null, updateQtySuccess, true, { item: item });
}

function updateQtySuccess(msg) {
    var item = this.eventdata.item;

    item.find("span[rel='total']").text(msg.itemtotal);
    updateTotals(msg);
}

function emptyWishlist() {
    window.location.href = "/wishlist/ajax/empty" + "/" + dealerId;
}

function updateTotals(msg) {

    var wt = $("#wishtab a");

    if (msg.count > 0) {
        wt.removeClass("light");
        wt.addClass("blue");
    }

    $("#witems strong").html(msg.count);
    $("#witems span").html(msg.total);
    $("#total").text(msg.total);
}

function saveWishlist() {

    var j = "[";

    $(".wishlist-item tr").each(function(i) {
        if (i > 0)
            j += ",";
        var id = $(this).attr("id").replace("i_", "");
        var qty = $(this).find("select").val();
        j += "{id: " + id + ", qty: " + qty + "}"
    });

    j += "]";
    $.post("/wishlist/ajax/save", { d: j }, function() { window.location.href = window.location.href });
}

function printwishlist()
{
    if (typeof (pageTracker) == "undefined") {
    }
    else {
        pageTracker._trackPageview(window.location.href.replace("#", "") + "/print/");
    }
    
    window.print();
    return false;
}
