
function VideoPlayer(startX, startY, videoPlayerId) {

	var self = this;
	var div = null;
	var id = VideoPlayer.ID++;
	var playPosition = {x: startX, y: startY};
	var videoPlayerId = videoPlayerId;
	var autoClose = false;

	VideoPlayer.register(id, self);

	self.file = "";
	self.productID = 0;
	self.iframe = null;
	self.container = null

	this.start = function(file, fromPopup, hideSignup, productID) {
		self.productID = productID;
		self.file = file;
		VideoPlayer.FROMPOPUP = fromPopup;
		
		//Show the site overview video with the new videoplayer
		if(file == "siteoverview7.f4v" || file == "siteoverview7-offer.f4v" || file == "siteoverview12_720x405.f4v" || file == "siteoverview12-sub_720x405.f4v" || file == "free-site-video_720x405.f4v" || file == "freepaid_720x405.f4v" || file == "siteoverview13-sub_720x405.f4v" || file == "siteoverview8_685x480.f4v" || file == "siteoverview8-offer_720x405.f4v") {
   			videoPlayerId = 4;
			playPosition = getPosition(VideoPlayer.VIDEO_PLAYERS[videoPlayerId].width, VideoPlayer.VIDEO_PLAYERS[videoPlayerId].height);
			VideoPlayer.VIDEO_PLAYERS[videoPlayerId].url = "/videoviewer/siteoverviewvideo";
		}

		buildDisplay(file, fromPopup ? 1 : 0, hideSignup ? 1 : 0);
		track("videoopen", "src=" + file + (fromPopup ? "|fromTab=1" : "") + "|page=" + VideoPlayer.META_DATA.page);
		VideoPlayer.isOpen = true;
	};

	this.playVideo = function(file, iframe) {
		self.productID = 0;
		self.file = file;
		var hideSignup = 1;
		var fromPopup = false;
		track("videoopen", "src=" + file + "|from=media-center");
		document.getElementById(iframe).src = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].url + "/" + id + "/" + file + "/" + fromPopup + "/" + hideSignup + "/" + self.productID + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].swf + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].width + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].height;
	};

	this.close = function() {
		if(div == null) return;
		div.style.display = "none";

		if(navigator.userAgent.search(/chrome/i) != -1) {
			div.parentNode.removeChild(div);
		} else if(navigator.userAgent.search(/safari/i) == -1) {
			div.parentNode.removeChild(div);
		}

		try {
			PopupCover.close();
		} catch(e) { }
		
		try {
			parent.alertVideoClose();
		} catch(e) { }
		
		VideoPlayer.isOpen = false;
	};

	function buildDisplay(file, fromPopup, hideSignup) {
		if(div != null) return;
		var time = new Date().getTime();
		div = document.createElement("div");
		self.container = div;
		//div = document.createElement("iframe");
		self.iframe = document.createElement("iframe");
		self.iframe.id = "videoPlayer_" + time;
		//self.iframe = div;
		div.style.position = "fixed";
		div.style.left = playPosition.x + "px";
		div.style.top = playPosition.y + "px";

		//if (videoPlayerId == 0) {
		if(false) {
			sizeRegExp = /_\d+x\d+\./;
			
			matches = file.match(sizeRegExp);
		 
			if (matches && matches.length > 0) {
				sizeStringContainer = matches[0];
				sizeString = sizeStringContainer.substr(1, sizeStringContainer.length - 2);
				sizes = sizeString.split(/x/);
				divWidth = sizes[0];
				divHeight = sizes[1];
				
				div.style.left = getPosition(divWidth, divHeight).x + "px";
				div.style.top = getPosition(divWidth, divHeight).y + "px";
				
				divWidth = Number(divWidth) + (divWidth * 0.08030);
				divHeight = Number(divHeight) + (divHeight * .1354);
				divWidth = Math.round(divWidth);
				divHeight = Math.round(divHeight);
			} else {
				divWidth = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].width;
				divHeight = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].height;
			}
		} else {
			
			pattern = /600x338.f4v/;
			var pos;
			if(pattern.test(file)) {
				pos = getPosition(600, 392);
			} else {
				pos = getPosition(VideoPlayer.VIDEO_PLAYERS[videoPlayerId].width, VideoPlayer.VIDEO_PLAYERS[videoPlayerId].height);
			}
			
			div.style.left = pos.x + "px";
			div.style.top = pos.y + "px";
			
			divWidth = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].width;
			divHeight = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].height;
		}
		
		//VideoPlayer.META_DATA.width = divWidth;
		//VideoPlayer.META_DATA.height = divHeight;

        div.style.width = Number(divWidth) + "px";
		div.style.height = Number(divHeight) + "px";
		self.iframe.width = Number(divWidth) + "px";
		self.iframe.height = Number(divHeight) + "px";

        div.style.zIndex = 500;
		div.style.overflow = "hidden";
		div.style.border = "none";
		//div.frameBorder = "0";
		//div.allowTransparency = "true";
		//div.style.backgroundColor = "transparent";
		self.iframe.frameBorder = "0";
		self.iframe.allowTransparency = "true";
		self.iframe.style.backgroundColor = "transparent";

		//div.src = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].url + "/" + id + "/" + file + "/" + fromPopup + "/" + hideSignup + "/" + self.productID + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].swf + "/" + divWidth + "/" + divHeight + "/" + getPageSize().width + "/" + getPageSize().height;
		//self.iframe.src = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].url + "/" + id + "/" + file + "/" + fromPopup + "/" + hideSignup + "/" + self.productID + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].swf + "/" + divWidth + "/" + divHeight + "/" + getPageSize().width + "/" + getPageSize().height;
		//VideoPlayer.CURRENT_IFRAME_URL_REQUEST = div.src;
		//VideoPlayer.CURRENT_IFRAME_URL_REQUEST = self.iframe.src;
		
		div.appendChild(self.iframe);
		
		var pattern = /siteoverview/i;
		if(pattern.test(file)) {
			ajax("/xml/service/get_homepage_version.php",{}, function(homepageVersion) {
				
				if(homepageVersion == 87 || homepageVersion == 93) {
					var signUpBtn = document.createElement("img");
					signUpBtn.src = "/artwork/home/homepage/sign-up-btn.png";
					signUpBtn.style.display = "block";
					signUpBtn.style.margin = "0 auto";
					signUpBtn.style.marginTop = "-90px";
					signUpBtn.style.cursor = "pointer";
					signUpBtn.style.position = "relative";
					signUpBtn.onclick = function() {
						gotoRegistration("videoPlayerSignUpButton");
					};
					div.appendChild(signUpBtn);
				}
			});
		}
		
		document.body.appendChild(div);
		if((typeof homepageVersion != "undefined") && pattern.test(file) && ((homepageVersion >= 141 && homepageVersion != 145 && homepageVersion != 146) || homepageVersion == 91)) {
			self.iframe.contentWindow.location.replace("/flowplayer/"+ file +"/" + id);
			VideoPlayer.CURRENT_IFRAME_URL_REQUEST = "/flowplayer/"+ file +"/" + id;
		} else {
			self.iframe.contentWindow.location.replace(VideoPlayer.VIDEO_PLAYERS[videoPlayerId].url + "/" + id + "/" + file + "/" + fromPopup + "/" + hideSignup + "/" + self.productID + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].swf + "/" + divWidth + "/" + divHeight + "/" + getPageSize().width + "/" + getPageSize().height);
			VideoPlayer.CURRENT_IFRAME_URL_REQUEST = VideoPlayer.VIDEO_PLAYERS[videoPlayerId].url + "/" + id + "/" + file + "/" + fromPopup + "/" + hideSignup + "/" + self.productID + "/" + VideoPlayer.VIDEO_PLAYERS[videoPlayerId].swf + "/" + divWidth + "/" + divHeight + "/" + getPageSize().width + "/" + getPageSize().height;
		}
	}
	
}

	function getPosition(width, height) {
		var wWidth, wHeight;
		if("innerHeight" in window) {
			wWidth = window.innerWidth;
			wHeight = window.innerHeight;
		} else {
			wWidth = document.documentElement.clientWidth;
			wHeight = document.documentElement.clientHeight;
		}
		
		var position = {x: 0, y: 0};
		position.x = Math.round((wWidth - width)/2);
		position.y = Math.round((wHeight - height)/2);
		
		if(position.y < 0)
			position.y = 0;
		
		return position;
	}
	
	function getPageSize() {
		var width = 0
		var height = 0;
		
		if(window.innerWidth == null) {
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else {
			width = window.innerWidth;
			height = window.innerHeight;
		}
		
		return {width: width, height: height};
	}

VideoPlayer.META_DATA = {url: "/videoplayer", width: 525, height: 470, page: "homepage"};
VideoPlayer.VIDEO_PLAYERS = new Array(
									  //{url: "/videoplayer", swf: "videoplayer.swf", width: 525, height: 470}, 
									  {url: "/videoplayer", swf: "videoplayer.swf", width: 788, height: 640}, 
									  {url: "/videoplayer", swf: "mouse_animation_frame.swf", width: 993, height: 610}, 
									  {url: "/videoplayer", swf: "videoplayer.swf", width: 700, height: 480}, 
									  {url: "/videoplayer", swf: "sizzle_videoplayer.swf", width: 640, height: 400}, 
									  {url: "/videoviewer", swf: "offervideoplayer.swf", width: 742, height: 560});
VideoPlayer.ID = 0;
VideoPlayer.FROMPOPUP = false;
VideoPlayer.CURRENT_IFRAME_URL_REQUEST = "";
VideoPlayer.refreshCounter = 0;
VideoPlayer.isOpen = false;
VideoPlayer.showSurvey = false;

VideoPlayer.ActivePlayers = { };
VideoPlayer.register = function(id, player) {
	VideoPlayer.ActivePlayers[id] = player;
};

VideoPlayer.close = function(id) {
	var player = VideoPlayer.ActivePlayers[id];
	if(player == null) return;
	track("videoclose", "src=" + VideoPlayer.ActivePlayers[id].file + (VideoPlayer.FROMPOPUP ? "|fromTab=1" : "") + "|page=" + VideoPlayer.META_DATA.page);
	VideoPlayer.ActivePlayers[id] = null;
	player.close();
};

VideoPlayer.move = function(id, deltaX, deltaY) {
	VideoPlayer.ActivePlayers[id].move(deltaX, deltaY);
};

function addOnResizeFunction(onResizeFunction) {
	if(typeof window.onresize == "function") {
		var oldOnResize = window.onresize;
		
		window.onresize = function() {
			onResizeFunction();
			oldOnResize();
		}
	} else {
		window.onresize = onResizeFunction;
	}
}

addOnResizeFunction(function() {
	if(VideoPlayer.isOpen) {
		var videoPlayerID = 0;
		for(var temp in VideoPlayer.ActivePlayers)
			videoPlayerID = temp

		var activeVideoPlayer = VideoPlayer.ActivePlayers[videoPlayerID];
			
		if(activeVideoPlayer && activeVideoPlayer.iframe) {
			var position = getPosition(parseInt(activeVideoPlayer.container.clientWidth), parseInt(activeVideoPlayer.container.clientHeight));
			activeVideoPlayer.container.style.top = position.y + "px";
			activeVideoPlayer.container.style.left = position.x + "px";
		}
	}
});
