﻿/*--------------------------------------------------------------------------
*
*  CoolIris Helpers 
*  rev. March 2 2009
*
*  Some functions adapted from:
*  http://lite.piclens.com/current/piclens.js
*
*  Requires AC_OETags.js for Flash detection
*--------------------------------------------------------------------------*/

// check if Cooliris Client is available
function hasCooliris() {
    // a flag to turn off the client
    if (this.DEBUG_NOCLIENT) {
        return false;
    }
    // check if the bridge has already been defined
    var clientExists = false;
    if (this.PLC) {
        clientExists = true;
    } else if (window.piclens && window.piclens.launch) {
        this.PLC = window.piclens;
        clientExists = true;
    } else { // if not, try to define it here...
        var context = null;
        if (typeof PicLensContext != 'undefined') { // Firefox
            context = new PicLensContext();
        } else {
            try {
                context = new ActiveXObject("PicLens.Context"); // IE
            } catch (e) {
                if (navigator.mimeTypes['application/x-cooliris']) { // Safari
                    context = document.createElement('object');
                    context.style.display = 'none';
                    context.width = 0;
                    context.height = 0;
                    context.type = 'application/x-cooliris';
                    document.documentElement.appendChild(context);
                } else {
                    context = null;
                }
            }
        }
        this.PLC = context;
        if (this.PLC) {
            clientExists = true;
        }
    }

    if (clientExists) { // check the version number
        if (this.BROWSER_SAF) { // for Safari, we just return true (the first v. was 1.8)
            return true;
        }
        var version;
        try { version = this.PLC.version; } catch (e) { return false; }

        var parts = version.split('.'); // minimum ver. is: 1.6.0.824
        if (parts[0] > 1) {			    // a ver. 2.X product
            return true;
        } else if (parts[0] == 1) {	    // a 1.X product
            if (parts[1] > 6) {		    // a 1.7.X product
                return true;
            } else if (parts[1] == 6) { // a 1.6 product
                if (parts[2] > 0) {	    // a 1.6.1.X product
                    return true;
                } else if (parts[2] == 0) {
                    if (parts[3] >= 824) { // 1.6.0.824 or newer...
                        return true;
                    }
                }
            }
        }
        return false; // a 0.X product
    } else {
        return false;
    }
}

function checkForPluginAndLaunchIfPossible(url, guid) {
    // if we have the correct version of piclens, pass it onto the client and do not use LITE
    if (this.hasCooliris()) {
        if (typeof (guid) != "undefined") {
            this.PLC.launch(url, 'uid', guid);
        } else {
            this.PLC.launch(url, '', '');
        }
        return true; // launched!
    }
    return false;
}

function openModal(feed) {    
    var feedUrl = feed;
    var browser = navigator.appName;
    var b_version = navigator.appVersion;
    var version = parseFloat(b_version);
    var rand = "";

    if (browser == "Microsoft Internet Explorer") {
        rand = "?" + Math.round(Math.random() * 1000000);
    }

    if (!checkForPluginAndLaunchIfPossible(feedUrl, '')) {
        if (!hasFlashReqVersion) {
            if (window.confirm("Adobe Flash Player 8 or CoolIris plugin is needed to view galleries on this site. Click OK to get Adobe Flash Player.")) {
                window.open('http://www.adobe.com/go/getflashplayer');
            }
            return false;
        }           
        $j('#showcaseModal').show();
        var cnhCoolIrisBox = $j('#cnhCoolIrisBox');
        var coolIrisHtmlArray = new Array();
        coolIrisHtmlArray.push('<object id="coolIris" width="925" height="410" type="application/x-shockwave-flash" data="http://apps.cooliris.com/embed/cooliris.swf' + rand + '">');
        coolIrisHtmlArray.push('<param name="movie" value="http://apps.cooliris.com/embed/cooliris.swf' + rand + '" />');
        coolIrisHtmlArray.push('<param name="flashvars" value="feed=');
        coolIrisHtmlArray.push(feedUrl);
        coolIrisHtmlArray.push('" />');
        coolIrisHtmlArray.push('<param name="allowFullScreen" value="true" />');
        coolIrisHtmlArray.push('<param name="allowScriptAccess" value="always" />');
        //        coolIrisHtmlArray.push('<embed type="application/x-shockwave-flash" src="http://apps.cooliris.com/embed/cooliris.swf" width="925" height="450" flashvars="');
        //        coolIrisHtmlArray.push(feedUrl);
        //        coolIrisHtmlArray.push('" allowFullScreen="true" allowScriptAccess="always" name="coolIris"></embed>');
        coolIrisHtmlArray.push('<');
        coolIrisHtmlArray.push('/object>');
        var coolIrisHtml = coolIrisHtmlArray.join('');

        cnhCoolIrisBox.html(coolIrisHtml);
    }
}

function closeModal() {
    var cnhCoolIrisBox = $j('#cnhCoolIrisBox');
    cnhCoolIrisBox.html('');
    $j('#showcaseModal').hide();
}


// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// -----------------------------------------------------------------------------

// Version check based upon the values entered above in "Globals"
var hasFlashReqVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);