$(document).ready(function() {
//Slider Function
	function slideLeft(){
		$image_count = 100 - ($("#pic_cont>.previous").length + 2);
		$("#pic_cont>.active").animate({marginLeft:'-428px'},500);
		if ( $("#pic_cont>.active").next().length == 0 ) {
			$nextActiveImg = $("#pic_cont>div:first");
		}
		else {
			$nextActiveImg = $("#pic_cont>.active").next();
		}
		
		if( $("#pic_cont>.previous:last").next().length == 0 ){
			$nextPreviousImg = $("#pic_cont>.deactive:first");
		}
		else {
			$nextPreviousImg = $("#pic_cont>.previous:last").next();
		}
		$("#pic_cont>.active").addClass('deactive');
		$("#pic_cont>.deactive>.text_box").css({visibility:'hidden'});
		$("#pic_cont>.active").removeClass('active');
		$("#pic_cont>.active").removeClass('deactive');
		$nextActiveImg.addClass('active').animate({width:'428px',height:'278px'},0);
		$nextPreviousImg.css({marginLeft:'375px'});
		$("#pic_cont>.previous").each( function(i){
				$div_zind = parseInt($(this).css('z-index'));
				$out_zind = $div_zind + 1;
				$(this).css('z-index',$out_zind);
		   });
		$("#pic_cont>.previous").animate({marginLeft:'-=75px'},500);
		$nextPreviousImg.addClass('previous').css('zIndex',$image_count);
		$("#pic_cont>.active").removeClass('previous');
		$nextPreviousImg.removeClass('deactive');
		$("#pic_cont>.active>.text_box").css({visibility:'visible'});
	}
	function slideRight(){
		$image_count = 100 - ($("#pic_cont>.previous").length + 2);
		
		if( $("#pic_cont>.active").prev().length == 0 ){
			$nextActiveImg = $("#pic_cont>div:last");
		}
		else {
			$nextActiveImg = $("#pic_cont>.active").prev();
		}  
		$("#pic_cont>.active").addClass('previous');
		$("#pic_cont>.previous>.text_box").css({visibility:'hidden'});
		$("#pic_cont>.active").removeClass('active');
		$nextActiveImg.addClass('active').css({zIndex:'100',marginLeft:'0px'});
		$nextActiveImg.removeClass('deactive');
		if ( $("#pic_cont>.deactive:first").prev().length != 0 ) {
			$nextDeactiveImg = $("#pic_cont>.deactive:first").prev();
		}
		else {
			$nextDeactiveImg = $("#pic_cont>.previous:last");
		}
		$nextDeactiveImg.addClass('deactive');
		$nextDeactiveImg.removeClass('previous');

		$("#pic_cont>.previous").each( function(i){
				$div_zind = parseInt($(this).css('z-index'));
				$out_zind = $div_zind - 1;
				$(this).css('z-index',$out_zind);
		   });
		$("#pic_cont>.previous").animate({marginLeft:'+=75px'},500);
		$("#pic_cont>.active>.text_box").css({visibility:'visible'});
		
		
	}
	
	//Rotation and Timing Event - Pictures
	function rotatePictures (){
		play = setInterval(function(){ //Set timer - this will repeat itself 
			slideLeft(); //Trigger the slider function
		}, 5000); //Timer speed 
	};
	//Rotation  and Timing Event - Text
	function rotateText(){
		play = setInterval(function(){ //Set timer - this will repeat itself 
			$activeTxt = $('[id^=txt_con_].active').next(); //Move to the next div
			if ( $activeTxt.length === 0) { //If counter reaches the end, go back to first
				$activeTxt = $('[id^=txt_con_]:first'); 
			}
			rotateTxt(); //Trigger the slider function
	
		}, 7000); //Timer speed 
	};
	$('#left_button').click(slideLeft);
	$('#left_button').click(function(){clearInterval(play)});
	$('#right_button').click(slideRight);
	$('#right_button').click(function(){clearInterval(play)});
	rotatePictures(); //Run function on launch
//	rotateText();
	function extra_for_loop() {
		for($i; $i < $prev_count; $i++){
			if( $start_point.length == 0 ){
				$start_point = $("#pic_cont .active");
				$curr_div = $start_point;
			}
			else {
				if($curr_div.next().length == 0){
					$curr_div = $curr_div.next();
				}
				else {
					$curr_div = $("#pic_cont .previous:first").not(".active");
				}
			}
			$zind_val = $image_count - $i + 1;
			$curr_div.css('zIndex',$zind_val);				
		}
	}
});
