$(document).ready(function() {
    // Get maximum value of photos.
    var array = $('#content').attr('class');   
    $.ajax({ 
        type: 'get',
        url: 'ajax.php?max='+array,
        success: function(data) {
            $('#max').html(data);
        }
    });    
    
    // Gallery init 
    $('#carousel-photo').jcarousel({ start: 1, scroll: 7 });
	
    // Thumbs behavior
    $('.thumb_link').click(function() {
        var array = $('#content').attr('class');        
        var id = $(this).attr('id').split('-')[1];
        $.ajax({ 
            type: 'get',
            async: false,
            url: 'ajax.php?id='+id+'&array='+array,
            success: function(data) { 
	            $('.current-photo img').fadeOut(200, function() {
	                $('.current-photo img').attr('src',data).fadeIn(200);
	                $('.current-photo').attr('id', "photo-"+id); 
	            });
        	} 
        });
        
        return false;
        
    });
    
    // prev Photo
    $('.prevLink').click(function() {
        var array = $('#content').attr('class');
        var currentPhoto = $('.current-photo').attr('id').split('-')[1];
        if (currentPhoto != '0') {
            var newId = parseInt(currentPhoto) - 1; 
            $.ajax({ 
                type: 'get',
                url: 'ajax.php?get=prev&id='+newId+'&array='+array,
                success: function(data) {
                    $('.current-photo img').fadeOut(200, function() {
                        $('.current-photo img').attr('src',data).fadeIn(200);
                        $('.current-photo').attr('id', "photo-"+newId); 
                    });
                } 
            });
            
            return false;
        }        
    });
    
    // next Photo
    $('.nextLink').click(function() {
        var max = $('#max').html();        
        var array = $('#content').attr('class');
        var currentPhoto = $('.current-photo').attr('id').split('-')[1];
        if (currentPhoto != max) {
            var newId = parseInt(currentPhoto) + 1; 
            $.ajax({ 
                type: 'get',
                url: 'ajax.php?id='+newId+'&array='+array,
                success: function(data) {
                    $('.current-photo img').fadeOut(200, function() {
                        $('.current-photo img').attr('src',data).fadeIn(200);
                        $('.current-photo').attr('id', "photo-"+newId); 
                    });
                } 
            });
            
            return false;
        }        
    });          
    
    // Link more
    $('#link-more').click(function() {
        $('#link-more').fadeOut(function() {
            $('#text-more').fadeIn();
        });
    });     
    
    // Menu item image 
    $(".menu-item").hover(function() {
        var obj = $(this);
        var id = $(this).attr('id');
        if ($(obj).attr('class') != 'menu-item active') {
            $('#menu-img img').fadeOut(100, function() {
                $('#menu-img img').attr('src', 'img/'+id+'-img.jpg').fadeIn(200);
                $('.menu-item').attr('class', 'menu-item'); // reset
                $(obj).addClass('active'); // make this active
            });
        }
    }, 
    function() { return false; });    
});
