var ic_gallery_boxes = Array();

function ic_gallery_add(ic_gallery_parent_contener) {
	ic_gallery_parent_contener = $(ic_gallery_parent_contener);
	ic_gallery_boxes.push(ic_gallery_parent_contener);
	ic_gallery_parent_contener.addEvent('click', function() {
		ic_gallery_overlay();
		ic_gallery_show(ic_gallery_parent_contener);
		ic_gallery_load_image();
	});
}

function ic_gallery_overlay() {
	// Création de l'overlay
	
	//if (Browser.Engine.trident && Browser.Engine.version <= 4) {
	if (Browser.ie6) {
		// IE 6 et autres qui n'ont pas de position fixed
		ic_gallery_overlay_div = new Element('div', {
			'styles': {
				'background-color': '#000000',
				'opacity': '0.5',
				'cursor': 'pointer',
				'position': 'absolute',
				'top': window.getScroll().y,
				'left': window.getScroll().x,
				'width': window.getSize().x,
				'height': window.getSize().y,
				'z-index': '100'
			}
		});		
	}
	else {
		ic_gallery_overlay_div = new Element('div', {
			'styles': {
				'background-color': '#000000',
				'opacity': '0.5',
				'cursor': 'pointer',
				'position': 'fixed',
				'top': 0,
				'left': 0,
				'width': '100%',
				'height': '100%',
				'z-index': '100'
			}
		});
	}
	
	// Destruction lors du clique sur l'overlay
	ic_gallery_overlay_div.addEvent('click', function() {
		ic_gallery_zoom_out(ic_gallery_contener, function() {
			$('ic_gallery_contener').destroy();
			ic_gallery_overlay_div.destroy();
			ic_gallery_parent_contener.setStyle('z-index', ic_gallery_parent_contener.retrieve('old-z-index'));
		});
	});
	
	ic_gallery_overlay_div.inject(document.body);
}

function ic_gallery_show(parent_contener) {
	ic_gallery_parent_contener = parent_contener;
	ic_gallery_parent_contener_coordinates = ic_gallery_parent_contener.getCoordinates();
	ic_gallery_parent_contener_size = ic_gallery_parent_contener.getSize();

	// Mise à l'avant plan de l'image
	ic_gallery_parent_contener.store('old-z-index', ic_gallery_parent_contener.getStyle('z-index'));

	ic_gallery_parent_contener.setStyles({
		'position': 'relative',
		'z-index': '100'
	});

	
	// Création du contener de l'image
	ic_gallery_contener = new Element('div', {
		'id': 'ic_gallery_contener',
		'styles': {
			'position': 'absolute',
			'top': ic_gallery_parent_contener_coordinates.top,
			'left': ic_gallery_parent_contener_coordinates.left,
			'width': ic_gallery_parent_contener_size.x,
			'height': ic_gallery_parent_contener_size.y,
			'background': 'url(\'http://gif.inter-chat.fr/images/loading2.gif\') no-repeat center center',
			'z-index': '101'
		}
	});
	
	ic_gallery_contener.inject(document.body);
}

function ic_gallery_load_parameters() {
	var ic_gallery_parameters_array = ic_gallery_parent_contener.get('rel').split('|');
	//var ic_gallery_parameters = new Hash();
	var ic_gallery_parameters = new Object();
	ic_gallery_parameters_array.each(function(item, index) {
		var regExp = /([^:]+)\:(.*)/;
		regExp.exec(item);
		//ic_gallery_parameters.set(RegExp.$1, RegExp.$2);
		ic_gallery_parameters[RegExp.$1] = RegExp.$2;
	});
	return ic_gallery_parameters;
}

function ic_gallery_load_image() {
	var ic_gallery_source;

	ic_gallery_parameters = ic_gallery_load_parameters();

	//var ic_gallery_image = new Asset.image(ic_gallery_parameters.get('original'), {
	var ic_gallery_image = new Asset.image(ic_gallery_parameters.original, {
		onload: function() {
			//ic_gallery_zoom_in(this, ic_gallery_parameters.get('original'));	
			ic_gallery_zoom_in(this, ic_gallery_parameters.original);	
			ic_gallery_parent_contener.setStyle('z-index', ic_gallery_parent_contener.retrieve('old-z-index'));
		},
		onerror: function() {
			ic_gallery_contener.destroy();
			ic_gallery_overlay_div.destroy();
			ic_gallery_parent_contener.setStyle('z-index', ic_gallery_parent_contener.retrieve('old-z-index'));
		}
	});
}

function ic_gallery_zoom_in(ic_gallery_image, element) {
	var width = ic_gallery_image.width;
	var height = ic_gallery_image.height;
	
	var left = window.getScroll().x + ((window.getSize().x - width) / 2).toInt();
	var top = window.getScroll().y + ((window.getSize().y - height) / 2).toInt();
	
	ic_gallery_image.set({
		styles: {
			'background-color': '#000000',
			'cursor': 'pointer',
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'width': '100%',
			'height': '100%',
			'z-index': '101'
		}
	});
	ic_gallery_contener.setStyle('opacity', 0);
	ic_gallery_image.inject(ic_gallery_contener);

	var zoom_in = new Fx.Morph(ic_gallery_contener);

	zoom_in.start({
		'width' : width,
		'height' : height	,
		'top': top,
		'left': left,
		'border': '5px solid #000000',
		'opacity': 1,
		'z-index': '101'
	}).chain(ic_gallery_legend_in);
}

function ic_gallery_zoom_out(ic_gallery_contener, fn) {
	document.removeEvent('keydown', ic_gallery_keyboard_events);
	ic_gallery_contener_left.destroy();
	ic_gallery_contener_right.destroy();
	
	if ($('ic_gallery_legend_div')) {
		$('ic_gallery_legend_div').destroy();
	}

	var zoom_out = new Fx.Morph(ic_gallery_contener);

	zoom_out.start({
		'width' : ic_gallery_parent_contener_size.x,
		'height' : ic_gallery_parent_contener_size.y	,
		'top': ic_gallery_parent_contener_coordinates.top,
		'left': ic_gallery_parent_contener_coordinates.left,
		'opacity': 0
	}).chain(fn	);
}

function ic_gallery_legend_in() {
	var ic_gallery_contener_coordinates = $(ic_gallery_contener).getCoordinates();
	var ic_gallery_contener_size = $(ic_gallery_contener).getSize();

	// Création des div directionnels
	ic_gallery_contener_left = new Element('div', {
		'styles': {
			'position': 'absolute',
			'cursor': 'pointer',
			'width': '50%',
			'height': '100%',
			'left': 0,
			'background-repeat': 'no-repeat',
			'background-position': 'center -15px',
			'background-image': 'url(\'http://gif.inter-chat.fr/images/blue/smileys/fleche2.gif\')',
			'z-index': '101'
		},
		'events': {
			'mouseover': function() {
				this.setStyle('background-position', 'center left');
			},
			'mouseout': function() {
				this.setStyle('background-position', 'center -15px');
			},
			'click': function() {
				ic_gallery_change(-1);
			}
		},
		'title': translate_title_legend
	}).inject(ic_gallery_contener);

	ic_gallery_contener_right = new Element('a', {
		'styles': {
			'position': 'absolute',
			'cursor': 'pointer',
			'width': '50%',
			'height': '100%',
			'right': 0,
			'background-repeat': 'no-repeat',
			'background-position': 'center -15px',
			'background-image': 'url(\'http://gif.inter-chat.fr/images/blue/smileys/fleche.gif\')',
			'z-index': '101'
		},
		'events': {
			'mouseover': function() {
				this.setStyle('background-position', 'center right');
			},
			'mouseout': function() {
				this.setStyle('background-position', 'center -15px');
			},
			'click': function() {
				ic_gallery_change(1);
			}
		},
		'title': translate_title_legend
	}).inject(ic_gallery_contener);

	ic_gallery_keyboard_events = function(e) {
		if (e.code == 37) {
			ic_gallery_change(-1);
		}
		else if (e.code == 39) {
			ic_gallery_change(1);
		}
		else if (e.code == 27) {
			ic_gallery_zoom_out(ic_gallery_contener, function() {
				ic_gallery_contener.destroy();
				ic_gallery_overlay_div.destroy();
				ic_gallery_parent_contener.setStyle('z-index', ic_gallery_parent_contener.retrieve('old-z-index'));
			});
		}
	};
	
	document.addEvent('keydown', ic_gallery_keyboard_events);
	
			/*
	var faces = ic_gallery_parameters.get('faces');
	if (faces) {
		var faces_array = faces.split(';');
		var offset = 0;
		
		while (faces_array[2 + offset]) {
			new Element('div', {
				'text': faces_array[4 + offset],
				'styles': {
					'position': 'absolute',
					'cursor': 'pointer',
					'width': faces_array[2 + offset] + 'px',
					'height': faces_array[3 + offset] + 'px',
					'top': faces_array[1 + offset] + 'px',
					'left': faces_array[0 + offset] + 'px',
					'border': '1px solid #000000',
					'color': '#000000'
				}
			}).inject(ic_gallery_contener);
			offset += 5;	
		}
	}	
	*/
	
	//if (!ic_gallery_parameters.get('legend')) {
	if (!ic_gallery_parameters.legend) {
		return;
	}

	var ic_gallery_legend_div = new Element('div', {
		'id': 'ic_gallery_legend_div',
		'styles': {
			'position': 'absolute',
			'top': ic_gallery_contener_coordinates.bottom - 6,
			'left': ic_gallery_contener_coordinates.left,
			'width': ic_gallery_contener_size.x - 22,
			'border': '5px solid #000000',
			'border-top': '1px solid #000000',
			'background-color': '#FFFFFF',
			'color': '#666',
			'font-family': 'Verdana, Arial, Geneva, Helvetica, sans-serif',
			'font-size': '10px',
			'font-weight': 'bold',
			'font-style': 'italic',
			'text-align': 'center',
			'padding': '6px',
			'opacity': 0,
			'z-index': '101'
		}
	});
	//ic_gallery_legend_div.set('html', ic_gallery_parameters.get('legend'));
	ic_gallery_legend_div.set('html', ic_gallery_parameters.legend);
	ic_gallery_legend_div.inject(ic_gallery_contener, 'after').morph({ opacity: [0, 1] });
}

function ic_gallery_change(direction) {
	ic_gallery_zoom_out(ic_gallery_contener, function() {
		ic_gallery_parent_contener.setStyle('z-index', ic_gallery_parent_contener.retrieve('old-z-index'));
		ic_gallery_contener.destroy();
		
		var index = ic_gallery_boxes.indexOf(ic_gallery_parent_contener);
		if (ic_gallery_boxes.length == index + direction) {
			ic_gallery_parent_contener = ic_gallery_boxes[0];
		}
		else if (index + direction < 0) {
			ic_gallery_parent_contener = ic_gallery_boxes[ic_gallery_boxes.length - 1];
		}
		else {
			ic_gallery_parent_contener = ic_gallery_boxes[index + direction];
		}
		
		ic_gallery_show(ic_gallery_parent_contener);
		ic_gallery_load_image();
	});
}
