// JavaScript Document

//TEMP set div ID
var div_id = "sq_outer_B";

// Fade animation effect
function setOpacity(level) {
  var div = document.getElementById(div_id);
  div.style.opacity = level;
  div.style.MozOpacity = level;
  div.style.KhtmlOpacity = level;
  //div.style.filter = "alpha(opacity=" + (level * 100) + ");";
}
// Fade speed
var duration = 1000;
//fade steps
var fade_steps = 30;

function fadeIn(){
  for (i = 0; i <= 1; i += (1 / fade_steps)) {
    setTimeout("setOpacity(" + i + ")", i * duration);
  }
}


window.size = function() //Function for determining the size of the window that the user is viewing the page in, including how far they have scrolled.  This allows us to position an element (like a popup) perfectly in the center of the user's browser window, no matter what size the window is. */
{
	var w = 0;
	var h = 0;
	//IE
	if(!window.innerWidth)
	{
	//strict mode
	if(!(document.documentElement.clientWidth == 0))
	{
	w = document.documentElement.clientWidth;
	h = document.documentElement.clientHeight;
	}
	//quirks mode
	else
	{
	w = document.body.clientWidth;
	h = document.body.clientHeight;
	}
	}
	//w3c
	else
	{
	w = window.innerWidth;
	h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function()
	{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;
	//IE
	if(!window.pageYOffset)
	{
	//strict mode
	if(!(document.documentElement.scrollTop == 0))
	{
	offsetY = document.documentElement.scrollTop;
	offsetX = document.documentElement.scrollLeft;
	}
	//quirks mode
	else
	{
	offsetY = document.body.scrollTop;
	offsetX = document.body.scrollLeft;
	}
	}
	//w3c
	else
	{
	offsetX = window.pageXOffset;
	offsetY = window.pageYOffset;
	}
	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;
	return{x:_x,y:_y};
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function reposition()
{
	var div=document.getElementById(div_id);
	if(div_id == "sq_outer_A")
	{
		var w = window.size().width - 100;
		var h = window.size().height - 50;
		var dims = window.center({width:w,height:h});
		//alert(div);
		
		div.style.width = w;
	}
	else if(div_id == "sq_outer_B")
	{
		var w = 500;
		var h = 565;
		var dims = window.center({width:w,height:h});
	}
	div.style.width = w;
	if(div.offsetHeight < h) 
			{
				h = div.offsetHeight;
			}
		
	div.style.height = h;	
	div.style.top = dims.y + "px";
	div.style.left = dims.x - 10 + "px";
}

function squeeze(showhide){
	var div=document.getElementById(div_id);
	if(div_id == "sq_outer_A")
	{
		var w = window.size().width - 100;
		var h = window.size().height - 50;
		var dims = window.center({width:w,height:h});
		//alert(div);
		
		div.style.width = w;
	}
	else if(div_id == "sq_outer_B")
	{
		var w = 500;
		var h = 565;
		var dims = window.center({width:w,height:h});
	}
	//div.style.height = h;
	var overlay = document.getElementById('sq_overlay');			
	if(showhide == "show"){
		overlay.style.display="block";//Display the graying-out effect
		overlay.style.height = getDocHeight();

		if(div_id == "sq_outer_A")
		{
			div.style.display="block"; // If the function is called with the variable 'show', show the login box 
		// Center the box in the window:
		}
		else if(div_id == "sq_outer_B")
		{
			div.style.display = "block";
			
			if(navigator.appName != "Microsoft Internet Explorer")
			{
				fadeIn();
			}
			reposition();
			
		}
		
		if(div.offsetHeight > h) 
		{
			h = div.offsetHeight;
		}
		else
		{
			window.onscroll = reposition;
		}
		
		div.style.height = h;
		
		div.style.top = dims.y + "px";
		div.style.left = dims.x - 10 + "px";
		//window.onscroll = reposition;	
		window.onresize = reposition;	
		
		//alert(window.size().width);
	}
	else if(showhide == "hide")
	{
		div.style.display="none"; // If the function is called with the variable 'hide', hide the login box 
		overlay.style.display="none"; // Hide the gray overlay
	}
	
	//Testing
	//alert(document.getElementById('sq_outer_A').clientWidth);
}

//Add in the transparent overlay.  Have to do it this way to get it into the right place in the membergate code.


function pop_init(){
	if(div_id == "sq_outer_A") 
	{
		squeeze('show');
	}
	else if(div_id == "sq_outer_B")
	{
		setTimeout("squeeze('show')",5000);
	} 
}
onload = function()
{
	//alert(div_id);
	var _pop = document.createElement('div');
	_pop.id = 'sq_overlay';
	document.body.appendChild(_pop);
	pop_init();
}
