﻿//------------ header navigation dropdown list ------------//
var DDSPEED = 10;  // 10
var DDTIMER = 15; // 15

var visibleShim = false;

var container = null;
var CONTEINER_ID = 'header2contents';
var CONTEINER_INITIAL_WIDTH = 995;

function ddMenu(id, d) {
    if (!document.getElementById) return;

    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');
    var m = document.getElementById(id + '-ddmargin');

    clearInterval(c.timer);
    if (d == 1) {
        clearTimeout(h.timer);
        if (c.maxh && c.maxh <= c.clientHeight) { return }
        else {
            c.style.display = 'block';
            var height = (c.style.height != '') ? c.style.height : '0px';
            c.style.height = 'auto';
            c.style.left = 'auto';
            var width = c.offsetWidth;
            c.maxh = c.clientHeight - 1;
            c.style.height = height;
            c.style.left = h.offsetLeft + 'px';
            c.style.top = (h.offsetTop + h.offsetHeight + 4) + 'px';

            m.style.display = 'block';
            m.style.left = h.offsetLeft + 'px';
            m.style.top = (h.offsetTop + h.offsetHeight) + 'px';
            m.style.width = ((h.offsetWidth > width) ? h.offsetWidth : width) + 'px';

            if (visibleShim == true) {
                var shim = document.getElementById(id + '-ddshim');
                shim.style.display = 'block';
                c.shim = shim;
            }

            // specify header2contents's width to avoid the end of dropdown is invisible.
            // only for IE and FireFox2
            if (container == null) {
                var ua = navigator.userAgent;
                if ((ua.indexOf('MSIE', 0) > 0 && ua.indexOf('Opera') < 0) || ua.indexOf('Firefox/2') > 0) {
                    var divs = document.getElementsByTagName('div')
                    for (var i = 0; i < divs.length; i++) {
                        if (divs[i].className == CONTEINER_ID) {
                            container = divs[i];
                            container.style.width = (CONTEINER_INITIAL_WIDTH * 2) + 'px';
                        }
                    }
                }
            }

            if (document.body && document.body.getAttribute('onunload') == null) {
                document.body.setAttribute('onunload', '');
            }
        }
        c.timer = setInterval(function() { ddSlide(c, m, 1) }, DDTIMER);
    } else {
        h.timer = setTimeout(function() { ddCollapse(c, m) }, 50);
    }
}

// collapse the menu //
function ddCollapse(c, m) {
    c.timer = setInterval(function() { ddSlide(c, m, -1) }, DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id) {
    if(!document.getElementById) return;

    var h = document.getElementById(id + '-ddheader');
    var c = document.getElementById(id + '-ddcontent');
    var m = document.getElementById(id + '-ddmargin');

    clearTimeout(h.timer);
    clearInterval(c.timer);
    if (c.clientHeight < c.maxh) {
        c.timer = setInterval(function() { ddSlide(c, m, 1) }, DDTIMER);
    }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c, m, d) {
    var currh = c.clientHeight;
    var dist;
    if (d == 1) {
        dist = (Math.round((c.maxh - currh) / DDSPEED));
    } else {
        dist = (Math.round(currh / DDSPEED));
    }
    if (dist <= 1) {
        dist = 1;
    }
    if (currh == 0 && d == -1) {
        dist = 0;
    }

    c.style.height = currh + (dist * d) + 'px';

    var opacity = currh / c.maxh;
    if ((currh <= 0 && d != 1) || (currh >= c.maxh && d == 1)) {
        clearInterval(c.timer);
        opacity = (d == 1) ? 1 : 0;

        if (d != 1) {
            c.style.display = 'none';
            m.style.display = 'none';
            if (visibleShim == true && c.shim != undefined) {
                c.shim.style.display = 'none';
            }
        }
    }
    c.style.opacity = opacity;
    c.style.filter = 'alpha(opacity=' + opacity * 100 + ')';

    if (visibleShim == true && c.shim != undefined && c.shim.style.display == 'block') {
        setSameSize(c.shim, c);
    }
}

/* set obj1 size same as obj2 */
function setSameSize(obj1, obj2) {

    obj1.style.left = obj2.offsetLeft + 'px';
    obj1.style.top = obj2.offsetTop + 'px';
    obj1.style.width = obj2.offsetWidth + 'px';
    obj1.style.height = obj2.offsetHeight + 'px';
}

function enableShim() {
    // Shims is need for IE6 or elder.
    var ua=navigator.userAgent;
    if(ua.indexOf('MSIE 6', 0) > 0
    || ua.indexOf('MSIE 5', 0) > 0
    || ua.indexOf('MSIE 4', 0) > 0) {
        visibleShim = true;

        if (document.getElementById) {
            var container = document.getElementById('header2contents');
            if (container) {
                var nums = ['one', 'two', 'three', 'four', 'five', 'six'];

                for (var num in nums) {
                    var shim = document.createElement('iframe');
                    shim.setAttribute('class', 'ddshim');
                    shim.setAttribute('id', nums[num] + '-ddshim');
                    shim.setAttribute('scrolling', 'no');
                    shim.setAttribute('frameborder', '0');
                    shim.setAttribute('src', 'javascript:false;');
                    shim.style.display = 'none';
                    shim.style.position = 'absolute';
                    shim.style.zIndex = '2';
                    shim.style.width = '0px';
                    shim.style.height = '0px';
                    shim.style.filter = 'alpha(opacity=0)';
                    container.appendChild(shim);
                }
            }
        }
    }
    else {
        visibleShim = false;
    }
}
