/**
* DPlayer related Javascript.
**/

function GetInnerSize () {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

function ResizeToInner (w, h, x, y) {
	// make sure we have a final x/y value
	// pick one or the other windows value, not both
	if (x==undefined) x = window.screenLeft || window.screenX;
	if (y==undefined) y = window.screenTop || window.screenY;
	// for now, move the window to the top left
	// then resize to the maximum viewable dimension possible
	window.moveTo(0,0);
	window.resizeTo(screen.availWidth,screen.availHeight);
	// now that we have set the browser to it's biggest possible size
	// get the inner dimensions.  the offset is the difference.
	var inner = GetInnerSize();
	var ox = screen.availWidth-inner[0];
	var oy = screen.availHeight-inner[1];
	// now that we have an offset value, size the browser
	// and position it
	window.resizeTo(w+ox, h+oy);
	window.moveTo(x,y);
}


var dreambroker = function() {
	
	return {
		maxAndGetInner: function() {
			window.moveTo(0,0);
			window.resizeTo(screen.availWidth-1, screen.availHeight-1);
			var innerW = $(window).width();
			var innerH = $(window).height();
			return [innerW, innerH];
		},
		getEmbedArea: function(availW, availH, vW, vH, cbar) {
			var rW = vW;
			var rH = vH+cbar;
			if ((availW <= rW) || (availH <= rH)) {
				var iw = Math.floor(vW * (availH / rH));
				var ih = Math.floor((vH) * (availW / rW))+cbar;
				if (iw > availW) {
					rW = availW;
					rH = Math.floor(((availW / vW) * vH)+cbar);
				}
				else if (ih > availH) {
					rH = availH;
					rW = Math.floor((vW * ((rH-cbar) / (vH))));
				}
				else {
					rW = availW;
					rH = availH;
				}
			}
			if (rW < 160)
				rW = 160;
			if (rH < (120+cbar))
				rH = (120+cbar);
			return [rW, rH];
		},
		shrinkAndCenter: function(w, h) {
			var innerW = $(window).width();
			var innerH = $(window).height();
			var oW = screen.availWidth-1 - innerW;
			var oH = screen.availHeight-1 - innerH;
			var nW = oW+w;
			var nH = oH+h;
			var xc = ((screen.availWidth - nW)/2);
			var yc = ((screen.availHeight - nH)/2);
			window.resizeTo(nW, nH);
			window.moveTo(xc,yc);
		}
	};		
}();
