 
var slideActive		=	true;
var stopAllSlides	=	false;

$(document).ready(function(){
	var currentPosition = 0;
	var slideWidth 		= 640;
	var delay			=	5; // time in seconds
	var slides 			= $('.slide');
	var numberOfSlides 	= slides.length;
	var activeURL		= $("#small_img_0").parent("a").attr("href");
	
    
	function isInteger( s ) {
		return !isNaN( parseInt( s ) );
	}
	
	
	
	function changeImageBorder(id, numslides){
		for(i=0; i<numslides;i++){
			if(i==id){    
				activeURL	=	$("#small_img_" + i).parent("a").attr("href"); 							
				$("#small_img_" + i).addClass('active_image');
			} else {
				$("#small_img_" + i).removeClass('active_image');				
			}
			// end if
		}
		// end for
	}
	// end   changeImageBorder
	
	
	function focusSlide(id){                     
	   	
		moveSlide(id); 

		changeImageBorder(id, numberOfSlides);

		currentPosition = id;	   
	    
	} 
	// end focus slide 
	
	function moveSlide(amount){
	   			
	  	$('#slideInner').animate({
		  'marginLeft' : slideWidth*(-parseInt(amount))
		});  
        	   
	 /*   $("#intro").prepend(">>>>> MOVING: " + amount + " <<<<<<< \n<br />\n"); 	*/
		 
	}
	// end moveSlide 
	                                                                                                                    	

	
	for(i=0; i< numberOfSlides;i++){		
    	
		$("#small_img_" + i).attr('name', i);
		$("#slide_bottom_header_" + i +' a').attr('name', i);
		
		$("#small_img_" + i).mouseenter(function(event){
			
			event.preventDefault();  
						
			changeImageBorder($(this).attr('name'), numberOfSlides);
			moveSlide($(this).attr('name'));
			currentPosition = $(this).attr('name');
			slideActive =	false;    
		}).mouseleave(function(){
			slideActive =	true;						
		});
		
		$("#slideBottomSmallImages img").each(function(){ 
			
		    var myID   	=	$(this).get(0).id.split("_");
		    var myIDNum	=  	myID[2];
									
			  $(this).parent("a").focus(function(event){
				
			  	event.preventDefault();     
															   
				if(isInteger(myIDNum)){									  					
				    focusSlide(myIDNum); 
					slideActive =	false;
				}
				// end if is integer title
 
			}).blur(function(){     

			     slideActive =	true; 
			});
		});   
				
		
		
		$("#slide_bottom_header_" + i + ' a').focus(function(event){ 			
			event.preventDefault(); 
		    focusSlide($(this).attr('name'));					
			slideActive =	false;    

		}).blur(function(){
			slideActive =	true;			
		});

	}
	// end for

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

	// Position the foreground corners for the bottom elements     
	
	$('#slideBottomContentBg').css({
	'display':'block',
	'position':'absolute',
	'top':406,
	'left':0,
	'width': 640,
	'height' : 14,
	'background': 'url(/img/homepage_slider/slider_lower_sides_bottom.png) top left no-repeat transparent',
	'z-index':9});  
     
  // Wrap all .slides with #slideInner div
  slides.wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
 	  'z-index': 2,
      'width' : slideWidth
    });

	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css({'width': slideWidth * numberOfSlides, "margin" : 0});

	// Add the initial image border style
	$("#small_img_0").addClass('active_image'); 

	$("#slideshow .slideBottomContent h6").css('z-index', 9);
	$(".playpause").hide();   
 

	$("#playpauseHelper").focus(function(event){ 
	    $(".playpause").show();
	}).keyup(function(event){
		//	 	
		if(event.keyCode== 13) {
			//
		    event.preventDefault();			
			//
			if($(".playpause").hasClass("paused")) {
				$(".playpause").removeClass("paused");
			} else {       
				$(".playpause").addClass("paused"); 
			}
			// end if has class	    	
		}
		// end if keycode (enter / 13) 
		
		if(event.keyCode== 9 && event.shiftKey){ 
			// if SHIFT && TAB, we're exiting backwards
			// for the sake of IE users we need to hide the playpause button
			// bless, 'em IE can't figure out how to tab past a hidden element
		     $(".playpause").hide();		 
		}
	});
	// end if focus
	
	$(".playpause").blur(function(){
		$(this).hide();
	});

	
	$("#slideshow").mouseenter(function(){
	   	$(".playpause").fadeIn(500);	 
	}).mouseleave(function(){
	   $(".playpause").fadeOut(200); 
    });
	    
	
	$("#slideshow .large_image").mouseenter(function(){
	    $(this).css({'cursor':'pointer'});
	}).click(function(){ 
	    if(activeURL != '' && activeURL != '#') document.location = activeURL;
	});
	
	$("#slideshow .slide p").mouseenter(function(){
	    $(this).css({'cursor':'pointer'});
	}).click(function(){ 
	    if(activeURL != '' && activeURL != '#') document.location = activeURL;
	}); 
	
	$("#slideshow .slide h6").mouseenter(function(){
	    $(this).css({'cursor':'pointer'});
	}).click(function(){ 
	    if(activeURL != '' && activeURL != '#') document.location = activeURL;
	});
	



	function handleSlide(){
		                                 
		if($(".playpause").hasClass("paused")) {
		   stopAllSlides = true; 
		} else {
			stopAllSlides = false;
		} 
		
		if(slideActive && !stopAllSlides ){
			
		  	if(currentPosition != numberOfSlides-1) {
				currentPosition = parseInt(currentPosition)+1;
			} else {
				currentPosition = 0;
			}

			changeImageBorder(currentPosition, numberOfSlides);
            moveSlide(currentPosition);		
		}
		// end if slideActive		       
	}
	// end handle slide
	
	setInterval(handleSlide, delay*1000);



	
});
