(function(){
    var 
    bit = window.bit = window.$ = function (node){
        return new bit.fn.init(node);
    };
    
    bit.fn = bit.prototype = {
        init:function(node){
            var selector = document.getElementById(node);
            if(!selector) return null;
            
            this[0] = selector;
            this.length = 1;
            
            var p = this.getPadding();
            var b = this.getBorder();
            
            this[1] = this.dimension();
            this[2] = this.dimension();
                          
            this[1][0] -= (p.right+p.left)+(b.l+b.r);
            this[1][1] -= (p.top+p.bottom)+(b.t+b.b);
            
            return this;
        },        
        getPadding:function () {    
            var paddingValue = this.css("padding");
            
            var padding = {
                top:0,
                right:0,
                bottom:0,
                left:0
            };
                
            if(navigator.userAgent.match(/(firefox|webkit)/gi)) { 
            
                // firefox,safari und Opera bekommen das padding , funktioniert nicht im  IE 
                padding.top    = parseInt(this.css("padding-top"));
                padding.right  = parseInt(this.css("padding-right"));
                padding.bottom = parseInt(this.css("padding-bottom"));
                padding.left   = parseInt(this.css("padding-left"));
                
            } else {
                // Element.cssValue gibt 0 zurück
            	if(!paddingValue) return padding;
                
            	// Opera und IE geben das Padding als String zurück Bsp "3px 0px 4px 2px"
                var paddingPattern = /([\d]+)/g;
                var paddingMatches = [];
                if(paddingMatches = paddingValue.match(paddingPattern)) {
                    switch(paddingMatches.length) {
                        case 2:
                            padding.top    = parseInt(paddingMatches[0]);
                            padding.right  = parseInt(paddingMatches[1]); 
                            padding.bottom = parseInt(paddingMatches[0]);
                            padding.left   = parseInt(paddingMatches[1]); 
                        break;
                        case 3:
                            padding.top    = parseInt(paddingMatches[0]);
                            padding.right  = parseInt(paddingMatches[1]); 
                            padding.bottom = parseInt(paddingMatches[2]);
                            padding.left   = parseInt(paddingMatches[1]); 
                        break;
                        case 4:
                            padding.top    = parseInt(paddingMatches[0]);
                            padding.right  = parseInt(paddingMatches[1]); 
                            padding.bottom = parseInt(paddingMatches[2]);
                            padding.left   = parseInt(paddingMatches[3]); 
                        break;
                        default:
                            padding.top    = parseInt(paddingMatches[0]);
                            padding.right  = parseInt(paddingMatches[0]);
                            padding.bottom = parseInt(paddingMatches[0]);
                            padding.left   = parseInt(paddingMatches[0]);
                    };
                }
            }
            
            return padding;
        },        
        getBorder:function() {
            var b = {
                t:0,
                r:0,
                b:0,
                l:0
            };

            if(/msie/i.test(navigator.userAgent)) {
                
                b.t = parseInt(this.css("borderTopWidth"));
                b.r = parseInt(this.css("borderRightWidth"));
                b.b = parseInt(this.css("borderBottomWidth"));
                b.l = parseInt(this.css("borderLeftWidth"));
                
                b.t = isNaN(b.t)?0:b.t;
                b.r = isNaN(b.r)?0:b.r;
                b.b = isNaN(b.b)?0:b.b;
                b.l = isNaN(b.l)?0:b.l;
            } else {
                b.t = parseInt(this.css("border-top-width"));
                b.r = parseInt(this.css("border-right-width"));
                b.b = parseInt(this.css("border-bottom-width"));
                b.l = parseInt(this.css("border-left-width"));
            }
            
            return b;
        },
        setAttrib:function(attr,obj){
            var attribObject = arguments[0];
            
            for(key in arguments[0]){
                if(obj)
                    eval("obj.style."+key+" = arguments[0][key]");
                else {
                    eval("this[0].style."+key+" = arguments[0][key]");
                }
            }
        },
        css:function(attrib){
            if(window.getComputedStyle) {
                var val = window.getComputedStyle(this[0],'').getPropertyValue(attrib);
                if(val) {
                    return val;
                }
                
            } else {
            
                if(this[0].currentStyle && this[0].currentStyle[attrib]){
                    return this[0].currentStyle[attrib];
                }
           
           }
        },        
        dimension:function(){
            var displaynone = false;
            
            if(this.css("display") == "none") {
                this[0].style.display = "block";  displaynone = true;
            }
                
            var h = parseInt(this[0].offsetHeight);
            var w = parseInt(this[0].offsetWidth);
                                    
            if(displaynone) this[0].style.display = "none";
            
            return [w,h];
        },
        clone:function(obj_to_clone){
            var new_ = null;
            
            if(obj_to_clone.constructor == Array) {
                new_ = [];
                
                for(var i = 0 ; i < obj_to_clone.length;i++) {
                    new_[i] = obj_to_clone[i];
                }
                
            }
            return new_;
        },
        attrib:function(name){
            var attrib = this[0].getAttribute(name);
            
            if(attrib) return attrib;
            else       return false;
        },
        toggleEvent:function(event,func1,func2){
            var status = 0;
            
            if(!this[0]) return;
            
            this.stopEvent(event);
            
            this.bind('click',function(evt){
            
                var e = evt || window.event;
            
                bit.event.stopEvent(e);
            
                if(status == 0) { 
                    func1();
                    status = 1;
                } else {
                    func2();
                    status = 0;
                }
            });
        }
    }
    
    bit.extend = function(src,target){
        for(key in src)
            target[key] = src[key];
    }
        
    bit.fn.init.prototype = bit.fn;    
    
    bit.event = {
        bind:function(type,callback){
            if(document.addEventListener) { //gute Browser
            	 if(type.match(/^on/))  {
                    type = type.replace(/^on/,"");
                }
                
                this[0].addEventListener(type,callback,false);
            } else { // IE
            	if(!type.match(/^on/)) type = "on"+type;
                this[0].attachEvent(type,callback);
            }
        },
        stopEvent:function(e){
            if(e.preventDefault) e.preventDefault();
            else                 e.returnValue = false;
        }
    }
    bit.extend(bit.event,bit.fn);
    
    bit.effect = {
        speed:0.2,
        interval:null,
        grow:function(x,y){
            
            var grow_x = true;
            var grow_y = true;           

            if ( typeof arguments[0] == "boolean" ) grow_x = arguments[0];
            if ( typeof arguments[1] == "boolean" ) grow_y = arguments[1];
            
            var self = this;
            
            if(this[0].style.display == "block") return;
            
            var dummy = document.createElement('div');
            this[0].parentNode.insertBefore(dummy,this[0]);
            dummy.appendChild(this[0].cloneNode(true));
            this[0].parentNode.removeChild(this[0]);
            
            $(this[0].id).setAttrib({
                display:"block"
            })
            
            this.setAttrib({
                height:"0px",
                width:"0px",
                overflow:"hidden",
                display:"block"
            },dummy);
            
            var velocity = 0;
            var stop = [false,false];           

            var cw = self[2][0];
            var ch = self[2][1];
            
            function slide(){

                velocity += self.speed;            
                
                if(grow_x) cw = Math.round(self[2][0] * Math.sin(velocity));
                if(grow_y) ch = Math.round(self[2][1] * Math.sin(velocity));
                                                    
                self.setAttrib({
                    height:( (ch>=self[2][1]) ? self[2][1] : parseInt(ch) ) + "px",
                    width :( (cw>=self[2][0]) ? self[2][0] : parseInt(cw) )+ "px"
                },dummy)
                                
                if(velocity > Math.PI/2) {
                    // dummy entfernen
                    var e = $(self[0].id);                   
                    
                    e.setAttrib({
                        height: parseInt(self[1][1]) + "px",
                        width : parseInt(self[1][0]) + "px"
                    });
                    
                    dummy.parentNode.insertBefore(e[0].cloneNode(true),dummy);
                    dummy.parentNode.removeChild(dummy);
                    
                    window.clearInterval(self.interval);
                    
                    return;
                }
            }
            this.interval = window.setInterval(slide,35);
            
        },
        shrink:function(x,y){         
        
            var self = this;    
            var velocity = Math.PI/2;
            
            var current_width  = 0;
            var current_height = 0;
            
            var grow_x = true;
            var grow_y = true;
            
            if(this[0].style.display == "none") return;
            
            var dummy = document.createElement('div');
            this[0].parentNode.insertBefore(dummy,this[0]);
            dummy.appendChild(this[0].cloneNode(true));
            this[0].parentNode.removeChild(this[0]);
            
            if ( typeof arguments[0] == "boolean" ) grow_x = arguments[0];
            if ( typeof arguments[1] == "boolean" ) grow_y = arguments[1];
            
            function slide(){
                velocity -= self.speed;
                
                if(grow_x) current_width  = self[2][0]-Math.round(self[2][0]*Math.cos(velocity));
                if(grow_y) current_height = self[2][1]-Math.round(self[2][1]*Math.cos(velocity));
                
                if(velocity < 0) {
                    var e = $(self[0].id);
                    
                    e.setAttrib({
                        width:self[1][0]+"px",
                        height:self[1][1]+"px",
                        display:"none"
                    });
                    
                    dummy.parentNode.insertBefore(e[0].cloneNode(true),dummy);
                    dummy.parentNode.removeChild(dummy);
                    
                    window.clearInterval(self.interval);
                    
                    return;
                } else {
                
                    var h = (grow_y)?current_height:self[2][1];
                    var w = (grow_x)?current_width:self[2][0];
                                        
                    self.setAttrib({
                    
                        height:( ( h > self[2][1] ) ? self[2][1] : h ) + "px",
                        width :( ( w > self[2][0] ) ? self[2][0] : w ) + "px",
                        overflow:"hidden"
                        
                    },dummy);
                }
            }
            this.interval = window.setInterval(slide,35);
        },
        switchDisplay:function(){
            if(this[0].style.display == "none")
                this[0].style.display = "block";
            else 
                this[0].style.display = "none";
        }
    }
    bit.extend(bit.effect,bit.fn);
})();
