/*-------------------------------------------------------------------------------*/
/*------------------ Patrick's Coda Slider Class adapted from glider project -----------------------*/
/*-------------------------------------------------------------------------------*/


Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
	initialize: function(wrapper, options){
	    this.scrolling  = false;
	    this.wrapper    = $(wrapper);
	    this.scroller   = this.wrapper.down('div.scroller');
	    this.sections   = this.wrapper.getElementsBySelector('div.section');
	    this.options    = Object.extend({ duration: 1.0, frequency: 3 }, options || {});
	
	    this.sections.each( function(section, index) {
	      section._index = index;
	    });    

	    this.events = {
	      click: this.click.bind(this)
	    };

	    this.addObservers();
			if(this.options.initialSection) this.moveTo(this.options.initialSection, this.scroller, { duration:this.options.duration });  // initialSection should be the id of the section you want to show up on load
			if(this.options.autoGlide) this.start();
	  },
	
  addObservers: function() {
    var controls = this.wrapper.getElementsBySelector('div.controls a');
    controls.invoke('observe', 'click', this.events.click);
  },	

  click: function(event) {
	this.stop();
    var element = Event.findElement(event, 'a');
	
    if (this.scrolling) this.scrolling.cancel();
    
    this.moveTo(element.href.split("#")[1], this.scroller, { duration:this.options.duration });     
    Event.stop(event);
  },

	moveTo: function(element, container, options){
			this.current = $(element);
			
			//swap tabs function
			$$(".controlTab").each(function(el){
				el.removeClassName('selected');
			});
			
			var selectedID = this.current.id+'Tab'; 
			var selectedTab = $(selectedID);
			selectedTab.addClassName('selected');
			
			//set the current tab in the slide menu
			//slideMenu.setCurrent(selectedTab);
			
			
			
			//end swap tabs
			
			Position.prepare();
			var containerOffset = Position.cumulativeOffset(container),
			elementOffset = Position.cumulativeOffset($(element));

		  this.scrolling 	= new Effect.SmoothScroll(container, 
				{duration:options.duration, x:(elementOffset[0]-containerOffset[0]), y:(elementOffset[1]-containerOffset[1])});
		  return false;
		},
		
  next: function(){
    if (this.current) {
      var currentIndex = this.current._index;
      var nextIndex = (this.sections.length - 1 == currentIndex) ? 0 : currentIndex + 1;      
    } else var nextIndex = 1;

    this.moveTo(this.sections[nextIndex], this.scroller, { 
      duration: this.options.duration
    });
  },
	
  previous: function(){
    if (this.current) {
      var currentIndex = this.current._index;
      var prevIndex = (currentIndex == 0) ? this.sections.length - 1 : 
       currentIndex - 1;
    } else var prevIndex = this.sections.length - 1;
    
    this.moveTo(this.sections[prevIndex], this.scroller, { 
      duration: this.options.duration
    });
  },

	stop: function()
	{
		clearTimeout(this.timer);
	},
	
	start: function()
	{
		this.periodicallyUpdate();
	},
		
	periodicallyUpdate: function()
	{ 
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.next();
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	}

});

Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }
   
    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;
   
    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } 
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});

/*-------------------------------------------------------------------------------*/
/*------------------------ Patrick's SLide Menu Class ---------------------------*/
/*-------------------------------------------------------------------------------*/
slideMenu = Class.create();
Object.extend(Object.extend(slideMenu.prototype, Abstract.prototype), {						
//var slideMenu = Class.create({
    initialize: function(menu, overColor, outColor) {
        this.menu = $(menu),
        this.current = this.menu.select('li.current').first();
		this.wrapper = this.menu.up('div');
		
		this.controlWidth = this.menu.getWidth();
		this.overColor = overColor;
		this.outColor = outColor;
		
		this.wrapper.setStyle({width: this.controlWidth+'px'});
		
		this.wrapper.observe('mouseout', function(event){
		
			var el = this.wrapper;
			var yLimitBtm = el.offsetTop + el.getHeight();
			var yLimitTop = el.offsetTop;
			var xLimitRight = el.offsetLeft + el.down('ul').getWidth();
			var xLimitLeft = el.offsetLeft;
			
			
			
			if((Event.pointerY(event) > yLimitBtm) || (Event.pointerY(event) <= yLimitTop) || (Event.pointerX(event) > xLimitRight) || (Event.pointerX(event) < xLimitLeft)){
				this.slideBackground(this.current);
			}
		}.bind(this));
		
		this.menu.select('li a').each(function(item) {
			Event.observe(item, 'click',
            function() {
                this.setCurrent(item.up('li'), 'nextprev');
            }.bind(this));
		}.bind(this));
		
        this.menu.select('li').each(function(item) {
            Event.observe(item, 'mouseover',
            function() {
				
                this.slideBackground(item);
            }.bind(this));
        }.bind(this));
		
		
		$('sliderNext').observe('click', function(){ 
												  
			var currentTab = this.current.next();
			var currentTab = $(currentTab);
			
			if(!currentTab.down('a')){
				
				var currentTab = this.menu.select('li').first();
			}
			
			this.setCurrent(currentTab, 'nextprev'); 
			this.slideBackground(currentTab);
		
			
		}.bind(this));
		
		
		$('sliderPrev').observe('click', function(){ 
												  
			var currentTab = this.current.previous();
			
			
			if(currentTab == null){
				
				var currentTab = this.menu.select('li').last().previous();
			}
			
			this.setCurrent(currentTab, 'nextprev'); 
			this.slideBackground(currentTab);
		
			
		}.bind(this));
		
	
        new Element.insert(this.menu, '<li class="background"><div class="leftSlideTab">&nbsp;</div></li>')
        this.back = this.menu.select('li.background').first();
        if (this.current) {
            this.setCurrent(this.current)
        };
    },

    setCurrent: function(element, from) {
		
		if(from == 'nextprev'){
			
		} else {
		   this.back.setStyle({
				left: (element.offsetLeft) + 'px',
				width: (element.offsetWidth) + 'px'
			});
		}
		var currentLink = element.down('a');
		//var overColor = this.overColor;
		//var outColor = this.outColor;
		
		var colorQueue = Effect.Queues.get('colorChange');
		colorQueue.each(function(effect) { effect.cancel(); });
		
		this.menu.select('li a').each(function(item){
			if(item !== currentLink){
				item.setStyle({color: this.outColor});
			} else {
				//item.setStyle({color: this.overColor});
				new Effect.Morph(item, {style: {color: this.overColor}, duration: 0.5, queue: {scope: 'colorChange', limit: 1 }});
			}
		}.bind(this));

        this.current = element;
    },
	
    slideBackground: function(slideToElement) {
		
		var queue = Effect.Queues.get('slideScope');
		queue.each(function(effect) { effect.cancel(); });

		
		var moveSlider = new Effect.Move(this.back, {
				transition: Effect.Transitions.SwingTo,
				x: slideToElement.offsetLeft,
				y: 0,
				mode: 'absolute',
				queue: {scope: 'slideScope', limit: 2 }
				
		});
	
			
	  var changeSliderWidth = new Effect.Morph(this.back, {
				transition: Effect.Transitions.SwingTo,
				style: {
					width: slideToElement.getWidth() + 'px'
				},
				queue: {scope: 'slideScope', limit: 2 }
		});
		
    } // end slideBackground functions
});
