window.addEvent('domready', function() {
	
	/* ----------Config Vars----------- */
	var slideTimer = 9000;  //time between slides (1 second = 1000), a.k.a. the interval duration
	var transitionTime = 750; //transition time (1 second = 1000)
	var items = $$('.slide_item');  //Get array of elements for sliding
	var prevBtn = $('prevbtn');
	var playBtn = $('playbtn');
	var nextBtn = $('nextbtn');
	
	var btnDot0 = $('Dot0');
	var btnDot1 = $('Dot1');
	var btnDot2 = $('Dot2');
	var btnDot3 = $('Dot3');
	var btnDot4 = $('Dot4');
	
	var itemNum = 0;  //initialize a variable to hold the current slide index
	var isPaused = 0;

	/* --------end config vars-------- */
	
	//Setup positions
	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', "988px");
		}
	
	});
	
	var numItems = items.length;  //get number of slider items
	//alert(numItems);
	//end setup
	
	var showPosition = function(){
		for(var i=0; i < numItems; i++)
		{		    
			var sID = "Dot" + i;
			var img = $(sID);
			if (img != null)
			{
			    if (i == itemNum)
			        img.src=g_imgDotOnButton;
			    else
			        img.src=g_imgDotOffButton;
			}
		}
	};
	
	var gotoSlide = function(index){
		
		if (index == itemNum)
			return;
			
        if(isPaused == 0){
	        isPaused = 1;
	        $clear(theTimer);
	        document.getElementById('playbtnimg').src=g_imgPlayButton;
        }
        
        var bMoveForward = (index < itemNum);
		
		//get item to slide out
		var curItem = items[itemNum];  
	
		itemNum = index;
		
		//now get item to slide in using new index
		var newItem = items[itemNum];		
		
		//set up our animation stylings for out and in motions (note:  Fx.Styles does NOT exist in moo 1.2, so we must use Fx.Morph or Fx.Tween)
		var item_in = new Fx.Morph(newItem, {
			     duration: transitionTime, 
			     transition: Fx.Transitions.Quad.easeInOut, 
			     wait:false
		});
		
		var item_out = new Fx.Morph(curItem, {
			     duration: transitionTime, 
			     transition: Fx.Transitions.Quad.easeInOut, 
			     wait:false
		});

		if (bMoveForward)
		    moveSlidesLeftToRight(item_in, item_out);
		else
		    moveSlidesRightToLeft(item_in, item_out);
			

		showPosition();
	};
	
	var moveSlidesRightToLeft = function(itemIn, itemOut){
		//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
		itemIn.start({
		'left': [988, 0]
		});
		
		//no beginning values needed, since we always want to push the old item out to the left
		itemOut.start({
		'left': '-988'
		});	
	};

	var moveSlidesLeftToRight = function(itemIn, itemOut){
		//we will set a beginning value here too, but this time to make it come from left to right
		itemIn.start({
		'left': [-988, 0]
		});
		
		//no beginning values needed
		itemOut.start({
		'left': '988'
		});
	};
	
	
	//Slider Stuff
	var slideForward = function(){ 
	
		//get item to slide out
		var curItem = items[itemNum];  
		
		//change index
		if(itemNum < (numItems - 1)){
			itemNum++; 
		}
		else{
			itemNum = 0;
		}
		
		//now get item to slide in using new index
		var newItem = items[itemNum];
		
		
		//set up our animation stylings for out and in motions (note:  Fx.Styles does NOT exist in moo 1.2, so we must use Fx.Morph or Fx.Tween)
		var item_in = new Fx.Morph(newItem, {
			     duration: transitionTime, 
			     transition: Fx.Transitions.Quad.easeInOut, 
			     wait:false
		});
		
		var item_out = new Fx.Morph(curItem, {
			     duration: transitionTime, 
			     transition: Fx.Transitions.Quad.easeInOut, 
			     wait:false
		});
		
		moveSlidesRightToLeft(item_in, item_out);
		
		showPosition();
	};
		
		
	var slideBackward = function(){ 
	
		//get item to slide out
		var curItem = items[itemNum];  
		
		//change index for reverse movement
		if(itemNum > 0){
			itemNum--; 
		}
		else{
			itemNum = (numItems - 1);
		}
		
		//now get item to slide in using new index
		var newItem = items[itemNum];
		
		
		var item_in = new Fx.Morph(newItem, {
			     duration: transitionTime, 
			     transition: Fx.Transitions.Quad.easeInOut, 
			     wait:false
		});
		
		var item_out = new Fx.Morph(curItem, {
			     duration: transitionTime, 
			     transition: Fx.Transitions.Quad.easeInOut, 
			     wait:false
		});
		
		
		moveSlidesLeftToRight(item_in, item_out);
		showPosition();
		
	};
	//end slideBackward
		
		
	//call the slider function periodically
	var theTimer = slideForward.periodical(slideTimer, this); 
	
    var initNextButton = function(){   
		nextBtn = $('nextbtn');      
        if (nextBtn==null)
        {
            setTimeout("initNextButton()", 1000);
        }
        else
        {
	        nextBtn.addEvent('click', function(){
		        if(isPaused == 0){
			        $clear(theTimer);
			        theTimer = slideForward.periodical(slideTimer);
		        }
		        slideForward();
	        });        
	    }
    };
    initNextButton();	
	
	
	
    var initPrevButton = function(){    
	   	prevBtn = $('prevbtn');
        if (prevBtn==null)
        {
            setTimeout("initPrevButton()", 1000);
        }
        else
        {
	        prevBtn.addEvent('click', function(){
		        if(isPaused == 0){
			        $clear(theTimer);
			        theTimer = slideForward.periodical(slideTimer);
		        }				     
		        slideBackward();
	        });
	    }
    };
    initPrevButton();		

    var initPlayButton = function(){    
		playBtn = $('playbtn');
        if (playBtn==null)
        {
            setTimeout("initPlayButton()", 1000);
        }
        else
        {
	        playBtn.addEvent('click', function(){
		        if(isPaused == 0){
			        isPaused = 1;
			        $clear(theTimer);
			        document.getElementById('playbtnimg').src=g_imgPlayButton;
		        }
		        else{
			        isPaused = 0;
			        slideForward();
			        theTimer = slideForward.periodical(slideTimer); 
			        document.getElementById('playbtnimg').src=g_imgPauseButton;
		        }
	         });
	    }
    };
    initPlayButton();	
    
    if (btnDot0 != null)
    	btnDot0.addEvent('click', function(){gotoSlide(0);});
    if (btnDot1 != null)
    	btnDot1.addEvent('click', function(){gotoSlide(1);});
    if (btnDot2 != null)
    	btnDot2.addEvent('click', function(){gotoSlide(2);});
    if (btnDot3 != null)
    	btnDot3.addEvent('click', function(){gotoSlide(3);});
    if (btnDot4 != null)
    	btnDot4.addEvent('click', function(){gotoSlide(4);});
    	
    showPosition();
	
});
