(function () {
var iframe, id = 'myspace-player', cmds, token, messageKey = 'myspace:', myspace = {}, referrerParts, referrerDomain;
if (document.getElementById(id)) return;
Myspace = window.Myspace || {};
Myspace.MiniPlayer = Myspace.MiniPlayer || {};
Myspace.MiniPlayer.MediaPlayer = function (init) {
var that = this,
events = [],
logger = new Logger();
this.on = function () {
applyEvents.call(this, on, arguments);
return this;
};
this.off = function () {
applyEvents.call(this, off, arguments);
return this;
};
this.one = function () {
applyEvents.call(this, one, arguments);
return this;
};
this.fire = function () {
fire.apply(this, arguments);
return this;
};
this.log = function () {
logger.log.apply(this, arguments);
return this;
}
this.color = '#000';
// adds a single event, or multiple events if object
function applyEvents(func, args) {
var arg = args[0];
if (typeof arg === 'object') {
for (var key in arg) {
func.call(this, key, arg[key]);
}
}
else {
func.apply(this, args);
}
}
// adds event
function on(eventName, callback, one) {
if (typeof eventName === 'string' && typeof callback === 'function') {
events.push(new Event(eventName, callback, one));
}
}
// removes event
function off(eventName, callback) {
events = events.filter(function (event) {
return event.name !== eventName && event.callback !== callback;
});
}
// adds one-time event
function one(eventName, callback) {
on(eventName, callback, true);
}
// executes event
function fire(eventName) {
var args = [].splice.call(arguments, 1);
events.filter(function (event) {
return event.name === eventName;
})
.map(function (event) {
return event.callback;
})
.forEach(function (callback) {
callback.apply(that, args);
});
// remove one-time events
events = events.filter(function (event) {
return !(event.name === eventName && event.one);
});
}
function Event(name, callback, one) {
this.name = name;
this.callback = callback;
this.one = !!one;
}
function Logger() {
this.log = function () {
log('log', arguments)
};
this.warn = function (args) {
log('warn', arguments);
};
function log(methodName, args) {
if (typeof console[methodName] === 'function') {
[].unshift.call(args, 'color: ' + that.color);
[].unshift.call(args, '%c[MediaPlayer] [' + that.name + ']');
console[methodName].apply(console, args);
}
}
}
typeof init === 'function' && init.apply(this, arguments);
};
var emitter = new Myspace.MiniPlayer.MediaPlayer(),
erred = false;
Myspace.MiniPlayer.on = emitter.on;
Myspace.MiniPlayer.off = emitter.off;
Myspace.MiniPlayer.one = emitter.one;
var width = 457,
height = 357;
var maxWidth = document.body.clientWidth * .75;
if (width > maxWidth) {
width = maxWidth + 56;
height = maxWidth * 9 / 16 + 56;
}
iframe = document.createElement('iframe');
iframe.id = id;
iframe.src = 'http://media.myspace.com/play/video/jamison-eating-54423573-54423573?play=1&mini=1';
iframe.width = width;
iframe.height = height;
iframe.setAttribute('frameborder', 0);
iframe.setAttribute('allowtransparency', true);
iframe.setAttribute('webkitallowfullscreen', '');
iframe.setAttribute('mozallowfullscreen', '');
iframe.setAttribute('allowfullscreen', '');
iframe.style.position = 'fixed';
iframe.style.right = '-' + iframe.width + 'px';
iframe.style.bottom = '-10px';
iframe.style.zIndex = '2147483647';
animation(true);
function animation(on) {
iframe.style.transition = on ? 'bottom 0.5s, right 0.5s' : '';
iframe.style.webkitTransition = on ? 'bottom 0.5s, right 0.5s' : '';
iframe.style.MozTransition = on ? 'bottom 0.5s, right 0.5s' : '';
}
cmds = {
'miniplayer.close': function (animate) {
if (animate === false) {
animation(false);
setTimeout(function () { animation(true); }, 100);
}
iframe.style.right = '-' + iframe.width + 'px';
},
'miniplayer.open': function (animate) {
if (animate === false) {
animation(false);
setTimeout(function () { animation(true); }, 100);
}
iframe.style.right = '-10px';
},
'player.context': function (data) {
if (data) save({ player: data });
reply(myspace.player);
},
'ad.started': function () {
emitter.fire('adStarted');
},
'ad.stopped': function () {
if (erred) {
erred = false;
}
else {
emitter.fire('adStopped');
}
},
'ad.error': function () {
erred = true;
emitter.fire('adError');
}
};
if (window.addEventListener) {
window.addEventListener('message', message);
}
else {
window.attachEvent('onmessage', message);
}
function message(event) {
var data;
if (event.source !== iframe.contentWindow || typeof event.data !== 'string' || !~event.data.indexOf(messageKey)) {
return;
}
try {
data = JSON.parse(event.data.substr(messageKey.length));
}
catch (e) {}
if (data) {
token = data.token;
if (data.cmd && typeof cmds[data.cmd] == 'function') {
cmds[data.cmd](data.data);
reply(data.data);
}
}
}
function reply(data) {
if (token) {
iframe && iframe.contentWindow && iframe.contentWindow.postMessage(messageKey + JSON.stringify({ token: token, data: data }), '*');
token = undefined;
}
}
function save(args) {
if (typeof args == 'string') {
return myspace[args];
}
else if (typeof args == 'object') {
if (sessionStorage) sessionStorage.myspace = JSON.stringify(merge(myspace, args));
return myspace;
}
}
function merge(obj1, obj2, deep) {
if (typeof obj1 != 'object') return obj2;
for (var key in obj2) deep && typeof obj1[key] == 'object' && typeof obj2[key] == 'object' ? merge(obj1[key], obj2[key]) : obj1[key] = obj2[key];
return obj1;
}
if (sessionStorage && sessionStorage.myspace) try { myspace = JSON.parse(sessionStorage.myspace); } catch (e) {}
document.body.appendChild(iframe);
})();