(function( $ ){
	var methods = 
	{
		init : function( options ) 
		{
			if (!$.fn.rotator.inited)
			{
				//Global Plug-in Initialisation
				$.fn.rotator.inited = true;
				$("body").append("<div id='fnPortfolioLoaders' style='position:absolute;left:-10000px;top:-10000px;'></div><div id='rotator' style='position:absolute;left:-10000px;top:-10000px;'></div>");
			}
			$.fn.rotator.currentOpen = this;
			
			return this.each(function()
			{
				var _images = options.images;
				var images = [];
				for (var i = 0; i < _images.length; i++)
					images[images.length] = new $.fn.rotator.GaleryImage(_images[i]);
				$(this).data({"images":images, "index":10000});
				var loadersHTML = '';
				var toLoadCount = 0;
				for (var i = 0; i < images.length; i++)
					if (!images[i].loaded)
						loadersHTML+="<img alt='"+i+"'>";
				$("#fnPortfolioLoaders").html(loadersHTML);
				$("#fnPortfolioLoaders img").load(function()
				{
					$($.fn.rotator.currentOpen).rotator("imageLoaded", $(this).attr("alt"));
				});
				for (var i = 0; i < images.length; i++)
					if (!images[i].loaded && $($("#fnPortfolioLoaders img").get(i)).size())
						$($("#fnPortfolioLoaders img").get(i)).attr("src", images[i].src);
				$.fn.rotator.loaderDeferred = $.Deferred();
				if ($("#fnPortfolioLoaders img").size() == 0)
					$.fn.rotator.loaderDeferred.resolve();
		
				$.when($.fn.rotator.loaderDeferred).then(function()
				{
					$($.fn.rotator.currentOpen).rotator("animation_tick");
				});
		
			});
		},
		"imageLoaded":function(index)
		{
			var images = $(this).data("images");
			images[index].loaded = true;
			images[index].width = $($("#fnPortfolioLoaders img").get(index)).width();
			images[index].height = $($("#fnPortfolioLoaders img").get(index)).height();
			$(this).data("images", images);
			$(this).rotator("checkAndTick");
		},
		"checkAndTick":function()
		{
			var images = $(this).data("images");
			for (var i = 0; i < Math.min(3, images.length); i++)
				if (!images[i].loaded) return;
			$.fn.rotator.loaderDeferred.resolve();
		},
		"animation_tick":function()
		{
			var images = $(this).data("images");
			var index  = $(this).data("index");
			index++;
			if (index >= images.length) index = 0;
			$(this).data("index", index);
			
			var img = images[index];
			if ($("#rotator_image_big").size())
			{  
				$("#rotator_image_big").html('').css({width:img.width, height:img.height});
			}
			else
			{
				var settings = $(this).data("settings");
				$("#rotator").html("<div id='rotator_image_big' style='width:"+img.width+"px;height:"+img.height+"px;position:absolute;'>Loading...</div>");
				
			}
			$("#rotator_image_big").html("<div id='rotator_image_cover' style='width:"+img.width+"px;height:"+img.height+"px;position:absolute;display:none;background:url("+img.src+");'></div>");
			$("#rotator_image_cover").data({bg:"url("+img.src+")"}).fadeIn(500, function()
			{ 
				$("#rotator_image_big").css("background", $(this).data("bg"));
				$(this).remove();
				setTimeout(function()
				{
					if ($.fn.rotator.currentOpen)
						$($.fn.rotator.currentOpen).rotator("animation_tick");
				}, 3000);
			});
			/*var tiles = $.fn.rotator.Tiles();
			var tilesHTML = '';
			for (var j = 0; j < tiles.length; j++)
				tilesHTML+="<div class='rotator_sh' style='position:absolute;"+tiles[j]+"'></div>";
			$("#rotator_image_big").html(tilesHTML);
			var i = 0;
			$("#rotator_image_big .rotator_sh").each(function()
			{
				$(this).data({width:$(this).width(), height:$(this).height(), index:i});
				i++;
				$(this).css({"background-image":"url("+img.src+")"});
				var bgPosition = ($("#rotator_image_big").offset().left-$(this).offset().left)+"px "+($("#rotator_image_big").offset().top-$(this).offset().top)+"px";
				if ($.browser.msie)
				{
					$(this).css({"background-position-x":bgPosition.split(" ")[0], "background-position-y":bgPosition.split(" ")[1]});
				}
				else
				{
					$(this).css({"background-position":bgPosition});
				}
				$(this).css({width:0, height:0, opacity:0.0});
			});
			$($.fn.rotator.currentOpen).rotator("slide_queye_tick", -1);*/
		},
		"slide_queye_tick":function(index)
		{
			if (!$.fn.rotator.currentOpen) return;
			index++;
			if (index >= $("#rotator_image_big .rotator_sh").size())
			{
				var images = $(this).data("images");
				var index  = $(this).data("index");
				$("#rotator_image_big").data("newbg", "url("+images[index].src+")");
				setTimeout(function()
				{
					$("#rotator_image_big").css({"background":$("#rotator_image_big").data("newbg")});
				}, 500);
				setTimeout(function()
				{
					if ($.fn.rotator.currentOpen)
						$($.fn.rotator.currentOpen).rotator("animation_tick");
				}, 3000);
			}
			else
			{
				var elem = $($("#rotator_image_big .rotator_sh").get(index));
				elem.animate({width:elem.data("width")/2, height:elem.data("height")/2, opacity:0.5}, 250, "easeInQuad",
				function()
				{
					
					$(this).animate({width:$(this).data("width"), height:$(this).data("height"), opacity:1.0}, 250, "easeOutQuad");
					$($.fn.rotator.currentOpen).rotator("slide_queye_tick", $("#rotator_image_big .rotator_sh").index($(this)));	
				});
			}
		}
		
	};
	$.fn.rotator = function( method ) 
	{

		if ( methods[method] ) 
		{
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} 
		else if ( method instanceof Object || ! method ) 
		{
			return methods.init.apply( this, arguments );
		} 
		else 
		{
			$.error( 'Method ' +  method + ' does not exist on jQuery.rotator' );
		}    

	};
	$.fn.rotator.GaleryImage = function(src)
	{
		this.src = src;
		this.loaded = false;
		this.width  = 0;
		this.height = 0;
	}
	$.fn.rotator.Tiles = function()
	{
		var count = Math.round(Math.random()*3)+2;
		var xOff = 0;
		var yOff = 0;
		var tiles = [];
		var hv = -1;
		for (var i = 0; i < count; i++)
		{
			hv = hv*(-1);
			if (hv<0) // h
			{
				var h = Math.round((100  - yOff)/3+(100  - yOff)*Math.random()/4);
				var v = (100 - xOff);
				tiles[tiles.length] = "left:"+xOff+"%;top:"+yOff+"%;width:"+v+"%;height:"+h+"%;";
				yOff+=h;
			}
			else // v
			{
				var v = Math.round((100  - xOff)/3+(100  - xOff)*Math.random()/4);
				var h = (100 - yOff);
				tiles[tiles.length] = "left:"+xOff+"%;top:"+yOff+"%;width:"+v+"%;height:"+h+"%;";
				xOff+=v;
			}
		}
		tiles[tiles.length] = "left:"+xOff+"%;top:"+yOff+"%;width:"+(100-xOff)+"%;height:"+(100-yOff)+"%;";
		return tiles;
	}
	$.fn.rotator.FullStop = function()
	{
		if ($.fn.rotator.currentOpen) $($.fn.rotator.currentOpen).rotator("hide");
	}


})( jQuery );



