//get the content tag
var content = document.getElementById("content");



/* IMAGES INFORMATION */

//get all the images tag in the content
var current_image = 0;
var all_images = content.getElementsByTagName("img");

//put the counter
var counter = document.getElementById("counter");
counter.innerHTML = "1"
//put the total
var total = document.getElementById("total");
total.innerHTML = all_images.length;

//put the caption
var caption = document.getElementById("caption");

/* */



/* GALLERY OR NOT? */

if(all_images.length == 0){
    var images = document.getElementById("images");
    images.innerHTML = "";
    images.style.display = "none";
}else{
    //hide a gallery
    hideGallery();

    //hide the p elements
    hidePElements();
    
    //create table elements
    var tab_elements;
    createTableElements();
    
    //get the tags images
    var image = document.getElementById("image");
    fillImageAndCaption();
}

/* */



/* FUNCTIONS */

//hide a gallery
function hideGallery(){
    var allPageTags = document.getElementsByTagName("div");
    for(i=0; i<allPageTags.length; i++){
        if(allPageTags[i].className == "gallery"){
            allPageTags[i].style.display = 'none';
        }
    } 
}

//hi de the p element
function hidePElements(){
    for(i=0;i<all_images.length;i++){
        var current_tag = all_images[i].parentNode.parentNode;
        if(current_tag.tagName == "P"){
            current_tag.style.display = 'none';
        }
    }
}

//create a table with all the elements
function createTableElements(){
    tab_elements = new Array();
    for(i=0;i<all_images.length;i++){
        var tab_current_img = new Array();
        var current_tag = all_images[i].parentNode.parentNode;
        if(current_tag.tagName == "P" || current_tag.tagName == "DT"){
            tab_current_img[0] = all_images[i].src;
            tab_current_img[1] = "";
        }else{
            tab_current_img[0] = all_images[i].src;
            tab_current_img[1] = current_tag.getElementsByTagName("P")[0].innerHTML;
        }
        tab_elements[i] = tab_current_img;
    }
}

//function to change image
function nextImage(){
    image.innerHTML = "";
    current_image++;
    if(current_image >= all_images.length) current_image = 0;
    fillImageAndCaption();
    counter.innerHTML = current_image+1;
}
function previousImage(){
    image.innerHTML = "";
    current_image--;
    if(current_image < 0) current_image = all_images.length - 1;
    fillImageAndCaption();
    counter.innerHTML = current_image+1;
}

//function that gives a value to the image and caption
function fillImageAndCaption(){
    image.innerHTML = "<img src='" + tab_elements[current_image][0] + "'/>";
    if(tab_elements[current_image][1].replace(/^\s+|\s+$/g,'') != ""){
        caption.style.display = "block";
        caption.innerHTML = tab_elements[current_image][1];
    }else{
        caption.innerHTML = "";
        caption.style.display = "none";
    }
}

/* */