﻿var maxLevel=5;
var imagesA=Array();
var captionsA=Array();
var parents=Array();
var dogNames=Array();
var imgPeds=Array();

function levelNum(t){
	if (parents[t]==null) {
		return 1;
	} else {
	return 1+Math.max(levelNum(parents[t][0]),levelNum(parents[t][1]));
	}
}

function buildGallery(idd,target,mode){
	var retval=false;
	if (imagesA[idd]!=null) {
		for (j=0;j<imagesA[idd].length;j++){
				if ((mode<=0) || (j==mode-1)){
					var imgpath=imagesDir+"dogs/";
					if (idd==28) imgpath=imagesDir+"gallery/";
					var aObj=document.createElement("a");
					var imgObj=document.createElement("img");
					imgObj.className="thumbs";
					aObj.rel="lightbox["+idd+"]";
					//aObj.title=captionsA[idd][j];
					imgObj.src=imgpath+((mode==-1)?"":"tn_")+imagesA[idd][j];
					aObj.href=imgpath+imagesA[idd][j];
					aObj.appendChild(imgObj);
					document.getElementById(target).appendChild(((mode==-1)?imgObj:aObj));				
					retval=true;
				}
			}
	}
	return retval;
}

function buildPed(t,target){
	if (imgPeds[t]!=""){
		
		pedImg=document.createElement("p");
		pedImg.setAttribute('align','center');
		link=document.createElement("a");
		link.setAttribute('href','images/pedigrees/'+imgPeds[t]);
		link.setAttribute('target','_blank');
		img=document.createElement("img");
		img.setAttribute('src','images/pedigree.png');
		img.setAttribute('border','0');
		link.appendChild(img);
		pedImg.appendChild(link);
		document.getElementById(target).appendChild(pedImg);	
		return;
	}
	var col, row;
	retTable_=document.createElement("TABLE");
	retTable=document.createElement("TBODY");
	retTable.className="pedigree";
	retTable_.className="pedigree_";
	coln=Math.min(levelNum(t),maxLevel);
	for (row=0;row<Math.pow(2,coln-1);row++){
	    actrow=document.createElement("TR");
		var cols=Array();
		for (col=0;col<coln;col++){
			if (col==0) {
				cols[col]=t;
			} else {
				if (parents[cols[col-1]]!=null) cols[col]=parents[cols[col-1]][Math.floor(row/Math.pow(2,coln-col-1))%2];
			}
			var centerCell;
			if ((col!=0) && (row%Math.pow(2,coln-col)==0)){
				centerCell=document.createElement("TD");
				centerCell.rowSpan=""+Math.pow(2,coln-col);
				centerCell.appendChild(document.createTextNode(" "));
			if (centerCell!=null) actrow.appendChild(centerCell);
			}
			var actcol;
			if ((col>0)&&((row%Math.pow(2,coln-col-1)==0)) || (col==coln-1)) {
				actcol=document.createElement("TD");
				actcol.rowSpan=""+Math.pow(2,coln-col-1);
				if (cols[col]!=null){ 
					if (col!=0){
						if (Math.floor(row/Math.pow(2,coln-col-1))%2==0) {
							actcol.className="upperCell";	
							//if (col>1)centerCell.className="centerCell";
						} else {
							actcol.className="lowerCell";
						}
					}
					actcol.appendChild(document.createTextNode(dogNames[cols[col]]));
				} else {
					actcol.appendChild(document.createTextNode(" "));
				}
			if (actcol!=null) actrow.appendChild(actcol);
			}
			
}
		retTable.appendChild(actrow);
	}
	retTable_.appendChild(retTable);
	document.getElementById(target).appendChild(retTable_);
}

function loadPed(){
	pedFile=loadXmlFile(dogFile);
	var x;
	for (i=0;i<pedFile.getElementsByTagName("dog").length;i++){
		x=pedFile.getElementsByTagName("dog")[i];
		var id=getAttr(x.attributes.getNamedItem("id"));
		var dogname=((x.getElementsByTagName("name").length>0)?x.getElementsByTagName("name")[0].childNodes[0].nodeValue:"");
		var imgped=((getAttr(x.attributes.getNamedItem("imgped"))=="yes")?(id+".jpg"):"");
		dogNames[id]=dogname;
		imgPeds[id]=imgped;
		if (x.getElementsByTagName("parent")[0]!=null){
		parents[id]=Array(2);
		parents[id][0]=x.getElementsByTagName("parent")[0].childNodes[0].nodeValue;
		parents[id][1]=x.getElementsByTagName("parent")[1].childNodes[0].nodeValue;
		}
		if (x.getElementsByTagName("image")[0]!=null){
		imagesA[id]=Array(x.getElementsByTagName("image").length);
		captionsA[id]=Array(x.getElementsByTagName("image").length);
		for (j=0;j<x.getElementsByTagName("image").length;j++){
			imagesA[id][j]=x.getElementsByTagName("image")[j].childNodes[0].nodeValue;
			captionsA[id][j]=getAttr(x.getElementsByTagName("image")[j].attributes.getNamedItem("caption"));
		}
		}
	}
}
