﻿iRIFR = 0;

(function ($) {
    $.fn.rifr = function (options) {
        // Set defaults
        var settings = {
            flashPath: "/UI/Flash/rifr.swf",
            flashHeight: 33,
            flashWidth: 500,
            fontSize: 1
        };
        // If options exist - merge with defaults
        this.each(function () {
            if (options) {
                opts = $.extend(settings, options);
            }
            // +1 to iRIFR variable
            iRIFR++
            // Check if flash is installed
            var hasFlash = false;
            try {
              var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
              if(fo) hasFlash = true;
            }catch(e){
              if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
            }
            // RIFR code
            // Set default text
            if (hasFlash = true) {
                var headingText = $(this).html();
                var elementClass = "rifr" + iRIFR;            
                if (headingText != null) {                
                    // HTML/Flash encode characters in string
                    headingText = headingText.replace("'", "&#39;").replace("&amp;", "%26");
                    flashPathBuilder = opts.flashPath + "?textInput=" + headingText + "&targetElement=\"" + elementClass + "\"&fontSize=" + opts.fontSize;
                    flashHTMLBuilder = "<OBJECT width='" + opts.flashWidth + "' height='" + opts.flashHeight + "' id='" + elementClass + "'>" +
                        "<param name='allowScriptAccess' value='always' />" +
                        "<PARAM name='movie' value='" + flashPathBuilder + "'></PARAM>" +
                        "<PARAM name='wmode' value='transparent'></PARAM>" + 
                        "<EMBED src='" + flashPathBuilder + "' name='" + elementClass + "' type='application/x-shockwave-flash' wmode='transparent' width='" + opts.flashWidth + "' height='" + opts.flashHeight + "' />" +
                        "</OBJECT>";
                    // Replace text with Flash
                    $(this).addClass("rifr " + elementClass).html(flashHTMLBuilder);
                }
            }
        });
    };
})(jQuery);

// Set height of flash element
function setFlashDimensions(flashHeight, flashWidth, targetElement) {
    var targetElement = targetElement.replace('"', '');
    //alert(targetElement);
    $("." + targetElement + " object, ." + targetElement + " embed").attr({
        width: flashWidth, 
        height: flashHeight
    });
}
