﻿function MMPApi(config, htmlContainer)
{
    this.config = config;
    this.guid = Math.round(Math.random() * 1000000).toString();
    this.htmlContainer = htmlContainer;
    this.mmp = null;

    if (window.MMPApiInstances == undefined) window.MMPApiInstances = new Array();
    window.MMPApiInstances.push({ mmp: this, guid: this.guid });

    this.apiConnected = function ()
    {
        var val = false;
        if (window["mmp" + this.guid] != undefined) val = this.mmp == window["mmp" + this.guid];
        return val;
    }
    this.Init = function ()
    {
        window["MMP_JS_Enable" + this.guid] = function (guid)
        {
            var enableJSApi = false;
            if ($("#mmp" + guid).length > 0)
            {
                window["mmp" + guid] = $("#mmp" + guid)[0];
                enableJSApi = true;
            }
            return enableJSApi;
        };
        window["MMP_JS_EVENT" + this.guid] = function (guid, eventType, errorCode, obj)
        {
            if (window.MMPApiInstances != undefined)
            {
                for (var i = 0; i < window.MMPApiInstances.length; i++)
                {
                    if (window.MMPApiInstances[i].guid == guid)
                    {
                        window.MMPApiInstances[i].mmp.FlashMMPEvent(eventType, errorCode, obj);
                        break;
                    }
                }
            }
        };
        if (this.config.src != undefined)
        {
            $(htmlContainer).flashembed(
			{
			    id: "mmp" + this.guid,
			    src: this.config.src,
			    width: this.config.width != undefined ? this.config.width : "100%",
			    height: this.config.height != undefined ? this.config.height : "100%",
			    wmode: this.config.wmode != undefined ? this.config.wmode : "opaque",
			    allowfullscreen: this.config.allowfullscreen != undefined ? this.config.allowfullscreen : true
			},
			{
			    xmlPath: this.config.xmlPath,
			    guid: this.guid
			});
            this.mmp = $("#mmp" + this.guid)[0];
        }
        return this;
    };
    this.FlashMMPEvent = function (eventType, errorCode, obj)
    {
        if (this.config.onMMPEvent != undefined) this.config.onMMPEvent(eventType, errorCode, obj);
    };

    this.GetAudioCodecId = function () { return this.apiConnected() ? this.mmp.mmpGetAudioCodecId() : -1; };
    this.GetBuffering = function () { return this.apiConnected() ? this.mmp.mmpGetBuffering() : false; };
    this.GetBytesLoaded = function () { return this.apiConnected() ? this.mmp.mmpGetBytesLoaded() : 0; };
    this.GetBytesTotal = function () { return this.apiConnected() ? this.mmp.mmpGetBytesTotal() : 0; };
    this.GetPan = function () { return this.apiConnected() ? this.mmp.mmpGetPan() : 0; };
    this.GetPaused = function () { return this.apiConnected() ? this.mmp.mmpGetPaused() : false; };
    this.GetStreamDuration = function () { return this.apiConnected() ? this.mmp.mmpGetStreamDuration() : 0; };
    this.GetTimeElapsed = function () { return this.apiConnected() ? this.mmp.mmpGetTimeElapsed() : 0; };
    this.GetVideoCodecId = function () { return this.apiConnected() ? this.mmp.mmpGetVideoCodecId() : -1; };
    this.GetVolume = function () { return this.apiConnected() ? this.mmp.mmpGetVolume() : 0; };

    this.SetPan = function (value) { if (this.apiConnected()) this.mmp.mmpSetPan(value); }
    this.SetVolume = function (value) { if (this.apiConnected()) this.mmp.mmpSetVolume(value); }

    this.Play = function (obj) { if (this.apiConnected()) { this.mmp.mmpPlay(obj); } };
    this.SeekTo = function (obj) { if (this.apiConnected()) this.mmp.mmpSeekTo(obj) };
    this.Stop = function () { if (this.apiConnected()) this.mmp.mmpStop(); };
    this.TogglePause = function () { if (this.apiConnected()) this.mmp.mmpTogglePause(); };
}

(function ($)
{
    $.fn.mmp = function (settings)
    {
        this.each(function ()
        {
            $(this).data("mmp", new MMPApi(settings, $(this)[0]));
        });
        return this;
    };
})(jQuery);
