/******************************************

	Javascript library for Digitaria
	Author: Michael Turnwall, Digitaria Inc.
	Created: 05.18.2006
	Modified: 05.19.2006
	addLoadEvent by Simon Willison - http://simon.incutio.com

******************************************/

/***** XMLHTTPRequest object section  *****/
// Create XMLHttpRequest object
var xmlhttp;
xmlhttp = false;
// native XMLHttpRequest object like firefox and safari
if(window.XMLHttpRequest)
{
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch(e)
	{
		xmlhttp = false;
	}
// IE/Windows ActiveX version (why do people still use this browser?)
}
else if(window.ActiveXObject)
{
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
}
if(!xmlhttp)
	alert("Error loading XMLHTTP object");
/****** End XMLHTTPRequest object creation  *****/

// onload function written by Simon Willison - http://simon.incutio.com
// use this function instead of window.onload - usage addLoadEvent(functionName)
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
// end addLoadEvent()

// scan all links looking for the rel attribute
// this attribute tells the library what functions to call and passes various 
var initScans = function() 
{
	var a = document.getElementsByTagName("a");
	for(var i = 0; i < a.length; i++)
	{
		a[i].onclick = function()
			{
				if(rel = this.getAttribute("rel"))
				{
					relSplit = new Array();
					relSplit = rel.split(".");
					if(relSplit[0] == "slide")
					{
						initSlide(relSplit[1],relSplit[2]);
						return false;
					}
					else if(relSplit[0] == "show")
					{
						showHide(relSplit[1]);
						return false;
					}
				}
			}
	}
}
var oldBox = new Array;
function initSlide(id, targetHeight)
{
	if(oldBox[0] != null)
	{
		if(oldBox[0] != id)
		{
			oldBox[1].style.display = "none";
			var oldHeight = oldBox[0].offsetHeight;
			slideUp(oldBox[0], oldHeight, id, targetHeight);
			slideDown(id, targetHeight);	
		}
		else if(oldBox[0] == id)
		{
			if(oldBox[1].style.display == "block")
			{
				
				oldBox[1].style.display = "none";
				var oldHeight = oldBox[0].offsetHeight;
				slideUp(oldBox[0], oldHeight);
			}
			else
			{
				slideDown(id, targetHeight);
			}
		}
		else
		{
			oldBox[1].style.display = "none";
			var oldHeight = oldBox[0].offsetHeight;
			slideUp(oldBox[0], oldHeight);
		}
	}
	else
	{
		slideDown(id, targetHeight);
	}	
}

function slideDown(id, targetHeight)
{
	var element = document.getElementById(id);
	var elHeight = element.offsetHeight;
	/*if(elHeight < targetHeight)
	{
		elHeight += 10;
		element.style.height = elHeight + "px";
		var timer1 = setTimeout("slideDown('" + id + "'," + targetHeight + ")",10);
	}
	else
	{
		var temp = id.substr(0,(id.length - 3));
		var elList = document.getElementById(temp);
		elList.style.display = "block";
		fadeIn(temp, 0, 50);
		oldBox[0] = id;
		oldBox[1] = elList;
	}*/
	element.style.height = (targetHeight * 1 + 5) + "px";
	var temp = id.substr(0,(id.length - 3));
	var elList = document.getElementById(temp);
	elList.style.display = "block";
	fadeIn(temp, 0, 50);
	oldBox[0] = id;
	oldBox[1] = elList;
}
function slideUp(id, oldHeight, newID, newHeight)
{
	//alert(oldHeight);
	var el = document.getElementById(id);
	var elHeight = el.offsetHeight;
	/*if(elHeight > 10)
	{
		elHeight -= 20;
		el.style.height = elHeight + "px";
		setTimeout("slideUp('" + id + "'," + oldHeight + ",'" + newID + "'," + newHeight + ")",20);
	}
	else
	{
		el.style.height = "0px";
		if(newID != "undefined")
			slideDown(newID, newHeight);
	}*/
	var temp = id.substr(0,(id.length - 3));
	var elList = document.getElementById(temp);
	fadeOut(temp, 100, 20);
	//elList.style.display = "none";
	el.style.height = "0px"
}

var oldHideID = "";
var oldHeadlineID = "";
function showHide(id)
{
	var element = document.getElementById(id);
	var display = element.style.display;
	var headTemp = id.substr(-4,(id.length - 4));
	//alert(headTemp);
	var headlineID = id.substr(0,(id.length - 4));
	headlineID += "Headline";
	//if(headTemp == "Body")
	//{
		headline = document.getElementById(headlineID);
		//alert(headlineID);
	//}
	if(oldHideID == id)
	{
		element.style.display = (element.style.display == "block") ? "none" : "block";
		headline.style.display = (headline.style.display == "block") ? "none" : "block";
		/*if(element.style.display == "block")
		{
			fadeOut(id,100,25);
			headline.style.display = "block";
		}
		else
		{
			element.style.display = "block";
			headline.style.display = "none";
		}*/
	}
	else if(oldHideID != id && oldHideID != "")
	{
		var oldEl = document.getElementById(oldHideID);
		var oldHeadline = document.getElementById(oldHeadlineID);
		oldEl.style.display = "none";
		oldHeadline.style.display = "block";
		element.style.display = "block";
		fadeIn(id,0,100);
		headline.style.display = "none";
	}
	else
	{
		element.style.display = "block";
		fadeIn(id,0,100);
		headline.style.display = "none";
	}	
	
	oldHideID = id;
	oldHeadlineID = headlineID;
}

function growIt(id)
{
	var imageObj = document.getElementById(id);
	var growW = imageObj.offsetWidth;
	var growH = imageObj.offsetHeight;
	var imgWidth = imageObj.getAttribute("width");
	var imgHeight = imageObj.getAttribute("height");
	if(imgWidth > imgHeight)
	{
		var ratio = imgHeight/imgWidth;
		//alert(ratio);
		growW += 20; growH += Math.round(20 * ratio);
	}
	if(growW < imgWidth && growH < imgHeight)
	{
		growW = growW + "px";
		growH = growH + "px";
		imageObj.style.width = growW;
		imageObj.style.height = growH;
		setTimeout("growIt('"+id+"')",10);
	}
	else if(growW < imgWidth)
	{
		growW = growW + "px";
		imageObj.style.width = growW;
		setTimeout("growIt('"+id+"')",10);
	}
	else
	{
		imageObj.style.width = imgWidth;
		imageObj.style.height = imgHeight;
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	
	// Safari < 1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity,speed)
{
	if (document.getElementById)
	{
		obj = document.getElementById(objId);
		if (opacity <= 100)
		{
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+","+speed+")", 50);
		}
	}
}
function fadeOut(objId,opacity,speed)
{
	if (document.getElementById)
	{
		obj = document.getElementById(objId);
		if (opacity >= 0)
		{
			setOpacity(obj, opacity);
			opacity -= 10;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+","+speed+")", speed);
		}
		else
		{
			obj.style.display = "none";
		}
	}
}

// Pop Up Window scripts 	
var win;
function openPop(windowName, url, width, height, scroll) {
	var params;
	params = "toolbar=0,";
	params += "location=0,";
	params += "directories=0,";
	params += "status=0,";
	params += "menubar=0,";
	params += "scrollbars="+scroll+",";
	params += "resizable=0,";
	params += "top=50,";
	params += "left=50,";
	params += "width="+width+",";
	params += "height="+height;
	win = window.open(url, windowName, params);
}

function openWin(windowName, url) {
	win = window.open(url, windowName);
}