/* Powered by Gaetano 'FiR3N3T' Padalino - gaetano<dot>padalino<at>gmail.com */

function show_pictures(tempgallery) {
    galleryArray = tempgallery;
    var while_pictures = 0; 

	while (while_pictures < galleryArray.length) {
		var new_image = new Element('img', {
		    'src': galleryArray[while_pictures],
		    'id': 'image_' + while_pictures,
		    'title': "Santa Tecla Hotel Palace",
			'events': {
			'click': function() {		
				    $('display_picture_img').innerHTML = "<img src=\"" + this.src.replace('_thumb', '') + "\" id=\"big_picture\" alt=\"" + while_pictures + "\" class=\"" + this.id + "\" />";
				    $('display_picture_container').fade(1);
				    $('big_picture').fade('hide');
					$('big_picture').fade(1);
					
					if(this.id.replace('image_', '')==0) {
						$('left').set('class', 'deactivated');	
						$('right').erase('class');
		            } else if (this.id.replace('image_', '') == galleryArray.length) {
						$('left').erase('class');	
						$('right').set('class', 'deactivated');	
					} else {
						$('left').set('class', 'activated');	
						$('right').erase('class');
					}

				}
			}
		});
		
		new_image.inject($('inside'));

	    var preload_image = new Element('img', {
	            'src': galleryArray[while_pictures][1],
	            'class': 'hide'
	        });


		preload_image.inject($('container'));
		
		while_pictures++;
}

if (galleryArray.length > 0) {

    $('display_picture_img').innerHTML = "<img src=\"" + galleryArray[0].replace('_thumb', '') + "\" id=\"big_picture\" alt=\"0\" class=\"image_0\" />";
    $('display_picture_container').fade(1);
    $('big_picture').fade(1);

    var id = "image_0";

    if (id.replace('image_', '') == 0) {
        $('left').set('class', 'deactivated');
        $('right').erase('class');
    } else if (id.replace('image_', '') == galleryArray.length) {
        $('left').erase('class');
        $('right').set('class', 'deactivated');
    } else {
        $('left').set('class', 'activated');
        $('right').erase('class');
    }
}
       
}
window.addEvent('domready', function() {

    show_pictures(tempgallery);

    $('display_picture_container').fade('hide');

    var vertical_moves = 0;
    var rows = Math.ceil(galleryArray.length / 3);

    if (rows > 5) {

        $('btn_up').addEvent('click', function(event) {
        if (!$('btn_up').hasClass('deactivated')) {
                vertical_moves--;
                $('btn_down').erase('class');
                $('inside').tween('margin-top', '-' + (62 * vertical_moves) + 'px');
                if (vertical_moves == 0) {
                    $('btn_up').set('class', 'deactivated');
                }
            }
        });


        $('btn_down').addEvent('click', function(event) {
        if (!$('btn_down').hasClass('deactivated')) {
                vertical_moves++;
                $('btn_up').erase('class');
                $('inside').tween('margin-top', '-' + (62 * vertical_moves) + 'px');
                if (vertical_moves == (rows - 5)) {
                    $('btn_down').set('class', 'deactivated');
                }
            }
        });
    } else {
    $('btn_up').set('class', 'deactivated');
    $('btn_down').set('class', 'deactivated');
    }

    var current_id = 1;

    $('left').addEvent('click', function() {
        if (!$('left').hasClass('deactivated')) {
            current_id = $('big_picture').get('class').replace('image_', '');
            current_id--;
            if (current_id > 0);
            {
                $('big_picture').fade('hide');
                $('big_picture').set('src', galleryArray[current_id].replace('_thumb', ''));
                $('big_picture').fade(1);
                $('big_picture').set('class', 'image_' + current_id);
                if (current_id == 0) { $('left').set('class', 'deactivated'); }
                if (current_id == (galleryArray.length - 1)) { $('right').erase('class'); }
            }
        }
    });

    $('right').addEvent('click', function() {
        if (!$('right').hasClass('deactivated')) {
            current_id = $('big_picture').get('class').replace('image_', '');
            current_id++;
            if (current_id > galleryArray.length);
            {
                $('big_picture').fade('hide');
                $('big_picture').set('src', galleryArray[current_id].replace('_thumb', ''));
                $('big_picture').fade(1);
                $('big_picture').set('class', 'image_' + current_id);
                if (current_id == 1) { $('left').erase('class'); }
                if (current_id == galleryArray.length - 1) { $('right').set('class', 'deactivated'); }
            }
        }
    });

/*
    $('display_picture_img').addEvent('click', function() {
        $('display_picture_container').fade(0);
        $('big_picture').fade(0);
    });
*/
});