﻿var ns4 = (document.layers) ? true : false;         //NS 4
var ie4 = (document.all) ? true : false;         //IE 4
var dom = (document.getElementById) ? true : false;   //NS 6 ou IE 5


function SetDiv(ID, Content) {
    if (dom) {
        getElementsByClassName(ID)[0].innerHTML = Content;
        return;
    }
    if (ie4) {
        document.all[ID].innerHTML = Content;
        return;
    }
    if (ns4) {
        with (eval('document.' + ID + '.document')) {
            open();
            write(Content);
            close();
        }
        return;
    }
}
function getElementsByClassName(className, tag, elm) {
    var testClass = new RegExp("(^|s)" + className + "(s|$)");
    tag = tag || "*";
    elm = elm || document;
    var elements = (tag == "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for (var i = 0; i < length; i++) {
        current = elements[i];
        if (testClass.test(current.className)) {
            returnElements.push(current);
        }
    }
    return returnElements;
};

var menuToggle = {
    create: function(target) {
        return {
            id: "no name",
            orientation: "vertical",
            myObject: target,
            min: 0,
            max: target.offsetHeight,
            speed: 500,
            isOpen: false,
            move: function(titleChange) {
                if (this.myObject.testTime == null) {
                    var start = (this.isOpen) ? this.max : this.min;
                    var end = (this.isOpen) ? this.min : this.max;
                    var c = (350 / 50);
                    var d = Math.round(this.speed / c);
                    var w = new Array();
                    this.myObject.currentStepX = 0;
                    this.myObject.testTime = null;
                    for (var i = 1; i <= d; i++) {
                        w.push(this.easeOutCubic(i * c, start, end - start, this.speed))
                    }
                    titleChange.className = (this.isOpen) ? "toggleTitle close" : "toggleTitle open"
                    this.go(this, c, w);
                }
            },
            go: function(b, c, w) {
                b.myObject.testTime = window.setInterval(function() {
                    var a = true;
                    if (w[b.myObject.currentStepX]) {
                        b.myObject.style['height'] = w[b.myObject.currentStepX] + 'px';
                        b.myObject.style['line-height'] = w[b.myObject.currentStepX] + 'px';
                        b.myObject.currentStepX++;
                        a = false
                    }
                    if (a) {
                        window.clearInterval(b.myObject.testTime);
                        b.myObject.testTime = null;
                        b.isOpen = (b.isOpen) ? false : true;
                        if (b.callBack) {
                            b.callBack()
                        }
                        return
                    }
                },
			                c);
            },
            easeOutCubic: function(t, b, c, d) {
                return c * ((t = t / d - 1) * t * t + 1) + b;
            }
        }
    }
};
