// Home Image functionality -------------------------------------------------		
	
$(document).ready(function(){
	var width = 841; // 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 = 1; // active slide by default
	var mathcurrent = current-1;
	$("#home-img-1").addClass("selected"); //applies the default styles
	$(".hs-previous").addClass("disabled"); //applies the default styles
	if(homeimgcount==1) {
		$(".hs-next").addClass("disabled"); //applies the default styles
		}
	slider_delay = 7000; //time between automatic slider change (milliseconds)
	
	// resets the selected state on all slides
	function resetnav() {
	  	$("#home-img-1").removeClass("selected");
		$("#home-img-2").removeClass("selected");
		$("#home-img-3").removeClass("selected");
		};
		
	//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 || homeimgcount==3) {
				moveTo_next();
			}
		} else if(current==2) {
			if(homeimgcount==2) {
				moveTo_1();
			} else if(homeimgcount==3) {
				moveTo_next();
			}
		} else if(current==3) {
			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("selected");
		$(".hs-next").removeClass("disabled");
		$(".hs-previous").addClass("disabled");
		list();
		instance++;
		startTimer(instance);
		};
		
	function moveTo_2() { //moves the slider to item 2
		current = 2;
		resetnav();
		$("#home-img-2").addClass("selected");
		$(".hs-previous").removeClass("disabled");
			if(homeimgcount>=2) {
				$(".hs-next").addClass("disabled");
		} else {
			$(".hs-next").removeClass("disabled");
		}
		list();
		instance++;
		startTimer(instance);
		};
		
	function moveTo_3() { //moves the slider to item 3
		current = 3;
		resetnav();
		$("#home-img-3").addClass("selected");
		$(".hs-next").addClass("disabled");
		$(".hs-previous").removeClass("disabled");
		list();
		instance++;
		startTimer(instance);
		};
		
	function moveTo_prev() { //moves the slider to the previous item
		if(homeimgcount>=1) {
			if(current==2) {
				resetnav();
				$("#home-img-1").addClass("selected");
				$(".hs-previous").addClass("disabled");
				$(".hs-next").removeClass("disabled");
				current = 1;
				list();
			 } else if(current==3) {
				resetnav();
				$("#home-img-2").addClass("selected");
				$(".hs-next").removeClass("disabled");
				current = 2;
				list();
			}
			instance++;
			startTimer(instance);
		}
		};
		
	function moveTo_next() { //moves the slider to next item
		if(homeimgcount>=2) {
			if(current==1) {
				resetnav();
				$("#home-img-2").addClass("selected");
				$(".hs-previous").removeClass("disabled");
				if(homeimgcount>=2) {
					$(".hs-next").addClass("disabled");
					}
				current = 2;
				list();
			 } else if(current==2) {
				if(homeimgcount==3) {
					resetnav();
					$("#home-img-3").addClass("selected");
					$(".hs-next").addClass("disabled");
					current = 3;
					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();
		});
	
	$(".hs-previous").click(function(event){
		event.preventDefault();
		moveTo_prev();
	});
		
	$(".hs-next").click(function(event){
		event.preventDefault();
		moveTo_next();
	});
	
	//starts the timer
	instance = 0;
	startTimer(instance)
});