$(document).ready( function() {

    $('#showcase .blocklink a').each(function() {
        this.href = '#';
    });

    display.loopStart(); // start rotation of display items

    expander.elemShowAll( 'img.ceti' ); // img.ceti
    expander.elemHideAll( '.ce', 0, '01' ); // div.ce
    expander.show( '01' ); // div.ce

    $('img.elem-close-parent').click( function() {
        var tag = $(this).parent().get(0);
        $(tag).hide();
    });
//    $('img').click( function() {
//        if ($(this).hasClass('elem-close-parent')) {
//            var tag = $(this).parent().get(0);
//            $(tag).hide();
//        }
//    });

} );

var expander = {
    idStrElem : "#ce",
    idStrImg : "#ceti",

    toggle : function( id ) {
         $(expander.idStrElem + id).slideToggle();
         if( $(expander.idStrImg + id).attr( 'alt' ) == '+' ) {
             $(expander.idStrImg + id).attr( 'src', 'images/c.png' );
             $(expander.idStrImg + id).attr( 'alt', '-' );
         } else {
             $(expander.idStrImg + id).attr( 'src', 'images/e.png' );
             $(expander.idStrImg + id).attr( 'alt', '+' );
         }
    },

    hide : function( id, speed ) {
         $(expander.idStrImg + id).attr( 'src', 'images/e.png' );
         $(expander.idStrImg + id).attr( 'alt', '+' );
         $(expander.idStrElem + id).hide(speed);
    },

    show : function( id, speed ) {
         $(expander.idStrImg + id).attr( 'src', 'images/c.png' );
         $(expander.idStrImg + id).attr( 'alt', '-' );
         $(expander.idStrElem + id).show(speed);
    },

    elemShowAll : function( elem, speed, exceptId ) {
        if( !speed || speed < 0 ) {speed = 0;}
        $(elem).each(function() {
            if( '#' + this.id == expander.idStrElem + exceptId ) {
                $('#' + this.id).hide(0);
            } else {
                $('#' + this.id).show(speed);
            }
        });
    },

    elemHideAll : function( elem, speed, exceptId ) {
        if( !speed || speed < 0 ) {speed = 0;}
        $(elem).each(function() {
            if( '#' + this.id == expander.idStrElem + exceptId ) {
                $('#' + this.id).show(0);
            } else {
                $('#' + this.id).hide(speed);
            }
        });
    }
}

var display = {
    loopEnabled : false,
    loopInterval : 8042,
    loopert : null,

    get : function() {
        return $('#showcase .list a.active').attr( 'id' ).replace( /\D+/g, '' );
    },

    set : function( id ) {
        $('#showcase .display .active').fadeOut(350, function() {
            $('#display-entry-' + id).fadeIn(500);
            $('#showcase .display .active').removeClass('active');
            $('#showcase .list a.active').removeClass('active');
            $('#display-entry-' + id).addClass('active');
            $('#display-link-' + id).addClass('active');
            if( display.loopEnabled ) {
                display.loopStop();
                display.loopStart();
            }
        });
    },

    loopStart : function() {
        display.loopStop();
        display.loopEnabled = true;
        display.loopert = window.setInterval( display.loop, display.loopInterval );
    },

    loopStop : function() {
        if( display.loopert != null ) {
            window.clearInterval( display.loopert );
        }
        display.loopert = null;
        display.loopEnabled = false;
    },

    loop : function() {
        if( !display.loopEnabled ) { return }
        var id = display.get();
        id++;
        if( id > 4 ) {id = 1}
        display.set( id );
    }
};
