var imgOut	= new Array();
var imgAlt	= new Array();

// Sets up the page
function setupPage() {
	// Loop through all links
	var siteLinks = window.document.getElementsByTagName('a');
	for (var i=0; i < siteLinks.length; i++) {
		// Set up navigation options
  		if (siteLinks[i].className == 'navImg') {

			siteLinks[i].onmouseover = rollOver;
			siteLinks[i].onmouseout  = rollOver;
			siteLinks[i].onfocus	 = rollOver;
			siteLinks[i].onblur	     = rollOver;

			imgOut[siteLinks[i].id] = window.document.images['img_' + siteLinks[i].id].src;
	   	}

		// Set up external links
  		if (siteLinks[i].rel == 'external') {
			siteLinks[i].target = '_blank';
	   	}
	}
}

/**
 * Finds left and top position for MARKET SECTOR NAV --ERROR-- Still assigns to body element :(
 * @param {Object} element
 * @author Rob rob.edwards
 */

function findPosition(obj) {
	var leftPosition = topPosition = 0;
	if (obj.offsetParent) {
		leftPosition = obj.offsetLeft
		topPosition = obj.offsetTop
		while (obj = obj.offsetParent) {
			leftPosition += obj.offsetLeft
			topPosition += obj.offsetTop
		}
	}
	return [leftPosition,topPosition];
}

/**
 * Sets the bubble style tool tip for the markets nav
 * @param {Object} id - the id of the current markets nav elelment
 * @param {Object} action - boolean, do we want to show or hide!!!?!?!?
 * @author Rob rob.edwards
 */

function setBubble(id, action) {
	//grab the markets bubble
	var marketBubble 	= window.document.getElementById('marketsBubble'); // the bubble
	var element 		= window.document.getElementById(id); // the image rollover element

	//set if we're showing or hiding
	if(action) {
		marketBubble.style.display = "block";
	} else {
		marketBubble.style.display = "none";
	}

	//OFFset bug in FF come back and fix

	// set the text
	window.document.getElementById('marketsBubbleText').innerHTML = element.id;

	// set the position
	var position = findPosition(element);
	var li = element.parentNode.parentNode;
	var liPosition = findPosition(li);

	marketBubble.style.left = ((position[0])-22)+"px";
	marketBubble.style.top = ((liPosition[1])+43)+"px";

}


// Does the actual image rollover
function rollOver(e) {
	if (!e) var e = window.event;

	if (typeof(imgAlt) == 'undefined' || typeof(imgOut) == 'undefined') {
		return;
	}

	if (e.type == 'mouseover' || e.type == 'focus' && typeof(imgAlt[this.id]) != 'undefined') {
		
		window.document.images['img_' + this.id].src = 'images/' + imgAlt[this.id];		
		setBubble(this.id, true);
	}
	else if (e.type == 'mouseout' || e.type == 'blur' && typeof(imgOut[this.id]) != 'undefined') {
		window.document.images['img_' + this.id].src = imgOut[this.id];
		setBubble(this.id, false);
	}
}

// Check to make sure the users browser supports images
if (window.document.images) {

imgAlt['Automation And Control'] = 'markets/AutomationAndControl-over.gif';
	imgAlt['Construction'] = 'markets/construction-over.gif';
	imgAlt['Data'] = 'markets/data-over.gif';
	imgAlt['Electrical'] = 'markets/electrical-over.gif';
	imgAlt['Fire and Security'] = 'markets/fireAndSecurity-over.gif';
	imgAlt['FM and Services'] = 'markets/fm-over.gif';
	imgAlt['HVAC'] = 'markets/HVAC-over.gif';
	imgAlt['Lighting'] = 'markets/lighting-over.gif';

	for (var item in imgAlt) {
		new Image().src = 'images/' + imgAlt[item];
	}
}

/**
 * Create Bookmark for IE browsers
 * @param {Object} urlAddress
 * @param {Object} pageName
 * @author Rob & Tony
 */

function addToFavorites(urlAddress, pageName) {

   if (window.external) {
       //alert(urlAddress);
       //alert(pageName);
       window.external.AddFavorite(urlAddress, pageName);
   }
   else {
       alert("Sorry! Your browser doesn't support this function.");
   }
}



/**
 * REQUIRED IF statement checks to see IF browser is IE
 * @param {Object}
 * @author Rob & Tony
 */
function checkForIe() {
    if(navigator.appName == "Microsoft Internet Explorer") {
        // grab the address in the address bar
        var urlAddress = window.location;
        //grab the page title
        var pageName = document.title;
        //grab the bookmark element
        var bookmark = document.getElementById('bookmark');

            //add text to bookmark element
            bookmark.innerHTML  = 'Bookmark this page';
            //add title to bookmark element
            bookmark.title      = 'Bookmark Us';
            //add function addTofav(address, title) to the href of the bookmark a
            bookmark.href       = 'javascript:addToFavorites("'+urlAddress+'", "'+pageName+'")';
            bookmark.click      = '';
    }
}
function setupFadeLinks() {
	arrFadeLinks[0] = "vacancies.php?sid=7#job723";arrFadeTitles[0] = "<h4>**NEW OPPORTUNITIES 2010**</h4><span>Location: North West</span>";arrFadeLinks[1] = "vacancies.php?sid=7#job788";arrFadeTitles[1] = "<h4>International Sales Executive - Consumables</h4><span>Location: North West</span>";arrFadeLinks[2] = "vacancies.php?sid=7#job806";arrFadeTitles[2] = "<h4>ASM - Specification Consultants x 2 - Commercial Interiors</h4><span>Location: North East</span>";arrFadeLinks[3] = "vacancies.php?sid=7#job723";arrFadeTitles[3] = "<h4>**NEW OPPORTUNITIES 2010**</h4><span>Location: Northern Ireland</span>";arrFadeLinks[4] = "vacancies.php?sid=7#job804";arrFadeTitles[4] = "<h4>Specification BDM - Commercial Interiors</h4><span>Location: EMEA</span>";arrFadeLinks[5] = "vacancies.php?sid=9#job5";arrFadeTitles[5] = "<h4>Sales Manager Energy Services</h4><span>Location: South</span>";arrFadeLinks[6] = "vacancies.php?sid=7#job802";arrFadeTitles[6] = "<h4>Sales Director - Construction</h4><span>Location: Midlands</span>";arrFadeLinks[7] = "vacancies.php?sid=7#job746";arrFadeTitles[7] = "<h4>**NEW REFERRAL INCENTIVE**</h4><span>Location: North</span>";arrFadeLinks[8] = "vacancies.php?sid=7#job802";arrFadeTitles[8] = "<h4>Sales Director - Construction</h4><span>Location: Northern Ireland</span>";arrFadeLinks[9] = "vacancies.php?sid=4#job781";arrFadeTitles[9] = "<h4>Specification Sales Engineer - London</h4><span>Location: South West</span>";arrFadeLinks[10] = "vacancies.php?sid=7#job768";arrFadeTitles[10] = "<h4>Specification Consultant - Commercial Interiors</h4><span>Location: West Midlands</span>";arrFadeLinks[11] = "vacancies.php?sid=7#job746";arrFadeTitles[11] = "<h4>**NEW REFERRAL INCENTIVE**</h4><span>Location: South East</span>";arrFadeLinks[12] = "vacancies.php?sid=9#job676";arrFadeTitles[12] = "<h4>Energy Sales Specialist</h4><span>Location: Scotland</span>";arrFadeLinks[13] = "vacancies.php?sid=7#job723";arrFadeTitles[13] = "<h4>**NEW OPPORTUNITIES 2010**</h4><span>Location: Midlands</span>";arrFadeLinks[14] = "vacancies.php?sid=7#job803";arrFadeTitles[14] = "<h4>Telemarketing/Researcher/Project Analysis - Commercial Interiors </h4><span>Location: National</span>";arrFadeLinks[15] = "vacancies.php?sid=4#job793";arrFadeTitles[15] = "<h4>Area Sales Engineer </h4><span>Location: EMEA</span>";arrFadeLinks[16] = "vacancies.php?sid=8#job738";arrFadeTitles[16] = "<h4>BDM - Utilities</h4><span>Location: Australia</span>";arrFadeLinks[17] = "vacancies.php?sid=7#job768";arrFadeTitles[17] = "<h4>Specification Consultant - Commercial Interiors</h4><span>Location: South</span>";arrFadeLinks[18] = "vacancies.php?sid=7#job803";arrFadeTitles[18] = "<h4>Telemarketing/Researcher/Project Analysis - Commercial Interiors </h4><span>Location: South</span>";arrFadeLinks[19] = "vacancies.php?sid=7#job802";arrFadeTitles[19] = "<h4>Sales Director - Construction</h4><span>Location: North West</span>";arrFadeLinks[20] = "vacancies.php?sid=7#job786";arrFadeTitles[20] = "<h4>Northern Regional Sales Manager - Commericial Interiors</h4><span>Location: Midlands</span>";arrFadeLinks[21] = "vacancies.php?sid=9#job5";arrFadeTitles[21] = "<h4>Sales Manager Energy Services</h4><span>Location: National</span>";arrFadeLinks[22] = "vacancies.php?sid=4#job793";arrFadeTitles[22] = "<h4>Area Sales Engineer </h4><span>Location: North East</span>";arrFadeLinks[23] = "vacancies.php?sid=7#job802";arrFadeTitles[23] = "<h4>Sales Director - Construction</h4><span>Location: South West</span>";arrFadeLinks[24] = "vacancies.php?sid=7#job804";arrFadeTitles[24] = "<h4>Specification BDM - Commercial Interiors</h4><span>Location: Australia</span>";arrFadeLinks[25] = "vacancies.php?sid=4#job784";arrFadeTitles[25] = "<h4>Business Development Manager - Australia</h4><span>Location: National</span>";arrFadeLinks[26] = "vacancies.php?sid=7#job768";arrFadeTitles[26] = "<h4>Specification Consultant - Commercial Interiors</h4><span>Location: EMEA</span>";arrFadeLinks[27] = "vacancies.php?sid=9#job5";arrFadeTitles[27] = "<h4>Sales Manager Energy Services</h4><span>Location: Australia</span>";arrFadeLinks[28] = "vacancies.php?sid=9#job5";arrFadeTitles[28] = "<h4>Sales Manager Energy Services</h4><span>Location: Northern Ireland</span>";arrFadeLinks[29] = "vacancies.php?sid=7#job801";arrFadeTitles[29] = "<h4>Account Manager - Construction</h4><span>Location: London</span>";arrFadeLinks[30] = "vacancies.php?sid=7#job801";arrFadeTitles[30] = "<h4>Account Manager - Construction</h4><span>Location: Northern Ireland</span>";arrFadeLinks[31] = "vacancies.php?sid=7#job806";arrFadeTitles[31] = "<h4>ASM - Specification Consultants x 2 - Commercial Interiors</h4><span>Location: West Midlands</span>";arrFadeLinks[32] = "vacancies.php?sid=7#job804";arrFadeTitles[32] = "<h4>Specification BDM - Commercial Interiors</h4><span>Location: Northern Ireland</span>";arrFadeLinks[33] = "vacancies.php?sid=4#job785";arrFadeTitles[33] = "<h4>MV Product Manager - Australia</h4><span>Location: North</span>";arrFadeLinks[34] = "vacancies.php?sid=4#job793";arrFadeTitles[34] = "<h4>Area Sales Engineer </h4><span>Location: Midlands</span>";arrFadeLinks[35] = "vacancies.php?sid=4#job793";arrFadeTitles[35] = "<h4>Area Sales Engineer </h4><span>Location: London</span>";arrFadeLinks[36] = "vacancies.php?sid=4#job784";arrFadeTitles[36] = "<h4>Business Development Manager - Australia</h4><span>Location: South</span>";arrFadeLinks[37] = "vacancies.php?sid=4#job781";arrFadeTitles[37] = "<h4>Specification Sales Engineer - London</h4><span>Location: North West</span>";arrFadeLinks[38] = "vacancies.php?sid=7#job803";arrFadeTitles[38] = "<h4>Telemarketing/Researcher/Project Analysis - Commercial Interiors </h4><span>Location: West Midlands</span>";arrFadeLinks[39] = "vacancies.php?sid=4#job781";arrFadeTitles[39] = "<h4>Specification Sales Engineer - London</h4><span>Location: Northern Ireland</span>";arrFadeLinks[40] = "vacancies.php?sid=7#job723";arrFadeTitles[40] = "<h4>**NEW OPPORTUNITIES 2010**</h4><span>Location: West Midlands</span>";arrFadeLinks[41] = "vacancies.php?sid=8#job738";arrFadeTitles[41] = "<h4>BDM - Utilities</h4><span>Location: North</span>";arrFadeLinks[42] = "vacancies.php?sid=7#job768";arrFadeTitles[42] = "<h4>Specification Consultant - Commercial Interiors</h4><span>Location: North East</span>";arrFadeLinks[43] = "vacancies.php?sid=7#job788";arrFadeTitles[43] = "<h4>International Sales Executive - Consumables</h4><span>Location: National</span>";arrFadeLinks[44] = "vacancies.php?sid=7#job806";arrFadeTitles[44] = "<h4>ASM - Specification Consultants x 2 - Commercial Interiors</h4><span>Location: Scotland</span>";arrFadeLinks[45] = "vacancies.php?sid=4#job793";arrFadeTitles[45] = "<h4>Area Sales Engineer </h4><span>Location: South East</span>";arrFadeLinks[46] = "vacancies.php?sid=9#job5";arrFadeTitles[46] = "<h4>Sales Manager Energy Services</h4><span>Location: North</span>";arrFadeLinks[47] = "vacancies.php?sid=7#job788";arrFadeTitles[47] = "<h4>International Sales Executive - Consumables</h4><span>Location: Scotland</span>";arrFadeLinks[48] = "vacancies.php?sid=7#job786";arrFadeTitles[48] = "<h4>Northern Regional Sales Manager - Commericial Interiors</h4><span>Location: North West</span>";arrFadeLinks[49] = "vacancies.php?sid=8#job738";arrFadeTitles[49] = "<h4>BDM - Utilities</h4><span>Location: London</span>";
}

//jQuery link switch

//find link
var arrFadeLinks;
var arrFadeTitles;

var i = 0;

function switchLink() {
	arrFadeLinks = new Array();
    arrFadeTitles = new Array();
  
    setupFadeLinks();

    if (i > arrFadeLinks.length-1) {
        i = 0;
    }
    
    //if link 1 then fade the link out       
    $("#fade_link").fadeOut('slow');
    
    //change the title and href
    setTimeout("setLink()",1000);
    
    //fade new link back in
    setTimeout('$("#fade_link").fadeIn("slow")',2000);
    
    i = i + 1;
    
    //repeat continuously
    setTimeout("switchLink()",8000);
};

function setLink() {
	
	$("#fade_link").attr({href: arrFadeLinks[i]});
	
	//$("#fade_link").href = arrFadeLinks[i];
	$("#fade_link").html(arrFadeTitles[i]);
};function loadPage() { setupPage(); switchLink(); }