$(document).ready(function(){
    resetpage();
	redraw_lines();
	show_researcher();
	show_birthplace();
	refreshnumbers();
});
function refreshnumbers(){
	$("#chkp span").remove();
	$("#chko span").remove();
	$("#chkc span").remove();
	$("#chkr span").remove();
	$("#chkp").append("<span>"+$(".chkp").size()+"</span>");
	$("#chko").append("<span>"+$(".chko").size()+"</span>");
	$("#chkc").append("<span>"+$(".chkc").size()+"</span>");
	$("#chkr").append("<span>"+($("table#researcher tr").size()-1)+"</span>");
}
function redraw_lines(){
 	$(".left").each(function(){
 	    i = $(this).attr("id").substr(1);
 	    var f = $("#f"+i).outerHeight();
 	    var m = $("#m"+i).outerHeight();
 	    var d = $("#d"+i).outerHeight();
		gen1 = $("#d"+i).hasClass("gen1");
 	    if(gen1 && f >= m*2){
	 		$(".x"+i+":first").css("height",f/2+d/2-20);
		}
		else if (gen1 && m >= f*2){
 	        $(".x"+i+":first").css("height",f+5);
 	        $(".x"+i+":last").css("height",m-d-5);
		}
		else {
	    	var newheight = ($("#l"+i).outerHeight() - d)/2;
	 		$(".x"+i).each(function(){
	  			var $this = $(this);
				$this.css("height",newheight);
			});
		}
	});
	$("#bg").css("width",$(document).width());
}
function refresh(){
    setchk("chkp");setchk("chko");setchk("chkc");setchk("chkd");setchk("chkr");
	$(document).ready(function(){
		redraw_lines()
	});
}
function getnext(pid,uniqueID,gen){
    $("#n"+uniqueID).html("<img src='/images/waiting.gif' height='15'>");
	var data = $.ajax({type: "GET",url: "ajax/pedigree_next.cfm?pid="+pid+"&gen="+gen, dataType: "html", async: false}).responseText;
	$(".last").removeClass("last");
	$(".last").removeClass("nexttolast");
	$(".x"+uniqueID).removeClass("start_empty");
	$("#n"+uniqueID).removeClass("next");
	$("#n"+uniqueID).html("<a name='a"+uniqueID+"' href='javascript:collapse("+pid+","+uniqueID+")' onmouseover='collin("+uniqueID+")' onmouseout='collout("+uniqueID+")' class='collapse'>X</a><BR>");
	$("#n"+uniqueID).append(data);
	window.location.href = "#a"+uniqueID;
	refresh();
	show_researcher();
	show_birthplace();
	refreshnumbers();
}
function collapse(pid,uniqueID){
	$("#n"+uniqueID).html("<a href='javascript:getnext("+pid+","+uniqueID+",0)'><img src=\"/images/rightarrowgray.gif\" width=12 height=13></a>");
	$("#n"+uniqueID).removeClass("collapse-highlight");
	$("#n"+uniqueID).addClass("next");
 	$(".mline,.fline,.xline").css("height",0);
 	refresh();
}
function collin(uniqueID){
	$("#n"+uniqueID).addClass("collapse-highlight");
}
function collout(uniqueID){
    $("#n"+uniqueID).removeClass("collapse-highlight");
}
function setchk(cls){
	checked = $("#"+cls).hasClass("selected");
	if(cls == "chkr"){
		if(checked){
			$(".data").removeClass("noborder");
			$("table#researcher").show();
		}
		else {
			$(".data").addClass("noborder");
			$("table#researcher").hide();
		}
	}
	else {
		if(checked){
			$("." + cls).show();
		}
		else {
			$("." + cls).hide();
		}
	}
	if(cls == "chkd"){
		if(checked){
			$(".nextolast").css("max-height",99);
		}
		else {
			if(checked){
				$(".nextolast").css("max-height",85);
			}
			else {
				$(".nextolast").css("max-height",15);
			}
		}
	}
 	if(cls == "chkp"){
		if(checked){
   			show_magnify();
		}
	}
}
function update(cls){
	if("chkp,chko,chkc,chkd,chkr".indexOf(cls) >= 0){
		if($("#"+cls).hasClass("selected")){
			$("#"+cls).removeClass("selected");
		}
		else {
			$("#"+cls).addClass("selected");
		}
	}
	setchk(cls);
	chk = $("#generations").val();
	chk += $("#chkp").hasClass("selected") ? "1" : "0";
	chk += $("#chko").hasClass("selected") ? "1" : "0";
	chk += $("#chkc").hasClass("selected") ? "1" : "0";
	chk += $("#chkd").hasClass("selected") ? "1" : "0";
	chk += $("#chkr").hasClass("selected") ? "1" : "0";
	createCookie("OFTPEDIGREE",chk,100);
	$(document).ready(function(){
		$(".mline,.fline,.xline").css("height",0);
		redraw_lines()
	});
}
function resetpage(){
	chk = readCookie("OFTPEDIGREE");
	if(/^[0-9]+$/.test(chk)){
		//set_dropdown(document.ped.generations,chk.substr(0,1));
		if(chk.substr(1,1) == "1"){
			$("#chkp").addClass("selected"); //pics
		}
		if(chk.substr(2,1) == "1"){
			$("#chko").addClass("selected"); //docs
		}
		if(chk.substr(3,1) == "1"){
			$("#chkc").addClass("selected"); //disc
		}
		if(chk.substr(4,1) == "1"){
			$("#chkd").addClass("selected"); //detail
		}
		if(chk.substr(5,1) == "1"){
			$("#chkr").addClass("selected"); //res
		}
	}
	setchk("chkp");setchk("chko");setchk("chkc");setchk("chkd");setchk("chkr");
}
function show_researcher(){
    var data = $.ajax({type: "GET",url: "ajax/pedigree_researcher.cfm", dataType: "html", async: false}).responseText;
    $("#tbody_researcher").html(data);
}
function show_birthplace(){
    var data = $.ajax({type: "GET",url: "ajax/pedigree_birthplace.cfm", dataType: "html", async: false}).responseText;
    $("#tbody_birthplace").html(data);
}
