﻿var FMP_Onload = function(msg) {
    FlashAudioPlayer.__FlashIsReady();
}

var FlashAudioPlayer = {
    __flash: null,
    __ua: function () {
        var UNDEF = "undefined",
		    OBJECT = "object",
		    SHOCKWAVE_FLASH = "Shockwave Flash",
		    SHOCKWAVE_FLASH_AX = "ShockwaveFlash.ShockwaveFlash",
		    FLASH_MIME_TYPE = "application/x-shockwave-flash",
		    EXPRESS_INSTALL_ID = "SWFObjectExprInst",
		    ON_READY_STATE_CHANGE = "onreadystatechange",

            win = window,
		    doc = document,
		    nav = navigator;

        var w3cdom = typeof doc.getElementById != UNDEF && typeof doc.getElementsByTagName != UNDEF && typeof doc.createElement != UNDEF,
			u = nav.userAgent.toLowerCase(),
			p = nav.platform.toLowerCase(),
			windows = p ? /win/.test(p) : /win/.test(u),
			mac = p ? /mac/.test(p) : /mac/.test(u),
			webkit = /webkit/.test(u) ? parseFloat(u.replace(/^.*webkit\/(\d+(\.\d+)?).*$/, "$1")) : false, // returns either the webkit version or false if not webkit
			ie = ! +"\v1", // feature detection based on Andrea Giammarchi's solution: http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html
			playerVersion = [0, 0, 0],
			d = null;
        if (typeof nav.plugins != UNDEF && typeof nav.plugins[SHOCKWAVE_FLASH] == OBJECT) {
            d = nav.plugins[SHOCKWAVE_FLASH].description;
            if (d && !(typeof nav.mimeTypes != UNDEF && nav.mimeTypes[FLASH_MIME_TYPE] && !nav.mimeTypes[FLASH_MIME_TYPE].enabledPlugin)) { // navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin indicates whether plug-ins are enabled or disabled in Safari 3+
                plugin = true;
                ie = false; // cascaded feature detection for Internet Explorer
                d = d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
                playerVersion[0] = parseInt(d.replace(/^(.*)\..*$/, "$1"), 10);
                playerVersion[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
                playerVersion[2] = /[a-zA-Z]/.test(d) ? parseInt(d.replace(/^.*[a-zA-Z]+(.*)$/, "$1"), 10) : 0;
            }
        }
        else if (typeof win.ActiveXObject != UNDEF) {
            try {
                var a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
                if (a) { // a will return null when ActiveX is disabled
                    d = a.GetVariable("$version");
                    if (d) {
                        ie = true; // cascaded feature detection for Internet Explorer
                        d = d.split(" ")[1].split(",");
                        playerVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
                    }
                }
            }
            catch (e) { }
        }
        return { w3: w3cdom, pv: playerVersion, wk: webkit, ie: ie, win: windows, mac: mac };
    } (),
    __FlashIsReady: function () {
        var fl = document.getElementById("MiniplayerIE");
        if (fl == null || typeof (fl.FMP_play) != "function")
            fl = document.getElementById("Miniplayer");
        FlashAudioPlayer.__flash = fl;

        if (FlashAudioPlayer.__instance != null && FlashAudioPlayer.__instance.__playWhenReady != null) {
            var item = FlashAudioPlayer.__instance.__playWhenReady
            FlashAudioPlayer.__instance.PlayFile(item.uri, item.options);
        }
    },
    __instance: null,
    __loading: false,
    PlayUri: function (uri, _options) {
        if (FlashAudioPlayer.__loading == true) {
            var f = function () { FlashAudioPlayer.PlayUri(uri, _options); };
            setTimeout(f, 100);
            return;
        }

        FlashAudioPlayer.__loading = true;

        if (FlashAudioPlayer.__instance == null) {
            FlashAudioPlayer.__instance = new FlashAudioPlayer.__wrapper();
        }

        FlashAudioPlayer.__instance.PlayFile(uri, _options);

        FlashAudioPlayer.__loading = false;
    },
    StopPlaying: function () {
        FlashAudioPlayer.__instance.Stop();
    },
    __wrapper: function () {
        this.__flash == null;
        this.__playWhenReady = null;

        var div = document.createElement("div");
        document.body.appendChild(div);

        var obj = document.createElement("object");
        div.appendChild(obj);

        var np = function (name, value) {
            var p = document.createElement("param");
            p.setAttribute("name", name);
            p.setAttribute("value", value);
            obj.appendChild(p);
        }

        obj.setAttribute("classid", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000");
        obj.setAttribute("width", "1");
        obj.setAttribute("height", "1");
        obj.setAttribute("align", "middle");
        obj.setAttribute("id", "MiniplayerIE");

        np("movie", "images/default/Miniplayer.swf");
        np("quality", "best");
        np("bgcolor", "#ffffff");
        np("play", "true");
        np("loop", "false");
        np("wmode", "transparent");
        np("scale", "showall");
        np("menu", "true");
        np("devicefont", "false");
        np("salign", "");
        np("allowScriptAccess", "always");


        var embed, AppendEmbedToObject;

        if (FlashAudioPlayer.__ua.ie) {
            var embedElement = function () {
                this.__arr = ["<embed"];
            };
            embedElement.prototype.toString = function () {
                this.__arr.join(" ");
                return this.__arr.join(" ") + "/>";
            }
            embedElement.prototype.setAttribute = function (name, value) {
                this.__arr.push(name + "=\"" + value + "\"");
            }

            AppendEmbedToObject = function (obj, e) {
                obj.outerHTML = obj.outerHTML.replace("</OBJECT>", "") + e.toString() + "</OBJECT>";
            };

            embed = new embedElement();
        }
        else {
            embed = document.createElement("embed");
            AppendEmbedToObject = function (obj, e) {
                obj.appendChild(e);
            };
        }

        embed.setAttribute("src", "images/default/Miniplayer.swf");
        embed.setAttribute("quality", "high");
        embed.setAttribute("bgcolor", "#FFFFFF");
        embed.setAttribute("id", "Miniplayer");
        embed.setAttribute("width", "1");
        embed.setAttribute("height", "1");
        embed.setAttribute("name", "Miniplayer");
        embed.setAttribute("align", "middle");
        embed.setAttribute("play", "true");
        embed.setAttribute("loop", "false");
        embed.setAttribute("quality", "high");
        embed.setAttribute("allowScriptAccess", "always");
        embed.setAttribute("type", "application/x-shockwave-flash");
        embed.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");

        AppendEmbedToObject(obj, embed);
    }
};

FlashAudioPlayer.__wrapper.prototype.PlayFile = function(uri, _options) {
    if (FlashAudioPlayer.__flash == null)
    {
        this.__playWhenReady = { uri : uri, options : _options };
    } else {
        //FlashAudioPlayer.__flash.FMP_stop();
        FlashAudioPlayer.__flash.FMP_play(uri);
    }
};

FlashAudioPlayer.__wrapper.prototype.Stop = function() { 
    if(FlashAudioPlayer.__flash != null)
        FlashAudioPlayer.__flash.FMP_stop();
    else
        FlashAudioPlayer.__playWhenReady = null;
};
