var MAX_COMPARE_VEHICLES = 3;
var COMPARE_VEHICLE_COOKIE_NAME = "dmi_gmcuv_cv";

function renderLink(num) {
    $("#numComparedVehicles").html(num);
    if (num == 1) {
        $("#numComparedVehiclesTxt").html("Vehicle");
    } else {
        $("#numComparedVehiclesTxt").html("Vehicles");
    }
}

function initCompareHeaderLink() {
    var comparedVehicles = loadCompareVehicleInfo();
    var compareLink = "/gate/compareVehiclesView?1=1";
    renderLink(comparedVehicles.size());
    $("#compareLink").attr("href", compareLink);
}


//We need to rewire the compare checkboxes in every successful render of the Ajax call. Wiring these checkboxes just once on load time is not
//enough because these checkboxes get re-rendered everytime a refinement is made.

function initCompareCheckBoxes(action) {


    //The compare info is retained if sorting, paginating and the first time the search results page is rendered any subsequent refinement
    //passes a null action thereby clearing the comparison list.
    if (action == "") {
        clearComparisonListInfo();
    }


    var comparedVehicles = loadCompareVehicleInfo();
    $(".selectWarn").hide();

    $("input[name=compare][type=checkbox]").each(function() {
        var currid = $(this).attr("id");
        if (currid != "%dealerID%|%stockNum%") {
            $(this).uniform();
        }

        if (comparedVehicles.contains(currid)) {
            $(this).parent().addClass("checked");
            $(this).attr("checked", true);
            $(this).attr("disabled", false);
        } else {
            $(this).parent().removeClass("checked");
            $(this).attr("checked", false);
        }
    });


    $("input[name=compare][type=checkbox]").click(function() {
        var id = $(this).attr("id");
        if ($(this).attr("checked") == true) {
            if (comparedVehicles.size() < MAX_COMPARE_VEHICLES) {
                $(this).parent().addClass("checked");
                if (!comparedVehicles.contains(id)) {
                    comparedVehicles.add(id);
                }
            } else if (comparedVehicles.size() == MAX_COMPARE_VEHICLES) {
                var selWarn = $(this).parent().parent().parent().find(".selectWarn");
                selWarn.fadeIn(2, function() {
                    $(this).html("<font size='-2' color='red'>Select up to 3 vehicles</font>");
                });

                $("input[name=compare][type=checkbox]").each(function() {
                    var currid = $(this).attr("id");
                    if (!comparedVehicles.contains(currid)) {
                        $(this).parent().removeClass("checked");
                        $(this).attr("checked", false);
                    }
                });

                window.setTimeout(function() {
                    selWarn.fadeOut(1000, function() {
                        selWarn.html("")
                    });
                }, 3000);
                /*$(this).parent().parent().parent().find(".selectWarn").fadeIn(200,function(){$(this).html("<font size='-2' color='red'>Select up to 3 vehicles</font>")});
                 $("input[name=compare][type=checkbox]").each(function() {
                 var currid = $(this).attr("id");
                 if (!comparedVehicles.contains(currid)) {
                 $(this).parent().removeClass("checked");
                 $(this).attr("checked",false);
                 }
                 });
                 $(this).parent().parent().parent().find(".selectWarn").fadeOut(4000, function(){$(this).html("")});*/
            }
        } else {   // If unchecking
            $(this).parent().removeClass("checked");
            var indexOf = comparedVehicles.indexOf(id);
            if (indexOf >= 0) {
                comparedVehicles.remove(indexOf);
            }
        }
        renderLink(comparedVehicles.size());
        saveCompareVehicleInfo(comparedVehicles);
    });

}


function saveCompareVehicleInfo(comparedVehicles) {
    var compareLink = "/gate/compareVehiclesView?1=1";
    var cookieInfo = "";
    for (var i = 0; i < comparedVehicles.size(); ++i) {
        var tuple = comparedVehicles.get(i);
        var indexOf = tuple.indexOf("|");
        var dealerID = tuple.substring(0, indexOf);
        var stockNum = tuple.substring(indexOf + 1, tuple.length);
        cookieInfo += tuple + ";";
    }
    $.cookie(COMPARE_VEHICLE_COOKIE_NAME, cookieInfo, {expires: 1, path:'/'});
    $("#compareLink").attr("href", compareLink);
    renderLink(comparedVehicles.size());
}

function clearComparisonListInfo() {
    $.cookie(COMPARE_VEHICLE_COOKIE_NAME, null, {path:'/'});
    $("#numComparedVehicles").html("0");
    $("#compareLink").attr("href", "/gate/compareVehiclesView");
}

function loadCompareVehicleInfo() {
    var comparedVehicles = new ArrayList();
    var comparedVeh = $.cookie(COMPARE_VEHICLE_COOKIE_NAME);
    if (!isEmpty($.trim(comparedVeh))) {
        if (comparedVeh.indexOf(";") >= 0) {
            var tuples = comparedVeh.split(";");
            for (var i = 0; i < tuples.length; ++i) {
                if (!isEmpty(tuples[i])) {
                    comparedVehicles.add(tuples[i]);
                }
            }
        }
    }
    return comparedVehicles;
}


