function getPosition(width, height) {
	var pageSize = getPageSize();
	
	var position = {x: 0, y: 0};
	position.x = Math.round((pageSize.width - width)/2);
	position.y = Math.round((pageSize.height - height)/2);
	
	if(position.y < 0)
		position.y = 0;
		
	if(position.x < 0)
		position.x = 0;
	
	return position;
}

function createElement(element, parameters) {
	var newElement = document.createElement(element);
	
	for(var temp in parameters) {
		if(temp == "className") {
			newElement.setAttribute("class", parameters[temp]);
		} else if(temp == "style") {
			//work around for IE 7
			newElement.style.cssText = parameters[temp];
		} else {
			newElement.setAttribute(temp, parameters[temp]);
		}
	}
	
	return newElement;
}

function getPageSize() {
	var width = 0
	var height = 0;
	
	if(window.innerWidth == null) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	} else {
		width = window.innerWidth;
		height = window.innerHeight;
	}
	
	return {width: width, height: height};
}

function PopupCover() {
	PopupCover.coverCount++;
	if(PopupCover.activeCover != null) return;
	PopupCover.activeCover = this;
	var cover = document.createElement("div");
	cover.className = "popupcover";

	validate();

	this.close = function() {
		cover.parentNode.removeChild(cover);
		PopupCover.activeCover = null;
	};

	function validate() {
		cover.style.width = "100%";
		cover.style.height = "100%";
	}

	document.body.appendChild(cover);
}

PopupCover.activeCover = null;
PopupCover.coverCount = 0;
PopupCover.close = function() {
	if(--PopupCover.coverCount < 0)
		PopupCover.coverCount = 0;
	if(PopupCover.coverCount > 0) return;
	if(PopupCover.activeCover != null)
		PopupCover.activeCover.close();
};

function track(tag, kvp, redirectUrl) {
	var vars = {tag: tag, kvp: kvp};
	if(redirectUrl != null)
		ajax('/xml/track.php', vars, function(){ location.href = redirectUrl; });
	else
		ajax('/xml/track.php', vars, function(){});
}
