if (typeof mediageneral == "undefined") var mediageneral = new Object();
mediageneral.MGVP = function (elementId, w, h, suppress_ads, position_list) {
	var id = mediageneral.MGVP.utils.addPlayer(this);
	this.initialized = false;
	this.__listeners = [];
	this.img;
	this.duration = 0;
	this.time = 0;
	this.width = (w != null) ? w : 320;
	this.height = (h != null) ? h : 240;
	
	this.element = document.getElementById(elementId);
	this.element.className = "MGVPContainer";
	while (this.element.firstChild) {
		this.element.removeChild(this.element.firstChild);
	}
	this.element.style.width = this.width + "px";
	this.element.style.height = this.height + "px";
	this.instance = id;
	
	var nVideoCell = document.createElement('div');
	nVideoCell.id = "video_" + this.instance;
	
	this.element.appendChild(nVideoCell);
	nVideoCell.className = "videoPlayer";
	

	this.previewContainer = document.createElement('div');
	this.previewContainer.id = "preview_" + this.instance;
	this.previewContainer.style.width = this.width + "px";
	this.previewContainer.style.height = this.height + "px";
	
	
	this.element.insertBefore(this.previewContainer, nVideoCell);
	this.previewContainer.className = "videoPreview";
	this.previewContainer.style.visibility = "hidden";

	this.muted = false;
	this.playing = false;
	this.buffering = false;
	try {
		this.videoplayer = new SWFObject("http://media.mgnetwork.com/mgvp/mgvp.swf", id, this.width, this.height, 8, "#000000");
		this.videoplayer.addParam("wmode", "opaque");
		this.videoplayer.addParam("allowScriptAccess", "always");
		this.videoplayer.addVariable("width", this.width);
		this.videoplayer.addVariable("height", this.height);
		this.videoplayer.addVariable("id", this.instance);
		this.videoplayer.addVariable("adrequest", ((suppress_ads) ? "0" : "1"));
		this.videoplayer.addVariable("positionlist", position_list);
		this.videoplayer.write(nVideoCell);
		
		if (typeof window.attachEvent != "undefined") {
			window.attachEvent("onbeforeunload", mediageneral.MGVP.utils.cleanupFP9IELeaks);
		}
		this.initialized = true;
	} catch (err) {
		this.element.innerHTML = "<pre>An error has occured. Code #100<br />" + err + "</pre>";
	}
}
mediageneral.MGVP.prototype = {
	dispatchQueue: function (queueObj, eventObj) {
		var queueName = "__q_" + eventObj.type;
		var queue = queueObj[queueName];
		if (queue != undefined) {
			var i;
			for (i in queue) {
				var o = queue[i];
				var oType = typeof(o);
				if (o[eventObj.type] != undefined) {
					o[eventObj.type](eventObj);
				} else {
					o.apply(queueObj, [eventObj]);
				}
			}
		}
	},
	delegate: function(obj, func) {
		var f = function () {
			var target = arguments.callee.target;
			var func0 = arguments.callee.func;
			return func0.apply(target, arguments);
		};
		f.target = obj;
		f.func = func;
		
		return f;
	},
	addEventListener: function (eventType, callback) {
		var queueName = "__q_" + eventType;
		if (this[queueName] == undefined) {
			this[queueName] = new Array();
		}
		this[queueName].push(callback);
	},
	dispatchEvent: function (eventObj) {
		if (eventObj.target == undefined) eventObj.target = this;
		this.dispatchQueue(this, eventObj);
	},
	setPreview: function (imgUrl) {
		if (this.initialized) {
			this.img = new Image();
			this.img.src = imgUrl;
			
			while (this.previewContainer.firstChild) {
				this.previewContainer.removeChild(this.previewContainer.firstChild);
			}
			this.previewContainer.style.visibility = "hidden";
			this.previewContainer.appendChild(this.img);
			setTimeout(this.delegate(this, this.onImageLoad), 500);
		}
	},
	onImageLoad: function () {
		var originW, originH, ratio;
		if (this.img.complete) {
			originW = this.img.width;
			originH = this.img.height;
			if (originW > this.width) {
				ratio = originW / this.width;
				this.img.width = this.width;
				this.img.height = originH / ratio;
			}
			if (originH  > this.height) {
				ratio = originH / this.height;
				this.img.height = this.height;
				this.img.width = originW / ratio;
			}
			if (!this.playing) this.previewContainer.style.visibility = "visible";
			this.dispatchEvent({target:this, type:"imageloaded"});
		} else {
			setTimeout(this.delegate(this, this.onImageLoad), 500);
		}
	},
	play: function (sp, flv, prv) {
		if (this.initialized) {
			if (prv) this.setPreview(prv);
			this.getInstance()[this.instance + "_play_video"](sp, flv);
			this.playing = !this.playing;
		}
	},
	pause: function () {
		if (this.initialized) {
			this.getInstance()[this.instance + "_pause"]();
			this.playing = !this.playing;
		}
	},
	seek: function (offset) {
		if (this.initialized) {
			this.getInstance()[this.instance + "_seek"](offset);
		}
	},
	mute: function () {
		if (this.initialized) {
			this.getInstance()[this.instance + "_mute"]();
			this.muted = !this.muted;
		}
	},
	getVolume: function () {
		if (this.initialized) {
			return this.getInstance()[this.instance + "_getVolume"]();
		}
	},
	setVolume: function (lvl) {
		if (this.initialized) {
			this.getInstance()[this.instance + "_setVolume"](lvl);
		}
	},
	getInstance: function () {
		if (this.initialized) {
			if (navigator.appName.indexOf("Microsoft") != -1) {
				return window[this.instance];
			} else {
				return document[this.instance];
			}
		}
	}
}
mediageneral.MGVP.events = {
	PLAY:'play',
	CONNECTIONWAITING:'connectionwaiting',
	CONNECTIONSUCCESS:'connectionsuccess',
	CONNECTIONFAILURE:'connectionfailure',
	PAUSE:'pause',
	MUTE:'mute',
	PLAYERROR:'playerror',
	COMPLETE:'complete',
	LOAD:'load',
	IMAGELOADED:'imageloaded',
	ADVANCE:'advance',
	BUFFER:'buffer'
}
/**/
mediageneral.MGVP.utils = {
	__players: [],
	dispatchEvent: function (target, eventType, addObj) {
		var evt = {type:eventType};
		var key;
		for (var i=0; i<mediageneral.MGVP.utils.__players.length; i++) {
			if (mediageneral.MGVP.utils.__players[i].instance == target) {
				evt.target = mediageneral.MGVP.utils.__players[i];
				if (typeof(addObj) != "undefined") {
					for (key in addObj) {
						evt.target[key] = addObj[key];
					}
				}
				mediageneral.MGVP.utils.__players[i].dispatchEvent(evt);
			}
		}
	},
	findPos: function (obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj == obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	},
	addPlayer: function (plyr) {
		var nLen = mediageneral.MGVP.utils.__players.push(plyr);
		var nName = "$mgvpPlayer_" + nLen;
		return nName;
	},
	cleanupFP9IELeaks: function () {
		__flash_unloadHandler = function() {};
		__flash_savedUnloadHandler = function() {};
	}
}
/**/
mediageneral.MGVP.plugins = {};
mediageneral.MGVP.plugins.OAS = {
	hostid: null,
	setHostId: function (id) {
		mediageneral.MGVP.plugins.OAS.hostid = id;
	},
	getHostId: function () {
		return mediageneral.MGVP.plugins.OAS.hostid;
	},
	setOASCompanionAdProxy: function (data) {
		try {
			parent.mediageneral.MGVP.plugin_utils.setCompanionAd(data);
		} catch (e) {
			try {
				mediageneral.MGVP.plugin_utils.setCompanionAd(data);
			} catch (ee) {}
		}
	}
}
mediageneral.MGVP.plugin_utils = {
	targeted_position:"!Left3",
	getSitePage: function () {
		try {
			return sitepage;
		} catch (e) {
			// No sitepage in immediate page
			try {
				return window.parent.sitepage;
			} catch (ee) {
				// no sitepage in parent
				return null;
			}
		}
	},
	setCustomPosition: function (pos) {
		mediageneral.MGVP.plugin_utils.targeted_position = pos;
	},
	setCompanionAd: function (content) {
		var idc = document.getElementById("videoCompanionPosition");
		if (content == undefined) {
			if (idc.nodeName.toLowerCase() != "iframe") {
				var ad_iframe = document.createElement('iframe');
				ad_iframe.id = "videoCompanionPosition";
				ad_iframe.scrolling = "no";
				ad_iframe.frameBorder = "0";
				ad_iframe.width = "300";
				ad_iframe.height = "250";
				ad_iframe.style.margin = "0px";
				ad_iframe.style.padding = "0px";
				idc.parentNode.replaceChild(ad_iframe, idc);
				idc = ad_iframe;
			}
			idc.src = "http://media.mgnetwork.com/mgvideocenter/iframe.html?sitepage=" + sitepage + "&poslist=" + oasPosList + "&flash_request=1&specific=" + mediageneral.MGVP.plugin_utils.targeted_position;
		} else {
			var ad_pos = document.createElement('div');
			ad_pos.id = "videoCompanionPosition";
			ad_pos.innerHTML = content;

			idc.parentNode.replaceChild(ad_pos, idc);
		}
	}
}
