var section = "join";

jQuery(function($) {
	
	//variables
	var position = 0;
	var incr = 800;
	var total = $("#slide_contents .slide").length;
	
	//start automatic loop
	//var loop = setInterval(move_right, 8000);
	
	//sets true length of slides
  $("#slide_contents").css("width",incr * total);
	
	//events for buttons
	$("#arrow_right").click(function(){
		move_right();
	});
	
	$("#arrow_left").click(function(){
		move_left();
	});
	
	
	//functions
	function move_right(){
		//clears interval first
		//restart_interval();
		
		//animates to next increment in possible
		if(position < total - 1){
			position++;
			$("#slide_contents").animate({ 
				left: (incr * position * - 1) + "px",
			}, 500 );
		}
		
		//otherwise go to last slide
		else if(position >= total - 1){
			$("#slide_contents").animate({ 
				left: "0px",
			}, 500 );
			
			position = 0;
		}
	}
	
	function move_left(){
		//clears interval first
		//restart_interval();
		
		//animates to next increment in possible
		if(position > 0){
			position--;
			$("#slide_contents").animate({ 
				left: (incr * position * -1) + "px",
			}, 500 );
		}
		
		//otherwise go to first slide
		else if(position == 0){
			$("#slide_contents").animate({ 
				left: (incr * (total - 1) * - 1) + "px",
			}, 500 );
			
			position = total - 1;
		}
	}
	
	//clears interval and restarts
	function restart_interval(){
		clearInterval(loop);
		loop = setInterval(move_right, 8000);
	}
	
	//characters left
	function count_chars_left(){
	  $("#chars_left").text("Characters left: " + (300 - $("#coauthor_quote").val().length));
	  return $("#coauthor_quote").val().length;
	}
	
	count_chars_left();
	
	function trim_textarea(){
    if(count_chars_left() > 300){
      $("#coauthor_quote").val($("#coauthor_quote").val().substr(0,300));
      $("#chars_left").text("Characters left: " + (300 - $("#coauthor_quote").val().length));
    }
    
    if(count_chars_left() > 250){
      $("#chars_left").css("color", "#ff0000");
    }
    else {
      $("#chars_left").css("color", $("body").css("color"));
    }
	}
	
	$("#coauthor_quote").keypress(function () {
	  trim_textarea();
	});
	
	$("#coauthor_quote").keyup(function () {
	  trim_textarea();
	});
	
	//highlight co-author name field
	$("#coauthor_name").focus();

	
});