﻿function showImage(imageUrl, title)
{
	var image = new Image();
	image.onload = function() { showImageCallback(image, title); };
	image.src = imageUrl;
	document.body.style.cursor = 'wait';
}

function showImageCallback(image, title)
{
	var imageElement = document.createElement("IMG");
	imageElement.src = image.src;
	showModalDialog(imageElement, image.width, image.height, title);
	document.body.style.cursor = 'default';
}

function showModalDialog(element, width, height, title)
{
	var outerElement = document.createElement("DIV");
	outerElement.style.position = "fixed";
	outerElement.style.zIndex = 1000;
	outerElement.style.top = outerElement.style.left = outerElement.style.right = outerElement.style.bottom = "0px";
	document.body.appendChild(outerElement);

	var backgroundElement = document.createElement("DIV");
	backgroundElement.style.position = "absolute";
	backgroundElement.style.top = backgroundElement.style.left = backgroundElement.style.right = backgroundElement.style.bottom = "0px";
	backgroundElement.style.opacity = "0.65";
	backgroundElement.style.filter = "alpha(opacity=65)";
	backgroundElement.style.backgroundColor = "black";
	backgroundElement.onclick = function() { document.body.removeChild(outerElement); };
	outerElement.appendChild(backgroundElement);
	
	var panelElement = document.createElement("DIV");
	panelElement.style.position = "absolute";
	panelElement.style.top = "50%";
	panelElement.style.left = "50%";
	panelElement.style.marginTop = (-height / 2 - 17) + "px";
	panelElement.style.marginLeft = (-width / 2 - 9) + "px";
	panelElement.style.width = (width + 16) + "px";
	panelElement.style.height = (height + 34) + "px";
	panelElement.style.opacity = "1.0";
	panelElement.style.border = "1px solid white";
	panelElement.style.backgroundColor = "black";
	outerElement.appendChild(panelElement);
	
	var titleElement = document.createElement("DIV");
	titleElement.style.position = "absolute";
	titleElement.style.top = "2px";
	titleElement.style.left = "8px";
	titleElement.style.color = "white";
	panelElement.appendChild(titleElement);
	
	var titleTextElement = document.createTextNode(title);
	titleElement.appendChild(titleTextElement);

	var closeElement = document.createElement("A");
	closeElement.href = "#";
	closeElement.onclick = function() { document.body.removeChild(outerElement); return false; };
	closeElement.style.position = "absolute";
	closeElement.style.top = "2px";
	closeElement.style.right = "8px";
	closeElement.style.color = "white";
	panelElement.appendChild(closeElement);
	
	var closeTextElement = document.createTextNode("close [X]");
	closeElement.appendChild(closeTextElement);

	element.style.position = "absolute";
	element.style.bottom = element.style.left = "8px";
	element.style.width = width + "px";
	element.style.height = height + "px"
	panelElement.appendChild(element);
}

function selectTab(tabPanel, index)
{
	var childElements = getChildElements(tabPanel);
	var tabs = getChildElements(childElements[0]);
	var panels = getChildElements(childElements[1]);

	for (var i = 0; i < tabs.length; i++)
	{
		tabs[i].className = (i == index ? "Selected" : "");
	}

	for (var i = 0; i < panels.length; i++)
	{
		panels[i].style.display = (i == index ? "block" : "none");
	}
}

function getChildElements(element)
{
	var index = 0;
	var elementArray = new Array(element.childNodes.length);
	
	for (var i = 0; i < element.childNodes.length; i++)
	{
		if (element.childNodes[i].nodeType == 1)
		{
			elementArray[index++] = element.childNodes[i];
		}
	}
	
	elementArray.length = index;
	
	return elementArray;
}

function trackLinks(pageTracker)
{
	for (var i = 0; i < document.links.length; i++)
	{
		var link = document.links[i];
		var isExternal = link.href.indexOf(document.location.host) == -1 && link.href != "#" && link.href.indexOf("javascript:") != 0;
		var isTrackedPage = link.href.indexOf(".aspx") == -1;
		
		if (link.target == null && isExternal)
		{
			link.target = "_blank";
		}
	
		if (link.onclick == null && (isExternal || isTrackedPage))
		{
			link.onclick = function() { pageTracker._trackPageview(this.href); };
		}
	}
}
