function classBehaviour(){
    //the handler names and handler arrays
    this.name = 'libClassBehaviour';
    this.initialized = false;
    this.handlerNames = ["first"];
    this.handlers = [];
    //crossbrowser vars
    this.isDOM = document.getElementById ? true : false;
    this.isIE = document.all ? true : false;
    this.allNodes = this.isDOM ? document.getElementsByTagName("*") : document.all;
    if (document.all && navigator.appVersion.lastIndexOf('MSIE 6.0') != -1) {
        document.write('<span class="CBinit" />');
    }
    //initialize
    this.includeHandlers(this.allNodes);
}

classBehaviour.prototype.includeHandlers = function(allNodes){
    // for all tags
    for (var n = 0; n < allNodes.length; n++) {
        inc = false;
        // if the item has a className
        if (allNodes[n].className) {
            nodeClasses = allNodes[n].className.split(" ");
            //for each class of the element
            for (s = 0; s < nodeClasses.length; s++) {
                if (nodeClasses[s].substr(0, 2) == 'CB') {
                    for (h = 0; h < this.handlerNames.length; h++) {
                        if (this.handlerNames[h] == nodeClasses[s].toLowerCase().substr(2)) {
                            nodeClasses[s] = "";
                        }
                        else {
                            inc = true;
                        }
                    }
                }
                else {
                    nodeClasses[s] = "";
                }
            }
            if (inc) {
                for (x = 0; x < nodeClasses.length; x++) {
                    if (nodeClasses[x] != "") {
                        document.write('\n<script type="text\/javascript" src="\/_layouts\/nucletron\/js\/' + nodeClasses[x].toLowerCase().substr(2) + '.js"><\/script>');
                        this.handlerNames[this.handlerNames.length] = nodeClasses[x].toLowerCase().substr(2);
                    }
                }
            }
        }
    }
    document.write('');
    //set an interval for IE to load CB
    document.write('<script type="text\/javascript">I = setTimeout("if(CB.initialized){CB.parseDocument();};", 500);<\/script>');
}

// parse the document for classnames
classBehaviour.prototype.parseDocument = function(){
    // for all tags
    for (var n = 0; n < this.allNodes.length; n++) {
        inc = false;
        // if the item has a className
        if (this.allNodes[n].className) {
            nodeClasses = this.allNodes[n].className.split(" ");
            //for each class of the element
            for (s = 0; s < nodeClasses.length; s++) {
                if (nodeClasses[s].substr(0, 2) == 'CB') {
                    for (h = 0; h < this.handlerNames.length; h++) {
                        nodeClass = nodeClasses[s].toLowerCase().substr(2);
                        // if the behaviour's name exists in the class name, apply it's events
                        if (nodeClass == this.handlerNames[h]) {
                            this.handlers[this.handlers.length] = eval('new ' + this.handlerNames[h] + '(this.allNodes[n])');
                        }
                    }
                }
            }
        }
    }
}
classBehaviour.prototype.getClassParameter = function(targetNode, paramName, defaultValue){
    // get the class parameter from the classname
    var classParameter = targetNode.className;
    // split the classname between the parameter name
    classParameter = classParameter.split(paramName + '_');
    // split the second piece between spaces and take the first part,  if there are two pieces
    classParameter = (classParameter.length > 1) ? classParameter[1].split(' ')[0] : null;
    // return the value
    return (classParameter != null) ? classParameter : defaultValue;
}
classBehaviour.prototype.getParentNode = function(node){
    prnt = node.parentNode;
    while (prnt.className.lastIndexOf('parentNode') == -1) {
        prnt = prnt.parentNode;
        if (prnt.nodeName == 'BODY') {
            break;
        }
    }
    return prnt;
}
classBehaviour.prototype.getElementsByClassName = function(node, classname){
    allNodes = node.getElementsByTagName("*");
    nodes = [];
    CBcnt = allNodes.length;
    for (q = 0; q < CBcnt; q++) {
        classes = allNodes[q].className.split(' ');
        for (n = 0; n < classes.length; n++) {
            if (classes[n] == classname) {
                nodes[nodes.length] = allNodes[q];
            }
        }
    }
    return (nodes);
}
/*TIMER FUNCTION FOR COMMON USE*/
function animate(d){
    this.d = d;
    
    this.time = 0;
    var self = this;
    if (!this.lock) {
        if (!this.d) {
            this.node.insertBefore(this.items[this.items.length - 1], this.items[0]);
            this.node.style.left = -(this.itemWidth) + 'px';
            this.node.style.clip = 'rect(0px ' + (this.itemWidth * 2) + 'px 200px ' + this.itemWidth + 'px)';
            
        }
        this.counter = setInterval(function(){
            self.update();
        }, 1);
        this.lock = true;
    }
}

//TODO: Build in other directions. Build in acceleration
function update(time){
    this.time += time;
    deltaX = 15;//(5 * -Math.cos(this.Period * this.time)) + 5;
    switch (this.direction) {
        case 'right':
            if (this.d ? parseInt(this.node.style.left) > -this.itemWidth : parseInt(this.node.style.left) < 0) {
                this.node.style.left = this.d ? (parseInt(this.node.style.left) - deltaX + 'px') : (parseInt(this.node.style.left) + deltaX + 'px');
                this.node.style.clip = this.d ? ('rect(0px ' + (this.itemWidth - (parseInt(this.node.style.left))) + 'px ' + this.itemHeight + 'px ' + Math.abs(parseInt(this.node.style.left)) + 'px)') : ('rect(0px ' + (250 - (parseInt(this.node.style.left))) + 'px ' + this.itemHeight + 'px ' + (-Math.abs(parseInt(this.node.style.left))) + 'px)');
            }
            else {
                clearInterval(this.counter);
                if (this.d) {
                    this.node.appendChild(this.items[0]);
                }
                this.node.style.left = 0;
                this.node.style.clip = 'rect(0px 250px 200px 0px)';
                this.setActive(this.d);
                this.lock = false;
            }
            break;
            
    }
}

function init(){
    CB.initialized = true;
}


var CB = new classBehaviour();
    
if (navigator.appVersion.lastIndexOf('MSIE 6.0') == -1
|| /MSIE (\d+\.\d+);/.test(navigator.userAgent)
|| navigator.appName == "Microsoft Internet Explorer") {
    init();
}