var ab;



$(document).ready(function(){ab = new ActifBox()});



function ActifBox()

{

		this.src = '';

		this.desc = '';
		
		this.author = '';
		
		this.authorLink = '';

		var bg = document.createElement('div');

		var cont = document.createElement('div');

		var box = document.createElement('div');		

		var controls = document.createElement('div');

		var footer = document.createElement('div');	

		var loading = document.createElement('div');		

		var img = new Image;

		this.imggroup = new Array;

		this.groupcount = 0;

		this.curid = 0;

		this.animation = 0;

		

		this.minsize = 300;

		this.nextprev = '<a href="" class="prev" onclick="ab.Prev(); return false;">&laquo; poprzednie</a><a href="" onclick="ab.Next(); return false;" class="next">następne &raquo;</a>';

		this.ctrlclose = '<a href="" class="close" onclick="ab.Hide(); return false;"></a>';

		

		$(cont).append(box);

		$(box).append(controls);

	

		$(box).append(loading);		

		

		$(box).attr("id","actifbox");

		$(bg).attr("id","actifbox-bg");



		$(img).attr("alt","");	

		

		$(bg).css({position: "absolute", top: "0px", left: "0px", opacity: "0.0"});

		$(cont).css({position: "absolute", top: "0px", left: "0px", width: "100%"});

		$(box).css({position: "relative", width: "300px", height: "300px", opacity: "0.0", overflow: "hidden"});



		$(loading).css({position: "absolute", opacity: "0.0"});



		$(controls).addClass('controls');

		

		$(footer).addClass('footer');

		$(loading).addClass('loading');

	

		this.Show = function(id)

		{

			var off = $(document).scrollTop();

			$(cont).css("top",off);

			$(document.body).append(bg);			

			$(document.body).append(cont);	

			SetBgDim();

			$(bg).animate({opacity: "0.5"});

			$(box).animate({opacity: "1.0"});

			this.src = $(id).attr('href');

			this.desc = $(id).attr('title');
			
			this.author = $(id).attr('author');
			
			this.authorLink = $(id).attr('authorLink');

			this.GenGroup(id);

			this.StartLoading();

		}

		

		this.Hide = function()

		{

			$(bg).animate({opacity: "0.0"});

			$(box).animate({opacity: "0.0"});

			$(cont).remove();

			$(controls).empty();

			$(bg).queue(function(){

				$(box).css({opacity: "0.0"});			

				$(bg).remove();

				HideImg();

			})

		}

		

		this.Next = function()

		{

			if (this.animation==1)

				return;

			this.curid++;

			if (this.curid == this.groupcount)

				this.curid = 0;

			HideImg();

			this.src = $(this.imggroup[this.curid]).attr('href');

			this.desc = $(this.imggroup[this.curid]).attr('title');

			$(img).css({opacity: "0.0"});

			$(footer).css({opacity: "0.0"});

			$(box).stop();

			$(img).stop();			

			this.StartLoading();

		}

		

		this.Prev = function()

		{

			if (this.animation==1)

				return;

			this.curid--;

			if (this.curid < 0)

				this.curid = this.groupcount-1;

			HideImg();

			this.src = $(this.imggroup[this.curid]).attr('href');

			this.desc = $(this.imggroup[this.curid]).attr('title');

			$(img).css({opacity: "0.0"});

			$(footer).css({opacity: "0.0"});

			$(box).stop();

			$(img).stop();			

			this.StartLoading();

		}		

		

		function HideImg()

		{

			$(img).css({opacity: "0.0"});

			$(footer).css({opacity: "0.0"});	

			$(img).remove();				

			$(footer).empty();

			$(footer).remove();

		}

		

		this.GenGroup = function(id)

		{

			var rel = $(id).attr('rel');

			var i;

			if (rel==null)

				this.groupcount=0;

			else

			{

				var g = new Array;

				$('a[rel='+rel+']').each(function(i){

					g[i]=this;

				});

				for (i=0; i<g.length; i++)

				{

					this.imggroup[i]=g[i];

					if (g[i]==id)

						this.curid = i;

				}

				this.groupcount = g.length;

				if (this.groupcount>1)

					$(controls).append(this.nextprev);				

			}

			$(controls).append(this.ctrlclose);

		}

		

		this.StartLoading = function()

		{

			this.animation = 1;

			$(img).css({opacity: "0.0"});

			$(footer).css({opacity: "0.0"});

			var w = $(box).width();

			var h = $(box).height();			

			$(loading).css({top: (h-$(loading).height())/2 + 20, left: (w-$(loading).width())/2 + 20, opacity: "1.0"});

			$(loading).show();

			$(box).append(img);					

			$(box).append(footer);

			$(img).load(function(){ab.ShowImg()});

			$(img).attr('src',this.src);

		}

		

		function SetBgDim()

		{

			var h = $(cont).height();

			

			if ($(document).height()>h)

				h = $(document).height();

			$(bg).height(h);

			$(bg).width($(window).width());

		}

		

		this.ShowImg = function()

		{

			SetBgDim();

			if (this.groupcount>1)

				$(footer).append('Zdjęcie '+(this.curid+1)+' z '+this.groupcount);

			if ((this.desc!=null) && (this.author==null) && (this.authorLink==null))

			{

				if (this.groupcount>1)

					$(footer).append(' | ');

				$(footer).append('wyświetlono zdjęcie produktu <strong>'+this.desc+'</strong>');

			}
			
			else if ((this.desc!=null) && (this.author!=null) && (this.authorLink==null))
			
			{
                                $(footer).append('realizacja dla: <strong>'+this.desc+'</strong>, autor: <strong>'+this.author+'</strong>');
                        }
                        
			else if ((this.desc!=null) && (this.author!=null) && (this.authorLink!=null))
			
			{
                                $(footer).append('realizacja dla: <strong>'+this.desc+'</strong>, autor: <a href="'+this.authorLink+'">'+this.author+'</a>');
                        }

			else

				$(footer).append('&nbsp;');

			this.SetBoxSize();

			$(box).queue(function(){

				$(img).animate({opacity: "1.0"},500);

				$(footer).css({opacity: "1.0"});

				SetBgDim();

				$(loading).hide();

				ab.animation = 0;

			});

		}

		

		this.SetBoxSize = function()

		{

			var w = $(img).width();

			var h = $(img).height() + GetFullSize(controls)[1] + GetFullSize(footer)[1];



			if (w<this.minsize)

				w = this.minsize;

			$(footer).width(w-parseInt($(footer).css('paddingLeft'))-parseInt($(footer).css('paddingRight'))+parseInt($(box).css('paddingLeft'))+parseInt($(box).css('paddingRight')));

			$(box).animate({height: h, width: w},500);				

		}

		

		function GetFullSize(obj)

		{

			if (obj==null)

				return [0,0];

			var w = $(obj).width();

			var h = $(obj).height();

			w = w + parseInt($(obj).css('paddingLeft')) + parseInt($(obj).css('paddingRight'));

			h = h + parseInt($(obj).css('paddingTop')) + parseInt($(obj).css('paddingBottom'));			

			return [w,h];

		}

}




