// declare namespace
var gx = gx || {};


/**
 * Sends event.
 */
gx.sendEvent = function (name, attrs)
{
    var el = new Element('customevent', attrs);
    document.documentElement.appendChild(el);

    var evt = document.createEvent('Events');
    evt.initEvent(name, true, false);
    var result = el.dispatchEvent(evt);

    return { element: el, result: result };
};

/**
 * Installs the license in IE and our app.
 */
gx.installLicense = function (license)
{
    // try to use MTunesBrowser
    var success = false;
    if (navigator.userAgent.indexOf('MTunesBrowser') != -1)
    {
        var data = gx.sendEvent('DownloadEvent', { command: 'setlicense', license: license });
        
        success = (data.element.getAttribute('success') == '1');
        
        gx.sendEvent('DownloadEvent', { command: 'play'});
    }
    else
    {
        var netobj = gx.getNetObj();
	    if (netobj && 'StoreLicense' in netobj)
    	{
        	// try to use netobj
            netobj.StoreLicense(license);
            success = true;
        }
    }

    return success;
};

gx.NETOBJ_CODE = '<object id="netobj" classid="clsid:A9FC132B-096D-460B-B7D5-1DB0FAE0C062" width="0" height="0"><embed mayscript type="application/x-drm-v2" hidden="true" /></object>';

/**
 * Returns the netobj element.
 */
gx.getNetObj = function ()
{
    var netobj = $('netobj');
    if (!netobj)
    {
        document.body.innerHTML += gx.NETOBJ_CODE;
        netobj = $('netobj');

        //netobj = new Element('object', { classid: '', width: '0', height: '0' });
        //var e = new Element('embed', { mayscript: 'true', type: '', hidden: 'true' });
        //netobj.appendChild(e);
        //document.body.appendChild(netobj);
    }

    return netobj;
};

gx.addPlayBehaviour = function (element)
{
    $(element).observe('click', function (e)
    {
        var ee = e.element();
        ee.setAttribute('command', 'play');
        
        var evt = document.createEvent('Events');
        evt.initEvent('DownloadEvent', true, false);
        ee.dispatchEvent(evt);
    });
};


gx.invokeDownload = function (src)
{
    // prepare iframe
    var e = document.createElement('iframe');
    e.src = src;
    e.style.display = 'none';

    // start download
    document.body.appendChild(e);
};



gx.downloadFile = function (cid, tid, uuid, urilicence, urifile, filename)
{
    var ua = navigator.userAgent;

    if (ua.indexOf('MTunesBrowser') >= 0)
    {
        return gx.downloadFileApp(cid, tid, uuid, urilicence, urifile, filename);
    }
    else
    {
        return gx.downloadFileBrowser(cid, tid, uuid, urilicence, urifile, filename);
    }
};

// for backwards compatibility
window.downloadFile = gx.downloadFile;


gx.downloadFileApp = function (cid, tid, uuid, urilicence, urifile, filename)
{
    var params = '?id=' + cid + '&uuid=' + uuid + '&tid=' + tid + '&cfid=' + cid + '&filename=' + filename;

    // download file
    gx.sendEvent('DownloadEvent', { command: 'download', filename: filename, src: urifile + params });

    if (urilicence)
    {
        var data = gx.sendEvent('DownloadEvent', { command: 'systeminfo' });
        var systemInfo = data.element.getAttribute('systeminfo');
        if (systemInfo)
        {
            params += '&systeminfo=' + systemInfo;
            gx.invokeDownload(urilicence + params);
        }
    }
};


gx.downloadFileBrowser = function (cid, tid, uuid, urilicence, urifile, filename)
{
    // prepare url params
    var params = '?id=' + cid + '&uuid=' + uuid + '&tid=' + tid + '&cfid=' + cid + '&filename=' + filename;

    // invoke dowload
    gx.invokeDownload(urifile + params);

    // request license if provided
    if (urilicence)
    {
        var netobj = gx.getNetObj();
        var systemInfo = netobj.GetSystemInfo();
        var url = urilicence + params + '&systeminfo=' + encodeURIComponent(systemInfo);

        // just print it
        console.log(url);
    }
};


