(function(){

    var queryStr = function() {
        var scripts = document.getElementsByTagName('script');
        var index = scripts.length - 1;
        var myScript = scripts[index];
        var queryStr = myScript.src.replace(/^[^\?]+\??/,'');
        return queryStr;
    }();
    
    function isValid(aVariable) {
        return aVariable != null && typeof(aVariable) != "undefined";
    }

    function getCI() {
        var plc;
        if (typeof(PicLensContext) != 'undefined') {
            // Firefox
            // Can't use isValid here due to ReferenceError
            plc = new PicLensContext();
        } else if (window.ActiveXObject) { // IE
            try {
                plc = new ActiveXObject("PicLens.Context");
            } catch (e) {
                plc = null;
            }
        } else { // Safari
            var ax = "application/x-cooliris";
            if (navigator.mimeTypes[ax]) { // Safari
                plc = document.createElement("object");
                plc.style.width = 0;
                plc.style.height = 0;
                plc.type = ax;
                document.documentElement.appendChild(plc);
            } 
        }
        return plc;
    }

    function launchOrLightbox(event) {
        var client = getCI();
        
        if (isValid(client))
        {
            client.launch("","","");
        }
        else
        {
            Shadowbox.open({
                content:    "http://apps.cooliris.com/embed/cooliris.swf?" + queryStr,
                player:     "swf",
                height:     400,
                width:      600
            });
        }
    }

    document.write("<img id='cooliris_enabled_button_lightbox' style='cursor:pointer' src='http://js.cooliris.com/apps/white-small.gif' title='This site is Cooliris-enabled'>");

    document.getElementById('cooliris_enabled_button_lightbox').onclick = launchOrLightbox;
    
})();