/*[version]2008-07-09 21:34[/version]*/
// Requires modules Acu.js APEInterceptor.js

/************************** class GalleryControl **************************/
function GalleryControl( containerOne, containerTwo/*, interceptor*/ ){
	this.currentGallery = null;
	this.galleries = new Array();

	this.containerOccupied = null;
	this.containerVacant = null;
	var interceptor = arguments[3];
	this.interceptors = new Array();
	if( interceptor ){
		this.interceptors.push( interceptor );
	}
	this.visible = false;

	var self = this;
	var _init = function(){
		var repositioner = function(){
			self.adjustPosition();
		};
		self.initDOM( containerOne, containerTwo );
		Acu.registerEvent( window, "resize", repositioner );
		Acu.registerEvent( window, "scroll", repositioner );
		self.preloadGalleryImages();
	};
	Acu.registerEvent( window, "load", _init );
}

GalleryControl.prototype.initDOM = function( containerOne, containerTwo ){
	this.containerOccupied = new GalleryContainer( containerOne, this );
	this.containerVacant = new GalleryContainer( containerTwo, this );
};

GalleryControl.prototype.addGallery = function( gallery ){
	this.galleries.push( gallery );
	if( !this.currentGallery ){
		this.currentGallery = gallery;
	}
};

GalleryControl.prototype.displayNextImage = function(){
	if( !this.currentGallery ){ throw "Cannot display next image: no gallery set"; }
	this.displayImageAtIndex( this.currentGallery.id, this.currentGallery.imageIndex + 1 );
};

GalleryControl.prototype.displayPreviousImage = function(){
	if( !this.currentGallery ){ throw "Cannot display previous image: no gallery found"; }
	this.displayImageAtIndex( this.currentGallery.id, this.currentGallery.imageIndex - 1 );
};

GalleryControl.prototype.displayImageAtIndex = function( galleryId, index ){
	if( !this.currentGallery || this.currentGalleryId != galleryId ){
		this.currentGallery = this.fetchGallery( galleryId );
	}
	if( !this.currentGallery ){ throw "Cannot display image: no gallery ser"; }
	if( !this.visible ){
		for( var i=0; i<this.interceptors.length; i++ ){
			this.interceptors[i].launch();
		}
	}
	var img = this.currentGallery.getImage( index );
	// Hat die Galerie nur ein Bild (single mode), zeige keine
	// Navigations-Buttons.
	var hideNavButtons = this.currentGallery.singleMode;
	var container = this.containerOccupied;

	this.containerVacant.loadImage( img );
	this.containerOccupied.hide();
	this.containerVacant.show( hideNavButtons );

	this.containerOccupied = this.containerVacant;
	this.containerVacant = container;

	this.visible = true;
};

GalleryControl.prototype.preloadGalleryImages = function(){
	for( var j=0; j<this.galleries.length; j++ ){
		var gallery = this.galleries[j];
		var a = gallery.images;
		for( var i=0; i<a.length; i++ ){
			var img = new Image();
			img.src = a[i].path;
		}
	}
};

GalleryControl.prototype.fetchGallery = function( galId ){
	for( var i=0; i<this.galleries.length; i++ ){
		var gallery = this.galleries[i];
		if( gallery.id == galId ) return gallery;
	}
	return null;
};

GalleryControl.prototype.addInterceptor = function( interceptor ){
	if( interceptor ){
		this.interceptors.push( interceptor );
	}
};


GalleryControl.prototype.close = function(){
	this.containerOccupied.hide();
	this.containerVacant.hide();
	for( var i=0; i<this.interceptors.length; i++ ){
		this.interceptors[i].shutDown();
	}
	this.visible = false;
};

GalleryControl.prototype.adjustPosition = function(){
/*	for( var i=0; i<this.interceptors.length; i++ ){
		this.interceptors[i].resize();
	}*/
	this.containerOccupied.reposition();
};







/************************** class GalleryImage **************************/
function GalleryImage( path, width, height/*, alt, title, randomData*/ ){
	this.path = path;
	this.width = width;
	this.height = height;
	this.alt = arguments[3] ? arguments[3] : "";
	this.title = arguments[4] ? arguments[4] : "";
	this.randomData = arguments[5] ? arguments[5] : {};
}




/************************** class Gallery **************************/
function Gallery( id, images/*, GalleryControl gc */ ){
	var gc = arguments[2];
	this.id = id;
	this.images = images;
	this.imageIndex = 0;
	this.singleMode = (images.length < 2);	// Wenn weniger als zwei gibt's eines oder keines.

	if( gc ) gc.addGallery( this );
}

Gallery.prototype.getImage = function( index ){
	index = (index<0) ? this.images.length-1 : index;
	index = (index>=this.images.length) ? 0 : index;
	this.imageIndex = index;
	return this.images[index];
};




/************************** class GalleryContainer **************************/
function GalleryContainer( id, papa/*, singleMode=false*/ ){
	this.id = id;
	this.papa = papa;
	this.singleMode = (arguments[2] === true);
	this.setup();
}

GalleryContainer.prototype.setup = function(){
	this.domElement = document.getElementById( this.id );
	this.image = document.getElementById( this.id + '_image' );
	this.btnNext = document.getElementById( this.id + '_next' );
	this.btnBack = document.getElementById( this.id + '_back' );
	this.btnClose = document.getElementById( this.id + '_close' );
	this.registerEventHandlers();
	this.galleryImage = null;
	if( this.singleMode ){
		this.btnNext.style.display = 'none';
		this.btnBack.style.display = 'none';
	}else{
		this.btnNext.style.display = '';
		this.btnBack.style.display = '';
	}
};

GalleryContainer.prototype.registerEventHandlers = function(){
	var self = this;
	var handler = function( e ){
		self.papa.displayNextImage();
	};
	Acu.registerEvent( this.image, 'click', handler );
	Acu.registerEvent( this.btnNext, 'click', handler );

	handler = function( e ){
		self.papa.displayPreviousImage();
	};
	Acu.registerEvent( this.btnBack, 'click', handler );

	if( this.btnClose ){
		handler = function( e ){
			self.papa.close();
		};
		Acu.registerEvent( this.btnClose, 'click', handler );
	}
};

GalleryContainer.prototype.loadImage = function( image ){
	this.galleryImage = image;
	this.image.src = image.path;
	this.image.width = image.width;
	this.image.height = image.height;
	this.image.alt = image.alt;
	this.image.title = image.title;
	this.image.randomData = image.randomData;
	this.reposition();
};

GalleryContainer.prototype.show = function(){
	var hideNavButtons = (arguments[0] === true);
	if( hideNavButtons ){
		this.btnNext.style.display = 'none';
		this.btnBack.style.display = 'none';
	}else{
		this.btnNext.style.display = '';
		this.btnBack.style.display = '';
	}
	this.domElement.style.display = '';
};

GalleryContainer.prototype.hide = function(){
	this.domElement.style.display = 'none';
};

GalleryContainer.prototype.reposition = function(){
	if( !this.galleryImage ) return;
	var image = this.galleryImage;
	var top = Math.floor( (Geometry.getViewportHeight()-image.height) / 2 ) - GalleryContainer.BORDER_WIDTH;
	var left = Math.floor( (Geometry.getDocumentWidth()-image.width) / 2 ) - GalleryContainer.BORDER_WIDTH;
	//var left = Math.floor( (Geometry.getViewportWidth()-image.width) / 2 ) - GalleryContainer.BORDER_WIDTH;
//alert( 'Math.floor( (' + GalleryContainer.MAIN_CONTAINER_WIDTH + '-' + image.width + ') / 2 ) + ' + GalleryContainer.BORDER_WIDTH );
	//var left = Math.floor( (GalleryContainer.MAIN_CONTAINER_WIDTH-image.width) / 2 ) - GalleryContainer.BORDER_WIDTH;

	if( navigator.appName != "Microsoft Internet Explorer" && navigator.vendor != "Apple Computer, Inc." ){
		left -= Math.floor( GalleryContainer.MOZ_SCROLLBAR_WIDTH / 2 );
	}

	top = (top<0) ? 0 : top;
	left = (left<0) ? 0 : left;
	top += Geometry.getVerticalScroll();

	/*var height = image.height;
	if( document.attachEvent ){
		height += GalleryContainer.IE_HEIGHT_CORRECTION;
	}*/
	this.domElement.style.left = left + 'px';
	this.domElement.style.top = top + 'px';
	this.domElement.style.width = image.width + 'px';
	this.domElement.style.height = image.height + 'px';


	var nt = Math.floor( (image.height-GalleryContainer.BUTTON_HEIGHT) / 2 ) + GalleryContainer.BORDER_WIDTH;
	var nl = image.width + GalleryContainer.BORDER_WIDTH + GalleryContainer.BUTTON_GAP;
	this.btnNext.style.top = nt + 'px';
	this.btnNext.style.left = nl + 'px';


	var bt = nt;
	var bl = GalleryContainer.BUTTON_GAP;
	this.btnBack.style.top = bt + 'px';
	this.btnBack.style.left = bl + 'px';

	if( this.btnClose ){
		var ct = 8;
		var cl = image.width + GalleryContainer.BORDER_WIDTH + 8;
		this.btnClose.style.top = ct + 'px';
		this.btnClose.style.left = cl + 'px';
	}
};

GalleryContainer.BUTTON_HEIGHT = 34;
GalleryContainer.BUTTON_WIDTH = 9;
GalleryContainer.BUTTON_GAP = 11;
GalleryContainer.BORDER_WIDTH = 31;
GalleryContainer.MOZ_SCROLLBAR_WIDTH = 15;
GalleryContainer.MAIN_CONTAINER_WIDTH = 760;
//GalleryContainer.IE_HEIGHT_CORRECTION = -3;








/************************** class GalleryInterceptor **************************/
// Der GallerInterceptor basiert auf dem Body, hätte es erlauben sollen,
// das Popup relativ zum Hauptcontainer auszurichten, wurde aber im IE
// vom Hintergrund ebenfalls verdunkelt. Alas bleiben wir beim APEInterceptor.
/*
	var handler = function( e ){
		var closeIt = true;
		if( e.target ){
			if( 	e.target.id == 'gal_one_back' || e.target.id == 'gal_one_next' || e.target.id == 'gal_one_image' ||
					e.target.id == 'gal_two_back' || e.target.id == 'gal_two_next' || e.target.id == 'gal_two_image' ){
				closeIt = false;
			}
		}
		if( e.srcElement ){
			if( 	e.srcElement.id == 'gal_one_back' || e.srcElement.id == 'gal_one_next' || e.srcElement.id == 'gal_one_image' ||
					e.srcElement.id == 'gal_two_back' || e.srcElement.id == 'gal_two_next' || e.srcElement.id == 'gal_two_image' ){
				closeIt = false;
			}
		}
		if( closeIt ){
			closeGallery();
		}
	};

*/
function GalleryInterceptor( color, mainContainerId, opacity/*, onclickHandler*/ ){
	this.color = color;
	this.colorSave = null;
	this.mainContainer = document.getElementById( mainContainerId );
	this.opacity = opacity;
	this.opacitySave = null;
	this.onclickHandler = arguments[3];
	this.heightSave = null;

	this.domElement = document.body;
}

GalleryInterceptor.prototype.launch = function(){
	this.heightSave = this.domElement.style.height;
	//this.domElement.style.height = Geometry.getDocumentHeight() + 'px';
	this.domElement.style.height = '100%';

	this.colorSave = this.domElement.style.backgroundColor;
	this.domElement.style.backgroundColor = this.color;

	if( window.attachEvent ){
		this.opacitySave = this.mainContainer.style.filter;
		var ieOpacity = this.opacity * 100;
		this.mainContainer.style.filter = 'alpha(opacity=' + ieOpacity + ')';
	}else{
		this.opacitySave = this.mainContainer.style.opacity;
		this.mainContainer.style.opacity = this.opacity;
	}

	if( this.onclickHandler ){
		Acu.registerEvent( this.domElement, "click", this.onclickHandler );
	}
};

GalleryInterceptor.prototype.shutDown = function(){
	this.domElement.style.backgroundColor = this.colorSave;
	this.domElement.style.height = this.heightSave;

	if( window.attachEvent ){
		this.mainContainer.style.filter = this.opacitySave;
	}else{
		this.mainContainer.style.opacity = this.opacitySave;
	}

	if( this.onclickHandler ){
		Acu.unregisterEvent( this.domElement, "click", this.onclickHandler );
	}
};


