// -- General --

function initHeaderBoxes() {
	initHeaderBox("usernameInput");
	initHeaderBox("passwordInput");
	initHeaderBox("searchBoxInput");
	initLoginBoxes();
	initAdvSearchBox();
}
function initHeaderBox(b) {
	var a = document.getElementById(b);
	if (a) {
		a.onfocus = cleanupField;
		a.onblur = resetField
	}
}

var emptyFields = new Array();
emptyFields.usernameInput = true;
emptyFields.passwordInput = true;
emptyFields.searchBoxInput = true;
var fieldContents = new Array();
fieldContents.usernameInput = 
	(userLang == "nl" ? "Gebruikersnaam.." : "Username..");
fieldContents.passwordInput = "123456";
fieldContents.searchBoxInput = 
	(userLang == "nl" ? "Typ hier je zoekterm in.." : "Type your search term..");
function cleanupField() {
	if (this.id == "usernameInput" && this.value == fieldContents.usernameInput) {
		this.style.color = "#6e2e0b";
		this.value = ""
	} else {
		if (this.id == "passwordInput"
				&& this.value == fieldContents.passwordInput) {
			this.style.color = "#6e2e0b";
			this.value = ""
		} else {
			if (this.id == "searchBoxInput"
					&& this.value == fieldContents.searchBoxInput) {
				this.style.fontStyle = "normal";
				this.style.color = "#6e2e0b";
				this.value = ""
			}
		}
	}
}
function resetField() {
	if (this.value == "") {
		if (this.id == "searchBoxInput") {
			this.style.fontStyle = "italic"
		}
		this.style.color = "#acabab";
		this.value = fieldContents[this.id]
	}
}


//-- Login --

function initLoginBoxes() {
	var b = new YAHOO.util.KeyListener("usernameInput", {
		keys : 13
	}, function() {
		submitLoginForm()
	});
	b.enable();
	var a = new YAHOO.util.KeyListener("passwordInput", {
		keys : 13
	}, function() {
		submitLoginForm()
	});
	a.enable()
}

function submitLoginForm() {
	var a = document.getElementById("usernameInput");
	if (a.value == "Gebruikersnaam..") {
		a.value = ""
	}
	var b = document.getElementById("passwordInput");
	if (b.value == "123456") {
		b.value = ""
	}
	document.getElementById("loginForm").submit()
};


// -- Advanced search --

// images
var advBoxId = "advSearchBox";
var imgPathUp;
var imgPathUp_hover;
var imgPathDown;
var imgPathDown_hover;

// anim data
var curDim = "beer";
var advVisible = false;
var boxHeights = [];
boxHeights["beer"] = 592;
boxHeights["prod"] = 262;
boxHeights["cafe"] = 180;
var atts = {
	height : {
		from : 1,
		to : 1
	}
};

function advControl(goDown, extraHeight) {
	if (goDown == undefined)
		goDown = !advVisible;
	if (extraHeight != undefined)
		boxHeights[curDim] += extraHeight;
	
	var setAtts = false;
	if (goDown && !advVisible) {
		atts.height.from = 1;
		atts.height.to = boxHeights[curDim];
		setAtts = true;
	} else if (!goDown && advVisible) {
		atts.height.from = atts.height.to;
		atts.height.to = 1;
		setAtts = true;
	}
	
	if (setAtts) {
		advVisible = !advVisible;
		animBox(atts);
	}
}

function switchDim(newDim) {
	if (newDim != curDim) {
		curDim = newDim;
		switchBoxes(newDim);

		if (advVisible) {
			atts.height.from = atts.height.to;
			atts.height.to = boxHeights[newDim];
			
			animBox(atts);
		}
	}
}

function switchBoxes(newDim) {
	document.getElementById("advSearchBeer").style.display = (newDim == "beer" ? "block" : "none");
	document.getElementById("advSearchProd").style.display = (newDim == "prod" ? "block" : "none") 
	document.getElementById("advSearchCafe").style.display = (newDim == "cafe" ? "block" : "none")
}

function animBox(anim) {
	// (?)
	document.getElementById(advBoxId).style.display = "block";
	
	var anim = new YAHOO.util.Anim(advBoxId, atts, 0.5);
	anim.onComplete.subscribe(function() {
		animComplete()
	});
	anim.animate();
}

function animComplete() {
	if (advVisible)
		document.getElementById("advSearchControl").style.backgroundImage = "url('"
				+ imgPathUp + "')"
	else
		document.getElementById("advSearchControl").style.backgroundImage = "url('"
				+ imgPathDown + "')"
}

//register event listeners
function initAdvSearchBox() {
	initImgPaths();
	
	document.getElementById("advSearchControl").onmouseover = advSearchControl_hover;
	document.getElementById("advSearchControl").onmouseout = advSearchControl_noHover;
	
	YAHOO.util.Event.on("advSearchControl", "click", function() {
		advControl();
	});
	
	var c = new YAHOO.util.KeyListener("searchBoxInput", {
		keys : 40
	}, function() {
		advControl(true)
	});
	c.enable();
	
	var b = new YAHOO.util.KeyListener("searchBoxInput", {
		keys : 38
	}, function() {
		advControl(false)
	});
	b.enable();
	
	var a = new YAHOO.util.KeyListener("searchBoxInput", {
		keys : 13
	}, function() {
		submitSearchForm()
	});
	a.enable();
	
	// YAHOO lib bug (?)
	new YAHOO.util.Anim("advSearchBox", {
		height : {
			to : 1
		}
	}, 0.1).animate()
}

function initImgPaths() {
	imgPathUp = "../images/" + lang_folder + "/advSearchControl_up.jpg";
	imgPathUp_hover = "../images/" + lang_folder + "/advSearchControl_up_hover.jpg";
	imgPathDown = "../images/" + lang_folder + "/advSearchControl_down.jpg";
	imgPathDown_hover = "../images/" + lang_folder + "/advSearchControl_down_hover.jpg";
}

function advSearchControl_hover() {
	if (advVisible) {
		this.style.backgroundImage = "url('" + imgPathUp_hover + "')"
	} else {
		this.style.backgroundImage = "url('" + imgPathDown_hover + "')"
	}
}
function advSearchControl_noHover() {
	if (advVisible) {
		this.style.backgroundImage = "url('" + imgPathUp + "')"
	} else {
		this.style.backgroundImage = "url('" + imgPathDown + "')"
	}
}

// Search form
function submitSearchForm() {
	if (curDim == "beer") {
		setNameField("bierNaam");
		document.getElementById("searchBeerForm").submit()
	} else if (curDim == "prod") {
		setNameField("prodNaam");
		document.getElementById("searchProducentForm").submit()
	} else if (curDim == "cafe") {
		setNameField("cafeNaam");
		document.getElementById("searchCafeForm").submit()
	}
}

function setNameField(a) {
	var b = document.getElementById("searchBoxInput");
	var c = document.getElementById(a);
	if (b.value != fieldContents.searchBoxInput) {
		c.value = b.value
	}
}
