var galleryController=new GalleryController('galleryViewer');

//Gallery Controller object
//responsible for co-ordinating animation and size changes

function GalleryController(elementName)
{
	this._currentState='hidden';
	this._triggerIsPending=false;
	this._showQueue=new Array();
	this._hideQueue=new Array();
	this._galleryElement=elementName;
	this.section=null;
	this.displayedSectionItem=0;
	this.itemsInSection=0;
}

GalleryController.prototype.registerRequest=function(type,options)
{
	options = options || {};
	switch(type) {
		case 'hide':
			this._hideQueue.push(options);
			break;
		case 'show':
			this._showQueue.push(options);
			break;
		default:
			return;
	}						
	if(!this._triggerIsPending) {
		this._triggerIsPending=true;
		setTimeout('galleryController.trigger()',100);
	}
}

GalleryController.prototype.trigger=function()
{
	this._triggerIsPending=false;
	if(this._currentState=='visible') {
		if(this._hideQueue.length>this._showQueue.length) {
			jQuery('div#' + this._galleryElement).fadeOut("normal",function(){galleryController.hideCallback();});
		} else {
			this.triggerRegisteredCallbacksForQueue(this._hideQueue);
		}
		this.triggerRegisteredCallbacksForQueue(this._showQueue);		
	} else {
		this.triggerRegisteredCallbacksForQueue(this._hideQueue);
		if(this._showQueue.length>this._hideQueue.length) {
			jQuery('div#' + this._galleryElement).fadeIn("normal",function(){galleryController.showCallback();});
		} else {
			this.triggerRegisteredCallbacksForQueue(this._showQueue);
		}
	}
}

GalleryController.prototype.showCallback=function()
{
	this._currentState='visible';
	this.triggerRegisteredCallbacksForQueue(this._showQueue);
}

GalleryController.prototype.hideCallback=function()
{
	this._currentState='hidden';
	this.triggerRegisteredCallbacksForQueue(this._hideQueue);
}


GalleryController.prototype.triggerRegisteredCallbacksForQueue=function(queue)
{
	while(queue.length>0) {
		var queueItem=queue.shift();
		if(typeof(queueItem.callback)=='function') {
			queueItem.callback.call(document.getElementById(this._galleryElement));
		}
	}
}


GalleryController.prototype.resize=function(rotation)
{
	var galleryRotationController=jQuery('div#' + this._galleryElement + 'RotationManager');
	switch(rotation) {
		case 'full': /* 600 x 600 */
			galleryRotationController.css('left','0px');
			galleryRotationController.css('right','0px');
			galleryRotationController.css('bottom','20px');
			break;
		case 'portrait': /* 424 x 600 */
			galleryRotationController.css('left','88px');
			galleryRotationController.css('right','88px');
			galleryRotationController.css('bottom','20px');
			break;
		case 'landscape': /* 600 x 424 */
			galleryRotationController.css('left','0px');
			galleryRotationController.css('right','0px');
			galleryRotationController.css('bottom','196px');
			break;
	}
}


GalleryController.prototype.showImageSet=function(section,count)
{
	this.section=section;
	this.displayedSectionItem=1;
	this.itemsInSection=count;
	this.displayImage(this.displayedSectionItem);
}


GalleryController.prototype.hasPreviousImage=function()
	{return (this.displayedSectionItem>1);}


GalleryController.prototype.hasNextImage=function()
	{return ((this.itemsInSection-this.displayedSectionItem)>0);}


GalleryController.prototype.showPreviousImage=function()
{
	if(this.hasPreviousImage()){
		this.displayedSectionItem--;
		this.displayImage(this.displayedSectionItem);
	}
}

GalleryController.prototype.showNextImage=function()
{
	if(this.hasNextImage()){
		this.displayedSectionItem++;
		this.displayImage(this.displayedSectionItem);
	}
}


GalleryController.prototype.displayImage=function(imageNumber)
{
	jQuery('div#' + this._galleryElement + 'Canvas').css('background-image','url("/images/gallery/' + this.section + '/' + imageNumber + '.jpg")');
	var previousImage=jQuery('div#' + this._galleryElement + 'Previous');
	if(this.hasPreviousImage()){
		previousImage.fadeIn("fast");
	} else {
		previousImage.fadeOut("fast");
	} 
	var nextImage=jQuery('div#' + this._galleryElement + 'Next');
	if(this.hasNextImage()){
		nextImage.fadeIn("fast");
	} else {
		nextImage.fadeOut("fast");
	} 
}


GalleryController.prototype.hideImage=function()
{
	jQuery('div#' + this._galleryElement + 'Canvas').css('background-image','none');
}

