var IMAGEPATH = "/images/";

//Add onload event to set up navigation buttons
addLoadEvent(navigation);

//Add onload event to set target of external links
addLoadEvent(externalLinks);

/*
 * Event handler to execute after page has loaded
 *
 * @param func - Function to execute once the page has loaded 
 */
function addLoadEvent(func)
{
	// Store existing onload event
	var oldonload = window.onload;
	// Assign function parameter to onload event if none exists
	if (typeof window.onload != 'function') {
		window.onload = func;
	// Create new function to call existing and new function
	} else {
		window.onload = function()
		{
			if (oldonload) {
				oldonload();
			}
			func();
		} // function()
	} // if/else
} // addLoadEvent

/*
 * Get the AJAX XMLHttpRequest object
 *
 * @return xmlHttp - XMLHttpRequest object
 */
function ajaxObject(func)
{
	var xmlHttp;

	// Firefox, Opera 8.0+, Safari
	try {
		xmlHttp=new XMLHttpRequest();
	}
	// Internet Explorer 6+
	catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			// Internet Explorer 5.5+
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
//				alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}

	return xmlHttp;
} // ajaxObject

// Change the 'rel="external"' attribute on all anchor tags to the _blank target
function externalLinks()
{
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	} // for...
} // externalLinks()

// Get array of elements with given CSS class
function getElementsByClass(cls)
{
	// Return list if supported by browser
	if (document.getElementsByClassName) {
		return document.getElementsByClassName(cls);
	}

	var retVal = new Array();
	var elements = document.getElementsByTagName("*");
	for(var i=0;i<elements.length;i++) {
		if(elements[i].className.indexOf(" ") >= 0) {
			var classes = elements[i].className.split(" ");
			for(var j=0;j<classes.length;j++){
				if(classes[j] == cls)
					retVal.push(elements[i]);
			}
		}
	    else if(elements[i].className == cls) {
	    	retVal.push(elements[i]);
	    }
	}
	return retVal;
} // getElementsByClass

// Get page name from URI
function getPageName(url)
{
	if (url == null) {
		url = location.href;
	}
	return url.substring(url.lastIndexOf('/') + 1);
} // getCurrentPage()

// Hide element
function hide(id)
{
	document.getElementById(id).style.display = "none";
} // hide()

// Check if current window is displayed in a popup box
function isPopup()
{
	// Determine current window width
	if (window.innerWidth)
	 	width=window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		 width=document.documentElement.clientWidth;
	else if (document.body)
	 	width=document.body.clientWidth;

	// Determine current window height
	if (window.innerHeight)
	 	height=window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		 height=document.documentElement.clientHeight;
	else if (document.body)
		 height=document.body.clientHeight;

	if ((width < 800) || (height < 560))
		return true;
	else
		return false;
} // isPopup

// Obfuscate mailto address
function mailto(address, domain, subject, text)
{
	var at = String.fromCharCode(64); // @
	document.write('<a href="mailto:');
	document.write(address + at + domain);
	if (subject) {
		document.write('?subject='+ subject);		
	}
	document.write('">');
	if (text != null)
		document.write(text);
	else
		document.write(address);
	document.write('</a>');
} // mailto()

/*
 * Highlight proper navigation button and add mouse behaviors
 */
function navigation()
{
	var button;

	button = document.getElementById('nav-about');
	if (document.body.id == 'about') {
		// Highlight "About" navigation button
		swap('about-img', 'nav-about_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('about-img', 'nav-about.gif')};
		button.onmouseout = function() {swap('about-img', 'nav-about_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('about-img', 'nav-about_on.gif')};
		button.onmouseout = function() {swap('about-img', 'nav-about.gif')};
	}	

	button = document.getElementById('nav-contact');
	if (document.body.id == 'contact') {
		// Highlight "Contact Us" navigation button
		swap('contact-img', 'nav-contact_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('contact-img', 'nav-contact.gif')};
		button.onmouseout = function() {swap('contact-img', 'nav-contact_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('contact-img', 'nav-contact_on.gif')};
		button.onmouseout = function() {swap('contact-img', 'nav-contact.gif')};
	}	

	button = document.getElementById('nav-faq');
	if (document.body.id == 'faq') {
		// Highlight "FAQ" navigation button
		swap('faq-img', 'nav-faq_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('faq-img', 'nav-faq.gif')};
		button.onmouseout = function() {swap('faq-img', 'nav-faq_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('faq-img', 'nav-faq_on.gif')};
		button.onmouseout = function() {swap('faq-img', 'nav-faq.gif')};
	}	

	button = document.getElementById('nav-home_services');
	if (document.body.id == 'home_services') {
		// Highlight "Homeowner Services" navigation button
		swap('home_services-img', 'nav-home_services_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('home_services-img', 'nav-home_services.gif')};
		button.onmouseout = function() {swap('home_services-img', 'nav-home_services_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('home_services-img', 'nav-home_services_on.gif')};
		button.onmouseout = function() {swap('home_services-img', 'nav-home_services.gif')};
	}	

	button = document.getElementById('nav-sprayed_systems');
	if (document.body.id == 'sprayed_systems') {
		// Highlight "Sprayed Systems" navigation button
		swap('sprayed_systems-img', 'nav-sprayed_systems_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('sprayed_systems-img', 'nav-sprayed_systems.gif')};
		button.onmouseout = function() {swap('sprayed_systems-img', 'nav-sprayed_systems_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('sprayed_systems-img', 'nav-sprayed_systems_on.gif')};
		button.onmouseout = function() {swap('sprayed_systems-img', 'nav-sprayed_systems.gif')};
	}	

	button = document.getElementById('nav-articles');
	if (document.body.id == 'articles') {
		// Highlight "Creative Articles" navigation button
		swap('articles-img', 'nav-articles_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('articles-img', 'nav-articles.gif')};
		button.onmouseout = function() {swap('articles-img', 'nav-articles_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('articles-img', 'nav-articles_on.gif')};
		button.onmouseout = function() {swap('articles-img', 'nav-articles.gif')};
	}	

	button = document.getElementById('nav-partners');
	if (document.body.id == 'partners') {
		// Highlight "Industry Partners" navigation button
		swap('partners-img', 'nav-partners_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('partners-img', 'nav-partners.gif')};
		button.onmouseout = function() {swap('partners-img', 'nav-partners_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('partners-img', 'nav-partners_on.gif')};
		button.onmouseout = function() {swap('partners-img', 'nav-partners.gif')};
	}	
} // navigate

/*
 * Randomize image
 *
 * @param id    - ID of image element to change
 * @param name  - Base image name
 * @param count - Number of images to randomize
 */
function randomImage(id, name, count)
{
	var elem = $(id);
	// Abort if element ID not found
	if (elem == null)
		return;
	// Abort if count not provided
	if (!count)
		return;
	var num = Math.floor((Math.random()*count)) + 1;
	var imgName = name + num + '.jpg';
	elem.src = IMAGEPATH + imgName;
}

// Display element
function show(id)
{
	document.getElementById(id).style.display = '';
} // show()

// Toggle element display
function showHide(id)
{
	var elem = document.getElementById(id);
	if (elem.style.display == 'none') {
		show(id);
	}
	else {
		hide(id);
	}
} // showHide()

/*
 * Swap image source
 */
function swap()
{
	if (!document.getElementById) {
		return;
	}

	var swapPath = IMAGEPATH;
	var i = 0;
	var elem;

	// If # arguments is odd, then first parameter is image path
	if ((swap.arguments.length%2) != 0) {
		swapPath=swap.arguments[0];
		i = 1;
	}

	// Use arguments[] array to loop through images to swap
	for ( ; i<swap.arguments.length; i+=2) {
		elem = document.getElementById(swap.arguments[i]);
		// Swap image source
		elem.src = swapPath + swap.arguments[i+1];
	}
} // swap()