var galleryInstance;
var serverPath = 'cms/';
var galleryURI = 'modules/immobilien/immobilienGallery.inc.php';

function immobilien_objectDetail_init() {
	//console.log('test');
	Element.hide($('immobilienGallery'));

	var galleryLinkObject = $('openGallery');
	var params = galleryLinkObject.href.toQueryParams();
	galleryInstance = new CB_Immobilien_Gallery(params.objectID);

	$('openGallery').onclick = function() {
		galleryInstance.openGallery();
		return false;
	}
	$('closeGallery').onclick = function() {
		galleryInstance.closeGallery();
		return false;
	}
}

// Startfunktion in den EventLoader für windows.onLoad laden
document.observe('dom:loaded', immobilien_objectDetail_init);

/*
* Gallery Klasse zur Erstellung einer Gallery aus den Medienmanager Dateien
*/
var Gallery = Class.create();

var fileList;

function CB_Immobilien_Gallery(objectID) {
	this.objectID = objectID;

	// Methodenliste
	this.openGallery            = CB_Immobilien_Gallery.openGallery;
	this.createGallery          = CB_Immobilien_Gallery.createGallery;
	this.changeActualImage      = CB_Immobilien_Gallery.changeActualImage;
	this.closeGallery           = CB_Immobilien_Gallery.closeGallery;
	this.dropThumbnails         = CB_Immobilien_Gallery.dropThumbnails;
	this.getImageDataByPosition = CB_Immobilien_Gallery.getImageDataByPosition;
	this.scaleThumbnailUp       = CB_Immobilien_Gallery.scaleThumbnailUp;
	this.scaleThumbnailDown     = CB_Immobilien_Gallery.scaleThumbnailDown;
}

CB_Immobilien_Gallery.openGallery = function() {
	this.dropThumbnails();

	// Höhe und Breite des sichtbaren Browserbereichs ermitteln
	var windowWidth, windowHeight;
	$$('html').each(
		function(node) {
			windowWidth  = node.getDimensions().width;
			windowHeight = node.getDimensions().height;
		}
	);

	// GalleryPosition festlegen
	var galleryX, galleryY;
	galleryX = windowWidth / 2 - 350;
	galleryY = windowHeight / 2 - 500;

	var url = galleryURI + '?action=getFileList&objectID=' + this.objectID;
	var test = new Ajax.Request(
		url,
		{
			method: "get",
			onSuccess: this.createGallery
		}
	);

	$('immobilienGallery').style.left = galleryX + 'px';
	new Effect.Appear($('immobilienGallery'));
}

CB_Immobilien_Gallery.createGallery = function(request) {
	if(!request.responseText) {
		return 0;
	}

	// JSON parsen und alle gefundenen Locations markieren
	fileList = eval('(' + request.responseText + ')');

	// jedes Bild der Liste durchgehen und einen Listeneintrag inkl. Link und Eventhandler erzeugen
	$H(fileList.list.image).each(
		function(element) {
			key = element.key;
			var elementContent = element.value;
			// <li> Elemente aufbauen per scriptaculous
			imageElement = Builder.node('img', {src : serverPath + 'media/uploads/' + fileList.branchPath + "thumb" + elementContent.filename})

			/*
			imageElement.onmouseover = function() {
				CB_Immobilien_Gallery.scaleThumbnailUp(this, elementContent.fileID);
			}.bind(this);
			imageElement.onmouseout = function() {
				CB_Immobilien_Gallery.scaleThumbnailDown(this, elementContent.fileID);
			}.bind(this);
			*/

			var linkElement = Builder.node('a', {href : '#'}, [
				imageElement
			]);

			linkElement.onclick = function(el) {
				galleryInstance.changeActualImage(elementContent.fileID);
			}

			var listElement = Builder.node('li', [
				linkElement,
			]);

			$('galleryFileList').appendChild(listElement);

			//new Insertion.Bottom($('galleryFileList'), listElement);
		}
	);
}
CB_Immobilien_Gallery.changeActualImage = function(listEntry) {
	//console.log(listEntry);
	/*
	new Effect.Appear($('galleryMainImage'),
		{
			duration: 2.0,
			beforeStart: function(effect) {
				$('galleryMainImage').src = serverPath + 'media/uploads/' + branchPath + filename;
			}
		}
	);
	*/
	imageData = this.getImageDataByPosition(listEntry);

	new Effect.Opacity($('galleryMainImage'), {
		duration: 1.0,
		transition:Effect.Transitions.linear,
		from: 1.0,
		to: 0.0,
		afterFinish:function(effect) {
			//Hack to ensure image will be changed completely
			new Effect.Opacity($('galleryMainImage'), {
				duration: 0.2,
				transition:Effect.Transitions.linear,
				from: 0.0,
				to: 0.0,
				afterFinish:function(effect) {
					$('galleryMainImage').src = serverPath + 'media/uploads/' + fileList.branchPath + imageData.filename;
					$('galleryMainImage').onload = function() {
						new Effect.Appear($('galleryMainImage'));
					}
				}
			});
		}
	});
}
CB_Immobilien_Gallery.closeGallery = function() {
	//$('immobilienGallery').style.display = 'none';
	var _gallery = this;
	new Effect.Fade($('immobilienGallery'), {
		afterFinish: function(effect) {
			_gallery.dropThumbnails();
		}
	});
}
CB_Immobilien_Gallery.dropThumbnails = function() {
	$$('#galleryFileList li').each(
		function(node) {
			node.remove();
		}
	);
}
CB_Immobilien_Gallery.getImageDataByPosition = function(position) {
	var imageData;

	/*
	*  ___  __  __   __
	*   |  |  | | | |  |
	*   |  |__| |_| |__|
	*  fileList doesn't work here
	*/
	$H(fileList.list.image).each(function(element) {
		if(element.key == position) {
			imageData = element.value;
		}
	});

	return imageData;
}
CB_Immobilien_Gallery.scaleThumbnailUp = function(element, pos) {
//console.log(fileList);
	imageData = this.getImageDataByPosition(pos);
	/*
	new Rico.Effect.Size(
		element,
		element.width*1.2,
		element.height*1.2,
		250,
		5
	);*/

	new Effect.Scale(element,
		120,
		{
			fps: 75,
			scaleFromCenter: true,
			scaleContent: false,
			scaleMode: { originalHeight: imageData.dimension.height, originalWidth: imageData.dimension.width }
		}
	);

}
CB_Immobilien_Gallery.scaleThumbnailDown = function(element, pos) {
	imageData = this.getImageDataByPosition(pos);

	/*
	new Rico.Effect.Size(
		element,
		imageData.dimension.width,
		imageData.dimension.height,
		250,
		5
	);
	*/

	new Effect.Scale(element,
		100,
		{
			fps: 75,
			scaleFromCenter: true,
			scaleContent: false,
			scaleFrom: 120,
			scaleMode: { originalHeight: imageData.dimension.height, originalWidth: imageData.dimension.width }
		}
	);
}