$(document).ready(function(){

//Create function to attach and run once.
function dksliders(slideobj) {

	//Set variables
	var waittime = 7000;
	var movespeed = 500;
	var slideWidth = 690;
	var slideHeight = 199;
	var buttonspacing = 10;
	var buttonwidth = 20;
	var buttonbgcolor = "#eee";
	var buttonbgcolorhighlight = "#444";
	var nextslide = 0;
	var currentslide = 0;
	var captionopac = "0.0";
	var captionmax = "0.7";
	var arrowmin = "0.3";

	var slides = $('li', slideobj);
	var scount = slides.length;
	var slideblock = $('ul',slideobj);
	var topslide = scount;

	slides.each(function(){
		$(this).css("z-index",topslide);
		topslide--;
	});

	topslide = scount+1;

	// Set CSS
	slideobj.css('overflow', 'hidden').width(slideWidth).height(slideHeight);
	slideblock.width(slideWidth*scount).height(slideHeight);

	slides.css({
		'float' : 'left',
		'width' : slideWidth,
		'margin' : '0',
		'padding' : '0'
	});

	slideblock.css({
		'margin' : '0',
		'padding' : '0'
	});

	//Implement controls: left arrow, right arrow
	slideobj.append("<span class='leftarrow'>Move Left</span>");
	$(".leftarrow",slideobj).click(function(){clearTimeout(timeout);move(nextslide-2, true);}).hover(function(){clearTimeout(timeout);$(this).animate({opacity:"1.0"});}, function(){timerstart(nextslide);$(this).animate({opacity:arrowmin});});

	slideobj.append("<span class='rightarrow'>Move Right</span>");
	$(".rightarrow",slideobj).click(function(){clearTimeout(timeout);move(nextslide, true);}).hover(function(){clearTimeout(timeout);$(this).animate({opacity:"1.0"});},function(){timerstart(nextslide);$(this).animate({opacity:arrowmin});});

	//Create caption box. Bind actions
	slideobj.append("<div class='dkcaption'></div>");
	var caption = $('.dkcaption',slideobj);
	caption.css({opacity: captionopac});
	caption.hover(function(){showpause(true);},function(){showpause(false);});

	//Implement controls: row of buttons
	var buttons = '';
	slides.each(function(index){
//		buttons += "<span class='dkbutton'>"+(index+1)+"</span>";
		buttons += "<span class='dkbutton'>&nbsp;</span>";
	});
	slideobj.append(buttons);
	buttons = $('.dkbutton',slideobj);
	bcount = buttons.length;
	buttons.each(function (index) {
		$(this).css({'left':slideWidth - (buttonspacing + buttonwidth) * bcount + (buttonspacing+buttonwidth)*index,'width':buttonwidth}).click(function() {clearTimeout(timeout); showpause(true); move(index,true);}).hover(function(){showpause(true);},function(){showpause(false);});
	});

	//Set caption padding-right to align with buttons;
	caption.css("padding-right",(buttonspacing + buttonwidth) * bcount + (buttonspacing+buttonwidth));

	//Code to pause timer when mouse is over the image
	slideblock.hover(function(){showpause(true);},function(){showpause(false);});

	//"move" function with argument for destination.
	function move(number, clicked) {
		clicked = clicked || false; //Default argument value hack.
		if (number > scount - 1) number = 0;
		if (number < 0) number = scount - 1;
		//animate slides

		if (currentslide != number) {
				slides.eq(number).show();
				slides.eq(currentslide).fadeOut().queue(function(){
				slides.eq(number).css("z-index",topslide);
				topslide++;
				$(this).dequeue();
			});

		}

		caption.animate({opacity:"0.0"}).queue(function(){
			$(this).text($("img",slides.eq(number)).attr('alt'));
			$(this).dequeue();
		});

		//Fade in
		caption.animate({opacity:captionopac});

		buttons.css("backgroundColor",buttonbgcolor)
		buttons.eq(number).css("backgroundColor",buttonbgcolorhighlight);

		currentslide = number;

		// if not clicked, settimeout with nextslidenumber
		nextslide = number + 1;
		if (!clicked) timerstart(nextslide);
	};

	//Timer function to remove code redundancy
	function timerstart(slide,clickthrough) {
		clickthrough = clickthrough || false;
		clearTimeout(timeout);
		timeout = setTimeout(function(){move(slide, clickthrough);}, waittime);
	};

	//Pause function to remove code redundancy and unify results of mouseover image, buttons, and caption
	function showpause(inout) {

		//Stop current animation
		caption.stop();

		if (inout) {

			//Pause Slideshow
			clearTimeout(timeout);
			//Fade Caption In
			caption.animate({opacity: captionmax},200); 			

		}else{

			//Start Slideshow
			timerstart(nextslide);
			//Fade Caption Out
			caption.animate({opacity: captionopac},500); 

		}

	}

	var timeout;
	move(nextslide);

};

	//Activate function on each slideshow instance.
	$('.dkslides').each(function() {dksliders($(this));});

});

