
///////////// PRELOAD IMAGES ////////////////

preloadArray = new Array();
function addPreload(imgSrc) { preloadArray[preloadArray.length] = imgSrc; }
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		for (i=0; i<preloadArray.length; i++) eval("preloadImage"+i+" = newImage('"+preloadArray[i]+"');");
		preloadFlag = true;
	}
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

addPreload("Media/images/ma_round1a.gif");
addPreload("Media/images/ma_round2a.gif");
addPreload("Media/images/ma_round3a.gif");
addPreload("Media/images/ma_round4a.gif");
addPreload("Media/images/ma_question.gif");
addPreload("Media/images/ma.gif");
addPreload("Media/images/images/bg_male.gif");
addPreload("Media/images/ma_round1b.gif");
addPreload("Media/images/ma_round2b.gif");
addPreload("Media/images/ma_round3b.gif");
addPreload("Media/images/ma_round4b.gif");




// initialises functionality for the social networking links
function initSN() {
	el = document.getElementById('sn');
	if (el) {
		snImages = el.getElementsByTagName('IMG');
		for(i=0;i<snImages.length;i++) {
			snImg = snImages[i];
			snImg.onmouseover = function() {
				el = document.getElementById('snInfo');
				el.innerHTML = this.getAttribute('ALT');
			}
			snImg.onmouseout = function() {
				el = document.getElementById('snInfo');
				el.innerHTML = '';
			}
		}
	}
}

function initContestants() {
//	if (document.getElementById('female')) {
//		// init inputs & labels
//		Female_divs = document.getElementById('female').getElementsByTagName('DIV');
//		Male_divs = document.getElementById('male').getElementsByTagName('DIV');
//		Female_matchups = new Array();
//		Male_matchups = new Array();
//		for(ii=0;ii<Female_divs.length;ii++) {
//			divEl = Female_divs[ii];
//			if (divEl.className=='matchup') Female_matchups[Female_matchups.length] = divEl;
//		}
//		for(ii=0;ii<Male_divs.length;ii++) {
//			divEl = Male_divs[ii];
//			if (divEl.className=='matchup') Male_matchups[Male_matchups.length] = divEl;
//		}
//		genders = new Array('Female','Male');
//		for(g=0;g<genders.length;g++) {
//			gender = genders[g];
//			matchups = eval(gender+'_matchups');
//			for(m=0;m<matchups.length;m++) {
//				matchEl = matchups[m];
//				inputs = matchEl.getElementsByTagName('INPUT');
//				if (inputs.length) {
////					labels = matchEl.getElementsByTagName('LABEL');
////					inputs[0].id = 'm_'+gender+'_'+m+'a';
////					inputs[1].id = 'm_'+gender+'_'+m+'b';
////					inputs[0].name = 'm_'+gender+'_'+m;
////					inputs[1].name = 'm_'+gender+'_'+m;			
////					inputs[0].onclick = function() {
////						this.checked = true;
////						document.getElementById(this.name+'b').checked = false;
////					}
////					inputs[1].onclick = function() {
////						this.checked = true;
////						document.getElementById(this.name+'a').checked = false;
//					}
//					/*
//					labels[0].setAttribute('thisInput',inputs[0].id);
//					labels[0].setAttribute('otherInput',inputs[1].id);
//					labels[1].setAttribute('thisInput',inputs[1].id);
//					labels[1].setAttribute('otherInput',inputs[0].id);
//					labels[0].onclick = labels[1].onclick = function() {
//						document.getElementById(this.getAttribute('thisInput')).checked = true;
//						document.getElementById(this.getAttribute('otherInput')).checked = false;
//					}
//					*/
//				}
//			}
//		}
//	}

    // init label classes
    labels = document.getElementsByTagName('LABEL');
    for (l = 0; l < labels.length; l++) {
        lEl = labels[l];
        lEl.className = 'labelClass';
    }
	
	// init popup profiles
	imgs = document.getElementsByTagName('IMG');
	labelData = new Array();
	for(ii=0;ii<imgs.length;ii++) {
	    imgEl = imgs[ii];
	    
		if (imgEl.className.indexOf('contestant')==3) {
			imgEl.style.cursor = 'pointer';
			imgEl.title = imgEl.alt + ' - click to see profile';

			imgEl.onclick = function() {
				gender = this.className.substr(0,2);
				if (gender=='ma') {
					gender = 'Male';
					}
 				if (gender=='fe') {
					gender = 'Female';
					}
				contestantId = this.className.substr(13);
				linkLoc = gender+"Contestant.aspx#contestant"+contestantId;
				window.open(linkLoc,'contestantProfile','width=500,height=400');
			}	
			gender = imgEl.className.substr(0,2);
			if (gender=='ma') {
					gender = 'Male';
					}
 				if (gender=='fe') {
					gender = 'Female';
					}
			contestantId = imgEl.className.substr(13);
			labelEl = imgEl.nextSibling.nextSibling;
			labelDataText = simplifyName(labelEl.innerHTML);
			linkLoc = gender+"Contestant.aspx#contestant"+contestantId;
			labelData[labelData.length] = new Array(labelDataText,linkLoc);
		} else if (imgEl.parentNode.className == 'contestant') {
		labelEl = imgEl.nextSibling;

		    
		    if (labelEl.className != 'labelClass') labelEl = imgEl.nextSibling.nextSibling;
		    
		    if (labelEl.className != 'labelClass') labelEl = imgEl.nextSibling.nextSibling.nextSibling;
		    
			if (labelEl.className != 'labelClass') labelEl = imgEl.nextSibling.nextSibling.nextSibling.nextSibling;



			
			labelDataText = simplifyName(labelEl.innerText);
			


			if (labelDataText) {
			    
				found = false;
				for(l=0;l<labelData.length;l++) {
					ld = labelData[l];
					if (ld[0]==labelDataText) {
						found = true;
						imgEl.setAttribute('linkLoc',ld[1]);
						imgEl.style.cursor = 'pointer';
						imgEl.title = 'Click to see profile';
						imgEl.onclick = function() {
							linkLoc = this.getAttribute('linkLoc');
							window.open(linkLoc,'contestantProfile','width=500,height=400');
						}
						break;
					}
				}
				if (!found) {
					// page code error
					alert('Automatic profile link setup failed for contestant "'+labelDataText+'". You must ensure the name of the contestants in rounds 2-4 matches exactly the name in round 1.');
				}
			}
		}
	}
 //link labels to popup profiles
	labelEls = document.getElementsByTagName('LABEL');
	for(l=0;l<labelEls.length;l++) {
	    el = labelEls[l];

	    if (el.innerHTML) {
	        
			el.style.cursor = "pointer";
			el.onclick = function() {
			    contestantName = this.innerHTML.toLowerCase();

			    // find contestantName in labelData array
			    for (ld = 0; ld < labelData.length; ld++) {
			        //alert(labelData[ld][0]);
			        if (simplifyName(contestantName) == labelData[ld][0]) {
			            linkLoc = labelData[ld][1];
			            window.open(linkLoc, 'contestantProfile', 'width=500,height=400');
			            break;
			        }
			    }
			}
		}
	}
}


function simplifyName(str) {
	if (str) {
		while(str.indexOf(' ')!=-1) str = str.replace(' ','');
		while(str.indexOf("\t")!=-1) str = str.replace("\t",'');
		while(str.indexOf("\n")!=-1) str = str.replace("\n",'');
		str = str.toLowerCase();
	}
	return(str);
}


function initBreakoutLinks() {
	if (document.getElementById('contestant1')) { // contestant bio page
		linkEls = document.getElementsByTagName('A');
		for(lElId=0;lElId<linkEls.length;lElId++) {
			linkEl = linkEls[lElId];
			if (linkEl.href) {
				linkEl.onclick = function() {
					winTarget = this.target;
					if (!winTarget) winTarget = '_blank';
					if (winTarget.toLowerCase()=='_blank') {
						popWin = window.open(this.href,'_blank','width=800,height=600,status=yes,scrollbars=yes,resizable=yes,menubar=yes,location=yes,toolbar=yes');
						if (popWin) return false;
					}
				}
			}
		}
	 	if (location.href.indexOf('#')!=-1) window.location = location.href;
		window.focus();
	}
}


//here you place the ids of every element you want.
var ids=new Array('female','male','neuter');

function switchid(id){	
	hideallids();
	showdiv(id);
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

////////////// ONLOAD SEQUENCE //////////////////

onload = function() {
	initBreakoutLinks();
	initContestants();
	preloadImages();
	
	// reset the background treatment
	el = document.getElementById('pageContent');
	if (el) el.style.background = 'none';
}
