//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset)
	{
		yScroll = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	}
	else if (document.body)
	{// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('', yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY)
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight)
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else
	{ 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	}
	else
	{
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) 
	return arrayPageSize;
}

function getElementWidth(element)
{
   if (typeof element.clip !== "undefined")
   {
      return element.clip.width;
   }
   else
   {
      if (element.style.pixelWidth)
      {
        return element.style.pixelWidth;
      }
      else
      {
        if(element.offsetWidth)
            return element.offsetWidth;
        else
            return element.clientWidth;
      }
   }
}

function getElementHeight(element)
{
   if (typeof element.clip !== "undefined")
   {
      return element.clip.height;
   }
   else
   {
      if (element.style.pixelHeight)
      {
        return element.style.pixelHeight;
      }
      else
      {
        if(element.offsetHeight)
            return element.offsetHeight;
        else
            return element.clientHeight;
      }
   }
}

function showPasswordBox(broadcastLink, displayName)
{
    var passwordBoxOKButton = document.getElementById("passwordBoxOKButton");
	var passwordBoxCaption = document.getElementById("passwordBoxCaption");
	var passwordBoxInput = document.getElementById("passwordBoxInput");
	var pageOverlay = document.getElementById("pageOverlay");
	var passwordBox = document.getElementById("passwordBox");
	
	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();

	passwordBoxOKButton.onclick =
	    function()
	    {
	        hidePasswordBox();
	        window.location = broadcastLink.href + encodeURIComponent(document.getElementById('passwordBoxInput').value);
	        return false;
	    }
	    
	pageOverlay.style.display = "block";
	passwordBox.style.display = "block";
	
	passwordBoxCaption.innerHTML = "Please enter the password for " + displayName + "'s broadcast";
	passwordBoxInput.style.width = getElementWidth(passwordBoxCaption) + 'px';
	passwordBox.style.width = getElementWidth(passwordBoxCaption) + 'px';
	passwordBoxInput.value = ""
	
	var passwordBoxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - getElementHeight(passwordBox)) / 2);
	var passwordBoxLeft = ((arrayPageSize[0] - 20 - getElementWidth(passwordBox)) / 2);
		
	passwordBox.style.left = (passwordBoxLeft < 0) ? "0px" : passwordBoxLeft + "px";
	passwordBox.style.top = (passwordBoxTop < 0) ? "0px" : passwordBoxTop + "px";
	
	pageOverlay.style.height = arrayPageSize[1] + "px";
	
	passwordBoxInput.focus();
}

function hidePasswordBox()
{
	var pageOverlay = document.getElementById("pageOverlay");
	pageOverlay.style.display = "none";
	var passwordBox = document.getElementById("passwordBox");
	passwordBox.style.display = "none";
}

function initializePasswordBox()
{
    var pageBody = document.getElementsByTagName("body").item(0);
	var pageOverlay = document.createElement("div");
	pageOverlay.setAttribute("id", "pageOverlay");
	pageOverlay.onclick = function () {hidePasswordBox(); return false;}
 	
 	pageBody.insertBefore(pageOverlay, pageBody.firstChild);
 	
 	var passwordBox = document.createElement("div");
 	passwordBox.setAttribute("id", "passwordBox");
	pageBody.insertBefore(passwordBox, pageOverlay.nextSibling);
	
	var passwordBoxCaption = document.createElement("label");
	passwordBoxCaption.setAttribute("id", "passwordBoxCaption");
	passwordBox.onkeypress =
	    function(e)
	    {
	    	var key;
            if(window.event)
                key = window.event.keyCode; /* IE */
            else if(e)
                key = e.which; /* FF */
            else
                key = event.which; 
	        if(key == 13 || key == 10)
	        {
	            document.getElementById("passwordBoxOKButton").click();
	            return false;
	        }
	        if(key == 27 || key == 0)
	        {
	            document.getElementById("passwordBoxCancelButton").click();
	            return false;
	        }
	    }
	passwordBox.appendChild(passwordBoxCaption);

	var passwordBoxForm = document.createElement("form");
	passwordBoxForm.setAttribute("id", "passwordBoxForm");
	passwordBoxForm.setAttribute("method", "get");
	passwordBoxForm.setAttribute("action", "");
	passwordBox.appendChild(passwordBoxForm);
	
	var passwordBoxInput = document.createElement("input");
	passwordBoxInput.setAttribute("id", "passwordBoxInput");
	passwordBoxInput.setAttribute("type", "text");
	passwordBoxForm.appendChild(passwordBoxInput);
	
	var passwordBoxOKButton = document.createElement("input");
	passwordBoxOKButton.setAttribute("id", "passwordBoxOKButton");
	passwordBoxOKButton.setAttribute("type", "button");
	passwordBoxOKButton.value = "OK";
	passwordBoxForm.appendChild(passwordBoxOKButton);
	
	var passwordBoxCancelButton = document.createElement("input");
	passwordBoxCancelButton.setAttribute("id", "passwordBoxCancelButton");
	passwordBoxCancelButton.setAttribute("type", "button");
	passwordBoxCancelButton.value = "Cancel";
	passwordBoxCancelButton.onclick =
	    function ()
	    {
	        hidePasswordBox();
	        return false;
	    }
	passwordBoxForm.appendChild(passwordBoxCancelButton);
}

function addLoadEvent(loadEvent)
{	
	var oldOnLoadEntry = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = loadEvent;
	}
	else
	{
		window.onload =
		    function()
		    {
		        oldOnLoadEntry();
		        loadEvent();
		    }
	}

}

addLoadEvent(initializePasswordBox);
