

///////////// 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("images/ma_round1a.gif");
addPreload("images/ma_round2a.gif");
addPreload("images/ma_round3a.gif");
addPreload("images/ma_round4a.gif");
addPreload("images/ma_stripes.gif");
addPreload("images/ma_question.gif");
addPreload("images/ma.gif");
addPreload("images/images/bg_male.gif");
addPreload("images/ma_round1b.gif");
addPreload("images/ma_round2b.gif");
addPreload("images/ma_round3b.gif");
addPreload("images/ma_round4b.gif");

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
		fe_divs = document.getElementById('female').getElementsByTagName('DIV');
		ma_divs = document.getElementById('male').getElementsByTagName('DIV');
		fe_matchups = new Array();
		ma_matchups = new Array();
		for(ii=0;ii<fe_divs.length;ii++) {
			divEl = fe_divs[ii];
			if (divEl.className=='matchup') fe_matchups[fe_matchups.length] = divEl;
		}
		for(ii=0;ii<ma_divs.length;ii++) {
			divEl = ma_divs[ii];
			if (divEl.className=='matchup') ma_matchups[ma_matchups.length] = divEl;
		}
		genders = new Array('fe','ma');
		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);
				contestantId = this.className.substr(13);
				linkLoc = gender+"_contestant.asp#contestant"+contestantId;
				window.open(linkLoc,'contestantProfile','width=500,height=400');
			}			
			gender = imgEl.className.substr(0,2);
			contestantId = imgEl.className.substr(13);
			labelEl = imgEl.nextSibling.nextSibling;
			labelDataText = simplifyName(labelEl.innerHTML);
			linkLoc = gender+"_contestant.asp#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.innerHTML);
			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.');
				}
			}
		}
	}
}

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 showTab(tabName) {
	availableTabs = new Array('female','male','winner');
	for(t=0;t<availableTabs.length;t++) {
		tab = availableTabs[t];
		tabEl = document.getElementById(tab);
		tabEl.style.display = (tab==tabName) ? 'block' : 'none';
	}
}

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() {
					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();
	}
}


///////////////// VIDEO EMBED /////////////////////////


function embedVideo() {
	el = document.getElementById('streambox');
	el.style.display = (el.style.display=='block') ? 'none' : 'block';
}

onload = function() {
	initBreakoutLinks();
	initSN();
	initContestants();
	preloadImages();
}







//////////////// BROWSER DETECTION ////////////////////

var browser=navigator.appName;
if (browser=="Microsoft Internet Explorer") {
	var version=parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5));
	if (version <= 6) {
		document.write('<link rel="stylesheet" href="ie6.css" media="screen, print" type="text/css" />');
	}
}





