jQuery(document).ready( function() {
    var currentCategory = null;
    var currentText = null;
    var jWriterHolder = null;
    
    var updatePhoto = function(url) {
        jQuery('#loader').fadeIn('fast');
        jQuery.getJSON(url, function(photo) {
            jQuery('#imageHolder img').attr('src', photo.fileLink);
            if (currentCategory != photo.category.id) {
                currentCategory = photo.category.id;
            }
            if (currentText != photo.text.id) {
                if (jWriterHolder != null) { jWriterHolder.endEffect(); }
                jQuery('#contentText').fadeOut('fast').html('');
                currentText = photo.text.id;
                jQuery('#contentText').html(photo.text.content).fadeIn('fast');
            }
            jQuery('#loader').hide();
        });
    }
    
    jQuery('#imageHolder img').click( function() {
        var url = 'next.php';
        updatePhoto(url);
        return false;
    });
    
    jQuery('#imageControl a').click( function() {
        var query = jQuery(this).attr('href');
        var url = '';
        switch (query) {
            case '#prev': url = 'previous.php'; break;
            case '#next': url = 'next.php';     break;
            default     : url = 'next.php';     break;
        }
        updatePhoto(url);
        return false;
    });
    
    updatePhoto('first.php');
});