/*
*	VMatrixDeployVPlayer class. This class is instantiated on each page that requires the ability
*	to launch a VPlayer. It manages the <div> overlay, <div> background screen, sizing of the <embed>
*	media, and other options provided as parameters to the object.
*/

function VmatrixDeployVPlayer(params)
{
	var that = this;
	var ie = document.all;
	var otherSWFs = [];
	
	this.vplayer = $('VMatrix_VPlayer');
	this.bgscreen = $('VMatrix_Screen');
	this.swf = $('VPlayerSWF');
	this.vdragger;
	
	if (params.showBackground == "true" || params.showBackground == true) this.showBackground = true;
	else this.showBackground = false;
	
	if (params.doNotFloat == "true" || params.doNotFloat == true) this.doNotFloat = true;
	else this.doNotFloat = false;
	
	this.backgroundColor = params.backgroundColor;
	this.autoPlay = params.autoPlay;
	
	this.baseURL = "http://www.vmatrixonline.com/vplayer2/";
	this.baseClientURL = this.baseURL + "/clients";
	this.client = params.client + "/";
	this.baseAssetURL = this.baseClientURL + this.client;
	
	this.hostURL = params.hostURL;
	if (this.hostURL && this.hostURL.indexOf("http://") == -1) this.hostURL = "http://" + this.hostURL;
	
	window.onresize = setSize;
	
	
	this.loadVPlayer = function()
	{
		// Hide other flash objects on screen to prevent overlapping / flickering
		hideAllFlashObjects();
		
		var vp = that.vplayer.style;
		vp.display = "block";
		
		var so = new SWFObject(that.baseURL + "swfs/VPlayer.swf", "VPlayerSWF", "550", "450", "9.0.115", that.backgroundColor == "white" ? "#FFFFFF" : "#000000");
		so.useExpressInstall(that.baseURL + "swfs/expressinstall.swf");
		so.addParam("wmode", "transparent");
		so.addParam("allowFullScreen", "true");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("baseURL", that.baseURL);
		if (that.hostURL) so.addVariable("hostURL", that.hostURL);
		if (that.ie) so.addVariable("browserType", "ie");
		if (window.gecko) so.addVariable("isGecko", "true");
		if (that.doNotFloat) so.addVariable("doNotFloat", "true");
		so.addVariable("client", that.client);
		so.addVariable("autoPlay", that.autoPlay);
		so.write("VMatrix_VPlayer");
		
		if (!that.doNotFloat) setSize();
		else vp.position = 'relative';
	}
	
	// Called by VPlayer.swf
	this.fixSize = function(w, h)
	{
		// This needs to be reassigned here or resizing the swf will not work
		that.swf = $('VPlayerSWF');
		
		that.swf.width = w;
		that.swf.height = h;
		
		// if that.doNotFloat = true, this function will not work because vp.position was set
		// to 'relative' above. So ignore it to save processor cycles.
		if (!that.doNotFloat) setSize();
	}
	
	// NOT called by VPlayer.swf
	function setSize()
	{
		// Avoid running this function when doNotFloat is true, called on window.resize above
		if (that.doNotFloat) return;
		
		var bgimage;
		var bg = that.bgscreen.style;
		var vp = that.vplayer.style;
		
		bg.display = "block";
		bg.zIndex = "90";
		bg.left = "0px";
		bg.top = "0px";
		bg.width = "100%";
		bg.height = window.getScrollHeight() + "px";
		
		if (that.backgroundColor == "white") bgimage = that.baseURL + "styles/images/overlay-white.png";
		else bgimage = that.baseURL + "styles/images/overlay.png";
		
		if (that.showBackground)
		{
			bg.backgroundImage = 'url("' + bgimage + '")';
		
			if (ie)
			{
				bg.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bgimage + "',sizingMethod='scale')";
				bg.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=80)";
			}
		}
		
		vp.display = "block";
		vp.zIndex = "100";
		
		var vpsize = that.vplayer.getSize();
		var offsets = that.getScrollOffsets();
		
		var left = ((window.getWidth() * 0.5) + offsets.scrollX) - (vpsize.size.x * 0.5);
		var top = ((window.getHeight() * 0.5) + offsets.scrollY) - (vpsize.size.y * 0.5);
		
		var fx = new Fx.Styles(that.vplayer, {duration:200, wait:false,  transition: Fx.Transitions.Back.easeOut});
		
		fx.start({
			'top':  top,
			'left': left
		});
	}
	
	// From: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	this.getBrowserSize = function()
	{
		var myWidth = 0, myHeight = 0;
		
		if (typeof(window.innerWidth) == 'number' )
		{
			// Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		
		return { 'width' : myWidth, 'height' : myHeight };
	}
	
	this.getScrollOffsets = function()
	{
		var scrOfX = 0, scrOfY = 0;
		
		if( typeof( window.pageYOffset ) == 'number' )
		{
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		}
		else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
		{
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		}
		else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
		{
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		
		return { 'scrollX' : scrOfX, 'scrollY' : scrOfY };
	}
	
	this.closeVPlayer = function()
	{
		var bg = that.bgscreen.style;
		var vp = that.vplayer.style;
		
		bg.display = "none";
		
		vp.display = "none";
		vp.top = "0px";
		vp.left = "0px";
		
		window.onresize = "";
		
		// Restore all hidden flash objects
		showAllFlashObjects();
	}
	
	function hideAllFlashObjects()
	{
		var SWFs = [];
		
		if (ie) SWFs = $A(document.getElementsByTagName('object'));
		else SWFs = $A(document.getElementsByTagName('embed'));
		
		SWFs.each(function(idx, i)
		{
			if (idx.name != "VPlayerSWF" && idx.style.visibility != 'hidden')
			{
				otherSWFs.push(idx);
				idx.style.visibility = 'hidden';
			}
		});
	}
	
	function showAllFlashObjects()
	{
		otherSWFs.each(function(idx, i)
		{
			idx.style.visibility = 'visible';
		});
	}
	
	
	// Makes all VPlayers on screen draggable	
	this.prepareDragging = function()
	{
		if (that.doNotFloat) return;
		
		$('VMatrix_VPlayer').className = 'VMatrix_dragger';
		
		/*
			This code supports finding multiple elements on screen with class name
			'VMatrix_dragger' and makes them ALL draggable at once. However VMatrix
			currently only supports one VPlayer on screen at a time, so there is only
			ever one class VMatrix_dragger. Keeping this code in place for future use where
			perhaps multiple videos can be on screen at the same time.
		*/
		
		var draggables = document.getElementsBySelector('.VMatrix_dragger');
		
		draggables.each(function(el)
		{
			dragContainerInit(el);
		});
		
		var indexLevel = 1;
	
		function dragContainerInit (el)
		{
			var dragContainerOptions =
			{
				handle: el,
				
				onStart: function()
				{
					indexLevel++;
				}.bind(this),
				
				onComplete: function()
				{
					that.vdragger.detach();
				}
			};
			
			that.vdragger = el.makeDraggable(dragContainerOptions);
			that.vdragger.detach();
		}
	};
	
	
	// These drag functions are called from VPlayer.swf
	this.startDragging = function()
	{
		if (that.vdragger) that.vdragger.attach();
	};
	
	this.stopDragging = function()
	{
		/*
			This is really tough on multiple browsers.
			Without this line, Firefox cannot stop dragging the window, hence if (window.gecko)
			
			On Safari and IE (windows), WITH this line dragging would only work if you click the
			drag bar, hold mouse down, and move AWAY from flash and release, then click again and drag.
			
			WITHOUT this line, at least you can just double click to drag. Single click? Impossible.
		*/
		
		if (window.gecko) if (that.vdragger) that.vdragger.stop();
	};
	
	// CONSTRUCTOR
	(function VMatrixDeployVPlayer()
	{
		that.prepareDragging();
	})();
}


/*
*	Extend String class to add query string parsing ability, as well as URI parsing
*	Thanks to: http://forum.mootools.net/viewtopic.php?pid=21481
*
*	** Requires MooTools, of course **
*/

String.extend(
{
	toQueryObject: function()
	{
		var o = {};
		
		$A(this.replace(/(^.*\?)|(#.*$)/g,'').split('&')).each(function(p)
		{
			p = p.split("=");
			o[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
		});
		
		return o;
	},
	
	parseUri: function()
	{
		var bits = this.match(/^(?:([^:\/?#.]+):)?(?:\/\/)?(([^:\/?#]*)(?::(\d*))?)((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[\?#]|$)))*\/?)?([^?#\/]*))?(?:\?([^#]*))?(?:#(.*))?/);
		
		return (bits)
		? bits.associate(['uri', 'scheme', 'authority', 'domain', 'port', 'path', 'directory', 'file', 'query', 'fragment'])
		: null;
	}
});