// Gallery
Gallery.prototype.removeLastPicture = _removeLastPicture;
Gallery.prototype.removeAllPictures = _removeAllPictures;

Gallery.prototype.addPicture = _addPicture;
Gallery.prototype.addAndSetPicture = _addAndSetPicture;
Gallery.prototype.initGallery = _initGallery;
Gallery.prototype.getPictureById = _getPictureById;
Gallery.prototype.getPictureIdByArrayId = _getPictureIdByArrayId;
Gallery.prototype.getArrayIdByPictureId = _getArrayIdByPictureId;
Gallery.prototype.setCurrentPicture = _setCurrentPicture;
Gallery.prototype.setPicture = _setPicture;

Gallery.prototype.setBestFitControl = _setBestFitControl;
Gallery.prototype.setActualSizeControl = _setActualSizeControl;



function Gallery (gallery_div, gallery_control_type, picture_link_type)
{
	this.pictures = new Array();
	this.galleryControls = new GalleryControl(gallery_control_type, this); 	// type = one-by-one, slide-show, picture-size
	this.pictureLinks = new PictureLink(picture_link_type);					// type = thumb-link, caption-link
	this.gallery_div = gallery_div;
	
	this.picture_number = null; // number of pictures in gallery
	this.current_picture = null; //current picture
	
	this.pictureSize = {
		best_fit : true
	}
	
	this.slideShow = {
		slide_show : false,
		show_interval : 0,
		show_speed : 3000
	}
}

function _initGallery (best_fit)
{
	pic_frame = document.createElement('div');
	pic_frame.id = 'picture_frame';
	pic_frame.className = 'picture_content';
	
	pic = document.createElement('div');
	pic.id = 'pic-view';
	pic.appendChild(document.createTextNode('No image'));
	pic_frame.appendChild(pic);
	
	caption = document.createElement('div');
	caption.id = 'pic-caption';
	pic_frame.appendChild(caption);
	
	desc = document.createElement('div');
	desc.id = 'pic-description';
	pic_frame.appendChild(desc);
	
	$(this.gallery_div).appendChild(pic_frame);
	
	this.galleryControls.createGalleryControl(this.gallery_div); 
	this.pictureLinks.createPictureLink(this.gallery_div, this.pictures);
	
	if (best_fit)
		this.galleryControls.setBestFit();
	else
		this.galleryControls.setActualSize();
		
	this.galleryControls.setSpeed(0, this.slideShow.show_speed);
}

function _addPicture (id, picture_src, thumb_src, caption, description)
{
	var number = this.pictures.length;
	
	picture = new Picture(id, number,  picture_src, thumb_src, caption, description, this);
	this.pictures.push(picture);
	
	this.picture_number = this.pictures.length;
	this.pictureLinks.addThumbLink(picture);
	this.pictureLinks.addCaptionLink(picture);
	
	if(this.picture_number == 1) 
		this.galleryControls.setActionButtons();
	
	return picture;
}

function _addAndSetPicture(id, picture_src, thumb_src, caption, description)
{
	picture = this.addPicture(id, picture_src, thumb_src, caption, description, this);
	
	this.setPicture(id);
	this.pictureLinks.setThumb(id, this.current_picture, picture.array_id, galleryConst.thumbWidthConst, galleryConst.thumbsWidthConst);
	this.pictureLinks.setCaption(id, this.current_picture);
}

function _getPictureById(picture_id)
{
	for (i=0; i<this.pictures.length; i++)
	{
		if (this.pictures[i].id == picture_id) 
			return this.pictures[i];
	}
	return null;
}

function _removeLastPicture()
{
	var lastPicture = this.getPictureIdByArrayId(this.pictures.length-1);
	
	//alert(this.pictures[lastPicture].id);
	//alert(this.picture_number);
	this.pictureLinks.removeThumbLink(lastPicture, this.picture_number - 1);
	this.pictureLinks.removeCaptionLink(lastPicture);
	this.pictures.pop();
	this.picture_number = this.pictures.length;
	if(this.pictures.length == 0)
		this.current_picture = null;
		
}

function _removeAllPictures()
{
	this.pictureLinks.removeAllThumbLink();
	this.pictureLinks.removeAllCaptionLink();
	this.pictures = null;
	this.pictures = new Array;
	this.current_picture = null;
	this.picture_number = null;
}

function _getPictureIdByArrayId(array_id)
{
	for (i=0; i<this.pictures.length; i++)
	{
		if (this.pictures[i].array_id == array_id) 
			return this.pictures[i].id;
	}
	return null;
}

function _getArrayIdByPictureId(picture_id)
{
	for(i=0; i<this.pictures.length;i++)
	{
		if(this.pictures[i].id == picture_id)
			return this.pictures[i].array_id;
	}
	return null;
}

function _setCurrentPicture(current_id)
{
	this.current_picture = current_id;
}

function _setPicture(picture_id)
{
	picture = this.getPictureById(picture_id);
	picture.showPicture(this.current_picture, this.pictureSize.best_fit, this.slideShow.slide_show);
	
	this.pictureLinks.setThumb(picture_id, this.current_picture, picture.array_id, galleryConst.thumbWidthConst, galleryConst.thumbsWidthConst);
	this.pictureLinks.setCaption(picture_id, this.current_picture);
	
	this.setCurrentPicture(picture_id);
}

function _setBestFitControl()
{
	this.galleryControls.setBestFit();
	this.setPicture(this.current_picture);
}

function _setActualSizeControl()
{
	this.galleryControls.setActualSize();
	this.setPicture(this.current_picture);
}
