/*	BysMoo WCMS, Copyright (c) ByYourSite since 2008. All rights reserved.

	This source file is free software; you can redistribute it and/or
	modify it under the terms of the MOO Public License as published
	by the MOO Development Group; either version 1.1 of the License, or
	(at your option) any later version.
*/
onDomLoad.addEvent(function(){
	lightbox.init();
});

var lightbox =
{
	links:  new Array(),
	window: null,
	image:  null,
	description:  null,

	container:  null,
	close:  null,
	prev_button:  null,
	next_button:  null,
	current_image_index: null,
	
	init: function() {
		
		lightbox.checkZoomImages();
		
		var links = MOO.getElementsByClassName('lightbox', 'a', document.body);
		for (var i=0;i<links.length;i++) {
			var link = links[i];
			Events.add(link, 'click', lightbox.galleryClick);
			link.lightbox_index = i;
			lightbox.links[i] = link;
		}
		lightbox.container = $A('div',{id:"lightbox_container"}, document.body);
		lightbox.window = $A('div',{id:"lightbox"}, lightbox.container);
		lightbox.image_container = $A('div',{id:"lightboximagecontainer"}, lightbox.window);
		lightbox.image = $A('img',{id:"lightboximage"}, lightbox.image_container);
		lightbox.description = $A('div',{id:"lightboxdescription"}, lightbox.window);
		lightbox.counter = $A('div',{id:"lightboxcounter"}, lightbox.window);
		lightbox.downloadlink = $A('a',{id:"lightboxdownloadlink",target:"_blank"}, lightbox.window);
		MOO.setNodeHTML(lightbox.downloadlink, $LNG["Download_high_resolution_image"]);
		
		lightbox.close = $A('div',{id:"lightboxclose"}, lightbox.window);

		Events.add(lightbox.close, 'click', lightbox.hide);
		Events.add(lightbox.container, 'click', lightbox.containerClick);
		Events.add(lightbox.image, 'load', lightbox.imageLoad);

		if (lightbox.links.length > 1) {
			lightbox.prev_button = $A('div',{id:"lightboxprev"}, lightbox.window);
			Events.add(lightbox.prev_button, 'click', lightbox.prev);
			MOO.setNodeHTML(lightbox.prev_button, $LNG["Prev"]);
			
			lightbox.next_button = $A('div',{id:"lightboxnext"}, lightbox.window);
			Events.add(lightbox.next_button, 'click', lightbox.next);
			MOO.setNodeHTML(lightbox.next_button, $LNG["Next"]);
		}
	},

	checkZoomImages: function() {
		var images = MOO.getElementsByClassName('zoom', 'img', document.body);
		for (var i=0;i<images.length;i++) {
			var image = images[i];
			var link = $C('a', {href: image.src, className: 'lightbox'});
			MOO.addNodeAfter(link, image);
			link.appendChild(image);
			console.log(image.src);
		}
	},
	
	galleryClick: function(e) {
		Events.stop(e);
		var node = e.target;
		if ('IMG' == e.target.tagName.toUpperCase() || 'SPAN' == e.target.tagName.toUpperCase()) {
			node = e.target.parentNode;
		}
		lightbox.current_image_index = node.lightbox_index;
		lightbox.show(node);
	},

	show: function(image_item) {
		lightbox.image.setAttribute('src', image_item.href);
		var span = $T('span', image_item);
		if (span[0]) {
			MOO.setNodeHTML(lightbox.description, span[0].innerHTML);
		} else {
			MOO.setNodeHTML(lightbox.description, '');
		}
		MOO.setNodeHTML(lightbox.counter, $LNG["Image"]+' '+(lightbox.current_image_index+1)+' '+$LNG["of"]+' ' + lightbox.links.length);
		
		var download = $T('i', image_item);
		if (download[0]) {
			lightbox.downloadlink.href = download[0].innerHTML;
		} else {
			lightbox.downloadlink.href = image_item.href;
		}
		addClassName(lightbox.container, 'show');
	},
	
	imageLoad: function(e) {
		var image = e.target;
		addClassName(lightbox.container, 'show');

		lightbox.fixImageNaturalWidthHeight(image);

		var container_width  = 542;
		var container_height = 382;
		
		lightbox.window.style.marginLeft = -Math.round(container_width/2)+'px';
		lightbox.window.style.marginTop  = -Math.round(container_height/2)+'px';
		
		var max_width  = 542;
		var max_height = 382;
		
		var image_width = image.naturalWidth;
		var image_height = image.naturalHeight;
		
		if (image_width > max_width || image_height > max_height) {
			if (image_width / image_height >= max_width / max_height) {
				image_height = max_height * (image_height / image_width);
				image_width  = max_width;
			} else {
				image_width  = max_width * (image_width / image_height);
				image_height = max_height;
			}
			image_width = Math.floor(image_width);
			image_height = Math.floor(image_height);
			image.style.width  = image_width  + 'px';
			image.style.height = image_height + 'px';
		}
		
		image.style.marginLeft = Math.floor((container_width/2)  - (image_width/2))+'px';
		image.style.marginTop  = Math.floor((container_height/2) - (image_height/2))+'px';
		
	},

	containerClick: function(e) {
		if ('lightbox_container' == e.target.id) {
			lightbox.hide(e);
		}
	},
	
	hide: function(e) {
		Events.stop(e);
		stripClassName(lightbox.container, 'show');
	},

	next: function() {
		if (lightbox.current_image_index >= lightbox.links.length-1) {
			lightbox.current_image_index = 0;
		} else {
			lightbox.current_image_index++;
		}
		lightbox.show(lightbox.links[lightbox.current_image_index]);
	},

	prev: function() {
		if (lightbox.current_image_index <= 0) {
			lightbox.current_image_index = lightbox.links.length-1;
		} else {
			lightbox.current_image_index--;
		}
		lightbox.show(lightbox.links[lightbox.current_image_index]);
	},

	fixImageNaturalWidthHeight: function(image) {
		if (image.naturalWidth == undefined) {
			var tmp_width = image.width;
			var tmp_height = image.height;

			image.removeAttribute('width');
			image.removeAttribute('height');

			image.setAttribute('naturalWidth', image.width);
			image.setAttribute('naturalHeight', image.height);

			image.setAttribute('width', tmp_width);
			image.setAttribute('height', tmp_height);
		}
	}

}
