var pageLoaded = false;
/*----- SlideShow Code -----*/
$(document).ready( function(){
	
	//---------- sliding window ----------//
	    var slidingElement;
	    var boxWidth;
	    var elementWidth;
	    var currentPosition;
		var currentImage = 0;
		var captionList = $('table.slidingElement img');
		var goingNext = true;
		
		elementsLoaded = function(){
		    pageLoaded = true;
		    
		    // Element to be slid inside of overflow box
		    slidingElement = $('table.slidingElement');
    		
		    // Width of the container (with css overflow applied)
		    boxWidth = $('div.slideShowWrapper').width();
			
		    // width of element inside of 
		    elementWidth = $(slidingElement).width() - boxWidth;

		    // get the position of the sliding element
		    currentPosition = $(slidingElement).css('left');
    		    
		    currentPosition = currentPosition.split('px');
		    currentPosition = currentPosition[0];
		}
		
		
		function previousSlide(){
			if(pageLoaded == false){
		        elementsLoaded();
		    }
			// create a positive number for comparison
			var absPositionValue = Math.abs(currentPosition);
			
			if(absPositionValue > 0){
				// subtract the width of the visible area from the current position
				currentPosition = (eval(currentPosition) + boxWidth);
				
				if(currentPosition == 0){
					currentImage = 0;
					goingNext = true;
					$('table.navigation td.description').html($(captionList[currentImage]).attr('title'));
					// if slider is back to starting position disable previous button
					$('table.navigation tr td.prevColumn a').addClass('disabled');
				} else {	
					currentImage--;
					$('table.navigation td.description').html($(captionList[currentImage]).attr('title'));
				}
				
				var cssPosition = currentPosition+"px";
				
				$(slidingElement).each(function(){
					$(this).animate({left: cssPosition}, {duration: 500, easing: 'easeOutQuad'});
				});
				
				// if previous button clicked remove disabled class from the next button
				$('table.navigation tr td.nextColumn a').removeClass('disabled');
			}
			return false;
		}
		
		function nextSlide(){
		    if(pageLoaded == false){
		        elementsLoaded();
		    }
			// create a positive number for comparison
			var absPositionValue = Math.abs(currentPosition);
			
			// if the current position is less the the width of the sliding element
			if(absPositionValue <= (elementWidth-boxWidth)){
				// subtract the width of the visible area from the current position
				currentPosition = (currentPosition - boxWidth);
				
				if(Math.abs(currentPosition) >= (elementWidth-boxWidth)){
					// if slider is back to starting position disable previous button
					currentImage++;
					goingNext = false;
					$('table.navigation tr td.nextColumn a').addClass('disabled');
					$('table.navigation td.description').html($(captionList[currentImage]).attr('title'));
				} else {
					currentImage++;
					$('table.navigation td.description').html($(captionList[currentImage]).attr('title'));
				}
				
				var cssPosition = currentPosition+"px";
				
				$(slidingElement).each(function(){
					$(this).animate({left: cssPosition}, {duration: 500, easing: 'easeOutQuad'});
				});
				// if next button clicked remove disabled class from the previous button
				$('table.navigation tr td.prevColumn a').removeClass('disabled');
			}
			return false;
		}
		
		// on click of the previous button
		$('table.navigation tr td.prevColumn a').click( function(){
			clearInterval(playSlidesInterval);
		    previousSlide();
		});
		
		// on click of the next button
		$('table.navigation tr td.nextColumn a').click( function(){
			clearInterval(playSlidesInterval);
			nextSlide();
		});
		
	function playSlides() {
		if(goingNext){
			nextSlide();
		} else {
			previousSlide();
		}
	}
	var playSlidesInterval = setInterval(function(){playSlides()}, 4000);
		
});
