Drawer = Class.create();
Drawer.prototype = {
	    
	    initialize: function (drawerPanel, options){
	        
	        var pageLoader = false;
	        this.drawerPanel = $(drawerPanel);
            	        
            /* 
             * Setup some sensible defaults for the options 
             */     
	        this.options = Object.extend({ duration: 0.25, current: 'panel_0' }, options || {});

	        this.events = {
	          click: this.click.bindAsEventListener(this)
	        };

	        this.__btn_Click();
	        
	        /*
	         * Hide the preloader
	         */
	        if (pageLoader){
	            this.slideTo('sub_home', this.scrollwindow, { duration:this.options.duration });
	            $('tab_home').down(0).addClassName('active');
	            //Effect.Fade('preload'); 
	        } 
        },
	
        __btn_Click: function() {
            
            /*
             * Find all internal anchors that start with #panel_
             */
            var subAnchors = $$('a[href^=#panel_])');
            //console.log(subAnchors);
            subAnchors.invoke('observe', 'click', this.events.click);
           
        },	

        click: function(event) {
            var element = Event.findElement(event, 'a');
			
			var cleaned = element.innerHTML.stripTags().toLowerCase().replace(/[\s-]+/g,"_").replace(/\W+/g,"");
			urchinTracker (currentPath + '/' + cleaned);

/*
             * get the name of the element, and highlight the tab associated
             * with that subdivision 
             */
            var elementName = element.href.split("#")[1];
            var buttonName = elementName.split("_")[1]; 
			
            /*
             * Loop through the navigation tabs and remove "active" classes
             */     
            var activeList = $('panelNav').getElementsByClassName('active');
            var activeNodes = $A(activeList);
            var panelList = $$('ul#panelNav li a');
            //$(panelList).hide();
            var updownArrow = $('panelArrows');
            
            activeNodes.each(function(activeNode){
                activeNode.removeClassName('active');
            });

			panelList.each(function(a) {
				var page = a.getAttribute('href').match(/[-_\w]+$/i)[0];
				$(page).hide();
				//if (page != this.options.current) { $(page).hide() }
				//console.log(this.options.duration); 
				//else { $(a.parentNode).addClassName('active') }

			});

            
            /*
             * Is the new tab name in the actual navigation? If not, don't try to add a classname, or it'll
             * toss an error
             */
            var tabNodeList = $('panelNav').immediateDescendants();
            var downState = 510;
			var upState = 300;
            
 			if(updownArrow){
				if(this.drawerPanel.getStyle('top') == (downState + "px")){
					this.drawer = new Effect.Vscroller(this.drawerPanel, {duration:this.options.duration, from: downState, to: upState});
					updownArrow.down(0).addClassName('active');
				}
				else{
					this.drawer = new Effect.Vscroller(this.drawerPanel, {duration:this.options.duration, from: upState, to: downState});
					updownArrow.down(0).removeClassName('active');
				}
			}           
            
            if(tabNodeList.include($(buttonName))){					
				if(this.drawerPanel.getStyle('top') == (downState + "px")){
					this.drawer = new Effect.Vscroller(this.drawerPanel, {duration:this.options.duration, from: downState, to: upState});
					$(buttonName).down(0).addClassName('active');
					updownArrow.down(0).addClassName('active');
					$(elementName).show(); 
				}
				else{
					//this.drawer = new Effect.Vscroller(this.drawerPanel, {duration:this.options.duration, from: upState, to: downState});
					//$(buttonName).down(0).removeClassName('active');
					$(elementName).show();
					$(buttonName).down(0).addClassName('active');
					updownArrow.down(0).addClassName('active');
					this.drawer.cancel();
				}
				//this.drawer = new Effect.Vscroller(this.drawerPanel, {duration:this.options.duration, from: downState, to: upState});
				/*$(buttonName).down(0).addClassName('up');*/
				Event.stop(event);
            }
			
					
        }
};

Slider = Class.create();
Slider.prototype = {
	    initialize: function (pageid, options){
	        var panelInit = false;
	        this.pageid = $(pageid);
            
            /*
             * Brute force way to check if team artisic member is part of "panel_2", if so slide there "on init"
             */
            //this.contentClass = $('content');
            //console.log(this.contentClass.className);
            if($('content').hasClassName('joni') || $('content').hasClassName('laurie') || $('content').hasClassName('leigh') || $('content').hasClassName('tony')) {
				panelInit = true;
			} 
			
	        this.scrollwindow = this.pageid.down('div#sliderWindow', 0); /* go "down" into the dom */
            
            /* -- debugging 
				console.log ("this.scrollwindow: " + this.scrollwindow); */
            /* -- end debugging */
            	        
            /* 
             * Setup some sensible defaults for the options 
             */     
	        this.options = Object.extend({ duration: 1.0 }, options || {});

	        this.events = {
	          click: this.click.bindAsEventListener(this)
	        };

	        this.__btn_Click();
	        
	        /*
	         * Hide the preloader
	         */
	        if (panelInit){
	            this.slideTo('panel_2', this.scrollwindow, { duration: 0.8 });
	            //$('tab_home').down(0).addClassName('active');
	            //Effect.Fade('preload'); 
	        } 
        },
	
        __btn_Click: function() {
            
            /*
             * Find all internal anchors that start with #sub_
             */
            var subAnchors = $$('a[href^=#panel_])');
            subAnchors.invoke('observe', 'click', this.events.click);
            //subAnchors.invoke('observe', 'mouseover', this.events.click);
           
        },	

        click: function(event) {
		    //this.stop();
            var element = Event.findElement(event, 'a');
            /*
             * get the name of the element, and highlight the tab associated
             * with that subdivision 
             */
            var elementName = element.href.split("#")[1];
            var loopCounter = elementName.split("_")[1];
            var previous = parseInt(loopCounter) - 1;
            var next = parseInt(loopCounter) + 1;
            var sectionNamePrevious = "panel_" + previous;
            var sectionNameNext = "panel_" + next;
            
			
			var cleaned = element.innerHTML.stripTags().toLowerCase().replace(/[\s-]+/g,"_").replace(/\W+/g,"");
			urchinTracker (currentPath + '/' + sectionNameNext);
			
            if (this.slider){
                this.slider.cancel();
            }
            this.slideTo(elementName, this.scrollwindow, { duration:this.options.duration });     
            Event.stop(event);
            var sectionList = $('slideContent').immediateDescendants();
            if(sectionList.include($(sectionNamePrevious))){
				$('style_arrow_previous').href = "#panel_" + previous;
            }
            
            if(sectionList.include($(sectionNameNext))){
				$('style_arrow_next').href = "#panel_" + next;
			}            
        },

	    slideTo: function(currentElement, slideWindow, options){
	        var windowOffset = Position.cumulativeOffset(slideWindow), elementOffset = Position.cumulativeOffset($(currentElement));
            /*
		     * subtract 25px to make up for left padding
             */ 
		    var elementAdjustX = elementOffset[0];
		    /* 
             * Call scroll effect - script.aculo.us extended effect
             */
		    this.slider = new Effect.Hscroller(slideWindow, {duration:options.duration, x:(elementAdjustX-windowOffset[0])});
		}
};

Effect.Vscroller = Class.create();
Object.extend (Object.extend (Effect.Vscroller.prototype, Effect.Base.prototype), {
        
        initialize: function(element) {
            
            this.element = $(element);
            var options = Object.extend({
                from: 0,
                to: 0
            } , arguments[1] || {}  );
            
            this.start(options);
            
          },
          setup: function() {
          },
          update: function(position) {   
            this.element.style.top = position + "px";
          }
});

Effect.Hscroller = Class.create();
Object.extend (Object.extend (Effect.Hscroller.prototype, Effect.Base.prototype), {

        initialize: function(element) {
            
            this.element = $(element);
            var options = Object.extend({
                x:    0
            } , arguments[1] || {}  );
            
            this.start(options);
          },
          setup: function() {
            this.originalLeft = this.element.scrollLeft;
            this.options.x -= this.originalLeft;
          },
          update: function(position) {   
            this.element.scrollLeft = this.options.x * position + this.originalLeft;
          }
});
