/**
 * Changes the background colour of a tile to yellow.
 * @param t a tile (see td.result_cell in searchresults.jsp)
 */
function setTileBackground(t)
{
    if (t == null)
    {
        return;
    }
    var id = t.id;
    var arrRules =
            [
                [ ["#", id, " div.year"],              "#FFFF99" ],
                [ ["#", id, " div.year h6"],           "#FFFF99" ],
                [ ["#", id, " div.msrp"],              "#FFFF99" ],
                [ ["#", id, " div.MPG"],               "#FFFF99" ],
                [ ["#", id, " div.consumer-rating"],   "#FFFF99" ],
                [ ["#", id, " div.viewallyears"],      "#FFFF99" ],
                [ ["#", id, " div.compare-list"],      "#FFFF99" ],
                [ ["#", id, " td.bottom-left"],        "url('/img/research/shared/orange_yellow_btmleft_25x18.gif') no-repeat scroll left top" ],
                [ ["#", id, " td.mid-section-bottom"], "url('/img/research/shared/bg_orange_yellow_generic_bt.gif') repeat-x scroll left top" ],
                [ ["#", id, " td.bottom-right"],       "url('/img/research/shared/orange_yellow_btmright_25x1.gif') no-repeat scroll left top" ]
            ];
    setBackground(id, arrRules);
}

/**
 * Resets the background colour of a tile to its original colours.
 * @param t a tile (see td.result_cell in searchresults.jsp)
 */
function resetTileBackground(t)
{
    if (t == null)
    {
        return;
    }
    var id = t.id;
    var arrRules =
            [
                [ ["#", id, " div.year"],              "#FEEED3" ],
                [ ["#", id, " div.year h6"],           "#FEEED3" ],
                [ ["#", id, " div.msrp"],              "#FEEED3" ],
                [ ["#", id, " div.MPG"],               "#FFFFFF" ],
                [ ["#", id, " div.consumer-rating"],   "#FEEED3" ],
                [ ["#", id, " div.viewallyears"],      "#FFFFFF" ],
                [ ["#", id, " div.compare-list"],      "#FFFFFF" ],
                [ ["#", id, " td.bottom-left"],        "url(/img/research/shared/orange_white_btmleft_w_25x1.gif) no-repeat scroll left top" ],
                [ ["#", id, " td.mid-section-bottom"], "url(/img/research/shared/bg_orange_white_generic_btm.gif) repeat-x scroll left top" ],
                [ ["#", id, " td.bottom-right"],       "url(/img/research/shared/orange_white_btmright_w_25x.gif) no-repeat scroll left top" ]
            ];
    setBackground(id, arrRules);
}

/**
 * Changes the background colour of a tile.
 * @param id the id of a tile.
 * @param rules the rules.
 * rules is an array of rules where each rule contains
 * - an array of strings that will be concatenated into a set of CSS selectors
 * - the new background value
 */
function setBackground(id, rules)
{
    if (id == null)
    {
        return;
    }
    if (rules == null)
    {
        return;
    }
    var rule;
    var array;
    var selector;
    var background;
    var elements;
    var element;
    var x;
    var y = rules.length;
    for (x = 0; x < y; x++)
    {
        rule = rules[x];
        array = rule[0];
        selector = array.join("");
        elements = $$(selector);
        if (elements.length < 1)
        {
            continue;
        }
        element = elements[0];
        background = rule[1];
        element.style.background = background;
    }
}

//Popup Layers
function showLayer(genId, layer, tileId, startYear, endYear, rowID, isHybrid, minPrice, maxPrice, bodyType, photoURL, adBodyType, AggregateRating, TotalRatingCount) {
    getGenerationData(genId, tileId, layer, startYear, endYear, isHybrid, minPrice, maxPrice, bodyType, photoURL, adBodyType, AggregateRating, TotalRatingCount);
}

function showBodyStyleLayer(bodyStyleGenId, layer, tileId, startYear, endYear, rowID, isHybrid, minPrice, maxPrice, bodyType, photoURL, adBodyType, AggregateRating, TotalRatingCount) {
    getBodyStyleGenerationData(bodyStyleGenId, tileId, layer, startYear, endYear, isHybrid, minPrice, maxPrice, bodyType, photoURL, adBodyType, AggregateRating, TotalRatingCount);
}

function showPopupLayer(layer) {
    if (layer != null && document.getElementById) {
        document.getElementById(layer).style.display = 'block';
    } else if (layer != null && document.all) {
        document.all[layer].style.display = 'block';
    }
}

//Hide a layer by name
function hidePopupLayer(layer) {
    if (layer != null && document.getElementById) {
        document.getElementById(layer).style.display = 'none';
    } else if (layer != null && document.all) {
        document.all[layer].style.display = 'none';
    }
    //added to fire UltraPop hide all others and show this one
    $(layer).hide();
}

function logAsisImage(src) {
    //depricated.  Use Global:
    asis(src);
}

function followLink(url) {
    self.location = url;
}

function showLinks() {
    for (var i = 1; i <= 4; i++) {
        if (document.getElementById("vl_" + i)){
            document.getElementById("vl_" + i).style.visibility = 'visible';
        }
    }
}

function hideLinks() {
    for (var i = 1; i <= 4; i++) {
        if (document.getElementById("vl_" + i)){
            document.getElementById("vl_" + i).style.visibility = 'hidden';
        }
    }
}

/*
 NOTE:  resetNavigationFilters has been replaced by resetSearchResultFilters in mi_search_results.js.
 */

var BuildAndPrice = Class.create({
    /**
     * initialize()
     */
    initialize: function(options) {
        this.options = {
            address: 'address-cct',
            form: 'startConfigure',
            zipLabel: 'address-label-cct'
        }
        Object.extend(this.options, options || {});
        this.address = this.options.address;
        this.form = this.options.form;
        this.zipError = 'Please enter a valid 5 digit ZIP Code.';
        this.zipLabel = this.options.zipLabel;

        $(this.form).observe('submit', this.submitHandler.bindAsEventListener(this));
    },

    submitHandler: function(e) {
        e.stop();
        if ($F(this.address) != undefined) {
            zipValidation.zipProcess();
        }
    },

    zipShowError: function() {
        $(this.zipLabel).setStyle({color: '#f00'});
        alert(this.zipError);
    },

    zipShowSuccess: function() {
        $(this.form).submit();
    }

});



