var album = {
    elements:4,
    count:0,
    segmentLength:140,
    urlGaleria:'/ajax/galeria/id_album/',
    init:function(){
        $('.album').each(function(){
            album.count++;
        });
        if(album.count > album.elements){
            this.enableRight();
        }
        this.mouseRight();
        this.mouseLeft();
    },
    
    mouseRight:function(){
        $('#albumArrowRight').click(function(){
            album.disableLeft();
            album.disableRight();
            var left = parseInt($('#albumSlide').css('left')) - album.segmentLength;
            var position = left/album.segmentLength;
            var difference = album.count - album.elements;
            var move = difference + position;
            if(move >= 0){
                if(move > 0){
                    album.enableRight();
                }
                $('#albumSlide').animate({
                    'left': left + 'px'
                }, 633);
            }
            album.enableLeft();
        })
        .css('cursor', 'pointer');
    },
    
    enableRight:function(){
        $('#albumArrowRight').css('display', 'block');
    },
    
    disableRight:function(){
        $('#albumArrowRight').css('display', 'none');
    },
    
    mouseLeft:function(){
        $('#albumArrowLeft').click(function(){
            album.disableLeft();
            album.disableRight();
            var left = parseInt($('#albumSlide').css('left'));
            if(left < 0){
                left += album.segmentLength;
                $('#albumSlide').animate({
                    'left': left + 'px'
                }, 633);
                album.enableRight();
            }
            if(left < 0){
                album.enableLeft();
            }else{
                album.enableRight();
            }
        })
        .css('cursor', 'pointer');
    },
    
    enableLeft:function(){
        $('#albumArrowLeft').css('display', 'block');
    },
    
    disableLeft:function(){
        $('#albumArrowLeft').css('display', 'none');
    },
    
    showGaleria:function(id_album){
        $.getJSON(album.urlGaleria+id_album, function(result) {
            $('#galeriaLayout').html('');
            for(i in result){
                var width = result[i].width + 2;
                var height = result[i].height + 2;
                var top = 164 - result[i].height;
                $('#galeriaLayout')
                .append(
                    $('<div>')
                    .addClass('left galeria')
                    .html(
                        $('<div>')
                        .addClass('imgLayer')
                        .html(
                            $('<div>')
                            .addClass('img')
                            .css({
                                width:width+'px', 
                                height:height+'px', 
                                top:top
                            })
                            .html(
                                $('<a>')
                                .attr({
                                    href:'/upload/galeria/'+result[i].image, 
                                    title:result[i].title, 
                                    rel:'galeria'
                                })
                                .html(
                                    $('<img>')
                                    .attr({
                                        src:'/upload/galeria/thumb_'+result[i].image, 
                                        alt:result[i].title
                                    })
                                    .css({
                                        width:result[i].width+'px', 
                                        height:result[i].height+'px'
                                    })
                                    )
                                )
                            )
                        )
                    .append(
                        $('<div>')
                        .addClass('title')
                        .html(result[i].title)
                        )
                    );
            }
            $("a[rel='galeria']").colorbox();
        });
    }
}
