$(document).ready(function(){
    var loading_linger_time = 1000; // In milliseconds

    $(".demo").css({visibility:"hidden"});

    $('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
    $('.nav').css('display','none'); // hides the nav initially

    $('ul.gallery_demo').galleria({
        history   : false, // deactivates the history object for bookmarking, back-button etc.
        clickNext : false, // helper for making the image clickable. Let's not have that in this example.
        insert    : '#main_image', // the containing selector for our main image.
        // If not found or undefined (like here), galleria will create a container
        // before the ul with the class .galleria_container (see CSS)
        onImage   : function(img,caption,thumb) {
            $('.nav').css('display','block');  // shows the nav when the image is showing
        },
        onThumb   : function(thumb_img) {
            // Corners need to be applied to image list-item after galleria has setup the thumbnails
            // in order to avoid intermittent element state conflicts with IE7
            thumb_img.parents("li:first").cornerz({radius:10});
        }
    });

    if(!$.browser.msie) {
        // fix mispositioned corners
        $('.galleria li div').css('position', 'static');
    } else {
        // fix mispositioned bottom corners
        fixMainImageCorners();

        $('#gallery_images').click(function(e){
            // catch click events and see if they are coming from the thumbnails
            if(e.srcElement.className == 'thumb') {
                // swap the main image
                $.galleria.activate($(e.srcElement).attr('rel'));
                // fix mispositioned bottom corners
                fixMainImageCorners();
            }
        });
    }

    // Main image container never changes, so set corners only once.
    $('#main_image').cornerz({radius:20});
    
    // End loading notice and glitch hiding
    setTimeout(function() {
        $("#loading").fadeOut();
    }, loading_linger_time);

    $(".demo").css({visibility:"visible"});
});

function fixMainImageCorners() {
    if($.browser.msie) {
        $('#main_image').height($('#main_image .galleria_wrapper img').height()-1);
    }
}
