//(C) Stephen Daly
// www.stephendaly.org
// Date: 11/3/2008
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// Checks if the browsers is IE or another.
// document.all will return true or false depending if its IE
// If its not IE then it adds the mouse event
if (!document.all)
document.captureEvents(Event.MOUSEMOVE)

// On the move of the mouse, it will call the function getPosition
document.onmousemove = getPosition;

// These varibles will be used to store the position of the mouse
var X = 0
var Y = 0
var dispmode='none';


var windowState = (function(){
	var readScroll = {scrollLeft:0,scrollTop:0};
	var readSize = {clientWidth:0,clientHeight:0};
	var readScrollX = 'scrollLeft';
	var readScrollY = 'scrollTop';
	var readWidth = 'clientWidth';
	var readHeight = 'clientHeight';

function otherWindowTest(obj){
	if((document.compatMode)&&(document.compatMode == 'CSS1Compat')&&(document.documentElement)){
		return document.documentElement;
	}else if(document.body){
		return document.body;
	}else{
		return obj;
	}
};
if((typeof this.innerHeight == 'number')&&(typeof this.innerWidth == 'number')){
	readSize = this;
	readWidth = 'innerWidth';
	readHeight = 'innerHeight';
}else{
	readSize = otherWindowTest(readSize);
}

if((typeof this.pageYOffset == 'number')&&(typeof this.pageXOffset == 'number')){
	readScroll = this;
	readScrollY = 'pageYOffset';
	readScrollX = 'pageXOffset';
}else{
	readScroll = otherWindowTest(readScroll);
}
return {
	getScrollX:function(){
		return (readScroll[readScrollX]||0);
	},
	getScrollY:function(){
		return (readScroll[readScrollY]||0);
	},
	getWidth:function(){
		return (readSize[readWidth]||0);
	},
	getHeight:function(){
		return (readSize[readHeight]||0);
	}
};
})();
var viewPortWidth = windowState.getWidth();
var viewPortHeight = windowState.getHeight();
var horizontalScroll = windowState.getScrollX();
var verticalScroll = windowState.getScrollY();

function getStyleObj(id){
	var obj = null;
	if(document.getElementById){
	obj = document.getElementById(id);
	}else if(document.all){
	obj = document.all[id];
	}else if(document.layers){
	obj = document.layers[id];
	}
	return (obj && obj.style) || obj;
}

// This is the function that will set the position in the above varibles 
function getPosition(args) 
{
  // Gets IE browser position
  if (document.all) 
  {
    X = event.clientX + document.body.scrollLeft
    Y = event.clientY + document.body.scrollTop
  }
  
  // Gets position for other browsers
  else 
  {  
    X = args.pageX
    Y = args.pageY
  }  
}
function backgroundFilter()
{
    var div;
    
    if(document.getElementById)
    // Standard way to get element
    div = document.getElementById('backgroundFilter'); 
    else if(document.all) 
    // Get the element in old IE's 
    div = document.all['backgroundFilter']; 
    
    // if the style.display value is blank we try to check it out here 
    if(div.style.display==''&&div.offsetWidth!=undefined&&div.offsetHeight!=undefined)
    {
        div.style.display = (div.offsetWidth!=0&&div.offsetHeight!=0)?'block':'none'; 
    }
    
    // If the background is hidden ('none') then it will display it ('block').
    // If the background is displayed ('block') then it will hide it ('none').
    div.style.display = dispmode;//(div.style.display==''||div.style.display=='block')?'none':'block';
}

function popUp()
{
    var div;
    
    if(document.getElementById)
    // Standard way to get element
    div = document.getElementById('popupWindow'); 
    else if(document.all) 
    // Get the element in old IE's 
    div = document.all['popupWindow']; 
    
    // if the style.display value is blank we try to check it out here 
    if(div.style.display==''&&div.offsetWidth!=undefined&&div.offsetHeight!=undefined)
    {
        div.style.display = (div.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none'; 
    }

    
    // If the PopUp is hidden ('none') then it will display it ('block').
    // If the PopUp is displayed ('block') then it will hide it ('none').
	dispmode=(div.style.display==''||div.style.display=='block')?'none':'block';
//    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';
	var c=readCookie('donateshown');
	if (c) {
		dispmode='none';
	} else {
		createCookie('donateshown','yes');
	}
    
	backgroundFilter();

	var hPos = Math.round(horizontalScroll+((viewPortWidth-384)/2));
	var vPos = Math.round(verticalScroll+((viewPortHeight-260)/2));
	hPos = (hPos < 0)?0:hPos;
	vPos = (vPos < 0)?0:vPos;

	
	div.style.display = dispmode;
	var divStyleRef = div.style; //getStyleObj('popupWindow');
	if(divStyleRef){	
		var positionMod = (typeof divStyleRef.top == 'string')?"px":0;
		divStyleRef.top = vPos + positionMod;
		divStyleRef.left = hPos + positionMod;
		//var x = (backgroundFilter.innerWidth / 2) - (div.offsetWidth / 2);
//		var y = (backgroundFilter.innerHeight / 2) - (div.offsetHeight / 2);         
	//	var positionMod = (typeof divStyleRef.top == 'string')?"px":0;
		//div.style.top = y;
		//div.style.left = x;
	}

}


