﻿//GLOBAL VARIABLES

var compatiblityWrapper, model, manufacturer, errorPanel, error, manufacturerProgress, modelProgress, compatSearch, clear;

$(document).ready(function() {

    //Populate Common Controls
    compatiblityWrapper = $(".CompatibilityWrapper");
    model = compatiblityWrapper.find(".Model");
    manufacturer = compatiblityWrapper.find(".Manufacturer");
    errorPanel = compatiblityWrapper.find(".ErrorPanel");
    error = errorPanel.find(".Error");
    manufacturerProgress = compatiblityWrapper.find(".CT_ManufacturerProgress");
    modelProgress = compatiblityWrapper.find(".CT_ModelProgress");
    compatSearch = model.find(".CT_Search");
    clear = compatiblityWrapper.find(".Clear");

    /******** SEARCH  ************/
    //Fix Case Sensitive jQuery "Contains"
    jQuery.expr[':'].contains = function(a, i, m) { return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; };
    $("INPUT:text[SearchAreaClass]").unbind("keyup").keyup(function() {
        var searchArea = $(this).attr("SearchAreaClass");
        var searchString = $(this).val();
        if (searchString.indexOf('(') > -1) {
            $(this).val($(this).val().replace("(", ""));
            alert('Invalid Character!');
            return;
        }
        if (searchArea != '') {
            searchArea = "." + searchArea; //Get the Class
            $(searchArea).find("A").parent().hide();
            $(searchArea).find("A:contains('" + searchString + "')").each(function() {
                $(this).parent().show();
                var regEx = eval('/' + searchString + '/i');
                var beginIndex = $(this).text().toLowerCase().lastIndexOf(searchString.toLowerCase());
                var endIndex = beginIndex + searchString.length;
                var originalText = $(this).text().substring(beginIndex, endIndex);
                $(this).find("DIV").html($(this).text().replace(regEx, "<span class='SearchTextHighlight'>" + originalText + "</span>"));
            });
        }
    });

    //If Step3 Class is displaying then enable Model Flyout to change Product
    if (compatiblityWrapper.find(".Step2").length > 0) {
        //Enable Flyout for Manufacturer
        manufacturer.unbind("mouseenter mouseleave").hover(function() {
            $(this).find(".Flyout").show();
            $(this).find(".Link").css("background-image", $(this).find(".Link").css("background-image").replace("_off", "_on"));
        }, function() {
            $(this).find(".Flyout").hide();
            $(this).find(".Link").css("background-image", $(this).find(".Link").css("background-image").replace("_on", "_off"));
        });
    }

    //If Step3 Class is displaying then enable Model Flyout to change Product
    if (compatiblityWrapper.find(".Step3").length > 0) {
        model.unbind("mouseenter mouseleave").hover(function() {
            $(this).find(".Flyout").show();
            $(this).find(".Link").css("background-image", $(this).find(".Link").css("background-image").replace("_off", "_on"));
        }, function() {
            $(this).find(".Flyout").hide();
            $(this).find(".Link").css("background-image", $(this).find(".Link").css("background-image").replace("_on", "_off"));
        });
    }
});
