// JavaScript Document
// Add a function to file when the window loads
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{	
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
				oldonload();
				func();
		}
	}
}

function addUnLoadEvent(func)
{
	var oldunload = window.unload;
	if(typeof window.unload != 'function')
	{	
		window.unload = func;
	}
	else
	{
		window.unload = function()
		{
				oldunload();
				func();
		}
	}
}

// function to insert elements
function insertAfter(newElement,targetElement)
{
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement)
	{
		parent.appendChild(newElement);
	}
	else
	{
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

// function loops through all the images in the list and attaches the event
function prepareGallery()
{
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById) return false;
	if(!document.getElementById("thumbs")) return false;
	var gallery = document.getElementById("thumbs");
	var links = gallery.getElementsByTagName("a");
	for (var i=0; i<links.length;i++)
	{
		links[i].onclick = function(){
			//setCaption(this);
			return showPic(this);
		}
		links[i].onkeypress = links[i].onclick;
	}
}

// function to swap out the large image with the thumbnail that has been clicked on
function showPic(whichpic)
{
	if(!document.getElementById("mainpic")) return true;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("mainpic");
	placeholder.setAttribute("src",source);
	return false;
}

// function to swap out the large image with the thumbnail that has been clicked on
function setCaption(whichpic)
{
	if(!document.getElementById("mainthumb")) return true;
	if(!document.getElementById("caption")) return true;
	var source = whichpic.getAttribute("title");
	var target = document.getElementById("caption");
	target.innerHTML=source;
	return false;
}


// function to add a class to an element
function addClass(element,value)
{
	if(!element.className)
	{
		element.className = value;
	}
	else
	{
		newClassName = element.className;
		newClassName += " ";
		newClassName += value;
		element.className = newClassName;
	}
}

// Insert any flash that is required on the current page
function insertFlash()
{
	if(flash.installed)
	var page = document.getElementsByTagName("body")
	var pagename = page[0].getAttribute("id")
	{
		if (pagename=="home")
		{
			var test = document.getElementById("content_main")
			test.innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"/flash/firstintro.swf\" width=\"617\" height=\"266\"><param name=\"movie\" value=\"/flash/firstintro.swf\" /><img src=\"/images/noflash/firstintro.jpg\" width=\"616\" height=\"266\" alt=\"\" /></object>"
		}		
	}
}

// Add a back link under the navigation menu if required on the current page
function insertBackLink()
{
	var page = document.getElementsByTagName("body")
	var pagename = page[0].getAttribute("id")	

	var navmenu = 	document.getElementById("nav_main")
	var backlink = document.createElement("a")
	var backlinktext = document.createTextNode("< back")
	backlink.appendChild(backlinktext)
	if (pagename=="residential")
	{
		backlink.setAttribute("href","/")
		backlink.setAttribute("title","back to homepage")
	}
	else if (pagename=="anlabyhouseapartments")
	{
		backlink.setAttribute("href","/content/residential")
		backlink.setAttribute("title","back to residential")		
	}
	backlink.setAttribute("id","backlink")
	insertAfter(backlink, navmenu)
}