/**
 * MacDonald Egan Javascript Utilities.
 * @author Dusted Design Partners Limited
 */
 
var theImages = new Array()

theImages[0] = '../images/developments/development_intro_01.gif'
theImages[1] = '../images/developments/development_intro_02.gif'

var theAltText = new Array()

theAltText[0] = 'THEIR CREATIVE APPROACH TO REGENERATION CREATES BETTER SCHEMES AND GREATER VALUE Jack Jones, Royal Bank of Scotland'
theAltText[1] = 'THE OLD SEAGER DISTILERY PROJECT IS AN EXEMPLAR OF HIGH DENSITY DEVELOPMENT Ken Livingstone, Major of London'

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));

function showImage(){
	myImage = new Image()
	myImage.src = theImages[whichImage]
	myImage.alt = theAltText[whichImage]
	if (document.getElementById("dev-intro")) {
		document.getElementById("dev-intro").src = myImage.src
		document.getElementById("dev-intro").alt = myImage.alt
	}
}
 
function getImg(id){
	do{
		var Img = null;
		Img = document.getElementById(id); 
		if(Img != null) break;
		Img = document.images[id];
		if(Img != null) break;
		Img = document[id];
		if(Img != null) break;
		alert("Failed to find image `"+id+"'");
	}while(0);
	return Img;
}

function showImg(thumbImg){
	// could check dimensions of thumbnail and resize target
	var ratio = thumbImg.height / thumbImg.width;
	var fullHeight = Math.round(375 * ratio);
	var FullImg = getImg('imgtarget');
	if(FullImg != null){
		FullImg.height = fullHeight;
		var src = thumbImg.src.replace(/\/thumbs\/([^\/\.])\.(jpe?g|gif)/, '/$1.$2');
		FullImg.src = src;
	}
}

function firstChild(node, name){
	for(var i = 0; i < node.childNodes.length; i++){
		if(node.childNodes[i].nodeName.toUpperCase() == name){
			return node.childNodes[i];
		}
	}
	return null;
}


function navOver(Lnk, on){
	if(Lnk.__isActive != null){
		return;
	}
	var Img = firstChild(Lnk, 'IMG');
	if(Img != null){
		if(on){
			Img.src = Img.src.replace(/_off/, '_on');
		}else{
			Img.src = Img.src.replace(/_on/, '_off');
		}
	}
}

function recursiveBehaviourAttach(Div){
	for(var i = 0; i < Div.childNodes.length; i++){
		var tag = Div.childNodes[i].nodeName.toUpperCase(); 
		if(tag == 'DIV'){
			recursiveBehaviourAttach(Div.childNodes[i]);
		}else if(tag == 'A'){
			// div has link.
			Div.childNodes[i].onmouseover = function(){
				navOver(this, 1);
			}
			Div.childNodes[i].onmouseout = function(){
				navOver(this, 0);
			}
			// preload image
			var Img = firstChild(Div.childNodes[i], 'IMG');
			if(Img != null){
				var tmpImg = new Image();
				tmpImg.src = Img.src.replace(/_off/, '_on');
			}
		}
	}
}


function recursiveNavHilight(Div){
	if(Div.id == 'nav'){
		// reached root.
		return;
	}
	// show div
	Div.style.display = 'block';
	// hilite gif
	var Lnk = firstChild(Div, 'A');
	if(Lnk != null){
		navOver(Lnk, 1);
		// secret property to avoid rolling out of active nav item
		Lnk.__isActive = true;
	}
	// drill up.
	recursiveNavHilight(Div.parentNode);
}



function initNav(activePath){
	showImage();
	// add rollovers to all nav divs
	var rootDiv = document.getElementById('nav');
	if(rootDiv == null){
		alert("Failed to find root div");
	}else{
		recursiveBehaviourAttach(rootDiv);
	}
	// hilite active path
	var IDs = activePath.split('/');
	var ID = 'nav_' + IDs.join('_');
	var Div = document.getElementById(ID);
	if(Div == null){
		//alert("Failed to get Div with id `"+ID+"'");
		return false;
	}else{
		recursiveNavHilight(Div);
		return true;
	}
}

//function randomQuote() {
//	var imgArray = [	"quote_01",
//						"quote_02",
//						"quote_03"
//				    ];
//	var altArray = [	"An Exemplar of process and design in London",
//						"Their creative approach to regenration creates better schemes and greater value",
//						"MacDonald Egan are one of a handlful of sophisiticated and clever developers who recognise the importance of good design"
//				    ];
//	var randNum = Math.round(Math.random()*(imgArray.length-1));
//	var randImg = imgArray[randNum];
//	var randAlt = altArray[randNum];
//	var homeQuoteId = document.getElementById("homequote");
//	if (homeQuoteId != null) {
//		alert ("found it");
//	} else {
//		alert ("dont got it");
//	}
//	
//	
//	homeQuoteId.src = "gifs/quotes/"+randImg+".gif";
//	homeQuoteId.alt = randAlt;
//	homeQuoteId.title = randAlt;
//}
window.onload = function() {
	showImage();
}