$(document).ready(function(){	
	var width = 950; // stardard width of the slide
	var homeimgcount = $('#hs-slider').children().size(); //number of slider items
	var homeimgwidth = homeimgcount * width; //width to make the containing UL
	$('#hs-slider').css({'width' : homeimgwidth + 'px'}); //applies the dynamic width to the UL
	var current = 2; // active slide by default
	var mathcurrent = current-1;
	$("#home-img-2").addClass("selected"); //applies the default styles
	slider_delay = 13000; //time between automatic slider change (milliseconds)

	// resets the selected state on all slides
	function resetnav() {
	  	$("#home-img-1").removeClass("");
		$("#home-img-2").removeClass("selected");
		$("#home-img-3").removeClass("");
		};

	//selects slide based on current
	function list() {
		mathcurrent = current-1;
		var slide = mathcurrent*-width;
		$("#hs-slider").animate({
			marginLeft:slide
			} , 500 )
		};

	jQuery.fn.delay = function(time,func){ //delay function
		this.each(function(){
			setTimeout(func,time);
		});
		return this;
	};

	function timerNext() { //shifts to the next slide
		if(current==1) {
			if(homeimgcount>=2) {
				moveTo_2();
			}
		} else if(current==2) {
			if(homeimgcount==2) {
				moveTo_1();
			} else if(homeimgcount>=3) {
				moveTo_3();
			}
		} else if(current==3) {
			if(homeimgcount==3) {
				moveTo_1();
			} else if(homeimgcount==4) {
				moveTo_4();
			}
		} else if(current==4) {
			moveTo_1();
		}
		instance++;
		startTimer(instance);
		};

	function startTimer(instance) { //runs the timer
		timer_instance = instance;
		slider_timer = 0;
		$(this).delay(slider_delay,function(){
			if(timer_instance == instance) {
				timerNext();
			}
			});
		};

	function moveTo_1() { //moves the slider to item 1
		current = 1;
		resetnav();
		$("#home-img-1").addClass("");
		list();
		instance++;
		startTimer(instance);
		};

	function moveTo_2() { //moves the slider to item 2
		current = 2;
		resetnav();
		$("#home-img-2").addClass("selected");
		list();
		instance++;
		startTimer(instance);
		};

	function moveTo_3() { //moves the slider to item 3
		current = 3;
		resetnav();
		$("#home-img-3").addClass("");
		list();
		instance++;
		startTimer(instance);
		};
		



	// button press events

	$("#home-img-1").click(function(event){
		event.preventDefault();
		moveTo_1();
	    });

	$("#home-img-2").click(function(event){
		event.preventDefault();
		moveTo_2();
	    });

	$("#home-img-3").click(function(event){
		event.preventDefault(); 
		moveTo_3();
		});
	


	//starts the timer
	instance = 0;
	startTimer(instance)
});
