//<SCRIPT type="text/javascript" src="menu2.js"></SCRIPT>
/**************************************
*    Copyright ©  Christian Genne     *
*      Alpha Interactive Design       *
**************************************/

var posleft = 0;
var top = 101;
var width = 135;
var linkObjects = new Array;
var nLinkObjects = 0;
var folderObjects = new Array;
var nFolderObjects = 0;
var common = "font-family:tahoma;font-size:8pt;";

/***************************************/

function Folder(parent, description) {
	this.type = 0;

	if (parent != 0) {
		parent.addChild(this);
		this.open = false;
	} else {
		this.open = true;
	}
	this.children = new Array;
	this.nChildren = 0;
	this.parent = parent;
	this.description = description;
	folderObjects[nFolderObjects] = this;
	this.ID = nFolderObjects++;

	this.isLastChild = true;
	lastFolder = this;

	this.addChild = addFolderChild;
	this.draw = drawFolder;
	this.changeState = changeFolderState;
	this.changeVis = changeFolderVis;
	this.setPosition = setPosition;

}

function changeFolderState(headFolder, state) {
	if (this.isLastChild == true) imageExtra = "2"; else imageExtra = "";
	if (headFolder) {
		this.open = !this.open;
		state = this.open;
		if (state == false) {
			this.docState.src = "images/plusline" + imageExtra + ".gif";
			this.docFolder.src = "images/icon-closed2.gif";
		} else {
			this.docState.src = "images/minusline" + imageExtra + ".gif";
			this.docFolder.src = "images/icon-open2.gif";
		}
	} else if (this.open == false) state = false;
	for (this.i = 0; this.i < this.nChildren; this.i++) {
		this.children[this.i].changeVis(state);
	}
	if (headFolder)
	{
		generatePositions();
	}
}

function changeFolderVis(state) {
	if (state == true) {
		if (browser == 0)
			this.docObj.style.visibility = "visible";
		else if (browser == 1)
			this.docObj.visibility = "show";
	} else {
		if (browser == 0)
			this.docObj.style.visibility = "hidden";
		else if (browser == 1)
			this.docObj.visibility = "hide";
	}
	this.changeState(false, state);
}

function addFolderChild(child) {
	if (this.nChildren>0) this.children[this.nChildren-1].isLastChild = false;
	this.children[this.nChildren] = child;
	this.nChildren++;
}

function drawFolder(left) {
	if (this != ground) {

		if (this.isLastChild == true) stateImage = "images/plusline2.gif"; else stateImage = "images/plusline.gif";
		if (this.open == true) folderImage = "images/icon-open2.gif"; else folderImage = "images/icon-closed2.gif";
		if (this.parent.open == true) vis = (browser == 1 ) ? "show" : "visible"; else vis = (browser == 1 ) ? "hide" : "hidden";
		if( browser == 1 ) {
			document.write("<LAYER id='folder" + this.ID + "' onmouseover='bgColor=\"silver\"' onmouseout='bgColor=\"\"' style='position:absolute;height:20px;visibility:" + vis + ";left:" + posleft + "px;'>");
		} else {
			document.write("<SPAN id='folder" + this.ID + "' onmouseover='style.background=\"silver\"' onmouseout='style.background=\"\"' style='position:absolute;height:20px;visibility:" + vis + ";left:" + posleft + "px;'>");
		}
		document.write("<TABLE border=0 width=" + width + " cellpadding=0 cellspacing=0><TR>");
		document.write(left);
		document.write("<TD width=16 height=20 valign=top>");
		document.write("<IMG id='stateimage" + this.ID + "' src='" + stateImage + "' border=0>");
		document.write("</TD><TD width=20 valign=top>");
		document.write("<IMG id='folderimage" + this.ID + "' src='" + folderImage + "' border=0>");
		document.write("</TD><TD>");
		document.write("<A href='javascript:clickFolder(" + this.ID + ")' style='" + common + "'>" + this.description + "</A>");
		document.write("</TD></TR></TABLE>");
		document.write("</SPAN>");

		if( browser == 1 ) {
			this.docState = document.layers["stateimage" + this.ID];
			this.docFolder = document.layers["folderimage" + this.ID];
			this.docObj = document.layers["folder" + this.ID];
		} else {
			this.docState = document.all["stateimage" + this.ID];
			this.docFolder = document.all["folderimage" + this.ID];
			this.docObj = document.all["folder" + this.ID];
		}

		for (this.i = 0; this.i < this.nChildren; this.i++) {
			if (this.isLastChild == false) extra = "<IMG src='images/line2.gif'>"; else extra = "";
			this.children[this.i].draw(left + "<TD width=20>" + extra + "</TD>");
		}
	} else {
		for (this.i = 0; this.i < this.nChildren; this.i++) {
			this.children[this.i].draw("");
		}
	}
}

/*****************************************/

function Link(parent, description, link) {
	this.type = 1;

	parent.addChild(this)
	this.description = description;
	this.link = link;
	linkObjects[nLinkObjects] = this;
	this.ID = nLinkObjects++;
	this.parent = parent;

	this.isLastChild = true;

	this.draw = drawLink;
	this.changeVis = changeLinkVis;
	this.setPosition = setPosition;
}

function drawLink(left) {

	if (this.isLastChild == true) stateImage = "images/line3.gif"; else stateImage = "images/line.gif";
	folderImage = "images/icon.gif";
	if (this.parent.open == true) vis = (browser == 1 ) ? "show" : "visible"; else vis = (browser == 1 ) ? "hide" : "hidden";
	if( browser == 1 ) {
		document.write("<LAYER id='link" + this.ID + "' onmouseover='bgColor=\"silver\"' onmouseout='bgColor=\"\"' style='position:absolute;height:20px;visibility:" + vis + ";left:" + posleft + "px;" + common + "'>");
	} else {
		document.write("<SPAN id='link" + this.ID + "' onmouseover='style.background=\"silver\"' onmouseout='style.background=\"\"' style='position:absolute;height:20px;visibility:" + vis + ";left:" + posleft + "px;" + common + "'>");
	}
	document.write("<TABLE border=0 width=" + width + " height=100% cellpadding=0 cellspacing=0><TR>");
	document.write(left);
	document.write("<TD width=16 height=20 valign=top>");
	document.write("<IMG id='stateimage" + this.ID + "' src='" + stateImage + "' border=0>");
	document.write("</TD><TD width=20 valign=top>");
	document.write("<IMG src='" + folderImage + "' border=0>");
	document.write("</TD><TD>");
	document.write("<A href='" + this.link + "' style='" + common + "'>" + this.description + "</A>");
	document.write("</TD></TR></TABLE>");
	document.write("</SPAN>");

	if( browser == 1 ) {
		this.docState = document.layers["stateimage" + this.ID];
		this.docObj = document.layers["link" + this.ID];
	} else {
		this.docState = document.all["stateimage" + this.ID];
		this.docObj = document.all["link" + this.ID];
	}

	tempHeight = this.docObj.offsetHeight;
	if (tempHeight % 20 > 0)
		tempHeight += 20 - tempHeight % 20;
	//this.docObj.style.height = tempHeight;
	//alert(this.docObj.offsetHeight);
}

function changeLinkVis(state) {
	if (state == true) {
		if (browser == 0)
			this.docObj.style.visibility = "visible";
		else if(browser == 1)
			this.docObj.visibility = "show";
	} else {
		if (browser == 0)
			this.docObj.style.visibility = "hidden";
		else if(browser == 1)
			this.docObj.visibility = "hide";
	}
}

/*****************************/

function clickFolder(id) {
	folderObjects[id].changeState(true);
}

/*****************************/

function setPosition(pos)
{
	this.position = pos;
	if(browser == 1) {
		this.docObj.top = pos;
	} else {
		this.docObj.style.top = pos;
	}
	//alert(this.description);
}

function generatePositions(folder, position)
{
	if (!folder)
	{
		folder = ground;
		position = top;
	}
	for (folder.i = 0; folder.i < folder.nChildren; folder.i++)
	{
		curFolder = folder.children[folder.i];
		curFolder.setPosition(position);
		//position += curFolder.docObj.offsetHeight;
		position += 20;
		if (curFolder.type == 0) 
		{
			if (curFolder.open == true) 
				position = generatePositions(curFolder, position);
		}
	}
	return position;
}
function initialize() {
	if (navigator.appName == "Microsoft Internet Explorer")
		browser = 0;
	else if (navigator.appName == "Netscape")
		browser = 1;
	else
		browser = 2;
	//alert(browser);
	//document.write(browser);
	ground.draw("");
	generatePositions();
}

ground = new Folder(0, "Grund");
	information = new Link(ground, "Information", "information.html");
	nyheter = new Link(ground, "Nyheter", "nyheter.html");
	horse = new Link(ground, "Hästar i träning", "traning.html");
	dunevad = new Folder(ground, "Dunevad travcamp");
		stallar = new Link(dunevad, "Stallar", "dunevad.html");
		traning = new Link(dunevad, "Träning", "dunevad2.html");
		vag = new Link(dunevad, "Vägbeskrivning", "horse_vagbeskrivning.html");
	aby = new Link(ground, "Åby stallet", "aby.html");
	jon = new Link(ground, "Om Jon", "om_jwp.html");
	personal = new Folder(ground, "Personal");
		tina = new Link(personal, "Tina", "personal_tina.html");
		sofie = new Link(personal, "Sofie", "personal_sofie.html");
		boris = new Link(personal, "Boris", "personal_boris.html");
		maria = new Link(personal, "Maria", "personal_maria.html");
	tips = new Link(ground, "Åby tips", "tips.html");
	starter = new Link(ground, "Veckans starter", "starter.html");
	salu = new Link(ground, "Hästar till salu", "tillsalu.html");
	unghast = new Link(ground, "Unghästpaketet 2010", "unghästpaket.html");
	priser = new Link(ground, "Prislista", "priser.html");
	mail = new Link(ground, "Mail", "mailto:jon.pedersen@telia.com");

initialize();