// Short cut for getElementById()
function El(id)
{
	return document.getElementById(id);
}

function HideElem(elemId)
{
	var tmp = El(elemId);
	if (tmp) tmp.style.display = 'none';
}

function FixLocation(divId, w, h)
{
	var tmpDiv = El(divId);
	if (tmpDiv == null) return;

	var topDelta;
	// Get the scrolly delta
	if (self.pageYOffset)
		topDelta = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		topDelta = self.document.documentElement.scrollTop;
	else if (document.body)
		topDelta = document.body.scrollTop;
	else
		topDelta = Math.floor(h / 2);

	topDelta -= Math.floor(h / 2);

	// set the position
	if (window.innerHeight)
		tmpDiv.style.top = (window.innerHeight / 2 + topDelta) + "px";
	else if (document.documentElement.clientHeight)
		tmpDiv.style.top = (document.documentElement.clientHeight / 2  + topDelta) + "px";
	else
		tmpDiv.style.top = (300 + topDelta) + "px";

	if (window.innerWidth)
		tmpDiv.style.left = (window.innerWidth / 2 - Math.floor(w / 2)) + "px";
	else if (document.documentElement.clientWidth)
		tmpDiv.style.left = (document.documentElement.clientWidth / 2 - Math.floor(w / 2)) + "px";
}

function BlockBackgroundAccess()
{
	// block access to elements on the back
	var tmpDiv = El("fullPageBlankDiv");
	tmpDiv.style.opacity = 0.4;
	tmpDiv.style.filter = "alpha(opacity=40)";
	tmpDiv.style.top = "0px";
	tmpDiv.style.left = "0px";

	var h = 0;
	if (window.innerHeight)
		h = window.innerHeight;
	else if (document.documentElement.clientHeight)
		h = document.documentElement.clientHeight;

	if (document.body.scrollHeight > document.body.offsetHeight)
	{
		tmpDiv.style.width = document.body.scrollWidth + "px";
		tmpDiv.style.height = Math.max(h, document.body.scrollHeight) + "px";
	}
	else
	{
		tmpDiv.style.width = document.body.offsetWidth + "px";
		tmpDiv.style.height = Math.max(h, document.body.offsetHeight) + "px";
	}
	tmpDiv.style.display = "block";
}
// PopUp a div and position it at the center of the page, w and h are the width and height
function PopUpCenter(divId, w, h)
{
	// set the location
	FixLocation(divId, w, h);

	var tmpDiv = El(divId);
	if (tmpDiv == null) return;
	// show the div
	tmpDiv.style.display = "block";
	BlockBackgroundAccess();
}

function PreloadJoinForm(url)
{
	var content = "<iframe src='" + url + "' style='border: none; width: 604px; height: 616px; background: none;' scrolling='no' frameborder='0' name='joinFrame'></iframe>";
	El("joinFormDiv").innerHTML = content;
}
function PopJoinForm(url, id)
{
	if (id) PreloadJoinForm(url + "&amp;pic=" + encodeURIComponent(id));
	PopUpCenter("joinFormDiv", 604, 560);
	// window.onbeforeunload = function() {};
}

function ReplaceJoinLinks(url)
{
	var links = document.getElementsByTagName("A"); // get an array of all the links.
	for (var i = 0; i < links.length; i++)
	{
   		if (links[i].className == "joinPageLink")
			links[i].href = url;
	}
}

function PopJoin()
{
	PopJoinForm(joinLink);
	var d = El("trailerDiv");
	if (d) d.innerHTML = oldHTML;
}

function KeepCenter(divId, w, h)
{
	FixLocation(divId, w, h);
	setTimeout("KeepCenter('" + divId + "', " + w + ", " + h + ")", 50);
}

var oldHTML = "";
var gid = "";

function SetTrailerCode(code)
{
	var d = El("trailerDiv");
	if (d)
	{
		oldHTML = d.innerHTML;
		d.innerHTML = code;
	}
}

// Left-trim a string and return the result
function LTrimStr(sString)
{
	if (sString == '') return '';

	i = 0;
	while (i < sString.length && sString.charAt(i) == ' ') i++;

	sString = sString.substring(i, sString.length);
	return sString;
}

// Get an instance of the XMLHttpRequest object
function GetXHRObject()
{
	var xhr = null; // The XMLHTTPRequest object

	if (window.XMLHttpRequest) // Gecko and Opera
		xhr = new XMLHttpRequest();
	else if (window.ActiveXObject) // Internet Explorer
		xhr = new ActiveXObject("Microsoft.XMLHTTP");

	return xhr;
}

// Send a GET request and load the received content into the trailer div
function LoadTrailerAsync(url, actn)
{
	var xhr = GetXHRObject();

	if (xhr == null)
	{
		alert("Sorry, your browser doesn't support AJAX");
		return;
	}
	xhr.open("GET", url, true);

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) // data received
		{
			var r = LTrimStr(xhr.responseText);
			if (r == "")
				alert("Transmission failed! Connection error or server too busy!");
			else if (r == "NoTrailer")
				PopJoinForm(joinLink);
			else
				eval(actn + "r);");
		}
	}
	// Send the request
	xhr.send(null);
}

function WMVTrailer(url)
{
	location.href = url;
}

function FlashTrailerPopUp(w, h, code)
{
	var d = El("playerDiv");
	d.style.width = (w + 21) + "px";
	d.style.height = (h + 5) + "px";
	El("playerClose").style.left = (w - 9) + "px";
	El("playerContent").innerHTML = code;
	d.style.display = "block";
	FixLocation("playerDiv", (w + 21), (h + 45));
	BlockBackgroundAccess();
}

function FlashTrailer(w, h, code)
{
	El("trailerDiv").innerHTML = code;
}

function TrailerEnded()
{
	El("trailerDiv").innerHTML = "&nbsp;";
}

function TrailerLarge()
{
	LoadTrailerAsync("/trailers/trailers.php?girlid=" + encodeURIComponent(gid) + "&size=big&type=wmv", "WMVTrailer(");
}

function TrailerMedium()
{
	LoadTrailerAsync("/trailers/trailers.php?girlid=" + encodeURIComponent(gid) + "&size=med&type=wmv", "WMVTrailer(");
}

function TrailerSmall()
{
	LoadTrailerAsync("/trailers/trailers.php?girlid=" + encodeURIComponent(gid) + "&size=small&type=wmv", "WMVTrailer(");
}

function FlashTrailerLarge()
{
	LoadTrailerAsync("/trailers/trailers.php?girlid=" + encodeURIComponent(gid) + "&size=big&type=flash", "FlashTrailer(640, 480, ");
}

function FlashTrailerMedium()
{
	LoadTrailerAsync("/trailers/trailers.php?girlid=" + encodeURIComponent(gid) + "&size=med&type=flash", "FlashTrailer(480, 360, ");
}

function FlashTrailerSmall()
{
	LoadTrailerAsync("/trailers/trailers.php?girlid=" + encodeURIComponent(gid) + "&size=small&type=flash", "FlashTrailer(320, 240, ");
}
