/* Copyright Jamalius Media 2010 */
/* Slider is functionally based on Soh Tanaka slider. */
/* V 1.0 */

$(document).ready(function() {

  // Config -----------------------------------------------------------------
  var thumbs_activ = false;
  if ($(".thumbs img").length > 0)  { thumbs_activ = true; }
  //alert("thumbs aktiv" +thumbs_activ);


  // Settings ---------------------------------------------------------------
  var timer_speed = 7000;       // Timer speed in milliseconds
	$(".slider_paging a:first").addClass("active");
	$(".slider_paging").show();
  if (thumbs_activ == true) {
    var thumbs_frame_width = 10;
    var thumbs_space_width = 5;   // Half space between thumbs
  	$(".thumbs a:first").addClass("active");
  }


  // Prepare slider ---------------------------------------------------------
  var images_sum_width = 0;
  var slider_images = $("#bilder 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];}
  }
  $("#bilder").css({'width' : images_sum_width});


  // Prepare thumbs ---------------------------------------------------------
  if (thumbs_activ == true) {
    var lightroom_width = $(".thumbsframe").width();  // Width of thumbframe
    var slider_thumbs = $(".thumbs img");             // Array of slider images
    var slider_thumbs_width = new Array();            // Array of slider images width
    var slider_thumbs_height = new Array();           // Array of slider images height
    var slider_thumbs_position = new Array();         // Array of start positions
    slider_thumbs_position[0] = 0;                    // Hor. position of thumb in slider
    var thumbs_sum_width = 0;                         // Reel thumbs width included frames etc.
    var biggest_thumb_width = 0;                      // Width of last thumb
    var thumbs_in_lightroom = 0;                      // Max number of thumbs showing in lightroom
    var next_thumbs_sliding = 0;                      // Number of next sliding thumb

    for (i=0; i<slider_thumbs.length; i++)
      {
      slider_thumbs_width[i] = $(slider_thumbs[i]).width() + thumbs_frame_width + thumbs_space_width*2;
      slider_thumbs_height[i] = $(slider_thumbs[i]).height();
      if (i>0) {
        slider_thumbs_position[i] = slider_thumbs_position[i-1] + slider_thumbs_width[i] + thumbs_space_width*2;
        }
      last_thumb_width = slider_thumbs_width[i];
      if (slider_thumbs_width[i] > biggest_thumb_width) { biggest_thumb_width = slider_thumbs_width[i]; }
      }
    thumbs_sum_width = biggest_thumb_width * slider_thumbs.length;
    if (thumbs_sum_width < lightroom_width) { thumbs_sum_width = lightroom_width; }
    thumbs_in_lightroom = Math.ceil(lightroom_width / biggest_thumb_width) - 1;
    next_thumbs_sliding = thumbs_in_lightroom;
    $(".thumbs").css({'width' : thumbs_sum_width});    // Set thumbs css width
  }


	// Animate image and frame ------------------------------------------------
	animateImage = function(new_width, new_height){
    $(".fotofenster").animate({
			width: new_width,
			height: new_height
		}, 500 );
    $("#fotos").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
		$(".slider_paging a").removeClass('active'); //Remove all active class
    if (thumbs_activ == true) {
      $(".thumbs a").removeClass('active');     // Remove all active class
    }
		$active.addClass('active');               // Add active class (the $active is declared in the rotateSwitch function)

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

    //alert("next" +next_thumbs_sliding);
    // Thumbs Animation -----------------------------------------------------
    if (thumbs_activ == true) {
      if (triggerID == 0) {
        $(".thumbs").fadeOut(50);
        $(".thumbs").animate({ left: -slider_thumbs_position[triggerID]}, 500 );
        $(".thumbs").fadeIn(500);
        next_thumbs_sliding = thumbs_in_lightroom;
      }
      if (triggerID > 0 && triggerID == next_thumbs_sliding) {
        $(".thumbs").animate({ left: (-slider_thumbs_position[triggerID] + biggest_thumb_width/2)}, 500 );
        next_thumbs_sliding = triggerID + thumbs_in_lightroom;
      }
      if (triggerID > 0 && triggerID > next_thumbs_sliding) {
        $(".thumbs").animate({ left: (-slider_thumbs_position[triggerID] + biggest_thumb_width/2)}, 500 );
        next_thumbs_sliding = triggerID + thumbs_in_lightroom;
      }
    }
	};

	// Rotation + Timing Event ------------------------------------------------
	rotateSwitch = function(){
		play = setInterval(function(){      // Set timer - this will repeat itself every 3 seconds
			$active = $('.slider_paging a.active').next();
			if ($active.length === 0) {       // If paging reaches the end...
				$active = $('.slider_paging a:first'); // Go back to first
			}
      if (thumbs_activ == true) {
      	$active = $('.thumbs a.active').next();
  			if ($active.length === 0) {       // If paging reaches the end...
  				$active = $('.thumbs a:first'); // Go back to first
  			}
      }
      $(".slider_control_hold").hide();
      $(".slider_control_play").hide();
      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
  $("#bilder img").hover(function() {
		clearInterval(play); //Stop the rotation
    $(".slider_control_play").hide();
    $(".slider_control_hold").show();
	}, function() {
    rotateSwitch(); //Resume rotation
    $(".slider_control_hold").hide();
    $(".slider_control_play").show();
	});


	//On Click
  $(".slider_paging a").click(function() {
		$active = $(this);    // Activate the clicked paging
	  //animateImage(slider_images_width[$active], slider_images_height[$active]); //Animate Image
		//Reset Timer
		clearInterval(play);  // Stop the rotation
		rotate();             // Trigger rotation immediately
		rotateSwitch();       // Resume rotation
		return false;         // Prevent browser jump to link anchor
	});
  if (thumbs_activ == true) {
  	$(".thumbs a").click(function() {
  		$active = $(this);    // Activate the clicked paging
  	  //animateImage(slider_images_width[$active], slider_images_height[$active]); //Animate Image
  		//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
	});
*/
});

