function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}

function pageHeight()
{
		return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function pageWidth()
{
		return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function changeVisibility(element, arg) {
	document.getElementById(element).style.visibility = arg;
}

function putPosition(element, image, down, right)
{
	// Get Image Location
	var img = document.getElementById(image);
	var position = new Array();
	position = findPos(img);
	
	// Get image hieght
	var newImg = new Image();
	newImg.src = document.getElementById(image).src;
	var height = newImg.height;
	
	// Set the postion
	var ele = document.getElementById(element);
	ele.style.left = position[0] + right + "px";
	ele.style.top = position[1] + down + "px";
	
	// Check to make sure not key item
	var cont = true;
	if(element == 'paintball' || element == 'remote_controlled_vehicles' || element == 'skateboarding' || element == 'airsoft')
	{
		cont = false;
	}
	
	// Get Window Height
	var winH = pageHeight();
		
	// Move the div up until in fits on screen
	while(cont)
	{
		var pos = ele.style.top.replace('px', '') - 0;
		//var off = ele.offsetHeight.replace('px', '') - 0;
		if(pos + ele.offsetHeight > winH && pos + ele.offsetHeight - img.height > position[1])
		{
			if(pos <= 0)
			{
				ele.style.top = '0px';
				cont = false;
			}
			else
			{
				ele.style.top = (pos - 10) + 'px';
			}
		}
		else
		{
			cont = false;
		}
	}
}

function findPos(element)
{
	var left = findPosX(element);
	var top = findPosY(element);
	
	return [left, top];
}

function show(element, image, down, right)
{
	putPosition(element, image, down, right);
	changeVisibility(element, "visible");
}

function remove(element)
{
	changeVisibility(element, "hidden");
}

var showing = null;
function showInfo(element, title)
{
	if(showing != null)
	{
		changeVisibility(showing, "hidden");
		changeVisibility("close", "hidden");
		changeVisibility("back_info", "hidden");
		showing = null;
		return;
	}
	
	// Get Image Location
	var ele = document.getElementById(element);
	
	var height = ele.height;
	
	// Set the image in middle of screen
	var top = (pageHeight() / 2) - (ele.offsetHeight / 2);
	var left = (pageWidth() / 2) - (ele.offsetWidth / 2);
		
	// Set the postion
	ele.style.left = left + "px";
	ele.style.top = top + "px";
	
	// Set close position
	var cls = document.getElementById("close");
	cls.style.top = top + ele.offsetHeight + 'px';
	cls.style.left = left + 'px';
	
	// Set title
	var tit = document.getElementById("info_title");
	tit.innerHTML = title;
	tit.style.top = top - tit.offsetHeight + 'px';
	tit.style.left = left + 'px';
	
	// Set visibility
	changeVisibility(element, "visible");
	changeVisibility("close", "visible");
	changeVisibility("back_info", "visible");
	changeVisibility("info_title", "visible");
	showing = element;
	
	// Stop scrolling
	window.scroll(0,0);
	document.body.style.overflow = 'hidden';
	document.body.scroll = 'no';
}

function closeInfo()
{
	changeVisibility("back_info", "hidden");
	changeVisibility(showing, "hidden");
	changeVisibility("close", "hidden");
	changeVisibility("info_title", "hidden");
	
	document.body.style.overflow = 'auto';
	document.body.scroll = 'yes';
	
	showing = null;
}