﻿/**
 * imagerotator - does whatever with images
 *
 * Copyright (c) 2009 Litium Affärskommunikation AB (http://www.litium.se)
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 */

jQuery.fn.imagerotator = function(settings){
	return this.each(function(){
		jQuery.imagerotator(this, settings);
	});
};

jQuery.imagerotator = function(o, settings){
	settings = jQuery.extend(
	{
		delay: "3500",
		speed: "slow",
		images: [""],
		imageLIs: [""],
		index:1,
		imageClass: "CMS_ImageRotator_Image",
		autoStart:false,
		isRunning:false,
		animating:false
	}, settings);
	
	if(settings.images != null && settings.images.length > 0)
	{
		$(o).css("overflow", "hidden");
	
		var parentUl = $("<ul></ul>");
		$(parentUl).css({"list-style":"none", "margin":"0", "padding":"0"});
		// Preloading images
		$.each(settings.images, function(i, image)
			{
				var li = $("<li></li>");
				var div = $("<div></div>");
				
				var img = $("<img />");
				$(img).attr("src", image.Url);
				
				//$(li).hide();
				$(li).css({"position":"absolute", "top":"0px", "left":"0px", "opacity":"0"});
				
				$(div).css({"width": $(o).width() + "px", "text-align":"center"});
				
				$(div).append(img);
				$(li).append(div);
				$(parentUl).append(li);
				
				settings.imageLIs[i] = li;
			}
		);
		
		$(o).append(parentUl);
		
		// Show first image
		$(settings.imageLIs[0]).show();
		$(settings.imageLIs[0]).css("opacity", "1");
		$(settings.imageLIs[0]).attr("active", "true");
		
//		settings.d1 = $("<div id=\"CMS_ImageRotator_Div1\"></div>");
//		settings.i1 = $("<img id=\"CMS_ImageRotator_Image1\" />");
//		settings.i1.attr("src", settings.images[0].Url);
//		$(settings.d1).append(settings.i1);
//		$(o).append(settings.d1);
		
		if(settings.images.length > 1)
		{
			settings.d2 = $("<div id=\"CMS_ImageRotator_Div2\"></div>");
			settings.i2 = $("<img id=\"CMS_ImageRotator_Image2\" />");
			settings.i2.attr("src", settings.images[1].Url);
			settings.i2.fadeTo(1, 0);
			$(settings.d2).append(settings.i2);
			$(o).append(settings.d2);
		
			settings.buttonPrev = $("<div id=\"CMS_ImageRotator_ButtonPrev\"></div>");
			settings.buttonNext = $("<div id=\"CMS_ImageRotator_ButtonNext\"></div>");
			
			settings.buttonPrev.click(function(){ prevClick(o, settings); });
			settings.buttonNext.click(function(){ nextClick(o, settings); });
			
			$(o).append(settings.buttonPrev);
			$(o).append(settings.buttonNext);
		}
			
//		if(settings.imageClass != null)
//		{
//			$(settings.id).attr("class", settings.imageClass);
//			$(settings.id).attr("class", settings.imageClass);
//		}
//		
//		//alert(settings.images[0].Height);
		if($(settings.imageLIs[0]).height() > 0)
			$(o).css("height", $(settings.imageLIs[0]).height() + "px");
//		
//		var clear = $("<div></div>");
//		clear.css("clear", "both");
//		$(o).append(clear);
		
		setTimeout(function() { UpdateHeight(o, settings, false); }, 350);
		
		if(settings.autoStart && settings.imageLIs.length > 1)
		{
			settings.isRunning = true;
			settings.timer = setTimeout( function(){ next(o, settings); }, settings.delay );
		}
	}
};

function prevClick(o, settings)
{
	if(settings.animating)
	{
		setTimeout(function(){ prevClick(o, settings); }, 100);
		return;
	}
		
	clearTimeout(settings.timer);
	settings.isRunning = false;
	prev(o, settings)
}

function nextClick(o, settings)
{
	if(settings.animating)
	{
		setTimeout(function(){ nextClick(o, settings); }, 100);
		return;
	}
	
	clearTimeout(settings.timer);
	settings.isRunning = false;
	next(o, settings)
}

function next(o, settings)
{
	if(settings.index < settings.images.length - 1){
		settings.index++;
	}
	else{
		settings.index = 0;
	}
	
	loadImage(o, settings);
}

function prev(o, settings)
{
	if(settings.index == 0){
		settings.index = settings.images.length - 1;
	}
	else{
		settings.index--;
	}
	
	loadImage(o, settings);
}

function loadImage(o, settings)
{
	settings.animating = true;
	
	var image = settings.imageLIs[GetIndex(settings)];
	
	$(o).children("ul").children("li[active='true']").each(
		function(){ 
			$(this).fadeTo(settings.speed, 0); 
			$(this).attr("active", "false");
	});
	
	//if(image.Height != null && image.Height != "" && image.Height != -1)
		$(o).animate({"height": $(image).height() + "px"}, settings.speed);
	//else // Otherwise rund the UpdateHeight script to update to new heigth
		//setTimeout( function() { UpdateHeight(o, settings, true); }, 100);
	
	$(image).fadeTo(settings.speed, 1,
		function()
		{
			$(image).attr("active", "true");
					
			//i1.hide();
			settings.animating = false;
			
			if(settings.isRunning)
				settings.timer = setTimeout( function(){ next(o, settings); }, settings.delay );
		}
	);
	
//	var i1 = settings.i1;
//	var i2 = settings.i2;
//	
//	if(i2.css("opacity") != "0"){
//		i1 = settings.i2;
//		i2 = settings.i1;
//	}

//	i1.fadeTo(settings.speed, 0);
//	i2.attr("src", image.Url);
//	
//	// Check if image height is present, it is when images comes from for example CMS
//	if(image.Height != null && image.Height != "" && image.Height != -1)
//		$(o).animate({"height": image.Height + "px"}, settings.speed);
//	else // Otherwise rund the UpdateHeight script to update to new heigth
//		setTimeout( function() { UpdateHeight(o, settings, true); }, 100);
//		
//	i2.fadeTo(settings.speed, 1,
//		function()
//		{				
//			//i1.hide();
//			settings.animating = false;
//			
//			if(settings.isRunning)
//				settings.timer = setTimeout( function(){ next(o, settings); }, settings.delay );
//		}
//	);
}

function UpdateHeight(o, settings, animate)
{
	var h1 = Number($(settings.imageLIs[GetIndex(settings)]).height());

//	var h1 = Number($(settings.i1).height());
//	var h2 = 0;
//	if(settings.i2 != null)
//		h2 = Number($(settings.i2).height());
//	
	var oh = Number($(o).height());
	
	if(h1 > oh)
	{
		if(settings.imageLIs.length > 1)
		{
			if(animate) $(o).animate({"height":h1 + "px"}, settings.speed);
			else $(o).css({"height":h1 + "px"});
		}
		else
			$(o).height(h1);
	}
	
	if(h1 == oh)
	{
		//alert(h1 + "\n" + oh);
		return;
	}
		
//	
//	console.log(h1, h2, oh, $(settings.i1).css("opacity"), $(settings.i2).css("opacity"));
//	
//	if($(settings.i1).css("opacity") == "1" && h1 > 0){
//		if(oh != 0 && oh == h1)
//			return;
//	
//		if(settings.images.length > 1)
//		{
//			if(animate) $(o).animate({"height":h1 + "px"}, settings.speed);
//			else $(o).css({"height":h1 + "px"});
//		}
//		else
//			$(o).height(h1);
//	}
//	if(settings.d2 != null)
//	{
//		if($(settings.i2).css("opacity") == "1" && h2 > 0){
//			if(oh != 0 && oh == h2)
//				return;
//			
//			if(settings.images.length > 1)
//			{
//				if(animate) $(o).animate({"height":h2 + "px"}, settings.speed);
//				else $(o).css({"height":h2 + "px"});
//			}
//			else
//				$(o).height(h2);
//		}
//	}
	
	setTimeout(function(){ UpdateHeight(o, settings, animate); }, 100);
}

function GetIndex(settings)
{
	if(settings.index == 0){
		return settings.images.length - 1;
	}
	else{
		return settings.index - 1;
	}
}