// 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 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("ctrlVehicle_ulFleetList")) return false;
	var gallery = document.getElementById("ctrlVehicle_ulFleetList");
	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("ctrlVehicle_imgMainImage")) return true;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("ctrlVehicle_imgMainImage");
	placeholder.setAttribute("src",source);
	
	if(!document.getElementById("ctrlVehicle_ancMainImage")) return true;
	var mi = document.getElementById("ctrlVehicle_ancMainImage");
	var miplaceholder = document.getElementById("ctrlVehicle_ancMainImage");
	miplaceholder.setAttribute("href", source.replace('/images/enthusiastfleet/large/','/images/enthusiastfleet/extralarge/'));
	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;
	}
}

// Add a popup onclick function to any links with a class of popup
function prepareLinks(){
	var links = document.getElementsByTagName("a");
	for(var i=0; i<links.length;i++){
			if(links[i].className == "popup"){
			links[i].onclick = function(){
				popUp(this.getAttribute("href"), 375, 400);
				return false;
			}
		}
	}
}

// Open a popup window
function popUp(winURL, width, height){
	//window.open(winURL,"popup","width=" + width + ",height=" + height + ",scrollbars=yes")	
	newwindow=window.open(winURL,"popup","width=" + width + ",height=" + height + ",scrollbars=yes");
}
