	/* 
 * javascript for ajax and mootools
 * 
*/

// variable attributes used

	var hpBeer_itemW			= 92;	// the with of the beer_item div (is set in stylesheet too!)
	var hpBeer_items			= 5;	// the number of items visible
	var hpBeer_maxWidth			= 230;	// the max width of the tijdslijn div
	var hpBeer_timerSpeed		= 100;	// milliseconds (1000 milliseconds = 1 second)
	var hpBeer_timerAdd			= 2;	// add x-pixels per x-milliseconds set in timerSpeed
	var hpBeer_div_in_in_width	= 3;	// the width of the in in div (is set in stylesheet too!)

// fixed attributes

	var hpBeer_xpos				= 0;
	var hpBeer_currStep			= 1;
	var hpBeer_steps			= null;
	var hpBeer_div_in			= null;
	var hpBeer_div_in_in		= null;
	var hpBeer_div_width		= null;
	var hpBeer_timerId			= 0;
	var hpBeer_div_in_xpos		= 0;
	var hpBeer_div_in_in_xpos	= 0;
	if (hpBeer_maxItems == undefined || hpBeer_maxItems == null || hpBeer_maxItems <= hpBeer_items) var hpBeer_maxItems = hpBeer_items;

// functions

function hpBeerMove(direction) {
	var hpBeer_div = new fx.Style('beer_item_mover', 'margin-left', {duration:1200});
	var normalLength = hpBeer_itemW * hpBeer_items;
	var shortLength = (hpBeer_maxItems % hpBeer_items) * hpBeer_itemW;
	if (shortLength == 0) shortLength = normalLength; // no rest... do normal length
	hpBeer_steps = Math.ceil(hpBeer_maxItems / hpBeer_items);
	if (direction == 'left') {
		if (hpBeer_currStep == hpBeer_steps) {			// laatste stap
			hpBeer_div.custom(hpBeer_xpos, (hpBeer_xpos + shortLength));
			hpBeer_xpos = hpBeer_xpos + shortLength;
			//--
			hpBeer_div_in.custom(hpBeer_div_in_xpos, ((hpBeer_currStep-1) * hpBeer_div_width) - hpBeer_div_width);	// move the in div to the start
			hpBeer_div_in_xpos = ((hpBeer_currStep-1) * hpBeer_div_width) - hpBeer_div_width;
			//--
			hpBeer_currStep--;
		} else if (hpBeer_currStep > 1) {				// normale lengte
			hpBeer_div.custom(hpBeer_xpos, (hpBeer_xpos + normalLength));
			hpBeer_xpos = hpBeer_xpos + normalLength;
			//--
			hpBeer_div_in.custom(hpBeer_div_in_xpos, ((hpBeer_currStep-1) * hpBeer_div_width) - hpBeer_div_width);	// move the in div to the previous step
			hpBeer_div_in_xpos = ((hpBeer_currStep-1) * hpBeer_div_width) - hpBeer_div_width;
			//--
			hpBeer_currStep--;
		}
	} else {
		if (hpBeer_currStep < hpBeer_steps-1) {			// normale lengte
			hpBeer_div.custom(hpBeer_xpos, (hpBeer_xpos - normalLength));
			hpBeer_xpos = hpBeer_xpos - normalLength;
			//--
			hpBeer_div_in.custom(hpBeer_div_in_xpos, (hpBeer_currStep * hpBeer_div_width));					// move the in div to the next step
			hpBeer_div_in_xpos = (hpBeer_currStep * hpBeer_div_width);
			//--
			hpBeer_currStep++;
		} else if (hpBeer_currStep == hpBeer_steps-1) {	// laatste stap
			hpBeer_div.custom(hpBeer_xpos, (hpBeer_xpos - shortLength));
			hpBeer_xpos = hpBeer_xpos - shortLength;
			//--
			hpBeer_div_in.custom(hpBeer_div_in_xpos, (hpBeer_currStep * hpBeer_div_width));					// move the in div to the next step
			hpBeer_div_in_xpos = (hpBeer_currStep * hpBeer_div_width);
			//--
			hpBeer_currStep++;
		} else {										// terug naar start
			hpBeer_div.custom(hpBeer_xpos, 0);
			hpBeer_xpos = 0;
			//--
			hpBeer_div_in.custom(hpBeer_div_in_xpos, 0);													// reset the in div to the start
			hpBeer_div_in_xpos = 0;
			//--
			hpBeer_currStep = 1;
		}
	}
}

function hpBeerTimer_start() {
	hpBeer_div_in		= new fx.Style('beer_tijdslijn_in', 'margin-left', {duration:1000});
	hpBeer_div_in_in	= new fx.Style('beer_tijdslijn_in_in', 'margin-left', {duration:1000});
	hpBeer_steps		= Math.ceil(hpBeer_maxItems / hpBeer_items);										// get steps
	hpBeer_div_width	= Math.round(hpBeer_maxWidth / hpBeer_steps);										// get the div_width
	document.getElementById("beer_tijdslijn_in").style.width = hpBeer_div_width + 'px';						// set the width of in div
	document.getElementById("beer_controls_play").style.display = 'none';
	document.getElementById("beer_controls_pauze").style.display = 'block';
	hpBeerTimerId		= setTimeout("hpBeerTimer_go()", hpBeer_timerSpeed);
}

function hpBeerTimer_go() {
	if(hpBeerTimerId) clearTimeout(hpBeerTimerId);
	var newInInXpos = (hpBeer_div_in_in_xpos + hpBeer_timerAdd);
	if(newInInXpos <= (hpBeer_div_width - hpBeer_div_in_in_width)) {
		document.getElementById("beer_tijdslijn_in_in").style.marginLeft = newInInXpos + 'px';				// add x-pixels to the in in div
		hpBeer_div_in_in_xpos = newInInXpos;
		hpBeerTimerId = setTimeout("hpBeerTimer_go()", hpBeer_timerSpeed);
	} else {
		hpBeerTimer_nextStep();
	}
}

function hpBeerTimer_nextStep() {
	if(hpBeerTimerId) clearTimeout(hpBeerTimerId);
	hpBeer_div_in_in.custom(hpBeer_div_in_in_xpos, 0);
	hpBeer_div_in_in_xpos = 0;
	hpBeerMove('right');
	hpBeerTimerId = setTimeout("hpBeerTimer_go()", 1500);
}

function hpBeerTimer_stop() {
	if(hpBeerTimerId) clearTimeout(hpBeerTimerId);
	hpBeer_div_in_in.custom(hpBeer_div_in_in_xpos, 0);
	hpBeer_div_in_in_xpos = 0;
	document.getElementById("beer_controls_play").style.display = 'block';
	document.getElementById("beer_controls_pauze").style.display = 'none';
}

function switchLeftCol(show, hide) {
	document.getElementById("subnav_food_top_"+hide).id = "subnav_food_top_"+show;
	document.getElementById("leftcol_"+show).style.display = 'block';
	document.getElementById("leftcol_"+hide).style.display = 'none';
}

function showCity(prefecture_id) {
	$('city_'+prefecture_id).style.display = 'inline';
}

var xhr;

function getCities(prefecture, type) {
	xhr = createXHR();
	xhr.onreadystatechange=getCitiesSuccess;
	xhr.open("POST", "lib/ajax/getCities.php?prefecture="+prefecture+"&type="+type);
	xhr.send(null);
}

function createXHR() {
	var xhr;
	try { xhr = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try { xhr = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) { xhr = false;
		}
	}
	if (!xhr && typeof XMLHttpRequest != 'undefined') xhr = new XMLHttpRequest();
	return xhr;
}

function getCitiesSuccess(res) {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var res = xhr.responseText;
			document.getElementById("city").innerHTML = res;
			if (document.getElementById("bierfinder_submit")) document.getElementById("bierfinder_submit").style.display = "block";
		} else {
			alert("Couldn't get city!");
		}
	}
}

function whereToFind(f) {
	document.getElementById('whereToResults').innerHTML = '<table width="100%" border="0" cellspacing="1" cellpadding="2"></table>';
	document.getElementById('loading').style.display = "block";
	var vars = "type="+f.type.value+"&city="+f.city.value;
	xhr = createXHR();
	//xhr.setCharacterEncoding("UTF-8");
	//xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
	xhr.onreadystatechange=whereToFindSuccess;
	xhr.open("POST", "lib/ajax/getWhereTo.php?"+vars);
	xhr.send(null);
}

function whereToFindSuccess(res) {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var res = xhr.responseText;
			document.getElementById("loading").style.display = "none";
			document.getElementById("whereToResults").innerHTML = '<table width="100%" border="0" cellspacing="1" cellpadding="2">'+res+'</table>';
		} else {
			alert("Couldn't get results!");
		}
	}
}

/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}