/* Copyright Matthias Dabitz */
/* Slider is functionally based on Soh Tanaka slider. */
/* V 1.1 */

$(document).ready(function() {

  // Config and styling
  var timer_speed = 7000;       // Timer speed in milliseconds
  var show_paging = true;
  var css_box_slider = ".slider";            // CSS-Class of slider container
  var css_frame_slider = ".slider_frame";    // CSS-Class of slider frame
  var css_slider_images = ".slider_images";  // CSS-Class of images inside of slider
  var css_slider_paging = ".slider_paging";  // CSS-Class of slider pages
  var css_slider_play = ".slider_control_play";  // CSS-Class of controls for play slider
  var css_slider_hold = ".slider_control_hold";  // CSS-Class of controls for hold slider


  // Prepare slider ---------------------------------------------------------
  var images_sum_width = 0;
  var slider_images = $(css_slider_images + " img"); // Array of slider images
  var slider_images_width = new Array();             // Array of slider images width
  var slider_images_height = new Array();            // Array of slider images height
  var slider_images_position = new Array();          // Array of start positions
  slider_images_position[0] = 0;
  for (i=0; i<slider_images.length; i++)
  {
    slider_images_width[i] = $(slider_images[i]).width();
    slider_images_height[i] = $(slider_images[i]).height();
  	images_sum_width = images_sum_width + slider_images_width[i];
    if (i > 0) {slider_images_position[i] = slider_images_position[i-1] + slider_images_width[i-1];}
  }
  $(css_slider_images).css({"width" : images_sum_width});


  // Prepare paging ---------------------------------------------------------
  if (show_paging == true) {
    $(css_slider_paging + " a:first").addClass("active");
  	$(css_slider_paging).show();
  }


	// Animate image and frame ------------------------------------------------
	animateImage = function(new_width, new_height){
    $(css_frame_slider).animate({
			width: new_width,
			height: new_height
		}, 500 );
    $(css_box_slider).animate({
			width:  new_width,
			height: new_height
		}, 500 );
	};


  // Sliding images + thumbs Function ---------------------------------------
	rotate = function(){
		var triggerID = $active.attr("rel") - 1;           // Get number of times to slide
		$(css_slider_paging + " a").removeClass("active"); // Remove all active class
		$active.addClass("active");                        // Add active class (the $active is declared in the rotateSwitch function)

		// Slider Animation -----------------------------------------------------
		$(css_slider_images).fadeOut(50),
		animateImage(slider_images_width[triggerID], slider_images_height[triggerID]);
		$(css_slider_images).animate({ left: -slider_images_position[triggerID]}, 500 );
		$(css_slider_images).fadeIn(500);

	};

	// Rotation + Timing Event ------------------------------------------------
	rotateSwitch = function(){
		play = setInterval(function(){      // Set timer - this will repeat itself every 3 seconds
      $(css_slider_hold).hide();
      $(css_slider_play).hide();
			$active = $(css_slider_paging + " a.active").next();
			if ($active.length === 0) {                    // If paging reaches the end...
				$active = $(css_slider_paging + " a:first"); // Go back to first
			}
      rotate();                         // Trigger the paging and slider function
		}, timer_speed);                    // Timer speed in milliseconds
	};


  // Preparing first Image --------------------------------------------------
  animateImage(slider_images_width[0],slider_images_height[0]);
	rotateSwitch();          // Run function on launch


  // Controlling slider -----------------------------------------------------
	// On hover the image
	$(css_slider_images).hover(function() {
		clearInterval(play);  // Stop the rotation
    $(css_slider_hold).show();
	}, function() {
    rotateSwitch();       // Resume rotation
    $(css_slider_hold).hide();
    $(css_slider_play).show();
	});


	//On Click
	$(css_slider_paging + " a").click(function() {
		$active = $(this);    // Activate the clicked paging
		//Reset Timer
		clearInterval(play);  // Stop the rotation
		rotate();             // Trigger rotation immediately
		rotateSwitch();       // Resume rotation
		return false;         // Prevent browser jump to link anchor
	});

	//On Play
/*	$(".control a.play").click(function() {
  	$(".control a.play").hide();
  	$(".control a.stop").show();
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});

	//On Stop
	$(".control a.stop").click(function() {
  	$(".control a.play").show();
  	$(".control a.stop").hide();
		//Reset Timer
		clearInterval(play); //Stop the rotation
		return false; //Prevent browser jump to link anchor
	});

	//On Back
	$(".control a.back").click(function() {
    $(".control a.play").hide();
  	$(".control a.stop").show();
    $active = $('.thumbs a.active').prev();
	  if ( $active.length === 0) { //If paging reaches the end...
		  $active = $('.thumbs a:last'); //go back to first
	  }
		clearInterval(play); //Stop the rotation
	  rotate(); //Trigger the paging and slider function
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});

	//On Next
	$(".control a.next").click(function() {
    $(".control a.play").hide();
  	$(".control a.stop").show();
    $active = $('.thumbs a.active').next();
	  if ( $active.length === 0) { //If paging reaches the end...
		  $active = $('.thumbs a:first'); //go back to first
	  }
		clearInterval(play); //Stop the rotation
	  rotate(); //Trigger the paging and slider function
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});
*/
});
