
// 'http://www.scriptiny.com/2011/04/javascript-dropdown-menu/
//         'http://www.scriptiny.com/qa/
//         'var dropdown = new TINY.dropdown.init("dropdown", { id: 'menu', active: 'menuhover', fade: true, slide: true, timeout: 200 }); 
//         'id - ID of the navigation (string - required) 
//         '- active - CSS class for active state (string)
//         '- fade - toggle fade tween (bool) - true
//         '- slide - toggle slide tween (bool) - true
//         '- speed- toggle slide tween (1-9) 
//         '- 5timeout - time in milliseconds to pause before hide on rollout (int) - 200

var TINY = {};

function T$(i) { return document.getElementById(i) }
function T$$(e, p) { return p.getElementsByTagName(e) }

function showfeedback(str) {
    //document.getElementById('feedback').innerHTML += str + '<br/>'; //'toggleshow ' + x + ' - ' + showBoo + '<br/>';
}

TINY.dropdown = function () {
    var p = { fade: 1, slide: 0, active: 0, timeout: 300 }, init = function (n, o) {
        for (s in o) { p[s] = o[s] } p.n = n; this.build()
    };

    init.prototype.build = function () {
        this.identifier = p.n;
        this.h = [];
        this.c = [];
        this.titlesJS = [];
        this.z = 1000;
        this.lastclicked = -1;
        var s = T$$('ul', T$(p.id));
        var l = s.length;
        var i = 0;
        p.speed = p.speed ? p.speed * .1 : .5;

        for (i; i < l; i++) {
            var h = s[i].parentNode;
            this.h[i] = h;
            this.c[i] = s[i];
            this.titlesJS[i] = s[i].getAttribute("linkattribute");
            h.onmouseout = new Function(p.n + '.toggleshow(' + i + ')');
            h.onmouseover = null;
            h.onclick = new Function(p.n + '.click(' + i + ')');

        }
    };

    init.prototype.click = function (x) {
        //note: h[x].onmouseover is used to check whether the menu is expanded (if == null then it is not expanded)
        this.lastclicked = x;
        if (this.h[x].onmouseover == null) {
            this.toggleshow(x, 1);

        }
        else {
            if (this.titlesJS[x] != '') { document.location.href = this.titlesJS[x]; } //eval(this.titlesJS[x]);
        }
    }


    init.prototype.toggleshow = function (x, showBoo) {

        //showfeedback((showBoo) ? 'show ' + x : 'hide ' + x)

        var c = this.c[x]
        var h = this.h[x];

        if (this.lastclicked == x) {
            h.onmouseover = new Function(this.identifier + '.toggleshow(' + x + ',1)'); //set onmouseover
        }

        clearInterval(c.t);
        clearInterval(c.i);
        //c.style.overflow = 'hidden';

        if (showBoo) {
            if (p.active && h.className.indexOf(p.active) == -1) { h.className += ' ' + p.active; }
            if (p.fade || p.slide) {
                c.style.display = 'block';
                if (!c.m) {
                    if (p.slide) {
                        c.style.visibility = 'hidden'; c.m = c.offsetHeight; c.style.height = '0'; c.style.visibility = ''
                    } else {
                        c.m = 100; c.style.opacity = 0; c.style.filter = 'alpha(opacity=0)'
                    }
                    c.v = 0
                }
                if (p.slide) {
                    if (c.m == c.v) {
                        c.style.overflow = 'visible'
                    } else {
                        c.style.zIndex = this.z;
                        this.z++;
                        c.i = setInterval(function () { slide(c, c.m, 1) }, 20)
                    }
                } else {
                    c.style.zIndex = this.z; this.z++; c.i = setInterval(function () { slide(c, c.m, 1) }, 20)
                }
            } else {
                c.style.zIndex = this.z;
                c.style.display = 'block'
            }
        } else {
            this.lastclicked = -1;
            c.t = setTimeout(function () { hide(c, p.fade || p.slide ? 1 : 0, h, p.active); }, p.timeout)
        }
    };


    function hide(c, t, h, s) {
        h.onmouseover = null;  //clear inmousover
        if (s) { h.className = h.className.replace(s, '') }
        if (t) {
            c.i = setInterval(function () { slide(c, 0, -1) }, 20)
        } else {
            c.style.display = 'none';
        }
    }


    function slide(c, t, d) {
        if (c.v == t) {
            clearInterval(c.i); c.i = 0;
            if (d == 1) {
                if (p.fade) { c.style.filter = ''; c.style.opacity = 1 }
                c.style.overflow = 'visible'
            }
        } else {
            c.v = (t - Math.floor(Math.abs(t - c.v) * p.speed) * d);
            if (p.slide) { c.style.height = c.v + 'px' }
            if (p.fade) { var o = c.v / c.m; c.style.opacity = o; c.style.filter = 'alpha(opacity=' + (o * 100) + ')' }
        }
    }

    return { init: init }

} ();
