///// Onload

var onload_events = new Array();

function set_onload(f)
{
	var i = onload_events.length;
	onload_events[i] = f;
}

function do_onload()
{
	if(onload_events.length == 0) return;

	for(var i=0; i<onload_events.length; i++)
	{
		eval(onload_events[i] + "()");
	}
}

onload=do_onload;

///// Trim

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

///// Cookies

function set_cookie(n,v,d) // Name, Value, Days
{
	if(d)
	{
		var date = new Date();
		date.setTime(date.getTime()+(d*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
		expires = "";
	}
	document.cookie = n + "=" + v + expires + "; path=/";
}

function get_cookie(n)
{
	var name = n + "=";
	var ca = document.cookie.split(';');

	for(var i=0; i<ca.length; i++)
	{
		var c = ca[i].ltrim();
		if(c.indexOf(name) == 0)
		{
			return c.substring(name.length,c.length);
		}
	}

	//return ca;
	//return null;
}

///// CSS Colors

// The available stylesheets
var styles = new Array("red","sand","green","blue","grey");

// Write in a stylesheet that changes the color scheme on the page
// This called right after the body tag (/inc/html/top.html), we're not waiting for the page to load
function set_css(input)
{
	var base_url = "/inc/styles/color-";
	var css_chosen = 0; // Defaul to red

	if(input)
	{
		// The input key
		//  1:random, 2:step-forward

		// First things first. In order to step through, or select a new style index, we need to find the current style
		// This will prevent us from
		// 1.) Selecting a new css index that's the same as the current one, and
		// 2.) We can identify when we're at the end of the style array and need to repeat the index

		// Get the css set in the cookie
		var cookie_value = get_cookie("css");

		// Find the index of the "styles" array that matches
		for(var i=0; i<styles.length; i++)
		{
			if(styles[i] == cookie_value)
			{
				var current_ix = i;
				break;
			}
		}

		if(input == 1)
		{
			/* The Random CSS */	
			// A random number based upon the number of stylesheets available, called from an event
			var r_number = Math.floor(Math.random()*styles.length);

			// Check to see if the new number "r_number" is the same as the current style "current_ix"
			// Don't do shit, unless they're the same
			// If they're the same, we need to find a new one
			//   - Add "1" to the index, and get the next number (unless it's the last index in the array, then go to the beginning "0")
			if(r_number == current_ix)
			{
				// It's the last in the index, so start over
				if((current_ix+1) == styles.length)
				{
					r_number = 0;
				}
				else
				{
					r_number = r_number+1;
				}
			}

			// Set the chosen css to the new random number
			css_chosen = r_number;
		}
		else if(input == 2)
		{
			// It's the last in the index, so start over
			if((current_ix+1) == styles.length)
			{
				css_chosen = 0;
			}
			else
			{
				css_chosen = current_ix+1;
			}
		}
/*
		else
		{
			css_chosen = 0; // Whoops, we screwed up somewhere
		}
*/
	}
	else // The style defined by the time of day below
	{
		/* Time of Day CSS */	
		var date = new Date();
		var hour = date.getHours();
	
		// Schedule
		// 5-9 , Grey
		// 10-12 , Green
		// 13-17, Sand
		// 18-21, Red
		// 22-4, Blue
	
		var day_part = 0;
	
		if(hour > 21) day_part = 3; // Blue
		else
		{
			if(hour > 17) day_part = 0; // Red
			else
			{
				if(hour > 12) day_part = 1; //Sand
				else
				{
					if(hour > 9) day_part = 2; // Green
					else
					{
						if(hour > 5) day_part = 4; // Grey
						else
						{
							day_part = 3; // Blue
						}
					}
				}
			}		
		}
		css_chosen = day_part;
	}

	// Choose method
	var css_select = css_chosen;

	var css_path = base_url + styles[css_select] + ".css";
	// Set a cookie with a name pair for "css" so we can determine which color is being used
	set_cookie("css",styles[css_select]);
	var cookie = get_cookie("css");

	// The LINK CSS tag
	var css = document.createElement("link");
	css.setAttribute("type","text/css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("media","all");
	css.setAttribute("href",css_path);
	// Appending the LINK tag
	var head = document.getElementsByTagName("head")[0];
	head.appendChild(css);

	// If this function is called from an event, and not when the page is loading
	if(input)
	{
		update_all_images();
	}
}

// The Heading, Menu Items and Footer images are dependent upon the "color" scheme set in the "set_css()" function above
// This function is called from the /inc/xsl/menu.xsl, /inc/xsl/menu-home.xsl & /inc/html/footer.html files
function update_image(o)
{
	var image = document.getElementById(o);
	var new_file = "/images/colors/" + get_cookie("css") + "/" + image.getAttribute("file");
	image.src = new_file;
}

// The above function is called directly after the HTML is written
// If we need to change them laster, we need to know all of the element IDs
function update_all_images()
{
	var all_images = new Array("menu-services","menu-equipment","menu-projects","menu-gallery","menu-contact","footer-image");

	for(var i=0; i<all_images.length; i++)
	{
		var image = document.getElementById(all_images[i]);

		if(image)
		{
			var new_file = "/images/colors/" + get_cookie("css") + "/" + image.getAttribute("file");
			image.src = new_file;
		}
	}

}

/* Google Analytics Code */

function trim(s)
{
	return s.replace(/^\s+|\s+$/g, '');
}

//*** This code is copyright 2003 by Gavin Kistner, !@phrogz.net
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt

//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}


function ga_track_links()
{
	// Add this to links that meet file type criterea:
	// <a href="http://www.example.com/files/map.pdf" onClick="javascript: pageTracker._trackPageview('/downloads/map'); "> 

	// 1. Find all links
	// 2. Filter which links meet the filter criteria
	// 3. Construct the arguements based on the criteria which will result in a "virtual" directory in GA, ex: /downloads/FILE TYPE/FILE NAME
	// 4. Add, or add to, an "onclick" event handler that calls the "pageTracker" function

	var filter = new Array("pdf","doc","xls");
	var virtual_directory = "/downloads/";

	//var article = document.getElementById("p-content");
	var a = document.getElementsByTagName("a");

	//if(article.addEventListener) alert("addEventListener works");

	for(var i=0; i<a.length; i++)
	{
		var href = a[i].href;

		var temp = href.toLowerCase();

		if((temp.indexOf("javascript:") == -1) && (temp.indexOf("@") == -1))
		{
			if(href.indexOf("/") > -1) href = href.substring(href.lastIndexOf("/")+1); // If theres a path just get the last part (file, folder, whatever)
			
			if(href.indexOf(".") > -1) // If it's a file
			{
				var file = href.split(".")[0];
				var ext = trim(href.split(".")[1]); // Trim function is in the this file
				var ga_arg = null; // Assume that it's not going to be used

				for(var ii=0; ii<filter.length; ii++)
				{
					if(ext == filter[ii])
					{
						ga_arg = virtual_directory + ext + "/" + file;
						break;
					}
				}

				if(ga_arg)
				{
					var obj = a[i];
					var ga_code = "pageTracker._trackPageview('" + ga_arg + "')";

					if(obj.addEventListener)
					{
						obj.addEventListener("click",function(){eval(ga_code)},false);
					}
					else if(obj.attachEvent)
					{
						obj.attachEvent("onclick",function(){eval(ga_code)});
					}				

					//alert(ga_code);
					
				}
			}
		}
	}
}

set_onload("ga_track_links");

