﻿
//write the function to replace the KBR elements

function printerFriendly()
{
    //$("head").append("<link href=\"/static/css/print.css\" media=\"screen\" type=\"text/css\" rel=\"stylesheet\">")
    window.print();
}

function replace_kbr()
{
    //get a list (count) of all td with class = "kbr"
    var tdList = $("td.KBR");
    var tdListKbrCount = tdList.length;
   
   //by default the answers are displayed in full, if javascript is enabled, this will apply the 
   //class tOff which limits the height to two lines
   $("td.KBR div.expandingText").addClass("tOff")
   
   //this goes through each group where the scrollHeight (height of actual text) 
   //is greater than the clientHeight (height of displayed text)
   //i.e. all groups where text gets cut off and an 'expand' button is needed
   $("td.KBR div.expandLink").filter(function () {
                                      //this is the filter to only apply the button where needed
                                      return ($(this).siblings(".expandingText").get(0).scrollHeight > $(this).siblings(".expandingText").get(0).clientHeight);
                                     }).each(function(i){
                                              //for each of these groups, add the button span
                                              $(this).append('<span class="expanding Off">' + expText + '</span>');
                                              //now find this added button span and set it's click property
                                              $(this).children(".expanding").click(function() {
        //toggle the span's class
        $(this).toggleClass("Off");
        
        if (this.innerHTML == colText) {
            this.innerHTML = expText;
        } else {
            this.innerHTML = colText;
        }
        //toggle the related text block's class
        $(this).parent().siblings(".expandingText").toggleClass("tOff");
        //set the height of the button's container to the height of the text block minus the height of the offset block
        var txtHeight = $(this).parent().siblings(".expandingText").get(0).clientHeight;
        var spacerHeight = $(this).parent().prev("div").get(0).clientHeight;
                                                                                            
        if (txtHeight == 0 && spacerHeight == 0) {
            txtHeight = $(this).parent().siblings(".expandingText").get(0).scrollHeight;
            spacerHeight = $(this).parent().prev("div").get(0).scrollHeight;
        }
        
        if (txtHeight == 0 || spacerHeight == 0) {
            $(this).parent().get(0).style.height = "auto";
        } else {
            $(this).parent().get(0).style.height = (txtHeight - spacerHeight) + "px";
        }                                                                             
        
        
      }); 
    });
        
}




function isMarked(ele, marking, markingType) {
    if (marking === '') return true;
    var markerContents = $(ele).children("span." + markingType).text();
    return markerContents.match(marking);
}


function setThisFilter(filter) {
   //alert("setting filter " + filter.attr("id"));
    var lastText = "";
    filter.children("option").each(function() {
        var opt = this;
        var count = 0;
        if (filter.attr("class") !== 'country') {
            //alert(opt.value +' ' + $("#contactList div.country:visible div.contact:visible").length);
            $("#contactList div.country:visible div.contact:visible").each(function() {
                if (isMarked(this, opt.value, filter.attr("class") + '-id')) {
                    count += 1;
                }
            });
        } else {
            $("#contactList div.country:visible").each(function() {
                if ($(this).attr('class').match(opt.value)) {
                    count += $(this).children("div:visible").length;
                }
            });
        }

        var oldText = $(opt).text();
        var newText = oldText.replace(/\(.*\)/, '(' + count + ')');
        //$(opt).text(newText);
        if (count === 0 && opt.value !== filter.attr("class")) {
            opt.disabled = true;
            opt.style.color = "graytext";
            
        } else {
            opt.disabled = false;
            opt.style.color = "black";
            
        }
    });

}

var countryFilter, productLineFilter, applicationFilter, filterType1, filterType2, filterType3;
var filterByLabel, thenByLabel1, thenByLabel2;

var filterSettings = {};

function getContactType() {
    var contactType = $("#contactTabs ul li.pin").attr("class");
    contactType = contactType.trim();
    contactType = contactType.replace(/^pin\s|\spin$/, '');
    return contactType.trim();
}

function filterByContactOnly() {
    var countryFilterVal = 'country';
    var productLineFilterVal = 'product-line';
    var applicationFilterVal = 'application';
    doFilter(countryFilterVal, productLineFilterVal, applicationFilterVal);
}

function filter() {
    var countryFilterVal = '';
    var productLineFilterVal = '';
    var applicationFilterVal = '';

    if (countryFilter.length !== 0 && !countryFilter.get(0).disabled) {
        countryFilterVal = countryFilter.get(0).value;
    }
    if (productLineFilter.length !== 0 && !productLineFilter.get(0).disabled) {
        productLineFilterVal = productLineFilter.get(0).value;
    }
    if (applicationFilter.length !== 0 && !applicationFilter.get(0).disabled) {
        applicationFilterVal = applicationFilter.get(0).value;
    }

    doFilter(countryFilterVal, productLineFilterVal, applicationFilterVal);   
}

function doFilter(countryFilterVal, productLineFilterVal, applicationFilterVal) {

    var contactType = getContactType();

    $("#contactList div.country").each(function() {

        var hasChildren = false;

        $(this).children("div.contact").each(function() {
            if (isMarked(this, productLineFilterVal, 'product-line-id')
            && isMarked(this, applicationFilterVal, 'application-id')
            && $(this).hasClass(contactType)) {
                $(this).show();
                hasChildren = true;
            } else {
                $(this).hide();
            }
        });


        if (($(this).hasClass(countryFilterVal) || countryFilterVal === '') && hasChildren) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });

}



function pickFilterFromType(filterTypeSelect) {
    if (filterTypeSelect.value === 'application') {
        applicationFilter.get(0).disabled=false;
        applicationFilter.children("option").get(0).selected = true;
        $(filterTypeSelect).before(applicationFilter);
        setThisFilter(applicationFilter);
        applicationFilter.change(onFilterChange);
    }
    if (filterTypeSelect.value === 'product-line') {
        productLineFilter.get(0).disabled = false;
        productLineFilter.children("option").get(0).selected = true;
        $(filterTypeSelect).before(productLineFilter);
        setThisFilter(productLineFilter);
        productLineFilter.change(onFilterChange)
    }
    if (filterTypeSelect.value === 'country') {
        countryFilter.get(0).disabled = false;
        countryFilter.children("option").get(0).selected = true;
        $(filterTypeSelect).before(countryFilter);
        setThisFilter(countryFilter);
        countryFilter.change(onFilterChange);
    }
}





function removeFollowingFilters(e) {
    //alert('removing filters after ' + e.id);
    var nextSet = $(e).next("select").next("span").next("select");
    if (nextSet.get(0)) { removeFollowingFilters(nextSet.get(0)); }
    nextSet.next("select").remove();
    nextSet.next("span").remove();
    if (nextSet.length > 0) { nextSet.get(0).disabled = true; }
    nextSet.remove();
}

function removeOptionFrom(filter, val) {
    //alert('removing ' + val);
    filter.children("option").each(function() {
         
        if (this.value === val) { $(this).remove(); }
    });
    if (filter.children("option:visible").length > 0) {
        filter.children("option:visible").get(0).selected = true;
    }
}

function addFilter(e) {
    var contactType = getContactType();
    if (e.value !== $(e).attr("class")) {
        var filterType = $(e).next("select");
        var newSelect = filterType.clone();
        if (filterType.attr("id") === filterType1.attr("id")) {
            //alert('adding filter type 2');
            filterType2 = newSelect;
            filterType2.attr("id", "filterType2");
            filterType2.change(onFilterTypeChange);
            removeOptionFrom(filterType2, filterType1.get(0).value);
            if (filterType2.children("option").length > 0) {
                $(e).next("select").next("span").after(thenByLabel1);
                $(e).next("select").next("span").after(filterType2);
                filterSettings[contactType]["filterType2"] = filterType2.get(0).value;
                filterSettings[contactType]["filterType3"] = '';
                pickFilterFromType(filterType2.get(0));
            }
        } else if (filterType.attr("id") === filterType2.attr("id")) {
            //alert('adding filter type 3');
            filterType3 = newSelect;
            filterType3.attr("id", "filterType3");
            filterType3.change(onFilterTypeChange);
            removeOptionFrom(filterType3, filterType1.get(0).value);
            removeOptionFrom(filterType3, filterType2.get(0).value);
            if (filterType3.children("option").length > 0) {
                $(e).next("select").next("span").after(thenByLabel2);
                $(e).next("select").next("span").after(filterType3);
                filterSettings[contactType]["filterType3"] = filterType3.get(0).value;
                pickFilterFromType(filterType3.get(0));
            }
        } else {
            //
        }
       
    }
}


function onFilterTypeChange() {
    //alert(this.id + ' changed');
    removeFollowingFilters($(this).prev("select").get(0));
    if ($(this).prev("select").length > 0) {$(this).prev("select").get(0).disabled = true; }
    $(this).prev("select").remove();
    
    filter();
    pickFilterFromType(this);
    
    var contactType = $("#contactTabs ul li.pin").attr("class");
    contactType = contactType.trim();
    contactType = contactType.replace(/^pin\s|\spin$/, '');
    contactType = contactType.trim();

    filterSettings[contactType][this.id] = this.value;
    if (this.id === "filterType1") {
        filterSettings[contactType]["filterType2"] = '';
    }
    if (this.id === "filterType1" || this.id === "filterType2") {
        filterSettings[contactType]["filterType3"] = '';
    }
    
    
}

function onFilterChange() {
    //alert('filter change');
    removeFollowingFilters(this);
    filter();
    addFilter(this);

    var contactType = $("#contactTabs ul li.pin").attr("class");
    contactType = contactType.trim();
    contactType = contactType.replace(/^pin\s|\spin$/, '');
    contactType = contactType.trim();
    
    filterSettings[contactType][$(this).attr("class")] = this.value;
}


function setFilterType(contactType, filterTypeId, filterTypeInst) {
    
    if (filterSettings[contactType][filterTypeId] !== '') {
        //alert("reloading " + filterTypeId);
        var filterType = filterSettings[contactType][filterTypeId];
        filterTypeInst.get(0).value = filterType;
        if (filterTypeInst.get(0).value === '') {
            filterTypeInst.children("option").get(0).selected = true;
        }
        filterTypeInst.change();
        
        var filterValue = filterSettings[contactType][filterType]
        if (typeof (filterValue) === 'undefined') {
            filterValue = filterType;
        }
        if (filterTypeInst.prev("select").length > 0) {
            filterTypeInst.prev("select").get(0).value = filterValue;
            filterTypeInst.prev("select").change();
        }
    }
}


function loadSettings(contactType) {
    setFilterType(contactType, 'filterType1', filterType1);
    setFilterType(contactType, 'filterType2', filterType2);
    setFilterType(contactType, 'filterType3', filterType3);
}


function setUpContactTabs() {


    if ($("#contactTabs ul li").length > 0) {
        $("#narrowResults").hide();
    }
    
    $("#contactTabs ul li").click(function() {
        if (!$(this).hasClass("pin")) {
            $(this).parent().children().removeClass("pin");
            //filterContactType($(this).attr("class"));
            $(this).addClass("pin");
            var contactType = getContactType();
            if (typeof (filterSettings[contactType]) === 'undefined') {
                filterSettings[contactType] = { 'filterType1': 'application' };
            }
            filterByContactOnly();
            loadSettings(contactType);
            //filter();

        }
    });

    

    filterByLabel = $("#contactFilters span.filter-by-label");
    thenByLabel1 = $("#contactFilters span.then-by-label");
    thenByLabel2 = thenByLabel1.clone();
    thenByLabel1.remove();
   
    countryFilter = $("#contactFilters select.country");
    if (countryFilter.length > 0) { countryFilter.get(0).disabled = true; }
    countryFilter.remove();
    countryFilter.change(onFilterChange);

    productLineFilter = $("#contactFilters select.product-line");
    if (productLineFilter.length > 0) { productLineFilter.get(0).disabled = true; }
    productLineFilter.remove();
    productLineFilter.change(onFilterChange);

    applicationFilter = $("#contactFilters select.application");
    applicationFilter.change(onFilterChange);

    filterType1 = $("#contactFilters select#filterType1");
    filterType1.change(onFilterTypeChange);

    $("#contactTabs ul li:first").click();
    filterType1.change();
}


$(document).ready(function() {

    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    }

    setUpContactTabs();

    var previewDomainString = "";
    if (typeof (previewDomain) !== 'undefined') {
        previewDomainString = previewDomain;
    }
    replace_kbr();
    $("div#branding").hover(function() { $(this).addClass("rollover"); },
                          function() { $(this).removeClass("rollover"); });
    $(".logo_image_rollover").hover(function() { this.src = previewDomainString + '/static/images/cabot_logo_rollover.gif'; },
                                  function() { this.src = previewDomainString + '/static/images/cabot_logo.gif'; });

    $("#narrowResults span.more, .pdl span.more_langs, .downloads_form_two span.more").show().hover(function() {
        $(this).addClass("moreSelected");
        $(this).next().stop();
        $(this).next().show();
        $(this).next().get(0).style.width = $(this).next().children(".linklists").children("table").get(0).clientWidth + 25;
    },
                                                      function() {
                                                          $(this).removeClass("moreSelected");
                                                          $(this).next().hide();
                                                      });

    $(".pdl span.more_langs").hover(function() {
        var x = $(this).next().get(0).offsetWidth;
        if (jQuery.browser.msie) { x = x + 10; }
        $(this).next().get(0).style.marginLeft = 151 - x + "px";
    },
                            function() { });

    $(".moreOptions").hover(function() {
        $(this).prev("span.more, span.more_langs").addClass("moreSelected");
        $(this).stop();
        $(this).show();
        //this.style.width = $(this).children(".linklists").children("table").get(0).clientWidth + 20;
    },
                          function() {
                              $(this).prev("span.more, span.more_langs").removeClass("moreSelected");
                              $(this).hide();
                          }
                          );


    $("ul#gateways li").hover(function() { $(this).addClass("over"); }, function() { $(this).removeClass("over"); });

    $("#search form").submit(function() {
        if ($("#query").get(0).value === "") {
            $("#query").get(0).focus();
            return false;
        }
        return true;
    });

    $(".downloads_form_advanced li.all_types input").click(function() {
        //alert ($(".downloads_form_advanced li.all_types input").get(0).checked);
        if ($(".downloads_form_advanced li.all_types input").get(0).checked) {
            $(".downloads_form_advanced li:not(.all_types) input").each(function(i) { this.checked = false; });
        }
    });
    $(".downloads_form_advanced li:not(.all_types) input").click(function() { $(".downloads_form_advanced li.all_types input").get(0).checked = false });



});

