function PictureGallery(pictures_per_page, start_galp_id)
{
	this.pictures_per_page = pictures_per_page;
	this.small_images = Array();
	this.big_images = Array();
	this.titles = Array();
	this.galp_ids = Array();
	this.mainTitles = new Array();
	this.images_counter = 0;
	this.page_s = 0;
	this.top_image_nr = 0;
	this.page_s_max = 0;
	this.start_galp_id = start_galp_id;
}

PictureGallery.prototype.addImage = function(galp_id, small_image, big_image, title)
{
	this.small_images[this.images_counter] = small_image;
	this.big_images[this.images_counter] = big_image;
	this.titles[this.images_counter] = title;
	this.galp_ids[this.images_counter] = galp_id;
	this.images_counter++;
	this.page_s_max = Math.ceil(this.images_counter/this.pictures_per_page);
}

PictureGallery.prototype.addTitle = function(title)
{
	this.mainTitles[this.images_counter -1] = title;
}
/*parametr jest numerem obrazka z całej listy obrazków zaczynając numerację od 0*/
PictureGallery.prototype.movetoimagenr = function(image_nr)
{
	this.top_image_nr = image_nr;
	this.updatePage_s();
	this.draw();
}

/*parametr jest numerem obrazka z aktualnej strony zaczynając numerację od 0*/
PictureGallery.prototype.movetoimage = function(image_nr)
{
	this.top_image_nr = image_nr * 1 + this.page_s * this.pictures_per_page;
	this.updatePage_s();
	this.draw();
}

PictureGallery.prototype.movetostart = function()
{
	for (var i=0;i<this.images_counter;i++)
	{
		if (this.galp_ids[i] == this.start_galp_id)
		{
			this.top_image_nr = i;
			this.start_galp_id = 0;
			this.updatePage_s();
		}
	}
}

PictureGallery.prototype.prev = function()
{
	if (this.top_image_nr>0)
	{
		this.top_image_nr--;

		this.updatePage_s();
		
		this.draw();
	}
}

PictureGallery.prototype.next = function()
{
	if (this.top_image_nr<this.images_counter-1)
	{
		this.top_image_nr++;
		
		this.updatePage_s();
		
		this.draw();
	}
}

PictureGallery.prototype.prev_s = function()
{
	if (this.page_s>0)
	{
		this.page_s--;
		this.draw();
	}
}
PictureGallery.prototype.next_s = function()
{
	if (this.page_s<this.page_s_max-1)
	{
		this.page_s++;
		this.draw();
	}
}

PictureGallery.prototype.updatePage_s = function()
{
	this.page_s = Math.floor(this.top_image_nr/this.pictures_per_page);
}

PictureGallery.prototype.draw = function()
{
	if (this.start_galp_id>0)
	{
		this.movetostart();
	}

	for (var i=0;i<this.pictures_per_page;i++)
	{
		image_nr = i+this.page_s*this.pictures_per_page;
		if (image_nr<this.images_counter)
		{
			if (image_nr == this.top_image_nr)
			{
				$('#a_popup_gallery_s-'+i).addClass('active');
				if (this.mainTitles[image_nr])
				{
					$('#galpopup-x h2').html(this.mainTitles[image_nr]);
				}
			}
			else
			{
				$('#a_popup_gallery_s-'+i).removeClass('active');
			}
			$('#a_popup_gallery_s-'+i+' > img').attr('src',this.small_images[image_nr]);
			$('#a_popup_gallery_s-'+i).attr('title',this.titles[image_nr]);
			$('#a_popup_gallery_s-'+i).css('display','block');
			$('#img_popup_gallery').attr('src',this.big_images[this.top_image_nr]);
		}
		else
		{
			$('#a_popup_gallery_s-'+i).css('display','none');
		}
	}
	
	if (this.page_s==0)
	{
		$('#div_popup_gallery_prev_s').css('display','none');
		$('#div_popup_gallery_prev_s_off').css('display','block');
	}
	
	if (this.page_s>0)
	{
		$('#div_popup_gallery_prev_s_off').css('display','none');
		$('#div_popup_gallery_prev_s').css('display','block');
	}
	
	if (this.page_s==this.page_s_max-1)
	{
		$('#div_popup_gallery_next_s').css('display','none');
		$('#div_popup_gallery_next_s_off').css('display','block');
	}
	
	if (this.page_s<this.page_s_max-1)
	{
		$('#div_popup_gallery_next_s_off').css('display','none');
		$('#div_popup_gallery_next_s').css('display','block');
	}

	
	if (this.top_image_nr==0)
	{
		$('#div_popup_gallery_prev').css('display','none');
		$('#div_popup_gallery_prev_off').css('display','block');
	}
	
	if (this.top_image_nr>0)
	{
		$('#div_popup_gallery_prev_off').css('display','none');
		$('#div_popup_gallery_prev').css('display','block');
	}
	
	if (this.top_image_nr==this.images_counter-1)
	{
		$('#div_popup_gallery_next').css('display','none');
		$('#div_popup_gallery_next_off').css('display','block');
	}
	
	if (this.top_image_nr<this.images_counter-1)
	{
		$('#div_popup_gallery_next_off').css('display','none');
		$('#div_popup_gallery_next').css('display','block');
	}
}

