FSite2.galleries = new Object();

FSite2.extendGalleries = function(element, params)
{
	if (!params.galleryClass)
		return;
	if (!element)
		element = document.body;
	var anchors = element.getElementsByTagName('a');
	var galleryId;
	var imageSet;
	var i;
	for (i = 0; i < anchors.length; i++)
	{
		if (!RegExp('\\b' + params.galleryClass + '\\b','gi').test(anchors[i].className))
			continue;
		if (anchors[i].rel)
		{
			galleryId = anchors[i].rel;
			imageSet = true;
		}
		else
		{
			galleryId = 'FSite2_gallery';
			imageSet = false;
		}
		if (!FSite2.galleries[galleryId])
			FSite2.galleries[galleryId] = new FSite2.Gallery(galleryId, params);
		FSite2.galleries[galleryId].addPhotoAnchor(anchors[i])
	}
}

FSite2.Gallery = function(galleryId, params)
{
	this.photos = new Array();
	this.galleryId = galleryId;
	this.backgroundId = params.galleryBackgroundId?params.galleryBackgroundId:(this.galleryId + '_background');
	this.layerId = params.galleryLayerId?params.galleryLayerId:(this.galleryId + '_layer');
	this.loaderId = params.galleryLoaderId?params.galleryLoaderId:(this.galleryId + '_loader');
	this.backgroundClass = params.galleryBackgroundClass?params.galleryBackgroundClass:'gallery_background';
	this.layerClass = params.galleryLayerClass?params.galleryLayerClass:'gallery_layer';
	this.loaderClass = params.galleryLoaderClass?params.galleryLoaderClass:'gallery_loader';
	this.prevClass = params.galleryPrevClass?params.galleryPrevClass:'gallery_prev';
	this.nextClass = params.galleryNextClass?params.galleryNextClass:'gallery_next';
	this.closeClass = params.galleryCloseClass?params.galleryCloseClass:'gallery_close';
	this.captionClass = params.galleryCaptionClass?params.galleryCaptionClass:'gallery_caption';
	this.navClass = params.galleryNavClass?params.galleryNavClass:'gallery_nav';
	this.photoClass = params.galleryPhotoClass?params.galleryPhotoClass:'gallery_photo';
	this.photoNextClass = params.galleryPhotoNextClass?params.galleryPhotoNextClass:'gallery_photo_next';
	this.photoPrevClass = params.galleryPhotoPrevClass?params.galleryPhotoPrevClass:'gallery_photo_prev';
	this.disabledClass = params.galleryDisabledClass?params.galleryDisabledClass:'gallery_disabled';
	this.backgroundClose = params.galleryBackgroundClose?params.galleryBackgroundClose:false;
	if (typeof params.galleryOnPhotoLoad == 'function')
		this.onPhotoLoad = params.galleryOnPhotoLoad;
	if (typeof params.galleryOnPhotoError == 'function')
		this.onPhotoError = params.galleryOnPhotoError;
	if (typeof params.galleryOnPhotoAbort == 'function')
		this.onPhotoAbort = params.galleryOnPhotoAbort;
	return this;
}

FSite2.Gallery.prototype.addPhotoAnchor = function(anchorElement)
{
	var newIndex = this.addPhoto(anchorElement.href, anchorElement.title);
	anchorElement.galleryObject = this;
	anchorElement.galleryIndex = newIndex;
//	anchorElement.href='#';
	anchorElement.onclick = function()
	{
		this.galleryObject.showPhoto(this.galleryIndex);
		return false;
	}
}

FSite2.Gallery.prototype.addPhoto = function(imageSource, imageTitle)
{
	var newIndex;
	for (k in this.photos)
		if (this.photos[k].source === imageSource)
			newIndex = Number(k);
	if (newIndex == null)
		newIndex = this.photos.length;
	this.photos[newIndex] = {source: imageSource};
	if (imageTitle)
		this.photos[newIndex].caption = imageTitle;
	return newIndex;
}

FSite2.Gallery.prototype.removePhoto = function(photoIndex)
{
	this.photos.splice(photoIndex, 1);
}

FSite2.Gallery.prototype.showPhoto = function(photoIndex)
{
	if (!this.photos[photoIndex])
		return;
	if (!this.backgroundLayer)
	{
		this.backgroundLayer = new FSite2.Layer(this.backgroundClass, true, this.backgroundId);
		this.backgroundLayer.object.galleryObject = this;
		if (this.backgroundClose)
			this.backgroundLayer.object.onclick = function()
			{
				this.galleryObject.close();
			}
	}
	if (!this.loaderLayer)
		this.loaderLayer = new FSite2.Layer(this.loaderClass, true, this.loaderId);
	if (!this.photoLayer)
	{
		this.photoLayer = this._createLayer();
		this._extendLayer();
	}
	if (!this.backgroundLayer.isVisible())
		this.backgroundLayer.show();
	this.backgroundLayer.fullScreen();
	this.photoLayer.hide();
	this.loaderLayer.show();

	this.photoElement.galleryIndex = photoIndex;
	this.photoElement.galleryImage = new Image();
	this.photoElement.galleryImage.galleryObject = this;
	this.photoElement.galleryImage.onload = function()
	{
		if (this.galleryObject.loaderLayer.object.style.display)
		{
			var result = true;
			if (typeof this.galleryObject.onPhotoLoad == 'function')
				result = this.galleryObject.onPhotoLoad();
			if (result)
			{
				this.galleryObject.photoElement.style.backgroundImage = 'url("' + this.src + '")';
				this.galleryObject.photoElement.style.height = this.height + 'px';
				this.galleryObject.photoElement.style.width = this.width + 'px';
				// @TODO pozbyć się tego poniżej...
				this.galleryObject.photoLayer.object.style.width = this.width + 'px';
				if (this.galleryObject.captionElement)
				{
					this.galleryObject.captionElement.innerHTML = '';
					if (this.galleryObject.photos[photoIndex].caption)
						this.galleryObject.captionElement.appendChild(document.createTextNode(this.galleryObject.photos[photoIndex].caption));
				}
			}
			this.galleryObject.loaderLayer.hide();
			this.galleryObject.photoLayer.show();
		}
	}
	this.photoElement.galleryImage.onerror = function()
	{
		if (this.galleryObject.loaderLayer.object.style.display)
		{
			var result = false;
			if (typeof this.galleryObject.onPhotoError == 'function')
				result = this.galleryObject.onPhotoError();
			if (result)
			{
				this.galleryObject.loaderLayer.hide();
				this.galleryObject.photoLayer.show();
			}
		}
	}
	this.photoElement.galleryImage.onabort = function()
	{
		if (this.galleryObject.loaderLayer.object.style.display)
		{
			var result = true;
			if (typeof this.galleryObject.onPhotoAbort == 'function')
				result = this.galleryObject.onPhotoAbort();
			this.galleryObject.loaderLayer.hide();
			this.galleryObject.loaderLayer.remove();
			this.galleryObject.loaderLayer = null;
			this.galleryObject.backgroundLayer.hide();
			this.galleryObject.backgroundLayer.remove();
			this.galleryObject.backgroundLayer = null;
		}
	}
	this.photoElement.galleryImage.src = this.photos[photoIndex].source;
	if (photoIndex > 0)
	{
		if (this.prevElement)
		{
			this.prevElement.galleryIndex = photoIndex - 1;
			this.prevElement.href = this.photos[photoIndex - 1].source;
			FSite2.unsetElementClass(this.prevElement, this.disabledClass);
		}
		if (this.photoPrevElement)
		{
			this.photoPrevElement.galleryIndex = photoIndex - 1;
			this.photoPrevElement.href = this.photos[photoIndex - 1].source;
			FSite2.unsetElementClass(this.photoPrevElement, this.disabledClass);
		}
		if (this.prevElement || this.photoPrevElement)
		{
			var tempImage = new Image();
			tempImage.src = this.photos[photoIndex - 1].source;
		}
	}
	else
	{
		if (this.prevElement)
		{
			this.prevElement.galleryIndex = null;
			this.prevElement.href = null;
			FSite2.setElementClass(this.prevElement, this.disabledClass);
		}
		if (this.photoPrevElement)
		{
			this.photoPrevElement.galleryIndex = null;
			this.photoPrevElement.href = null;
			FSite2.setElementClass(this.photoPrevElement, this.disabledClass);
		}
	}
	if (this.nextElement)
		this.nextElement.className = this.nextElement.className.replace(RegExp('\\s*\\b' + this.disabledClass + '\\b\\s*', 'gi'), '');
	if (photoIndex < this.photos.length - 1)
	{
		if (this.nextElement)
		{
			this.nextElement.galleryIndex = photoIndex + 1;
			this.nextElement.href = this.photos[photoIndex + 1].source;
			FSite2.unsetElementClass(this.nextElement, this.disabledClass);
		}
		if (this.photoNextElement)
		{
			this.photoNextElement.galleryIndex = photoIndex + 1;
			this.photoNextElement.href = this.photos[photoIndex + 1].source;
			FSite2.unsetElementClass(this.photoNextElement, this.disabledClass);
		}
		if (this.nextElement || this.photoNextElement)
		{
			var tempImage = new Image();
			tempImage.src = this.photos[photoIndex + 1].source;
		}
	}
	else
	{
		if (this.nextElement)
		{
			this.nextElement.galleryIndex = null;
			this.nextElement.href = null;
			FSite2.setElementClass(this.nextElement, this.disabledClass);
		}
		if (this.photoNextElement)
		{
			this.photoNextElement.galleryIndex = null;
			this.photoNextElement.href = null;
			FSite2.setElementClass(this.photoNextElement, this.disabledClass);
		}
	}
}

FSite2.Gallery.prototype.close = function()
{
	if (this.photoLayer)
	{
		this.photoLayer.hide();
		this.photoLayer.remove();
		this.photoLayer = null;
	}
	if (this.loaderLayer)
	{
		this.loaderLayer.hide();
		this.loaderLayer.remove();
		this.loaderLayer = null;
	}
	if (this.backgroundLayer)
	{
		this.backgroundLayer.hide();
		this.backgroundLayer.remove();
		this.backgroundLayer = null;
	}
}

FSite2.Gallery.prototype._createLayer = function()
{
	var photoLayer = new FSite2.Layer(this.layerClass, true, this.layerId);
	if (photoLayer.object.innerHTML)
		this._photoLayerTemplate = photoLayer.object.innerHTML;
	else if (this._photoLayerTemplate)
		photoLayer.object.innerHTML = this._photoLayerTemplate;
	photoLayer.object.galleryObject = this;
	this.closeElement = document.getElementById(this.galleryId + '_close');
	this.photoElement = document.getElementById(this.galleryId + '_photo');
	this.photoPrevElement = document.getElementById(this.galleryId + '_photo_prev');
	this.photoNextElement = document.getElementById(this.galleryId + '_photo_next');
	this.navElement = document.getElementById(this.galleryId + '_nav');
	this.prevElement = document.getElementById(this.galleryId + '_prev');
	this.nextElement = document.getElementById(this.galleryId + '_next');
	this.captionElement = document.getElementById(this.galleryId + '_caption');
	if (!this.closeElement && !this.photoElement && !this.photoPrevElement && !this.photoNextElement && !this.navElement &&
		!this.prevElement && !this.nextElement && !this.captionElement)
	{
		this.closeElement = FSite2.createElement('div', {'id': this.galleryId + '_close'});
		photoLayer.object.appendChild(this.closeElement);
		this.photoElement = FSite2.createElement('div', {'id': this.galleryId + '_photo'});
		photoLayer.object.appendChild(this.photoElement);
		this.photoPrevElement = FSite2.createElement('a', {'id': this.galleryId + '_photo_prev'});
		this.photoElement.appendChild(this.photoPrevElement);
		this.photoNextElement = FSite2.createElement('a', {'id': this.galleryId + '_photo_next'});
		this.photoElement.appendChild(this.photoNextElement);
		this.navElement = FSite2.createElement('div');
		photoLayer.object.appendChild(this.navElement);
		this.prevElement = FSite2.createElement('a', {'id': this.galleryId + '_prev'});
		this.navElement.appendChild(this.prevElement);
		this.nextElement = FSite2.createElement('a', {'id': this.galleryId + '_next'});
		this.navElement.appendChild(this.nextElement);
		this.captionElement = FSite2.createElement('div', {'id': this.galleryId + '_caption'});
		this.navElement.appendChild(this.captionElement);
	}
	return photoLayer;
}

FSite2.Gallery.prototype._extendLayer = function()
{
	if (this.closeElement)
	{
		this.closeElement.galleryObject = this;
		FSite2.setElementClass(this.closeElement, this.closeClass);
		this.closeElement.onclick = function()
		{
			this.galleryObject.close();
		}
	}
	if (this.photoElement)
	{
		this.photoElement.galleryObject = this;
		FSite2.setElementClass(this.photoElement, this.photoClass);
		if (this.photoPrevElement)
		{
			this.photoPrevElement.galleryObject = this;
			this.photoPrevElement.onfocus = function() {this.blur();};
			FSite2.setElementClass(this.photoPrevElement, this.photoPrevClass);
			this.photoPrevElement.onclick = function()
			{
				if (this.galleryIndex != null)
					this.galleryObject.showPhoto(this.galleryIndex);
				return false;
			}
		}
		if (this.photoNextElement)
		{
			this.photoNextElement.galleryObject = this;
			this.photoNextElement.onfocus = function() {this.blur();};
			FSite2.setElementClass(this.photoNextElement, this.photoNextClass);
			this.photoNextElement.onclick = function()
			{
				if (this.galleryIndex != null)
					this.galleryObject.showPhoto(this.galleryIndex);
				return false;
			}
		}
	}
	if (this.navElement)
	{
		this.navElement.galleryObject = this;
		FSite2.setElementClass(this.navElement, this.navClass);
	}
	if (this.nextElement)
	{
		this.nextElement.galleryObject = this;
		this.nextElement.onfocus = function() {this.blur();};
		FSite2.setElementClass(this.nextElement, this.nextClass);
		this.nextElement.onclick = function()
		{
			if (this.galleryIndex != null)
				this.galleryObject.showPhoto(this.galleryIndex);
			return false;
		}
	}
	if (this.prevElement)
	{
		this.prevElement.galleryObject = this;
		this.prevElement.onfocus = function() {this.blur();};
		FSite2.setElementClass(this.prevElement, this.prevClass);
		this.prevElement.onclick = function()
		{
			if (this.galleryIndex != null)
				this.galleryObject.showPhoto(this.galleryIndex);
			return false;
		}
	}
	if (this.captionElement)
	{
		this.captionElement.galleryObject = this;
		FSite2.setElementClass(this.captionElement, this.captionClass);
	}
}

