/*
SNIPER PLUGIN
=============
Author:   Andrija Brljak (Binom)
Modified: 10.11.2010
Version:  1.0.1

OPTIONS PROPERTIES:
===================
.zoom      -> html element    -> container for magnified display
.bindEvent -> string          -> event name to be bound for triggering the plugin (e.g. "mouseover", "click")
.onOpen    -> function(thumb) -> event callback for open
.onClose   -> function(thumb) -> event callback for close
.onError   -> function(thumb) -> event callback for image load error

EXAMPLE USAGE:
==============
<a href="full.jpg" id="myThumb"><img src="thumb.jpg" alt="" /></a>
<div id="zoom"></div>
<script type="text/javascript">
	$("#myThumb").sniper({ zoom:"#zoom", loading:"#loading", bindEvent:"mouseover" });
</script>
*/
function SniperPlugin(options, thumb)
{
	if($(thumb).is("a"))
	{
		var Status  = 0;
		var Img = $(document.createElement("img")).attr("src", $(thumb).attr("href")).css("position", "relative").error(function() { Status = -1; }).load(function() { Status = 1; });
		var Hide = function(e)
		{
			$(options.zoom).hide();
			if(options.onClose) options.onClose(thumb);
		}
		var Init = function(e)
		{
			if(Status >= 0)
			{
				$(options.zoom).children("img").remove();
				$(options.zoom).append(Img).show();
				if(options.onOpen) options.onOpen(thumb);
			}
			else
			{
				if(options.onError) options.onError(thumb);
			}
		};
		var ZoomAt = function(e)
		{
			if($(options.zoom).is(":visible"))
			{
				var imgWidth   = $(Img).width();
				var imgHeight  = $(Img).height();
				var zoomWidth  = $(options.zoom).width();
				var zoomHeight = $(options.zoom).height();
				var left = (imgWidth  * (e.pageX - $(ThumbImg).offset().left) / $(ThumbImg).width()  - (zoomWidth  * 0.5)) * -1;
				var top  = (imgHeight * (e.pageY - $(ThumbImg).offset().top)  / $(ThumbImg).height() - (zoomHeight * 0.5)) * -1;
				if(left > 0) left = 0;
				if(top  > 0) top = 0;
				if(left < (imgWidth  - zoomWidth)  * -1) left = (imgWidth  - zoomWidth)  * -1;
				if(top  < (imgHeight - zoomHeight) * -1) top  = (imgHeight - zoomHeight) * -1;
				var zoomImg = $(options.zoom).children("img");
				$(zoomImg).css("left", left).css("top", top);
			}
		};
		$(options.zoom).css("overflow", "hidden").hide();
		var ThumbImg = $(thumb).children("img").load(function()
		{
			$(this).css("position", "relative").css("top", "50%").css("left", "50%").css("margin-left", $(this).width() * -0.5).css("margin-top", $(this).height() * -0.5);
		});
		$(ThumbImg).attr("src", $(ThumbImg).attr("src"));
		$(thumb).css("display", "block").css("overflow", "hidden").css("position", "relative").attr("href", "#").click(function(){return false;}).mousemove(ZoomAt).mouseout(Hide).bind(options.bindEvent != undefined ? options.bindEvent : "mouseover", function(e)
		{
			$(options.zoom).is(":visible") ? Hide(e) : Init(e);
		});
	}
}
(function($)
{
    $.fn.extend(
	{
		sniper: function(options)
		{
			return this.each(function()
			{
				$(this).data("sniper", new SniperPlugin(options, this));
			});
		}
    });
})(jQuery);

/*

SCROLLER PLUGIN
===============
Author:   Andrija Brljak (Binom)
Modified: 12.11.2010
Version:  0.9.1

*/
function ScrollerPlugin(options, container)
{
	if(options.grid)
	{
		if(!options.grid.spacing) options.grid.spacing = 0;
		if(isNaN(options.grid.x) || options.grid.x < 1) options.grid.x = 1;
		if(isNaN(options.grid.y) || options.grid.y < 1) options.grid.y = 1;
	}
	if(!options.speed) options.speed = 250;
	var offsetX = 0;
	var lr_jumpCount = 0;
	var currentItem = 0;
	var isStopped = true;
	var Canvas = $(container).children(":first").css("position", "absolute").css("width", 5000).css("height", 5000);
	var OriginalItems = $(Canvas).children();
	var CenterRelative = function(e){ $(e).css("position", "relative").css("left", "50%").css("top", "50%"); }
	var CenterGridItem = function(e)
	{
		if ($(e).is("a"))
		{
			$(e).css("display", "block").css("overflow", "hidden").css("height", "100%").css("width", "100%").css("left", 0).css("top", 0).css("padding", 0);
			var eImg = $(e).children(":first");
			$(eImg).css("display", "none");
			$(eImg).css("top", 0);
			$(eImg).css("left", 0);
			if ($(eImg)[0].complete == false)
			{
				$(eImg).load(function()
				{
					CenterElement(this);
				});
			}
			else
			{
				CenterElement(eImg);
			}
		}
		else
		{
			$(e).css("margin-left", $(e).width() * -0.5).css("margin-top", $(e).height() * -0.5);
		}
	};

	var CenterElement = function(el)
	{
		/* ######################################################################################################################## WIDTH I HEIGHT SU NULL U IE*/
		var imgWidth = $(el).width();
		var imgHeight = $(el).height();
		if (imgWidth > 0 && imgHeight > 0)
		{
			$(el).css("margin-left", imgWidth * -0.5);
			$(el).css("margin-top", imgHeight * -0.5);
			$(el).css("display", "block");
			CenterRelative(el);
		}
	};
	
	if(options.grid)
	{
		var boxWidth  = ($(container).width()  - ((options.grid.x - 1) * options.grid.spacing)) / options.grid.x;
		var boxHeight = ($(container).height() - ((options.grid.y - 1) * options.grid.spacing)) / options.grid.y;
		$(OriginalItems).each(function(i)
		{
			var wrapper = $(document.createElement("div"));
			$(wrapper).css("overflow", "hidden").css("width", boxWidth).css("height", boxHeight).css("position", "relative");
			$(wrapper).css("float", "left").css("margin-right", options.grid.spacing).css("margin-bottom", options.grid.spacing);
			/* ######################################################################################################################## U IE nešto se ovdje krivo pozicionira */
			CenterRelative(this);
			$(Canvas).append(wrapper);
			$(wrapper).append(this);
			if ($(this).is("img") && this.complete == false)
			{
				$(this).load(function() { CenterGridItem(this); });
			}
			else
			{
				CenterGridItem(this);
			}
		});
	}
	var Size = function(element) { return {x:$(element).position().left, y:$(element).position().top, width:$(element).outerWidth(true), height:$(element).outerHeight(true)}; };
	var Tween = function(callback, tweenSpeed, tweenEase)
	{
		$(Canvas).animate({marginLeft:offsetX}, options.speed, "swing", function()
		{
			isStopped = true;
			if(callback) callback();
			E_OnScrollComplete();
		});
	};
	var E_OnBeforeScroll   = function() { if(options.onBeforeScroll)   options.onBeforeScroll($(container).data("scroller")); }
	var E_OnScrollComplete = function() { if(options.onScrollComplete) options.onScrollComplete($(container).data("scroller")); }
	
	var MoveLeft = function(callback)
	{
		if(isStopped)
		{
			var doMove = false;
			var Items = $(Canvas).children();
			for(var i = 0; i < Items.length; i++)
			{
				if(Size($(Items[i])).x > $(container).width()) { doMove = true; break; }
			}
			if(doMove)
			{
				E_OnBeforeScroll();
				isStopped = false;
				var last = $(Canvas).children(":last");
				offsetX = 0;
				$(Canvas).css("margin-left", $(last).outerWidth(true) * -1).prepend(last);
				SetCurrentItem(-1);
				Tween(callback);
			}
		}
		return false;
	};
	var MoveRight = function(callback)
	{
		if(isStopped)
		{
			var doMove = false;
			var Items = $(Canvas).children();
			for(var i = 0; i < Items.length; i++)
			{
				if(Size($(Items[i])).x + offsetX > $(container).width()) { doMove = true; break; }
			}
			
			if(doMove)
			{
				E_OnBeforeScroll();
				isStopped = false;
				var first = $(Canvas).children(":first");
				var dim = Size(first);
				offsetX = (dim.width + dim.x) * -1;
				SetCurrentItem(1);
				Tween(function()
				{
					$(Canvas).append(first).css("margin-left", 0);
					offsetX = 0;
					if(callback) callback();
				});
			}
		}
		return false;
	};
	this.GetIndex = function() { return currentItem; };
	this.JumpBy = function(step)
	{
		if(step != 0)
		{
			lr_jumpCount = 0;
			step > 0 ? JumpRight(step) : JumpLeft(Math.abs(step));
		}
		return false;
	};
	this.JumpTo   = function(index) { return this.JumpBy(index - currentItem); };
	var JumpLeft  = function(step)  { MoveLeft(function()  { JumpStep(JumpLeft,  step); }); };
	var JumpRight = function(step)  { MoveRight(function() { JumpStep(JumpRight, step); }); };
	var JumpStep  = function(closure, limit) { lr_jumpCount++ < limit - 1 ? closure(limit) : lr_jumpCount = 0; };
	var SetCurrentItem = function(increment)
	{
		currentItem += increment;
		if(currentItem == $(Canvas).children().length) currentItem = 0;
		if(currentItem < 0) currentItem = $(Canvas).children().length - 1;
	};
	$(options.prev).click(function() { return MoveLeft(); });
	$(options.next).click(function() { return MoveRight(); });
	$(container).css("position", "relative").css("overflow", "hidden");
	if(options.onItemsReady) options.onItemsReady(this, options.grid ? $(Canvas).children("div").children() : $(Canvas).children());
}


(function($)
{
    $.fn.extend(
	{
		scroller: function(options)
		{
			return this.each(function()
			{
				$(this).data("scroller", new ScrollerPlugin(options, this));
			});
		}
    });
})(jQuery);
