
YAHOO.util.Event.onDOMReady(function () {

	var statisticsTip = new YAHOO.widget.Tooltip("statisticstip", {
		context: "statistics",
		text: "n=observed samples",
		showDelay: 200,
		hideDelay: 500
		} );
	var aafreqTip = new YAHOO.widget.Tooltip("aafreqtip", {
		context: "aafreq",
		text: "African American",
		showDelay: 200,
		hideDelay: 500
		} );
	var cafreqTip = new YAHOO.widget.Tooltip("cafreqtip", {
		context: "cafreq",
		text: "Caucasian",
		showDelay: 200,
		hideDelay: 500
		} );
	var asfreqTip = new YAHOO.widget.Tooltip("asfreqtip", {
		context: "asfreq",
		text: "Asian",
		showDelay: 200,
		hideDelay: 500
		} );
	var mefreqTip = new YAHOO.widget.Tooltip("mefreqtip", {
		context: "mefreq",
		text: "Mexican",
		showDelay: 200,
		hideDelay: 500
		} );
	var pafreqTip = new YAHOO.widget.Tooltip("pafreqtip", {
		context: "pafreq",
		text: "Pacific Islander",
		showDelay: 200,
		hideDelay: 500
		} );

	addTableRolloverEffect('highLightTable','tableRollOverEffect1','tableRowClickEffect1');
	// Used for sample plot to allow style switching; keep last cuz error will halt execution of function.
	onloadStyleSheet();
});

if (typeof pmt == "undefined" || !pmt) {
	// The global pmt namespace
	var pmt = {};
}

function toggle(idname,imgname) { 
		document.getElementById(idname).style.display = (document.getElementById(idname).style.display == 'none') ? 'block' : 'none'; 
		document.getElementById(imgname).src = (document.getElementById(imgname).src.indexOf('arrow_up.gif')  > -1) ? '/images/arrow_down.gif' : '/images/arrow_up.gif'; 
}

function toggleAll(itemname,state){ 
		var tmp = document.getElementsByTagName('div'); 
		for (i=0;i<tmp.length;i++){ 
			if (tmp[i].className == itemname) 
				tmp[i].style.display = state;
			} 
		var imagestate = (state == 'block') ? '../../images/arrow_down.gif' : '../../images/arrow_up.gif';
		tmp = document.getElementsByTagName('IMG');
		for (i=0;i<tmp.length;i++){ 
			if (tmp[i].className == itemname && tmp[i].src.indexOf('arrow_up.gif') > -1){
					tmp[i].src = imagestate;
					}
			else if (tmp[i].className == itemname && tmp[i].src.indexOf('arrow_down.gif') > -1){
					tmp[i].src = imagestate;
					}
				}
		} 

// Allow for nested blocks of tabs
function tabSwitch(divId,cellId) {
	var blockDiv = $("#" + divId);
	var tabDiv = $("#" + cellId);
	var cellClassNameBase = tabDiv.attr("class").split('_')[0];
	var tabTable = tabDiv.closest('table');
	if (blockDiv.css("display") == "block") {
		return;
	}
	$('div', tabTable).each(function(i) {
		if (this.id == cellId) {
			this.className = cellClassNameBase + '_on';
		} else {
			this.className = cellClassNameBase + '_off';
		}
	});
	tabTable.nextAll('div.tabContentGrp').each(function(i) {
		if (this.id == divId) {
			this.style.display = "block";
		}
		else {
			this.style.display = "none";
		}
	});
}

function ShowPopupSnpStat(e,genotypes){
		var genoArray = genotypes.split('::');
		var cont = '<table class="popup" bgcolor="#458B00">';
		headerStr = '<tr><td><font color="#FFFFFF">' + 'genotype' + '</font></td>' + '<td><font color="#FFFFFF">' + 'number' + '</font></td></tr>';
		cont += headerStr;
		for (i=0;i<genoArray.length;i++){ 
			var valueArray = genoArray[i].split(':');
			genoStr = '<tr><td><font color="#FFFFFF"><center>' + valueArray[0] + '</center></font></td>' + '<td><font color="#FFFFFF"><center>' + valueArray[1] + '</center></font></td></tr>';
			cont += genoStr;
		}
		cont += '</table>';
		
		hp = document.getElementById("hoverpopup");
		hp.innerHTML = cont;
		
		// Set position of hover-over popup
		hp.style.left = e.pageX - 110 + "px";
		hp.style.top = e.pageY + 30 + "px";

		// Set popup to visible
		hp.style.visibility = "Visible";
}

function ShowPopupMutStat(e,mutationsObserved){
			cont = '<table class="popup" bgcolor="#458B00">'
				+ '<tr><td><font color="#FFFFFF">variants observed: ' + mutationsObserved + '</font></td></tr>'
				+ '</table>';
			
			hp = document.getElementById("hoverpopup");
			hp.innerHTML = cont;
			
			// Set position of hover-over popup
			hp.style.left = e.pageX - 120 + "px";
			hp.style.top = e.pageY + 25 + "px";
	
			// Set popup to visible
			hp.style.visibility = "Visible";
}

function HidePopup(){
	var hp = document.getElementById("hoverpopup");
	hp.style.visibility = "Hidden";
}

/* Used by both Haploview and PopGen. */
function checkForm(){
	var startPos = document.haploViewForm.startPos.value.replace(/\s/g, "");
	var stopPos = document.haploViewForm.stopPos.value.replace(/\s/g, "");
	var geneStartPos = document.haploViewForm.geneStartPos.value;
	var geneStopPos = document.haploViewForm.geneStopPos.value;
	var dRegExp = /\D/;
	if (dRegExp.test(startPos) == true || dRegExp.test(stopPos) == true) {
		alert("Invalid non-numerical entry.");
		return false;
	}
	else if (parseInt(startPos) > parseInt(stopPos)) {
		alert("Invalid range.");
		return false;
	}
	
	var elm = document.getElementsByTagName('select');
	for (i=0;i<elm.length;i++){ 
		if (elm[i].value == null || elm[i].value == ""){
			window.alert("Error: Selct " + elm[i].name);
			return false;
		}
	}
	var c = 0;
	elm = document.getElementsByTagName('input');
	for (i=0;i<elm.length;i++){ 
		if (elm[i].name.match('ethnicGroup') && elm[i].disabled == false && elm[i].checked){
			c += 1;
		}
		else if (elm[i].value == 'marker' && elm[i].checked){
			return true;
		}
	}
	if (c == 0){
		window.alert("Error: Choose ethnic group");
		return false;
		}
	
}


$(document).ready(function() {
	$('table.highlight tr.highlight')
		.hover(function() {
			$(this).addClass('tableRollOverEffect1');
		}, function() {
			$(this).removeClass('tableRollOverEffect1');
		})
		.click(function() {
			$(this).siblings().removeClass('tableRowClickEffect1');
			$(this).addClass('tableRowClickEffect1');
		});
});


