$(document).ready(function(){

    // position 1
    var interval = setInterval("showPicture()", 10000);

    var ppal = getId();
    showPpal(ppal);

    $('#'+ppal).click(function(){

        var lastNid = getId();
        var nid = $(this).attr('id');

        hidePpal(lastNid);
        showPpal(nid);

        clearInterval(interval);
    });

    $('.foto_thumb_slide').click(function(){

        var lastNid = getId();
        var nid = $(this).attr('id');

        hidePpal(lastNid);
        showPpal(nid);

        clearInterval(interval);
    });

    //position 2
    var pictures = $('.cuadro_slide_2 .cont_slide2_bloque');

    if (pictures.length <= 3) {
        $('#flechas_slide2_bloque_next').hide();
        $('#flechas_slide2_bloque_prev').hide();
    }

    var totalRows;
    if (pictures.length <= 3)
        totalRows = pictures.length;
    else
        totalRows = 3;

    //show only for pictures
    for (var i=0; i < totalRows; i++) {
        $(pictures[i]).show();
        if (i == 0)
            var firstIndex = i;
        if (i == 2)
            var nextIndex = i+1;
    }

    $('#flechas_slide2_bloque_next').click(function(){
        //console.log('next first ' + firstIndex + ' next ' + nextIndex);

        if (nextIndex < pictures.length) {
            $(pictures[firstIndex]).hide();
            $(pictures[nextIndex]).show();

            if (pictures[nextIndex+1] != undefined) {
                nextIndex = nextIndex + 1;
                firstIndex = firstIndex + 1;
            }
        }
    });

    $('#flechas_slide2_bloque_prev').click(function(){
        //console.log('prev first ' + firstIndex + ' next ' + nextIndex);


        $(pictures[nextIndex]).hide();
        $(pictures[firstIndex]).show();

        if ((firstIndex -1) >= 0) {
            firstIndex = firstIndex -1;
            nextIndex = nextIndex -1;
        }

    });

});

function showPicture() {

    var last = $('.foto_gde_slide:visible').attr('id');
    lastNid = getId();

    //get all divs with foto_gde_slide class
    var elements = $('.foto_gde_slide');

    var lengthNid = null;
    var nid = null;
    var next = null;
    var index = null;

    for (var i=0; i < elements.length; i++) {
        //search next div
        if ($(elements[i]).attr('id') == last) {
            index = i+1;
            if (index == (elements.length)) {
                index = 0;
            }
            nid = getId(elements[index]);
            break;
        }
    }

    //hide last div
    hidePpal(lastNid);
    //show next div
    showPpal(nid);
}

function hidePpal(nid) {
    $('#'+nid+'_foto').hide();
    $('#'+nid+'_info').hide();
    $('#' + nid).removeClass('foto_thumb_slide_sel').addClass('foto_thumb_slide');
    $('#' + nid + '_img').removeClass('flecha_thumb_slide_sel').addClass('flecha_thumb_slide');
    $('#' + nid + '_img img').attr('src', 'images/flecha_thumb.gif');
    $('#' + nid + '_title').removeClass('txt_thumb_slide_sel').addClass('txt_thumb_slide');
}

function showPpal(nid) {
    $('#'+nid+'_foto').show();
    $('#'+nid+'_info').show();
    $('#' + nid).removeClass('foto_thumb_slide').addClass('foto_thumb_slide_sel');
    $('#' + nid + '_img').removeClass('flecha_thumb_slide').addClass('flecha_thumb_slide_sel');
    $('#' + nid + '_img img').attr('src', 'images/flecha_thumb_sel.gif');
    $('#' + nid + '_title').removeClass('txt_thumb_slide').addClass('txt_thumb_slide_sel');
}

function getId(selector) {
    if (selector == null) {
        var lengthNid = $('.foto_gde_slide:visible').attr('id').length - 5;
        return $('.foto_gde_slide:visible').attr('id').substr(0,lengthNid);
    } else {
        var lengthNid = $(selector).attr('id').length - 5;
        return $(selector).attr('id').substr(0,lengthNid);
    }
}

