window.addEvent('domready', function(){
  if($('slideArea')){
    var numScrollElements=$$('div.slideContent').length;
    var currentPos=1;
    var newPos=1;
    //set wrapper_width
    var wrapperWidth=($('slideArea').getStyle('width').toInt()) * numScrollElements;
    $('slideWrapper').setStyle('width', wrapperWidth);
    //scroll FX
    var scroll = new Fx.Scroll('slideArea', {
    	wait: false,
    	duration: 600,
    	transition: Fx.Transitions.Sine.easeOut,
    	fps: 50
    });
    var scrollElements=$$('div.slideContent');
    for(i=0; i < scrollElements.length; i++){
    	scrollElements[i].id='slideContent'+(i+1);
    }  
  	//create controls
  	if(numScrollElements > 1){
      var btnToLeft=new Element('a', {
        'events': {
          'click': function(){ slideBtnLeftClick(); }
        },
        'id': 'slideBtn_toleft'
      });
      btnToLeft.injectInside('slideBtnArea'); 	
      var btnToRight=new Element('a', {
        'events': {
          'click': function(){ slideBtnRightClick(); }
        },
        'id': 'slideBtn_toright'
      });
      btnToRight.injectInside('slideBtnArea');
  	}
    //create direct controls
    for(var i=1; i <= numScrollElements; i++){          
      var tmpObj=new Element('a', {
        'events': {
          'click': function(){
            slideDirectBtnClick(this);
          }
        },
        'id': 'slideDirectBtn'+i,
        'class': 'slideDirectBtn'
      });
      tmpObj.injectInside('slideDirectBtnArea');
    }
    doTheScroll();
  }  
  
  //handles btn clicks
  function slideBtnLeftClick(){
		if(currentPos-1 < 1) newPos=numScrollElements;
		else newPos=currentPos-1;
    doTheScroll();
  }
  function slideBtnRightClick(){
		if(currentPos+1 > numScrollElements) newPos=1;
		else newPos=currentPos+1;
    doTheScroll();  	
  }
  //handles direct btn clicks
  function slideDirectBtnClick(callObj){
    newPos=callObj.id.replace('slideDirectBtn', '').toInt();
    doTheScroll();
  }
  //do the scroll wrapper
  function doTheScroll(){      
    scroll.toElement('slideContent'+newPos);
    updateSlideDirectBtns();
    currentPos=newPos;
  }
  function updateSlideDirectBtns(){
    $('slideDirectBtn'+currentPos).setStyle('backgroundPosition', '0px 0px');
    $('slideDirectBtn'+newPos).setStyle('backgroundPosition', '-10px 0px');        
  }
});
