function write_html_gallery(gallery_id) {
    /**
     * Crear los elementos
     */
    var gallery_div = new Element('div', {
        'id': 'gallery.' + gallery_id,
        'class': 'container slideshow_gallery_bloque'
    });
    gallery_div.setStyle({
        'display': 'none'
    });
    var slideshow_div = new Element('div', {
        'id': 'images_gallery_home.' + gallery_id,
        'class': 'slideshow_images'
    });
	var watermark_div = new Element('div', {
        'class': 'slideshow_watermark'
    });
	var watermark_img = new Element('img', {
        'src': watermark_url
    });
    var images = new Array();

    var json = ($('json_gallery.' + gallery_id).value).evalJSON(true);
    if (!json) {
        return;
    }
    for (var i = 0, len = json.length; i < len; ++i) {
        var json_image = $H(json[i]);
        images.push(new Element('img', {
            'id': 'img_gallery.' + gallery_id + '.' + json_image.get('id') ,
            'src': json_image.get('src') + '&' + json_image.get('max'),
            'title': json_image.get('title'),
            'footer': json_image.get('footer'),
            'alt': ''
        }));
    }
    var images_length = images.length;
    var slideshow_details_div = new Element('div', {
        'class': 'slideshow_details'
    });
    var slideshow_details_prev = new Element('div', {
        'class': 'slideshow_details_prev'
    });
	
    var slideshow_details_data = new Element('div', {
        'class': 'slideshow_details_gallery_data'
    });
    //('<span id="slideshow_popup_footer.' + gallery_id + '"></span>&nbsp;(<span id="slideshow_popup_current.' + gallery_id + '">1</span>&nbsp;' + translate_de + '&nbsp;' + (images_length) + ')')
    if (images_length>1) {
		slideshow_details_data.update(translate_imagen + '&nbsp;<span id="slideshow_popup_current.' + gallery_id + '">1</span>&nbsp;' + translate_de + '&nbsp;' + (images_length));
	}
	
	var slideshow_details_next = new Element('div', {
        'class': 'slideshow_details_next'
    });
    var slideshow_previous = new Element('button', {
        'id': 'slideshow_previous.' + gallery_id,
        'class': 'slideshow_previous img_button clickable'
    });
    var slideshow_next = new Element('button', {
        'id': 'slideshow_next.' + gallery_id,
        'class': 'slideshow_next img_button clickable'
    });
    var slideshow_previous_image = new Element('img', {
        'src': 'html/images/lightwindow/prevlabel.gif',
        'alt':''
    });
    var slideshow_next_image = new Element('img', {
        'src': 'html/images/lightwindow/nextlabel.gif',
        'alt':''
    });

    var slideshow_close = new Element('button', {
        'id': 'slideshow_close.' + gallery_id,
        'class': 'img_button'
    }).update(translate_close);


    /**
     * Crear los handlers
     */
    slideshow_previous.observe('click', manual_gallery.bindAsEventListener(this, gallery_id, 'prev', images_length));
    slideshow_next.observe('click', manual_gallery.bindAsEventListener(this, gallery_id, 'next', images_length));
    slideshow_close.observe('click', close_gallery.bindAsEventListener(this, gallery_id));

    /**
	 * Juntar los elementos
	 */
	gallery_div.appendChild(slideshow_div);
    for (var i = 0, len = images_length; i < len; ++i) {
        var image = images[i];
        image.setStyle({
            'display': 'none'
        });
        slideshow_div.appendChild(image);
    }
    gallery_div.appendChild(watermark_div);
	watermark_div.appendChild(watermark_img);
    gallery_div.appendChild(slideshow_details_div);
    slideshow_details_div.appendChild(slideshow_details_prev);
    slideshow_details_div.appendChild(slideshow_details_data);
    slideshow_details_div.appendChild(slideshow_details_next);
    slideshow_details_prev.appendChild(slideshow_previous);
    slideshow_previous.appendChild(slideshow_previous_image);
    slideshow_details_next.appendChild(slideshow_next);
	if (images_length > 1) {
		slideshow_next.appendChild(slideshow_next_image);
	}

    $('slideshow').appendChild(gallery_div);
    $('slideshow_close').appendChild(slideshow_close);

}

function open_gallery(gallery_id, current_id, max) {
    var gallery = $('gallery.' + gallery_id);
    if (!gallery) {
        write_html_gallery(gallery_id);
    }
    $('slideshow_close.' + gallery_id).show();
    var slideshow_previous = $('slideshow_previous.' + gallery_id);
    var slideshow_next = $('slideshow_next.' + gallery_id);
    slideshow_previous.hide();
    slideshow_next.hide();
    if (current_id!=0) {
        slideshow_previous.show();
    }
    if (current_id!=max) {
        slideshow_next.show();
    }
	var show_image = $('img_gallery.' + gallery_id + '.' + current_id);
	show_image.show();
	//$('slideshow_title').update(show_image.getAttribute('title'));
	$('slideshow_title').update(show_image.getAttribute('footer'));
	if ($('slideshow_popup_current.' + gallery_id)) {
		$('slideshow_popup_current.' + gallery_id).update(current_id + 1);
	}

    var json = ($('json_gallery.' + gallery_id).value).evalJSON(true);
    //$('slideshow_popup_footer.' + gallery_id).update(json[current_id].footer);

    var slideshow_overlay = $('slideshow_overlay');
    slideshow_overlay.setStyle({
		'height': ($('main_content').getHeight() + 20) + 'px'
    });
    $('slideshow_overlay').show();
    $('slideshow').show();
    $('gallery.' + gallery_id).show();
}

function close_gallery(e) {
    var gallery_id = arguments[1];
    $('images_gallery_home.' + gallery_id).select('img').each(function(img){
        if (img.visible()) {
            img.hide();
        }
    });
    $('slideshow_overlay').hide();
    $('slideshow').hide();

	if (Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5)) == 7) {
		$('slideshow_close.' + gallery_id).remove();
		$('gallery.' + gallery_id).remove();
	} else {
		$('slideshow_close.' + gallery_id).hide();
		$('gallery.' + gallery_id).hide();
	}
}

function manual_gallery(e){
    var gallery_id = arguments[1];
    var to = arguments[2];
    var max = arguments[3] - 1;

    var current_id = 0;
    var new_current_id = 0;

    var slideshow_previous = $('slideshow_previous.' + gallery_id);
    var slideshow_next = $('slideshow_next.' + gallery_id);
    slideshow_previous.hide();
    slideshow_next.hide();

    $('images_gallery_home.' + gallery_id).select('img').each(function(img){
        if (img.visible()) {
            current_id = parseFloat(img.id.split('.')[2]);

            if (to=='next') {
                if (current_id==max) {
                    slideshow_previous.show();
                    return;

                }
            } else {
                if (current_id==0) {
                    slideshow_next.show();
                    return;
                }
            }

            var show_image = $('img_gallery.' + gallery_id + '.' + current_id);
            show_image.fade({
                duration: .3
            });

            if (to=='next') {
                new_current_id = current_id+1;
                show_image = $('img_gallery.' + gallery_id + '.' + new_current_id);
            } else {
                new_current_id = current_id-1;
                show_image = $('img_gallery.' + gallery_id + '.' + new_current_id);
            }

            if (show_image) {
                Effect.Appear('img_gallery.' + gallery_id + '.' + new_current_id, {
                    duration: 0.3,
                    afterFinish: function() {

                        var json = ($('json_gallery.' + gallery_id).value).evalJSON(true);
                        //$('slideshow_popup_footer.' + gallery_id).update(json[new_current_id].footer);

                        //$('slideshow_title').update(show_image.getAttribute('title'));
                        $('slideshow_title').update(show_image.getAttribute('footer'));
                        $('slideshow_popup_current.' + gallery_id).update(new_current_id + 1);

                        if (new_current_id!=0) {
                            slideshow_previous.show();
                        }
                        if (new_current_id!=max) {
                            slideshow_next.show();
                        }


                    }
                });

            }
        }
    });

}
