/*----------------------------------------------
	
	global.js
	(c) 2008-2009 Stefan Heule

----------------------------------------------*/


/** this function gets called, when the user selects a date in the datepicker */
function dp_onclick_callback(year, month, day) {
	month = month+1; // javascript starts counting months with 0
	if (month < 10) month = '0'+month;
	if (day < 10) day = '0'+day;
	if (location.href.search('/menu/') != -1) {
		location.href = location.href.replace(/([0-9]{4}-[0-9]{2}-[0-9]{2}|aktuell)/, year+'-'+month+'-'+day);
	} else {
		location.href = '/menu/'+year+'-'+month+'-'+day+'/';
	}
}

/* show all hidden mensas */
function show_hidden_mensas() {
	// update 'more'-button
	hide_element_by_id('moremensas');
	set_hidden_count(0);
	
	// show all hidden mensas
	var oldpar = document.getElementById('hiddenmensas');
	var newpar = document.getElementById('nothiddenmensas');
	var els = new Array(oldpar.childNodes.length), el;
	for (var i = 0; i < oldpar.childNodes.length; i++) {
		els[i] = oldpar.childNodes[i];
	}
	for (var i = 0; i < els.length; i++) {
		el = els[i];
		if (el.nodeType == 1 && el.id != 'starthiddenmensas') {
			oldpar.removeChild(el);
			newpar.appendChild(el);
		}
		
	}
	
	// update mensa_string
	var tmp = '';
	if (get_mensa_string().indexOf(';') === 0) {
		tmp = get_mensa_string().substr(1)+';';
	} else {
		tmp = get_mensa_string().replace(';',',')+';';
	}
	set_mensa_string(tmp);
}

/* hide a specific mensa */
function hide_mensa(id) {
	// remove from shown mensas, and prepend to hidden mensas
	var el = document.getElementById('mensa_'+id);
	var oldpar = document.getElementById('nothiddenmensas');
	var newpar = document.getElementById('hiddenmensas');
	var start = document.getElementById('starthiddenmensas');
	var newstart = document.createElement('div');
	newstart.id = 'starthiddenmensas';
	oldpar.removeChild(el);
	newpar.insertBefore(el, start);
	newpar.removeChild(start);
	newpar.insertBefore(newstart, el);
	
	// show 'more'-button
	show_element_by_id('moremensas');
	increase_hidden_count();
	
	// update mensa_string
	var m = get_mensa_string().split(';');
	var m1 = m[0].split(',');
	var res = '';
	for (var i = 0, j = 0; i < m1.length; i++) {
		if (m1[i] != id) {
			if (j != 0) res += ',';
			res += m1[i];
			j++;
		}
	}
	res = res+';'+id;
	if (m[1].length != 0) res += ','+m[1];
	set_mensa_string(res);
}

/* show more menus */
function more_menus(id) {
	show_element_by_id('moremenus_'+id);
	hide_element_by_id('showmenus_'+id);
}

/* functions to update the 'more'-button */
function increase_hidden_count() {
	var el = document.getElementById('moremensascount');
	el.innerHTML = (el.innerHTML-1) +2;
}
function set_hidden_count(count) {
	var el = document.getElementById('moremensascount');
	el.innerHTML = count;
}

/* functions to manipulate the current mensa_string */
function get_mensa_string() {
	var el = document.getElementById('saveview');
	if (el != null) return el.href.substring(el.href.substring(0,el.href.length-3).lastIndexOf('/')+1, el.href.length-1);
	else return null;
}
function set_mensa_string(str) {
	var el = document.getElementById('saveview');
	if (el != null) el.href = '/user/mensa-ansicht-speichern/'+str+'/';
}













