﻿var defaultOpacity = .90;
var defaultFadeRate = 25;
var defaultFadeStep = .02;

function Get(targetItem)
{
	return document.getElementById(targetItem);
}


function setClass(targetItem, className)
{
	targetItem.className = className;
}

function show(targetItem)
{
	target = Get(targetItem);
	target.style.display = "";
}
function hide(targetItem)
{
	target = Get(targetItem);
	target.style.display = "none";
}

function fadeOut_default(targetItem)
{
	fadeOut(targetItem, defaultFadeStep, defaultFadeRate);
}

function fadeOut(targetItem, fadeStep, fadeRateInMs)
{	
	target = Get(targetItem); 
	if (target.cancelFade != null)
	{
		target.cancelFade = null;
		target.fading = null;
		return;
	}
	target.fading = 'y';
	currentOpacity = target.style.opacity;
	

	if (currentOpacity == null)
		currentOpacity = 1;
		
	currentOpacity = currentOpacity - fadeStep;
	if (currentOpacity < 0)
	{
		hide (targetItem);
		currentOpacity = 0;
		target.cancelFade = null;
		target.fading = null;		
	}
	else
	{
		faderTimer = setTimeout('fadeOut("' + targetItem + '",' + fadeStep + ',' + fadeRateInMs + ')', fadeRateInMs);  
	}
	setOpacity(targetItem, currentOpacity);
}

function setOpacity(targetItem, newOpacity)
{	
	target = Get(targetItem);
	target.style.filter = "alpha(opacity=" + newOpacity * 100 + ")";
	target.style.opacity = newOpacity;
	target.setAttribute("style.-moz-opacity", newOpacity);
}
function openWindow(targetURL, name)
{
	window.open(targetURL, name);
}

function swapImage(targetItem, firstImage, secondImage)
{
	target = Get(targetItem);
	targetImageStart = target.src.lastIndexOf("/") + 1;
	targetImageName = target.src.substr(targetImageStart);
	firstImageName = firstImage.substr( firstImage.lastIndexOf("/") + 1);
	secondImageName = secondImage.substr(secondImage.lastIndexOf("/") + 1);
	if (targetImageName.toLowerCase() == firstImageName.toLowerCase())
		target.src = target.src.substr(0, targetImageStart) + secondImageName;
	else
		target.src = target.src.substr(0, targetImageStart) + firstImageName;
}

function showOrHide(targetItem)
{
	target = Get(targetItem);
	if (target.style.display == "none")
		target.style.display = "inline";
	else
		target.style.display = "none";
}

function showMenu(targetMenu, targetButton)
{	
	button = Get(targetButton);
	menu = Get(targetMenu);
	
	setOpacity(targetMenu, defaultOpacity);
	
	if (menu.fading != null)
	{
		
		menu.cancelFade = 'y'; 
	}

	anchorBottomLeft(button, menu);


	show(targetMenu);
}

function anchorBottomLeft(anchor, target)
{
	x = posX(anchor)
	y = posY(anchor) + anchor.clientHeight;	
	target.style.left = x;	
	target.style.top = y;
}

function posY(target)
{
	try
	{
		var y = 0;
		if ( target.offsetTop == null)
			return 0	
		if ( ! isNaN(target.offsetTop))
			y = target.offsetTop;	
		if (target.offsetParent != null)
			y += posY(target.offsetParent);

		return y;
	}
	catch (e) {}
	return 0;
}

function posX(target)
{
	try
	{
		var x = 0;
		if ( target.offsetLeft == null)
			return 0
		if ( ! isNaN(target.offsetLeft))
			x = target.offsetLeft;	
		if (target.offsetParent != null)
			x += posX(target.offsetParent);
		return x;
	}
	catch (e) {}
	return 0;
	
}

function setPosition(targetItem, newX, newY)
{
	target = Get(targetItem);
	target.style.left = newX + "px";	
	target.style.top = newY + "px";
}

function center(targetItem, allowNegatives) // allowNegatives defaults to false
{
	target = Get(targetItem);
	if (window.innerWidth)
	{
		newX = Math.floor((window.innerWidth - target.clientWidth) / 2);
		newY = Math.floor((window.innerHeight - target.clientHeight) / 2);
	}
	else
	{
		newX = Math.floor((document.documentElement.clientWidth - target.clientWidth) / 2);
		newY = Math.floor((document.documentElement.clientHeight - target.clientHeight) / 2);
	}
	if ( ! allowNegatives)
	{
		if (newX < 0)
			newX = 0;
		if (newY < 0)
			newY = 0;
	}
	setPosition(targetItem, newX, newY);
}

function alignHorizontal(targetItem, allowNegatives)
{
	target = Get(targetItem);
	if (window.innerWidth)
	{
		newX = Math.floor((window.innerWidth - target.clientWidth) / 2);
	}
	else
	{
		newX = Math.floor((document.documentElement.clientWidth - target.clientWidth) / 2);
	}
	if ( ! allowNegatives)
	{
		if (newX < 0)
			newX = 0;
	}
	target.style.left = newX + "px";
}