var DDSPEED = 5;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id, d, hl) {
    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');
    clearInterval(c.timer);
    if (d == 1) {
        if(hl)
            h.style.color = '#066C6F';
        clearTimeout(h.timer);
        if (c.maxh && c.maxh <= c.currh) { return }
        else if (!c.maxh) {
            c.style.display = "block";
            c.style.visibility = 'visible';
            c.style.height = 'auto';
            c.maxh = c.offsetHeight;
            c.currh = 0;
            c.style.height = '0px';
        }
        c.timer = setInterval(function() { ddSlide(c, 1) }, DDTIMER);
    } else {
        if(hl)
            h.style.color = '#404040';
        h.timer = setTimeout(function() { ddCollapse(c) }, 50);
    }
}

// collapse the menu //
function ddCollapse(c) {
    c.timer = setInterval(function() { ddSlide(c, -1) }, DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id) {
    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');
    clearTimeout(h.timer);
    clearInterval(c.timer);
    if (c.currh < c.maxh) {
        c.timer = setInterval(function() { ddSlide(c, 1) }, DDTIMER);
    }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c, d) {
    var currh = c.currh;
    var dist;
    if (d == 1) {
        dist = (Math.round((c.maxh - currh) / DDSPEED));
    } else {
        dist = (Math.round(currh / DDSPEED));
    }
    if (dist <= 1 && d == 1) {
        dist = 1;
    }
    c.currh = (currh + (dist * d));
    c.style.height = (currh + (dist * d)) + 'px';
    c.style.opacity = currh / c.maxh;
    c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';

    // Collapse
    if (currh < 5 && d != 1) {
        clearInterval(c.timer);
        c.style.height = "0px";
        c.style.visibility = 'hidden';
        c.maxh = 0;
        c.currh = 0;
        return;
    }

    // Expand
    if (currh > (c.maxh - 2) && d == 1) {
        clearInterval(c.timer);
    }
}

function GoAndera(val) {
    if (val.length == 0)
        return false;

    if (!isNaN(val)) {
        window.open("https://secure.andera.com/index.cfm?fiid=982B0AE5FA814FA88B1B74135E5D82F6");
        return false;
    } else {
        switch (val.toLowerCase()) {
            case "mortgage":
                location.href = '/pages/pLoans_mortgage.aspx?prodCAT=mortgage';
                break;
            case "creditcards":
                location.href = '/pages/productCompare.aspx?prodCAT=pCredit';
                break;
        }
    }
}

function HoverSelection(o, val) {
    if (o && (val == 1)) {
        o.style.backgroundColor = "#B3B3B3";
        o.style.color = "White";
    }

    if (o && (val == 0)) {
        o.style.backgroundColor = "#FFFFFF";
        o.style.color = "#52524E";
    }
}

function toggleHide(id) {
    var c = document.getElementById(id + '-ddcontent');
    if(c.currh > 0)
        setTimeout(function() { ddCollapse(c) }, 50);
}

function toggleShow(id) {
    var c = document.getElementById(id + '-ddcontent');
    c.style.display = "block";
    c.style.visibility = 'visible';
    c.style.height = 'auto';
    c.maxh = c.offsetHeight;
    c.currh = 0;
    c.style.height = '0px';
    c.timer = setInterval(function() { ddSlide(c, 1) }, DDTIMER);
}