/*
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
		{
			$(this).find(".arrows").show();
		}

		//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 span").width();
			var imageSum = $(this).find(".image_reel img").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 span").height();
			var imageSum = $(this).find(".image_reel img").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()
		{
			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").animate({
							left: -image_reelPosition
						}, 300 );
					}
					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").animate({
							top: -image_reelPosition
						}, 300 );
					}
					return false;

				}

			});

		}
		else
		{

			//Pagin events
			$(container).find(".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 = $(this).parent().parent().find(".image_reel img").size(); // antall bilder
					var imgWidth = $(this).parent().parent().find("img").width(); // bildehøyde
					var lastWidth = ( imgWidth * imageSum ) - imgWidth; // siste posisjon
					var image_reelPositionRaw = $(this).parent().parent().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 ( $(this).attr("title") == 'previous' ) {

						if ( image_reelPosition == 0 ) {
							image_reelPosition = lastWidth;
							$(this).parent().parent().find(".image_reel").animate({
								left: -image_reelPosition
							}, 300 );
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) - imgWidth;
							$(this).parent().parent().find(".image_reel").animate({
								left: -image_reelPosition
							}, 300 );
						}

					}
					else {

						if ( image_reelPosition == -lastWidth ) {
							//last one - go to first
							image_reelPosition = 0;
							$(this).parent().parent().find(".image_reel").animate({
								left: -image_reelPosition
							}, 300 );
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) + imgWidth;
							$(this).parent().parent().find(".image_reel").animate({
								left: -image_reelPosition
							}, 300 );
						}

					}

					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").animate({
								top: -image_reelPosition
							}, 300 );
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) - imgHeight;
							$(this).parent().parent().find(".image_reel").animate({
								top: -image_reelPosition
							}, 300 );
						}

					}
					else {

						if ( image_reelPosition == -lastHeight ) {
							//last one - go to first
							image_reelPosition = 0;
							$(this).parent().parent().find(".image_reel").animate({
								top: -image_reelPosition
							}, 300 );
						}
						else {
							image_reelPosition = Math.abs(image_reelPosition) + imgHeight;
							$(this).parent().parent().find(".image_reel").animate({
								top: -image_reelPosition
							}, 300 );
						}

					}

					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 img").size(); // antall bilder
		var imgWidth = $(container).find("img").width(); // bildehøyde
		var lastWidth = ( imgWidth * imageSum ) - imgWidth; // siste posisjon
		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").animate({
				left: -image_reelPosition
			}, 300 );
			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").animate({
				left: -image_reelPosition
			}, 300 );

			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").animate({
				top: -image_reelPosition
			}, 300 );
		}
		else {
			image_reelPosition = Math.abs(image_reelPosition) + imgHeight;
			$(container).find(".image_reel").animate({
				top: -image_reelPosition
			}, 300 );
		}

		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", 6000);
//	$(".HasSubMenu").animate({ opacity: 0.9}, 0 );

});
