function tryReadyScript(time_elapsed, onReadyFunction) {   
    // Continually polls to see if jQuery is loaded.
    if (typeof jQuery == "undefined") { // if jQuery isn't loaded yet...
        if (time_elapsed <= 5000) { // and we havn't given up trying...
            setTimeout("tryReadyScript(" + (time_elapsed + 200) + ", "+onReadyFunction+")", 200); // set a timer to check again in 200 ms.
        }
    } else {
        onReadyFunction();
    }
}

function initGallery() {
    if (typeof jQuery == 'undefined') {
        // It's responsible for waiting until jQuery loads before using it.
        getScript("resources_core/js/jquery.js");                    
    }
    
    tryReadyScript(0, galleryReady); 
}

function galleryReady() {
    if(jQuery().lightBox) {
        showGallery();
    } else {
        jQuery.getScript("resources_core/js/jquery/lightbox/jquery.lightbox.js", function() {
            showGallery();
        });
    }
}

function showGallery() {
    jQuery('head').append('<link rel="stylesheet" type="text/css" href="resources_core/js/jquery/lightbox/css/jquery.lightbox-0.5.css" />');
    jQuery("a.lightwindow,a[rel='lightwindow']").lightBox({
        fixedNavigation:true,
        imageLoading:  'resources_core/js/jquery/lightbox/images/lightbox-ico-loading.gif',
    	imageBtnClose: 'resources_core/js/jquery/lightbox/images/lightbox-btn-close.gif',
    	imageBtnPrev:  'resources_core/js/jquery/lightbox/images/lightbox-btn-prev.gif',
    	imageBtnNext:  'resources_core/js/jquery/lightbox/images/lightbox-btn-next.gif',
    	imageBlank:	   'resources_core/js/jquery/lightbox/images/lightbox-blank.gif'
    });
}
addOnLoad(initGallery);
