var icons = {

	init: function() {
	
		//add handlers
		$$('#icons td div').each( function(elm) {
		
			elm.style.backgroundColor = 'white';
			elm.observe( 'mouseover', icons.enlarge );
			elm.observe( 'mouseout', icons.shrink );
		
		});
	
		//add handlers
		$$('#icons td.icon img').each( function(elm) {
		
			elm.style.backgroundColor = 'white';
			elm.observe( 'mouseover', icons.enlarge );
			elm.observe( 'mouseout', icons.shrink );
		
		});
	
	},
	
	enlarge: function(e) {
	
		e.stop();
		elm = e.findElement().up('td');

		if( elm.className == 'icon' )	elm.next('td').addClassName('hover');
		else elm.addClassName('hover');

	},
	
	shrink: function(e) {
	
		e.stop();
		elm = e.findElement().up('td');

		if( elm.className == 'icon' )	elm.next('td').removeClassName('hover');
		else elm.removeClassName('hover');
	
	}

}

document.observe( 'dom:loaded', icons.init );



//handles the slides on the homepage
var slides = {
    
	current_slide: 1,
	ai: null,
	automate_dir: 'R',
	dur: 0.8, //duration of effects
	automate_dur: 4, //how long to stay on each slide when using automatic movement
	w: 900,
	n: 3,
  
  init: function() {
    
    $('slides_arrow_left').observe( 'click', function() { slides.animate('L'); clearTimeout( slides.ai ) } );
    $('slides_arrow_right').observe( 'click', function() { slides.animate('R'); clearTimeout( slides.ai ) } );
	
		//automate slide moving if nothing is pressed
		if( this.n > 1 ) this.ai = setTimeout( function() { slides.automate() }, slides.automate_dur * 1000 );
		
		//by request of Ian...
		$('slideshow').observe( 'click', function() { clearTimeout( slides.ai ) })
      
  }, 

  animate: function( dir ) {
      
    //check we are allowed to move, if not return
    //var c = parseInt( $('innertrack').getStyle( 'left' ) );
    //if( c >= 0 && dir == 'L' || c <= this.max_x && dir == 'R' ) return;
    
    //cancel queues
    $('slides_arrow_left').stopObserving( 'click' );
    $('slides_arrow_right').stopObserving( 'click' );
    
    //move image
    if( dir == 'R' ) {
    
    	var imgToMove = $$('#innertrack img')[0];
    	var newImg = imgToMove.clone();

    	$('innertrack').insert( { 'bottom': newImg } );
    
    } else {
    
    	var imgToMove = $$('#innertrack img')[2]; // 2 = total images - 1
    	var newImg = imgToMove.clone();

    	$('innertrack').insert( { 'top': newImg } );
    	$('innertrack').setStyle( 'left: -' + slides.w +'px' );
    
    }


    //move
    new Effect.Move( 'innertrack', {
      x: this.w * ( dir == 'L' ? 1 : -1 ),
      y: 0,
      mode: 'relative',
      duration: slides.dur,
      queue: 'end', scope: 'slides', limit: 1,
      afterFinish: function() {
      
        $('slides_arrow_left').observe( 'click', function(e) { e.stop(); slides.animate('L'); clearTimeout( slides.ai ) } );
        $('slides_arrow_right').observe( 'click', function(e) { e.stop(); slides.animate('R'); clearTimeout( slides.ai ) } );

        imgToMove.remove();
          
      	if( dir == 'R' ) {   	    	
    			$('innertrack').setStyle( 'left: ' + ( parseInt( $('innertrack').getStyle( 'left' ), 10 ) + slides.w ) +'px' );
    		}

      }
    } );
  
  },
	
	automate: function() {
		
		//update current slide
		this.current_slide = this.automate_dir == 'L' ? this.current_slide - 1 : this.current_slide + 1;
		
		//move
		this.animate( this.automate_dir );
		
		//recurse
		this.ai = setTimeout( function() { slides.automate() }, ( slides.automate_dur + slides.dur ) * 1000 );
		
	}

    
}

document.observe( 'dom:loaded', slides.init.bind(slides) );
