(function() {
    var thistag;
    thistag = (thistag = document.getElementsByTagName('script'))[thistag.length-1];

    if(!thistag || !thistag.src.match(/player\/\d+?\.js/)) {
        var scripts = document.getElementsByTagName('script');
        for(var i = 0, sl = scripts.length; i < sl; i++) {
            if(scripts[i].src.match(/player\/\d+?\.js/)) {
                thistag = scripts[i];
                break;
            }
        }
    }

    var query = {};
    if(thistag.src.match(/\#/)) {
        var vars = thistag.src.split('#')[1].split('&');
        for(var i=0; i < vars.length; i++) {
            var pair = vars[i].split('=');
            query[pair[0]] = pair[1];
        }
    }
    
    var format_id = false;
    
    var formats = {
            1: ['375px', '150px', 'general'],
            2: ['375px', '240px', 'general-trackdisp'],
            3: ['100%', '50px', 'bar-top'],
            4: ['100%', '50px', 'bar-bottom'],
            5: ['175px', '50px', 'mini'],
            6: ['360px', '50px', 'mini-expanded'],
            '': ''
    },
        format = format_id ? formats[format_id] : formats[5];
        nocache = query['nocache'] || false;

    var frm = document.createElement('iframe');
    frm.setAttribute('id', 'iradeo-player');
    frm.setAttribute('name', 'iradeo-player');
    frm.setAttribute('frameborder', '0');
    switch(format[2]) {
        case 'bar-top':
            frm.setAttribute('style',
                'border:0;width:100%;height:' + format[1] + ';left:0;top:0;position:fixed;z-index:10000;');
            frm.setAttribute('width','100%');
            frm.setAttribute('height', format[1].replace('px', ''));
        break;

        case 'bar-bottom':
            frm.setAttribute('style',
                'border:0;width:100%;height:' + format[1] + ';left:0;bottom:0;position:fixed;z-index:10000;');
            frm.setAttribute('width','100%');
            frm.setAttribute('height', format[1].replace('px', ''));
        break;

        default:
            frm.setAttribute('style',
                'border:0;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;width:' + format[0] + ';height:' + format[1] + ';');
            frm.setAttribute('width', format[0].replace('px', ''));
            frm.setAttribute('height', format[1].replace('px', ''));
        break;
    }

    if(query['element'] || typeof embedElement != 'undefined') {
        document.getElementById(embedElement || query['element']).appendChild(frm);
    } else {
        thistag.parentNode.insertBefore(frm, thistag.nextSibling);
    }

    var url = thistag.src.split('#')[0].replace(/(\.\d+)?.js/, '') + (nocache ? '#' + (new Date()).getTime() : '');
    if(format_id) url += (nocache ? '&' : '#') + 'format=' + format[2];
    frm.setAttribute('src', url);
})();
