window.addEvent('domready', function (){	
if($('slideshow')){

  /* ----------Config Vars----------- */
	var slideTimer = 5000;                      //time between slides (1 second = 1000), a.k.a. the interval duration
	var transitionTimeNormal = 300;             //transition time for normal play(1 second = 1000)
	var transitionTimeMouseOver = 0;            //transition time for mouseOver (1 second = 1000)
	var items = $$('.slide_item');              //get array of elements for sliding
	var numItems = items.length;                //get number of slider items	
	var numNav = new Array();                   //create an array to hold our dynamically created number navigation
	var items_container = $('slideshow');       //
	var itemNum = 0;                            //initialize a variable to hold the current slide index
	var isPaused = 0;
	var isSliding = 0;
	var maxAutoSlide = 25;
	
	if( numItems < maxAutoSlide )
	{
    maxAutoSlide = numItems;
	}
	
	var transitionTime = transitionTimeNormal;
	/* -------- end config vars -------- */
  
  //--------- setup stuff ---------//
  items.each(function(element, index){
		
		//since the viewer obviously has javascript on, we can remove the 'first_item' class
		if(index == 0){
			element.removeClass('first_item');
			element.setStyle('left', "0");
		}
		else{
			element.setStyle('left', "330px");
		}
		
		element.addEvents({
      'mouseover' : function(){
        isPaused = 1;
      },
      'mouseout' : function(){
        isPaused = 0;
      }
    });
	});
	
	//--------- end setup stuff ---------//
  
  //---------------------------------------------------------------------------------------------------------
	//	function: slideMove
	//	description: moves the appropriate slides in/out of view
	//	parameters:
	//		int direction - specifies type of movement (0=back, 1=forward, 2=jump to frame
	//		int passedID (optional) - index value to jump to when direction = 2
	//---------------------------------------------------------------------------------------------------------
  var curItem = items[itemNum];
  var newItem;
  
  var slideMove = function(direction, passedID){
		
		if( isPaused == 0 && isSliding == 0 && numItems > 1 )
    {
      //get item to slide out
      curItem = items[itemNum];
      
      //change index based on value of 'direction' parameter
      if(direction == 1){
        if(itemNum < (numItems - 1)){
          itemNum++; 
        }
        else{
          itemNum = 0;
        }
      }
      else if(direction == 0){
        if(itemNum > 0){
          itemNum--; 
        }
        else{
          itemNum = (numItems - 1);
        }
      }
      else {
        if(itemNum != passedID){
          itemNum = passedID;
        }
      }
        
      //now get item to slide in using new index
      newItem = items[itemNum];
      
      var item_in = new Fx.Morph(newItem, {
        duration: transitionTime, 
        transition: Fx.Transitions.Cubic.easeOut, 
        link: 'ignore',
         
        //click prevention functions
        onStart: function(){
          isSliding = 1;  //prevents extra clicks
        },
         
        onComplete: function(){ 
          isSliding = 0;  //allow clicks again
        }
      });
      
      var item_out = new Fx.Morph(curItem, {
         duration: transitionTime, 
         transition: Fx.Transitions.Cubic.easeOut,
         link: 'ignore'
      });
      
      //we will set a beginning value here
      //this is so that it gives the illusion of continuous motion from one direction, even after the first cycle of items
      item_in.start({
      'left': [237, 0]
      });
      
      //no beginning values needed, since we always want to push the old item out to the left
      item_out.start({
      'left': '-330'
      });
    }
    
    numItems = maxAutoSlide;
	};
	//--------------- end slideMove ---------------------
	
	//call the slider function periodically
	if( isPaused == 0 && isSliding == 0 )
	{
    numItems = maxAutoSlide;
    var theTimer = slideMove.periodical(slideTimer, this, 1);
  }
}
});
