(function(){
    var imageUrls = ['http://cooliris.com/static/images/badges/cooliris_enabled_lg.png',
                     'http://cooliris.com/static/images/badges/cooliris_enabled_sm.png'];

    // Width, height corresponding to images above
    var imageSizes = [[125, 50],
                      [88, 31]];
                      
    var fallbackUrl = "http://www.cooliris.com";

    var imageIndex = function() {
        var scripts = document.getElementsByTagName('script');
        var index = scripts.length - 1;
        var myScript = scripts[index];
        var queryStr = myScript.src.replace(/^[^\?]+\??/,'');
        var imageIndex = parseInt(queryStr.replace("image=", ''), 10);
        return imageIndex;
    }();
    
    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 launchOrDownload(event) {
        var client = getCI();
        if (isValid(client))
        {
            if (window.ActiveXObject) {
                var feed;
                var head = document.getElementsByTagName('head')[0];
                var links = head.getElementsByTagName('link'),
                    index = 0,
                    link;
                    
                while (link = links[index++]) {
                    if (link.getAttribute("type") == "application/rss+xml") {
                        feed = link.getAttribute("href");
                        break;
                    }
                }
                client.launch(feed,"","");
                return false;
            }
            else
            {
                event.preventDefault();
                client.launch("","","");
            }
        }
    }
    
    var buttonID = 'cooliris_enabled_button';
    
    var buttonHTML = "<a href='" + fallbackUrl + "' target='_blank' " + 
                        "id='" + buttonID + "' style='outline:none'>" + 
                           "<img style='border:0'" + 
                               " width='" + imageSizes[imageIndex][0] + "'" +
                               " height='" + imageSizes[imageIndex][1] + "'" +
                               " src='" + imageUrls[imageIndex] + "'" +
                               " title='This site is Cooliris-enabled'>" + 
                     "</a>";

    document.write(buttonHTML);

    document.getElementById(buttonID).onclick = launchOrDownload;
    
})();