/*
Carousel images jQuery function
Written by S A Sureshkumar.
saskumar@facebook.com
+94 71 53 64 797
01:30 AM 17 March 2011
*/

jQuery.fn.carousel = function()
{
	var element = $(this[0]);
	var args = arguments[0] || {};
    var duration = args.duration;
    var direction = args.direction;
	var easing = args.easing;
	var imgSize;
	var imgWidth;
	var imgHeight;
	
	var auto = true;
	
	element.find("ul").append($("<li>")).find("li:last").append($(element.find("li:first").html()));
	imgSize = element.find("li").size();
	var t = new Image();
	t.src = element.find("li:first").find("img").attr("src");
    imgWidth = t.width;
	imgHeight = t.height;
	if(imgWidth == 0)
	{
		element.find("li:first").find("img").load(function()
		{
			imgWidth = this.width;
			imgHeight = this.height;
			element.width(imgWidth);
			element.find("div").width(imgWidth);
			element.find("ul").width(imgSize * imgWidth);
			element.find("li").height(imgHeight);
			element.find("li img").each(function(i, e)
			{
				$(this).css({height : imgHeight, width: imgWidth});
			});
		});
	}
	else
	{
		element.width(imgWidth);
		element.find("div").width(imgWidth);
		element.find("ul").width(imgSize * imgWidth);
		element.find("li").height(imgHeight);
		element.find("li img").each(function(i, e)
		{
			$(this).css({height : imgHeight, width: imgWidth});
		});
	}
	var currentImage = 1;
	doCarousel = function()
	{
		if(direction == "left")
		{
			if(element.find("ul").css("left") == (-imgWidth * (imgSize - 1) + "px"))
			{
				currentImage = 1;
				element.find("ul").animate({left: -imgWidth * (currentImage - 1)}, 0);
			}
			element.find("ul").animate({left: -imgWidth * currentImage }, 1500,easing);
			currentImage++;
			if(currentImage >= imgSize)
			{
				element.find("ul").animate({left: 0}, 0);
				currentImage = 1;
			}
		}
		else if(direction == "right")
		{
			if(element.find("ul").css("left") == "auto" || element.find("ul").css("left") == "0px")
			{
				currentImage = imgSize;
				element.find("ul").animate({left: -imgWidth * (currentImage - 1)}, 0);
				currentImage--;
			}
			else
			{
				currentImage--;
			}
			element.find("ul").animate({left: -imgWidth * (currentImage - 1) }, 1500,easing);
			if(currentImage == 1)
			{
				currentImage = imgSize;
				element.find("ul").animate({left: -imgWidth * (currentImage - 1)}, 0);
			}
		}
	}
	play = setInterval(doCarousel, duration);
	element.find("li").bind("mouseenter mouseleave", function(e)
	{
		if(e.type == "mouseenter")
		{
			clearInterval(play);
		}
		else
		{
			if(auto)
			{
				play = setInterval(doCarousel, duration);
			}
		}
	});
	
	
	element.find(".photoName, .photoDescription").hide();
	
	element.find("ul").draggable(
	{
		axis: 'x',
		drag: function(event,ui)
		{
			if(element.find("ul").css("left").replace(/px/, "") > 0 || element.find("ul").css("left").replace(/px/, "") < ((imgSize - 2) * -imgWidth))
			{
				return false;
			}
		},
		start: function(event, ui)
		{
			clearInterval(play);
			auto = false;
			element.find('.nav a[href="#stop"]').attr({"href":"#play", "class":"play"});
		},
		stop: function(event, ui)
		{
			var pos = Math.abs(element.find("ul").css("left").replace(/px/, ""));
			if(pos % imgWidth != 0)
			{
				pos = pos / imgWidth;
				aaa = -1 * Math.round(pos) * imgWidth;
				element.find("ul").animate({left: aaa }, 1500);
			}
		}
	});
	
	
	
};
