/*
Copyright (c) 2010 Mediasparx (http://www.mediasparx.com)
Notes: Image rotator. Preloaded included below.
Todo:	- Offset showoff start
		- Nested version
*/

var timerid;

function imageRotator(container, method, direction, auto) {

	$(container).each(function()
	{

		if( method == "paging" )
		{
			$(this).find(".paging").show();
			$(this).find(".paging a:first").addClass("active");
		}
		else
		{
			$(".arrows").show();
		}

//		Custom thumbnail move
		$(".image_reel .listRow .tplvar-listitem-summary .AnchorImageBox a").appendTo('#slideselect');

		$('#slideselect a').each(function(index) {
//			$(this).attr("href", index);
		});

		$("#slideselect a").click(function() {

			$(this).blur();

			clearTimeout(timerid);
			if( auto > 0 )
			{
				// Autoplay - restart
				var temp = auto * 5;
				timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', temp);
			}

			var windowWidth = $(".Slideshow").width();
			var image_index = $(this).attr("href");
			var image_reelPosition = windowWidth * image_index;

			$(".image_reel").fadeOut('fast', function() {
				$(this).animate({
					left: -image_reelPosition
					}, 1, function() {
						$(this).fadeIn('slow');
				});
			});

			return false;
		});

//		Custom thumbnail paging

		var selectWidth = $(".select-window").width();
		//Compensate padding/margin
		selectWidth = selectWidth + 10;

		//Get size of images, how many there are, then determin the size of the image reel.
		var selectWindowWidth = $("#slideselect img").width();
		//Compensate padding/margin
		selectWindowWidth = selectWindowWidth + 10;
		var selectImageSum = $("#slideselect img").size();
		var selectImageReelWidth = selectWindowWidth * selectImageSum;

		//Adjust the image reel to its new size
//		$("#slideselect").css({'width' : selectImageReelWidth});

		if ( selectImageReelWidth > selectWidth ) {
			// Use arrows to show all

			$(".selectors").show();

			$(".selectors a").click(function() {

				clearTimeout(timerid);
				if( auto > 0 )
				{
					// Autoplay - restart
					var temp = auto * 5;
					timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', temp);
				}

				var imageSum = selectImageSum // antall bilder
				var imgWidth = selectWindowWidth; // bildehøyde
				var lastWidth = ( imgWidth * imageSum ) - selectWidth; // siste posisjon
				var image_reelPositionRaw = $("#slideselect").position(); // posisjon
				var image_reelPositionPx = image_reelPositionRaw.left; // posisjon fra venstre i pixel
				var image_reelPositionImg = image_reelPositionPx / imgWidth; // aktivt bilde (ca)
				image_reelPositionImg = Math.floor(image_reelPositionImg); // aktivt bilde
				var image_reelPosition = image_reelPositionImg * imgWidth;

				if ( $(this).attr("title") == 'previous' ) {

					if ( image_reelPosition == 0 ) {
						image_reelPosition = lastWidth;
						$("#slideselect").fadeOut('fast', function() {
							$(this).animate({
								left: -image_reelPosition
								}, 1, function() {
									$(this).fadeIn('slow');
							});
						});
					}
					else {
						image_reelPosition = Math.abs(image_reelPosition) - imgWidth;
						$("#slideselect").fadeOut('fast', function() {
							$(this).animate({
								left: -image_reelPosition
								}, 1, function() {
									$(this).fadeIn('slow');
							});
						});
					}

				}
				else {

					if ( image_reelPosition == -lastWidth ) {
						//last one - go to first
						image_reelPosition = 0;
						$("#slideselect").fadeOut('fast', function() {
							$(this).animate({
								left: -image_reelPosition
								}, 1, function() {
									$(this).fadeIn('slow');
							});
						});
					}
					else {
						image_reelPosition = Math.abs(image_reelPosition) + imgWidth;
						$("#slideselect").fadeOut('fast', function() {
							$(this).animate({
								left: -image_reelPosition
								}, 1, function() {
									$(this).fadeIn('slow');
							});
						});
					}

				}

				return false;

			});

		} else {
			// Don't use arrows

		}



//		Custom slutt

//		$(this).find(".image_reel .listRow a").append('<div class="grid"></div>');
//		$(this).find(".image_reel .listRow a .grid").css({"opacity" : "0.25"});

		//Inject <span> for Tool tip
// 		$(this).find(".image_reel .listRow a").append('<span></span>');
//		$(this).find(".image_reel .listRow span").css({"opacity" : "0"});
//		$(this).find(".image_reel .listRow .tplvar-listitem-title-append").css({backgroundColor:'#111', opacity:0.4})
//		.css({"opacity" : "0.9"});

		if( direction == "horisontal" )
		{
			//Get size of images, how many there are, then determin the size of the image reel.
			var windowWidth = $(this).find(".image_reel").width();
			var imageSum = $(this).find(".image_reel .listRow").size();
			var imageReelWidth = windowWidth * imageSum;
			//Adjust the image reel to its new size
			$(this).find(".image_reel").css({'width' : imageReelWidth});
		}
		else
		{
			//Get size of images, how many there are, then determin the size of the image reel.
			var windowHeight = $(this).find(".image_reel").height();
			var imageSum = $(this).find(".image_reel .listRow").size();
			var imageReelHeight = windowHeight * imageSum;
			//Adjust the image reel to its new size
			$(this).find(".image_reel").css({'height' : imageReelHeight});

			// Clear all to get images vertical
			$(this).find(".image_reel a").css({"clear" : "both"});
		}

		//Hover Effect for Tooltip
//		$(".image_reel a").hover(function()
		$(".image_reel").hover(function()
		{
			clearTimeout(timerid);
//			$(this).find("span").stop().animate({ opacity: 0.7}, 200 ).show();
		}
		, function()
		{
//			$(this).find("span").stop().animate({ opacity: 0}, 200 );
			if( auto > 0 )
			{
				// Autoplay
				timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', auto);
			}
		});

		if( method == "paging" )
		{

			//Pagin events
			$(container).find(".paging a").click(function() {

				clearTimeout(timerid);
				if( auto > 0 )
				{
					// Autoplay - restart
					var temp = auto * 5;
					timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', temp);
				}

				if( direction == "horisontal" )
				{

					var triggerID = $(this).attr("rel") - 1;
					var imgWidth = $(this).parent().parent().find("img").width();
					var image_reelPosition = triggerID * imgWidth;

					//Ignore if Active
					if ( $(this).hasClass("active")) {
						//Do Nothing
					}
					else {
						//Set active paging
						$(this).parent().parent().find(".paging a").removeClass("active");
						$(this).addClass("active").blur();

						//image_reel Image
						$(this).parent().parent().find(".image_reel").fadeOut('fast', function() {
							$(this).animate({
								left: -image_reelPosition
								}, 1, function() {
									$(this).fadeIn('slow');
							});
						});
					}
					return false;

				}
				else
				{

					var triggerID = $(this).attr("rel") - 1;
					var imgHeight = $(this).parent().parent().find("img").height();
					var image_reelPosition = triggerID * imgHeight;

					//Ignore if Active
					if ( $(this).hasClass("active")) {
						//Do Nothing
					}
					else {
						//Set active paging
						$(this).parent().parent().find(".paging a").removeClass("active");
						$(this).addClass("active").blur();

						//image_reel Image
						$(this).parent().parent().find(".image_reel").fadeOut('fast', function() {
							$(this).animate({
								top: -image_reelPosition
								}, 1, function() {
									$(this).fadeIn('slow');
							});
						});
					}
					return false;

				}

			});

		}
		else
		{

			//Not Pagin events
			$(".arrows a").click(function() {

				clearTimeout(timerid);
				if( auto > 0 )
				{
					// Autoplay - restart
					var temp = auto * 5;
					timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', temp);
				}

				if( direction == "horisontal" )
				{

					var imageSum = $(".image_reel .listRow").size(); // antall bilder
					var imgWidth = $(".image_reel").width() / imageSum; // bildehøyde
					var lastWidth = ( imgWidth * imageSum ) - imgWidth; // siste posisjon
					var image_reelPositionRaw = $(".image_reel").position(); // posisjon
					var image_reelPositionPx = image_reelPositionRaw.left; // posisjon fra topp i pixel
					var image_reelPositionImg = image_reelPositionPx / imgWidth; // aktivt bilde (ca)
					image_reelPositionImg = Math.floor(image_reelPositionImg); // aktivt bilde
					var image_reelPosition = image_reelPositionImg * imgWidth;

					if ( $(this).attr("title") == 'previous' ) {

						if ( image_reelPosition == 0 ) {
							image_reelPosition = lastWidth;
							$(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									left: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) - imgWidth;
							$(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									left: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}

					}
					else {

						if ( image_reelPosition == -lastWidth ) {
							//last one - go to first
							image_reelPosition = 0;
							$(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									left: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) + imgWidth;
							$(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									left: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}

					}

					return false;

				}
				else
				{

					var imageSum = $(this).parent().parent().find(".image_reel img").size(); // antall bilder
					var imgHeight = $(this).parent().parent().find("img").height(); // bildehøyde
					var lastHeight = ( imgHeight * imageSum ) - imgHeight; // siste posisjon
					var image_reelPositionRaw = $(this).parent().parent().find(".image_reel").position(); // posisjon
					var image_reelPositionPx = image_reelPositionRaw.top; // posisjon fra topp i pixel
					var image_reelPositionImg = image_reelPositionPx / imgHeight; // aktivt bilde (ca)
					image_reelPositionImg = Math.floor(image_reelPositionImg); // aktivt bilde
					var image_reelPosition = image_reelPositionImg * imgHeight;

					if ( $(this).attr("title") == 'previous' ) {

						if ( image_reelPosition == 0 ) {
							//first one - go to last
							image_reelPosition = lastHeight;
							$(this).parent().parent().find(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									top: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) - imgHeight;
							$(this).parent().parent().find(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									top: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}

					}
					else {

						if ( image_reelPosition == -lastHeight ) {
							//last one - go to first
							image_reelPosition = 0;
							$(this).parent().parent().find(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									top: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) + imgHeight;
							$(this).parent().parent().find(".image_reel").fadeOut('fast', function() {
								$(this).animate({
									top: -image_reelPosition
									}, 1, function() {
										$(this).fadeIn('slow');
								});
							});
						}

					}

					return false;

				}

			});

		}

		if( auto > 0 )
		{
			// Autoplay
			timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', auto);
		}

	});

}

function NextImage(container, method, direction, auto) {
	clearTimeout(timerid);

	if( direction == "horisontal" )
	{

		var imageSum = $(container).find(".image_reel .listRow").size(); // antall bilder
		var imgWidth = $(container).find(".image_reel").width() / imageSum; // bildebredde
		var lastWidth = ( imgWidth * imageSum ) - imgWidth; // siste posisjon
//		alert (imgWidth);
		var image_reelPositionRaw = $(container).find(".image_reel").position(); // posisjon
		var image_reelPositionPx = image_reelPositionRaw.left; // posisjon fra topp i pixel
		var image_reelPositionImg = image_reelPositionPx / imgWidth; // aktivt bilde (ca)
		image_reelPositionImg = Math.floor(image_reelPositionImg); // aktivt bilde
		var image_reelPosition = image_reelPositionImg * imgWidth;

		if ( image_reelPosition == -lastWidth ) {
			//last one - go to first
			image_reelPosition = 0;
			$(container).find(".image_reel").fadeOut('fast', function() {
				$(this).animate({
					left: -image_reelPosition
					}, 1, function() {
						$(this).fadeIn('slow');
				});
			});
			image_reelPositionImg = 0;

			if( method == "paging" )
			{
				$(container).find(".paging").find("a:last").removeClass("active");
				$(container).find(".paging").find("a:first").addClass("active").blur();
			}
		}
		else {
			image_reelPosition = Math.abs(image_reelPosition) + imgWidth;
			$(container).find(".image_reel").fadeOut('fast', function() {
				$(this).animate({
					left: -image_reelPosition
					}, 1, function() {
						$(this).fadeIn('slow');
				});
			});

			if( method == "paging" )
			{
				var temp = Math.abs(image_reelPositionImg);
				$(container).find(".paging").find("a:eq("+ temp +")").removeClass("active");
				var temp = Math.abs(image_reelPositionImg) + 1;
				$(container).find(".paging").find("a:eq("+ temp +")").addClass("active").blur();
			}
		}

	}
	else
	{

		var imageSum = $(container).find(".image_reel img").size(); // antall bilder
		var imgHeight = $(container).find("img").height(); // bildehøyde
		var lastHeight = ( imgHeight * imageSum ) - imgHeight; // siste posisjon
		var image_reelPositionRaw = $(container).find(".image_reel").position(); // posisjon
		var image_reelPositionPx = image_reelPositionRaw.top; // posisjon fra topp i pixel
		var image_reelPositionImg = image_reelPositionPx / imgHeight; // aktivt bilde (ca)
		image_reelPositionImg = Math.floor(image_reelPositionImg); // aktivt bilde
		var image_reelPosition = image_reelPositionImg * imgHeight;

		// Next
		if ( image_reelPosition == -lastHeight ) {
			//last one - go to first
			image_reelPosition = 0;
			$(container).find(".image_reel").fadeOut('fast', function() {
				$(this).animate({
					top: -image_reelPosition
					}, 1, function() {
						$(this).fadeIn('slow');
				});
			});
		}
		else {
			image_reelPosition = Math.abs(image_reelPosition) + imgHeight;
			$(container).find(".image_reel").fadeOut('fast', function() {
				$(this).animate({
					top: -image_reelPosition
					}, 1, function() {
						$(this).fadeIn('slow');
				});
			});
		}

		if( method == "paging" )
		{
			$(container).find(".paging a eq:1").removeClass("active");
			$(container).find(".paging a eq:2").addClass("active").blur();
		}

	}

	timerid = setTimeout('NextImage("'+ container +'","'+ method +'","'+ direction +'","'+ auto +'")', auto);

}

// Image preloader
/*
$.preloadImages = function() {
    for(var i = 0; i<arguments.length; i++) {
        jQuery('<img>').attr('src', arguments[i]);
    }
};

$.preloadImages('/custom/gfx/teaser/0.gif', '/custom/gfx/teaser/1.gif');
*/

// Startup
$(function() {

	imageRotator(".Slideshow", "not", "horisontal", 0);

});
