// JavaScript Document
(function($){
 $.fn.slider = function() {

    
		//Set up variables
		showSlideTime = 5000;
		transitionTime = 500;
		showDescription = false;
		maxSlides = 2;
		slideID = '#'+($(this).attr('id'));
		
		width = $(slideID).width();
		height = $(slideID).height();
		slides = $(slideID+' li').length;
		
		$('#slider_crop').after('<div id="slider_timer" class="box_shadow_0_1_2_36"></div>');
		$('#slider_timer').css({'backgroundColor':'#fcfcfc','width':'932px','height':'4px','position':'absolute','top':'4px','left':'4px','margin':'0','padding':'0','fontSize':'2px'}).animate({'opacity':1,'width':'0'},0);
		for (i=0;i<(slides-maxSlides);i++) {
			currentNumSlides = $('#slider li').length;
			removeSlide = (Math.floor(Math.random()*currentNumSlides))+1;
			$(slideID+' li:nth-child('+removeSlide+')').remove();
		}
		slides = $(slideID+' li').length;
		slidenumber = Math.floor(Math.random()*maxSlides);
		$(slideID).stop().animate({'marginLeft':-(slidenumber*width)},0);
		$(slideID).width((width*slides)+'px');
		$('#slider_prev, #slider_next').show();
		
		if (showDescription) {
			$('#slider_description').show();
			descriptionPosition();
			$('#slider_description').animate({'marginLeft':descriptionMarginLeftA,'marginTop':'35px'},0, function() {
				$('#slider_description h1').html($(slideID+' li:nth-child('+(slidenumber+1)+') img').attr('title'));
				$('#slider_description p').html(($(slideID+' li:nth-child('+(slidenumber+1)+') img').attr('alt'))+'<br><a href="#">see more</a>');
				$('#slider_description').animate({'marginLeft':descriptionMarginLeftB},transitionTime/2);
			});
		}
		startTimer();
		
		$('#slider_prev').click(function(){
			slidenumber--;
			handleSlides();
			resetTimer();
		});
		
		$('#slider_next').click(function(){
			slidenumber++;
			handleSlides();
			resetTimer();
		});
		
		$('#slider_wrapper').hover(function(){
			$('#slider_timer').stop();
		},function() {
			startTimer();
		});
		
		function moveSlide() {
			$(slideID).stop().animate({'marginLeft':-(slidenumber*width)},transitionTime, 'swing');
		}
		
		function handleSlides() {
			if (slidenumber>(slides-1)) {
				slidenumber = 0;
			} else if (slidenumber<0) {
				slidenumber = (slides-1);
			}
			moveSlide();
			if (showDescription) {
			  updateDescription();
		  };
		}
		
		function updateDescription() {
			descriptionPosition();
			$('#slider_description').animate({'marginTop':height},transitionTime/2, function() {
				$('#slider_description').animate({'marginLeft':descriptionMarginLeftA, 'marginTop':'35px'},0, function() {
					$('#slider_description h1').html($(slideID+' li:nth-child('+(slidenumber+1)+') img').attr('title'));
					$('#slider_description p').html(($(slideID+' li:nth-child('+(slidenumber+1)+') img').attr('alt'))+'<br><a href="#">see more</a>');
					$('#slider_description').animate({'marginLeft':descriptionMarginLeftB},transitionTime/2);
				});
			});
		}
		
		function descriptionPosition() {
			if (($(slideID+' li:nth-child('+(slidenumber+1)+') img').attr('descriptionPosition')) == "Left") {
				descriptionMarginLeftA = width+"px";
				descriptionMarginLeftB = "60px";
			} else {
				descriptionMarginLeftA = "-280px";
				descriptionMarginLeftB = (width-280-60)+"px";
			}
		}
		
		function startTimer() {
			$('#slider_timer').animate({'width':'940'},showSlideTime*(1-(($('#slider_timer').width())/940)), 'linear', function() {
				slidenumber++;
				handleSlides();
				resetTimer();
				startTimer();
			});
		}
		
		function resetTimer() {
			$('#slider_timer').animate({'width':'0'},0);
		}
		
 };
})(jQuery);
