/* Written Summer 2007 by JKS for ProCoreSolutions.com */

//on load of page body execute the following functions
function global_loader() {
	//preload_images();
	find_menu_item();
	init();
}

//This function downloads all images referenced in linked style sheets of the current page... author unknown... jks 3/27/2006
var img = new Array();
function preload_images() {
  var css = document.styleSheets;
  for(var i=0;i<css.length;i++){
    var rule = new Array();
    try{
      rule = (css[i].cssRules)?css[i].cssRules:((css[i].rules)?css[i].rules:[]);
    } catch(e){};
    for(var j=0;j<rule.length;j++){
      rule[j].style.background.replace(/[\w\/\:\-\.]+\.(gif|png|jpg)/gi,
        function(imgSrc){(img[img.length] = new Image()).src=imgSrc;});
    }
  }
}

//this function swaps out the images on the header's top nav on rollover... jks 06/04/2007
function swap_image_h(img) {
	image = img.src;
	
	//swap normal to highlighted
	if (image.indexOf("_h.gif") == -1) {
		img.src = image.replace(/\.gif/, "_h.gif");
	}
	
	//swap highlighted back to normal
	if (image.indexOf("_h.gif") != -1) {
		img.src = image.replace(/_h\.gif/, ".gif");
	}
}

//functions generates a random number between 1 and the argument provided... jks 04/15/2010
function random_number(high_num_range) {
	var num_range = high_num_range + 1;
	var randomnumber=Math.floor(Math.random()*num_range);
	
	if (randomnumber == 0) { return random_number(high_num_range); }
	else { return randomnumber; }
}



/*************************************/
/***Left Navigation Menu Functions ***/
/*************************************/
function find_menu_item() {
	var all_menu_items = document.getElementById('left_nav_menu').getElementsByTagName('a');
	var curr_href = document.location.href;

	
	/* if the curr_href (URL) ends in "/" it adds /index.shtml so it can be found in all_menu_items */
	var last_part_of_string = curr_href.substring(curr_href.length - 1);
	if (last_part_of_string == "/") { var curr_href = document.location.href + "index.shtml"; }
	
	for (var i = 0; i < all_menu_items.length; i++) {
		var link_href = all_menu_items[i].href;
		if (curr_href.indexOf(link_href) != -1) {
			displayat(all_menu_items[i].parentNode.id);
		}
	}
}

function displayat(menu_item) {
	var leftnav_top_level = /^(m\d)$/;
	var leftnav_submenu = /^(m\d_\d)$/;

	//determine if the sub_menu is the menu_item selected
	if (leftnav_submenu.test(menu_item)) {
		var top_level_menu_item = menu_item.substring(0,2);
		var sub_selected = 1;
	} else {
		var top_level_menu_item = menu_item;
		var sub_selected = 0;
	}

	//highlight the top level menu item
	var selected_menu_item = document.getElementById(top_level_menu_item);
	var the_link = document.getElementById(top_level_menu_item).getElementsByTagName('a')[0];
	the_link.className = 'highlight_orange ignore';
	the_link.style.color = "#FFFFFF";
	the_link.style.backgroundColor = "#E6B96A";
	
	//display the sub menu if one exists
	var submenu_id = top_level_menu_item.replace("m", "d");
	if (document.getElementById(submenu_id)) {
		var submenu = document.getElementById(submenu_id);
		submenu.className = 'submenu_visible ignore';
	}
	
	//if the menu_item is the selected one, then highlight it
	if (sub_selected == 1) {
		var selected_submenu_item = document.getElementById(menu_item).getElementsByTagName('a')[0];
		selected_submenu_item.style.backgroundColor = "#505050";
		selected_submenu_item.className = 'highlight_dark_grey ignore';
	}
}

/**************************************
End Left Navigation Scripting
Start Link Fader
**************************************/
/******************************************************************
*	Script name: Link fader (http://projects.impenetrable.org/linkfader/)
*	Version: 1.0
*	Date: 12.05.02
*	Usage: Freeware - You may modify this script as you wish,
*		as long as you don't remove or edit this header comment.
*
*	Script by: Fayez Zaheer (viol8r on #webdesign [uk.zanet.org.za])
*	Email: fayez at impenetrable.org
*	Web site: http://impenetrable.org
* 	Original idea: http://anarchos.xs.mw/fade.phtml
******************************************************************/
/*****base script modified for ProCore Solutions... jks*****/

// Fade-to colour without the # (6 character value only!)
var fadeTo = "FFFFFF";
var fadeToTextColor = "black";
// Fade in colour increment/decrement by
var fiBy = 2;
// Fade out colour increment/decrement by
var foBy = 2;
// Speed - milliseconds between each colour change in the fade
var speed = 12;
// Class name of links to NOT fade
var ignoreClass = "ignore";

var opera, ie, dom, x = 0, oc, fader, ocs = new Array();
var oc2, fader2, ocsTextColor = new Array(); //for text color fading

if (navigator.userAgent.indexOf("Opera") != -1) opera = true
else if (document.all && !opera) ie = true
else if (!document.all && document.getElementById) dom = true;
//add no more run here

function convertRGB(z) {
	var newfcS = "", splitter = "";
	splitter = z.split(",");
	splitter[0] = parseInt(splitter[0].substring(4, splitter[0].length));
	splitter[1] = parseInt(splitter[1]);
	splitter[2] = parseInt(splitter[2].substring(0, splitter[2].length-1));
	for (var q = 0; q < 3; q++) {
		splitter[q] = splitter[q].toString(16);
		if (splitter[q].length == 1) { splitter[q] = "0" + splitter[q]; }
		newfcS += splitter[q];
	}
		return newfcS;
}

function currentColour(index) {
	var temp, cc;
	if (opera) cc = document.links[index].style.backgroundColor
	else if (ie) cc = document.links[index].currentStyle.backgroundColor
	else if (dom) cc = document.defaultView.getComputedStyle(document.links[index], '').getPropertyValue("background-color");
	if (cc.length == 4 && cc.substring(0, 1) == "#") {
		temp = "";
		for (var a = 0; a < 3; a++)
			temp += cc.substring(a+1, a+2) + cc.substring(a+1, a+2);
			cc = temp;
		}
	else if (cc.indexOf("rgb") != -1) cc = convertRGB(cc)
	else if (cc.length == 7) cc = cc.substring(1, 7)
	else cc = fadeTo;
	return cc;
}

/************ADDED JKS************/
function currentColourTextColor(index) {
	var temp, cc;
	if (opera) cc = document.links[index].style.color
	else if (ie) cc = document.links[index].currentStyle.color
	else if (dom) cc = document.defaultView.getComputedStyle(document.links[index], '').getPropertyValue("color");
	if (cc.length == 4 && cc.substring(0, 1) == "#") {
		temp = "";
		for (var a = 0; a < 3; a++)
			temp += cc.substring(a+1, a+2) + cc.substring(a+1, a+2);
			cc = temp;
		}
	else if (cc.indexOf("rgb") != -1) cc = convertRGB(cc)
	else if (cc.length == 7) cc = cc.substring(1, 7)
	else cc = fadeToTextColor;
	return cc;
}


function convert2Dec(hex) {	
	var rgb = new Array();
	for (var u = 0; u < 3; u++) { rgb[u] = parseInt(hex.substring(u*2, u*2+2), 16); }
	return rgb;
}

function newRGB(f, n, d) {
	var change;
	if (d == 1) change = fiBy
	else change = foBy;
	for (var g = 0; g < 3; g++) {
		if (n[g] > f[g] && n[g] - change >= 0) n[g] -= change;
		if (n[g] < f[g] && n[g] + change <= 255) n[g] += change;
	}
	return n;
}

function fade(index, d) {
	var fc, nc, temp = new Array(), finished = false;
	nc = convert2Dec(currentColour(index));
	if (d == 1) fc = convert2Dec(fadeTo);
	if (typeof ocs[x] == 'undefined') { ocs[x] = 'ffffff'; } //for some reason, contact us fails, this solves that
	if (d != 1) fc = convert2Dec(ocs[x]);
	temp = convert2Dec(currentColour(index));
	nc = newRGB(fc, nc, d);
	if ((nc[0] == temp[0]) && (nc[1] == temp[1]) && (nc[2] == temp[2])) { finished = true; }
	if (!finished) document.links[x].style.backgroundColor = "rgb(" + nc[0] + "," + nc[1] + "," + nc[2] + ")"
	else { clearInterval(fader); }
}

/************ADDED JKS************/
function fadeTextColor(index, d) {
	var fc, nc, temp = new Array(), finished = false;
	nc = convert2Dec(currentColourTextColor(index));
	if (d == 1) fc = convert2Dec(fadeTextColorTo);
	if (typeof ocsTextColor[x] == 'undefined') { ocsTextColor[x] = '7D7D7D'; } //for some reason, contact us fails, this solves that
	if (d != 1) fc = convert2Dec(ocsTextColor[x]);
	temp = convert2Dec(currentColourTextColor(index));
	nc = newRGB(fc, nc, d);
	if ((nc[0] == temp[0]) && (nc[1] == temp[1]) && (nc[2] == temp[2])) { finished = true; }
	if (!finished) document.links[x].style.color = "rgb(" + nc[0] + "," + nc[1] + "," + nc[2] + ")"
	else { clearInterval(fader2); } //alert('.'); }
}

function findLink(over) {
	if (document.layers) return;
	if (fader) {
		clearInterval(fader);
		document.links[x].style.backgroundColor = "#" + ocs[x];
	}
	
	/************ADDED JKS************/
	if (fader2) { 
		clearInterval(fader2);
		document.links[x].style.color = "#" + ocsTextColor[x];
	}
	
	if (over && !this.id) { this.id = over; }
	x = 0;
	while (!(this.id == document.links[x].id) && (x < document.links.length)) { x++; }
	if (this.id == document.links[x].id) {
	
		var leftnav_link = document.links[x].parentNode.id;
		var leftnav_levelone_pattern = /^(m\d)$/;
		var leftnav_leveltwo_pattern = /^(m\d_\d)$/;
		//var leftnav_levelthree_pattern = /^(m\d_\d_\d)$/;
		//var leftnav_levelfour_pattern = /^(m\d_\d_\d_\d)$/;
		if (leftnav_levelone_pattern.test(leftnav_link)) { fadeTo = 'E6B96A'; fadeTextColorTo = 'FFFFFF'; } //added fade also to fade the text color of the link ( #FBB40C )
		if (leftnav_leveltwo_pattern.test(leftnav_link)) { fadeTo = '505050'; fadeTextColorTo = 'FFFFFF'; }
		//if (leftnav_levelthree_pattern.test(leftnav_link)) { fadeTo = 'CCBA89'; }
		//if (leftnav_levelfour_pattern.test(leftnav_link)) { fadeTo = 'fffff0'; }
		
		oc = currentColour(x);
		fader = setInterval("fade(" + x + ", 1)", speed);
		
		/************ADDED JKS************/
		oc2 = currentColourTextColor(x);
		fader2 = setInterval("fadeTextColor(" + x + ", 1)", speed);
	}
}

function clearFade() {
	if (document.layers) return;
	if (fader) clearInterval(fader);
	fader = setInterval("fade(" + x + ", 0)", speed);
	
	/**********for text color start, jks **********/
	if (fader2) clearInterval(fader2);
	fader2 = setInterval("fadeTextColor(" + x + ", 0)", speed);
	/**********for text color end, jks **********/
}

function init()
	{
		for (var i = 0; i < document.links.length; i++)
			{
				ocs[i] = currentColour(i);
				ocsTextColor[i] = currentColourTextColor(i);
				
				var currentOver = document.links[i].onmouseover;
				var currentOut = document.links[i].onmouseout;
				var ignoreIt = document.links[i].className == 'ignore';
				
				if (!ignoreIt) document.links[i].id = "link" + i;
				var leftnav_link = document.links[i].parentNode.id; //find the links parentNode.. jks
				var leftnav_link_pattern = /m\d[_\d]*/; //added RegEx & expression to verify the containing li's id is a leftnav menu item (li).. jks
				if (leftnav_link_pattern.test(leftnav_link)) {
					if (!currentOver && !currentOut && !ignoreIt)
						{
							document.links[i].onmouseover = findLink;
							document.links[i].onmouseout = clearFade;
						}
				}
			}
			
}


//TEMP FIX FOR LEFTNAV
function news_highlight() {
	var the_link = document.getElementsByName('news')[0];
	
	the_link.onmouseout = "";
	the_link.onmouseover = "";
	
	the_link.style.backgroundColor = "#505050";
	the_link.className = 'highlight_dark_grey ignore';
	
	
	var the_link = document.getElementById('m4').firstChild
	the_link.onmouseout = "";
	the_link.onmouseover = "";
	the_link.className = 'highlight_orange ignore';
	the_link.style.color = "#FFFFFF";
	the_link.style.backgroundColor = "#E6B96A";
}