(function(sk, $){

	/*
		Vytvořil:
		06.09.2011 | Michal Matuška
		
		Změny:
		09.09.2011 | Michal Matuška - přidání možnosti počítat pouze jednu souřadnici 
		
		Opravené bugy:
		13.10.2011 | Přidáno .stop() - oprava opakované počítání pozice s animací
		
		Vyžaduje:
		-
		
		Popis
		pomocný objekt pro zarovnání bloku k nějakému elementu, dokumentu či oknu. Dá se použít opakovaně pomocí methody set, která nastaví novou pozici. 
		Při nastavení "speed" se změněná pozice bude animovat.
		
	*/

	sk.utils.Position = function(options)
	{	
		this.options = $.extend({
			// element ke kterému se má zarovnávat
			anchor: '',
			// zarovnávaný blok
			element: '',
			// zarovnání bloku [x, y] 
			// 0 - zleva, 0.5 - vystředit, 1- zprava | 0 - zhora, 0.5 - vystředit, 1- zespoda
			align: [0,0], 
			// určení místa v elementu ke kterému se bude zarovnávat  [x, y]
			// 0 - od leva, 0.5 - od středu, 1- od prava | 0 - od zhora, 0.5 - od středu, 1- od spodu
			place: [0,0],
			// offset nové pozice [x, y]
			offset: [0,0],
			// Rychlost animace. Nula animaci vypne
			speed: 0,
			// počítat x-souřadnici
			x: true,
			// počítat z-souřadnici
			y: true,
			// fixní pozice
			fixed: false		
		}, options);
		
		var o = this.options;
		
		this.$anchor = this.getElement(o.anchor);
		this.$element = this.getElement(o.element);
		this.speed = o.speed;
		 
		return this;
	}
	
	// PROTOTYPE
	var _fn = sk.utils.Position.prototype;
	
	_fn.init = function()
	{
		if ( !this.$anchor || !this.$element || !this.$anchor.length || !this.$element.length )
		{
			return this;
		}
		
		return this.set();	
	};
	
	_fn.destroy = function()
	{
		this.$element
			.css({'position':'', 'left':'', 'top':''});
		
		return this;	
	};
	
	_fn.set = function()
	{
		this.$element
			.stop()
			.css({'position': this.options.fixed ? 'fixed' : 'absolute' })
			[this.speed === 0 ? 'css' : 'animate'](this.getPosition(), this.speed);
			
		return this;  	
	};
	
	_fn.getPosition = function(options)
	{
		var anchor = this.getDimensions(this.$anchor, true),
		    element = this.getDimensions(this.$element),
		    parent = this.getDimensions(this.$element.offsetParent()),
			o = this.options,
			pos = {};
			
		if(o.x)
		{
			pos.left = anchor.left + (anchor.width * o.place[0]) - (element.width * o.align[0]) + o.offset[0] - parent.left;
		}
		if(o.y)
		{
			pos.top = anchor.top + (anchor.height * o.place[1]) - (element.height * o.align[1]) + o.offset[1] - parent.top;
		}
		
		if(o.fixed)
		{
			var $w = $(window);
			pos.left = pos.left - $w.scrollLeft();
			pos.top = pos.top - $w.scrollTop();
		}
			                                               	
		return pos;	
	};
	
	_fn.getDimensions = function(element, check)
	{
		if(check && element.get(0) === document)
		{
			return {
				left: 0,
				top:  0,
				width: element.width(),
				height: element.height()	
			}
		}
		else if(check && element.get(0) === window)
		{
		    return {
				left: element.scrollLeft(),
				top:  element.scrollTop(),
				width: element.width(),
				height: element.height()	
			}
		}
		else
		{
			var off = element.offset();
			
		    return {
				left: off.left,
				top:  off.top,
				width: element.outerWidth(),
				height: element.outerHeight()	
			}
		}	
	};
	
	_fn.getElement = function(element)
	{
		if(element){
			return (element.jquery) ? element : $(element);
		}
		else{
			return false;
		}
	};

})(sk, jQuery);
