var WIC = {
    GViewer: {},
    Ajax: {},
    
    registerRollOver: function (imageid, overurl, preload) {
        var image = $(imageid);
        var original_url = image.src;
        Event.observe(image, 'mouseover', function() { this.src = overurl; });
        Event.observe(image, 'mouseout', function() { this.src = original_url; });
        if (preload == true) {
            var dummyimage = new Image();
            dummyimage.src = overurl;
            dummyimage = null;
        }
    }
}

WIC.GViewer = {

    photos: [],

    loadPrevious: function(current_photo_id) {
        // get the previous photo id
        var index = this.photos.indexOf(current_photo_id) - 1;
        if(index == -1) {
            return;
        }
        WIC.Ajax.GViewer_loadPhoto(this.photos[index], 'gviewer_main_image', 'gviewer_mainview');
        this.set_selected('gviewer_small_'+this.photos[index]);
    },
    loadNext: function(current_photo_id) {
        // get the next photo id
        var index = this.photos.indexOf(current_photo_id) + 1;
        if(index + 1 > this.photos.length) {
            return;
        }
        WIC.Ajax.GViewer_loadPhoto(this.photos[index], 'gviewer_main_image', 'gviewer_mainview');
        this.set_selected('gviewer_small_'+this.photos[index]);
    },
    moveRight: function() {
        width = $('gviewer_bar').offsetWidth;
        pos = -.75 * width ;
        new Effect.Move($('gviewer_bar_internal'), {x: pos, y: 0, mode: 'relative'});
    },
    moveLeft: function() {
        width = $('gviewer_bar').offsetWidth;
        pos = .75 * width;
        new Effect.Move($('gviewer_bar_internal'), {x: pos, y: 0, mode: 'relative'});
    },
    set_selected: function(elem_id) {
        elem = $(elem_id);
        // deselect all
        var currently_selected = $$('.gviewer_small');
        currently_selected.each(function(x) {
            x.removeClassName('selected');
        });
        elem.addClassName('selected');
        this.centerPhoto(elem);
    },
    barHover: function(elem_id) {
        return;
        elem = $(elem_id);
        if(elem.hasClassName('selected')) {
            return;
        }
        new Effect.Opacity(elem, {from: 0.5, to: 1.0, duration: 0.3});
    },
    barHoverEnd: function(elem_id) {
        return;
        elem = $(elem_id);
        if(elem.hasClassName('selected')) {
            return;
        }
        new Effect.Opacity(elem, {from: 1.0, to: 0.5, duration: 0.3});
    },
    centerPhoto: function(elem) {
        // get width of the whole bar
        var width = $('gviewer_bar').offsetWidth;
        var pos = -1 * elem.offsetLeft + width/2 - elem.offsetWidth/2;
        new Effect.Move($('gviewer_bar_internal'), {x: pos, y: 0, mode: 'absolute'});
    }
}

WIC.Ajax = {
    
    // content that includes loading image while waiting for ajax content to load
    LoadingTPL: '',

    GViewer_loadPhoto: function(photo_id, div_to_replace, parent_div) {
        var url = 'ajax/wic_gallery_gviewer_fetch_image.php';
//        $(parent_div).update(this.LoadingTPL);
        new Ajax.Request(url, {
            parameters: { photo_id: photo_id },
            onSuccess: function(response, json) {
                var result = JSON.parse(response.responseText);
                $(parent_div).update(result.display);
                Effect.Appear('gviewer_main_image', {duration: 0.3, delay: 0.3});
            },
            onFailure: function(response) {
                $(parent_div).update('an error occurred');
            }
        });
    }
}
