var http_root = '/archiv/lndw_2009';
var img_path_href = '/archiv/lndw_2009/images';

function page_offset() {
	var a = page_offset_array();
  return a[0]+','+a[1];
}

function page_offset_array() {
  var X = 0; var Y = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    Y = window.pageYOffset;
    X = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    Y = document.body.scrollTop;
    X = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    Y = document.documentElement.scrollTop;
    X = document.documentElement.scrollLeft;
  }
  return [0+X,0+Y];
}

function inner_height() {
	if (window && (typeof(window.innerWidth)=='number') && window.innerHeight) {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return document.getElementById('body').offsetHeight;
}

function inner_width() {
	if (window && (typeof(window.innerWidth)=='number') && window.innerWidth) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return document.getElementById('body').offsetWidth;
}

function GeheZu(ref) {
	var f = document.cg_form;
	var oldOffsetValues = '';
	if (f.PageOffsets)
		oldOffsetValues = f.PageOffsets.value;
	var ref_ext = 'PageOffsets='+oldOffsetValues+encodeURIComponent(';'+location.pathname+':'+page_offset());
	ref=ref+((ref.match(/\?/)) ? '&':'?')+ref_ext+((arguments.length>1) ? arguments[1]:'');
	location.href = ref;
}

function ReturnableCall(ref) {
	var options = arguments[1];
	if (options == undefined) options = {};
	var f = document.cg_form;
	var oldOffsetValues = '';
	if (f.PageOffsets) oldOffsetValues = f.PageOffsets.value;
	var stack = '';
	var newURL = document.URL;
	if (!newURL.match(/\?/)) if (f.direct_called && f.direct_called.value && f.direct_called.value.length) newURL = f.direct_called.value;
	newURL = reject_param(newURL,'forced_offset');
	if (f.direct_caller && f.direct_caller.value) {
		if (options['caller_wait_for']) newURL = reject_param(newURL,options['caller_wait_for']);
		stack = '&direct_called=1&direct_caller='+encodeURIComponent(newURL)+ '&prev_call=' + encodeURIComponent(f.direct_caller.value) + '&prev_offset=' + encodeURIComponent(f.caller_offset.value);
	} else 
		stack = '&direct_called=1&direct_caller='+encodeURIComponent(newURL);
	var ref_ext = 'caller_offset='+page_offset()+'&caller_wait_for='+((options['caller_wait_for']) ? options['caller_wait_for']:'')+stack+'&PageOffsets='+oldOffsetValues+encodeURIComponent(';'+location.pathname+':'+page_offset());
	ref = ref+((ref.match(/\?/)) ? '&':'?')+ref_ext+((options['add_string']) ? options['add_string']:'');
	if (options['do_command']) {
		do_command(options['do_command'],ref);
	} else location.href=ref;
}


function add_page_offset() {
	var f = document.cg_form;
	var oldOffsetValues = '';
	if (f.PageOffsets) {
		f.PageOffsets.value = f.PageOffsets.value + ';'+location.pathname+':'+page_offset();
	}
}

function reject_param(URL,name) {
	var intro = '&'+name+'=';
	var start = URL.indexOf(intro);
	if (start>-1) {
		var subURL = URL.substr(start+intro.length);
		subURL = subURL.replace(/[^\?\&]*/,'');
		return URL.substr(0,start)+subURL;
	} else return URL;
}


function do_command(com) {
	var f = document.cg_form; f.com.value=com; add_page_offset();
	if (arguments.length>1) f.com_arg.value=arguments[1];
	var d = new Date();
	f.com_time.value = d.getYear()+'-'+d.getMonth()+'-'+d.getDay()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
	f.submit();
}

function abs_coord(e) {
	var l=0; var t=0;
	do {l+=e.offsetLeft || 0;	t+=e.offsetTop || 0; e=e.offsetParent;} while (e);
	return [l,t];
}

function toggle_div(id) {
	var d = document.getElementById(id);
	if (d && d.style)
		if (d.style.display=='none') d.style.display = '';
		else d.style.display = 'none';
}

function routen_gross_img_view() {
	MouseOverText.env='';
	var d = document.getElementById('routen_gross_img_div');
	if (d)
		if (d.style.display=='none') {
			var c = abs_coord(document.getElementById('SMALL_MAP_ALL_IMG'));
			d.style.top = (c[1])+'px';
			d.style.left = (c[0]-8)+'px';
			d.style.display = '';
			document.getElementById('MAP_ALL_IMG').src=img_path_href+'/routen.gross.gif';
		}
}
function routen_gross_img_hide() {
	var d = document.getElementById('routen_gross_img_div');
	if (d)
		d.style.display='none';
}

// MouseOverText - START //

var MouseOverText = {
	is_closing:{},
	to_do:{},
	env: '',
	pre: '',
	parent:{},
	intervals:{},
	absolutize_at_once: function() {
		var divs = document.getElementsByTagName('DIV');
		for(var i = 0; i < divs.length; i++) {
			var div = divs[i];
			if (div.id.substr(0,7)=='Div4MOT') {
				div.style.display='none'; div.style.position = 'absolute';
				var parent = div.getAttribute('parent');
				if (parent && parent.length>0) MouseOverText.parent[div.id.substr(7)]=parent;
			}
		}
	},
	show: function(id) {
		var org_id = id; if (id.substr(0,6)=='SMALL_') id = id.substr(6);
		MouseOverText.is_closing[id] = false;
		var div = document.getElementById("Div4"+id);
		if (MouseOverText.to_do[id]) {
			eval(MouseOverText.to_do[id]);
			MouseOverText.to_do[id]=false;
		}
		if (div.style.display == "") return;
		var el = document.getElementById(org_id);
		var l=0; var t=0; var h=el.offsetHeight;
		if (el.tagName=='AREA') {
			var cs = el.getAttribute('coords').split(/,/);
			if (cs.length>=4) {
				l=l+parseInt(cs[0]);
				t=t+parseInt(cs[3]);
			}
				cs = abs_coord(document.getElementById(MouseOverText.env+el.parentNode.id+"_IMG"));
				l = l + cs[0];
				t = t + cs[1];
		} else {
				do { l+=el.offsetLeft || 0; t+=el.offsetTop || 0; el=el.offsetParent;} while (el);
				t = t + h;
		}
		div.style.left = l + "px";
		div.style.top = t + "px";
		div.style.display = "";	
  	if (MouseOverText.parent[id]) MouseOverText.is_closing[MouseOverText.parent[id]] = false;
	},
	hold: function(id) {
		var org_id = id; if (id.substr(0,6)=='SMALL_')	id = id.substr(6);
  	MouseOverText.is_closing[id] = false;
  	if (MouseOverText.parent[id]) MouseOverText.is_closing[MouseOverText.parent[id]] = false;
	},
	hide: function(id) {
		var org_id = id; if (id.substr(0,6)=='SMALL_')	id = id.substr(6);
	  MouseOverText.is_closing[id] = false;
		document.getElementById("Div4"+id).style.display = "none";
	},
	close: function(id) {
		var org_id = id; if (id.substr(0,6)=='SMALL_')	id = id.substr(6);
		MouseOverText.is_closing[id] = true;
		if (! MouseOverText.intervals[id]) MouseOverText.intervals[id]= new Array();
		MouseOverText.intervals[id].push(window.setInterval("MouseOverText.close_('"+id+"')",50));
	},
	close_: function(id) {
		var org_id = id; if (id.substr(0,6)=='SMALL_')	id = id.substr(6);
		window.clearInterval(MouseOverText.intervals[id].shift());
		if (MouseOverText.is_closing[id]) MouseOverText.hide(id);
	}
}

// MouseOverText - END //

// handle default text for search input

function clean_search_default_content() {
	var el = document.getElementById("meine_Suche");
	if (el)
		if (el.value=='Suchbegriff...')
			el.value='';
}
function set_search_default_content() {
	var el = document.getElementById("meine_Suche");
	if (el)
		if (el.value=='')
			el.value='Suchbegriff...';
}

function print_view() {
	var f = document.cg_form;
	if (location.href.match(/\.html$/)) {
		window.open(location.href.replace(/\.html$/,'_printable.html'));
	} else {
		if (f.printable) {
			f.printable.value=1;
			f.target="_blank";
			f.submit();
		}
	}
}

function prgpkt_ecard() {
	var d = document.getElementById("prgpkt_sel");
	var prg_id=d.getAttribute("prg_id");
	var h_id=d.getAttribute("h_id");
	ReturnableCall(http_root+"/meine/ecards.php?referer=Programmpunkt&programmpunkt_id="+prg_id+"&haltestelle_id="+h_id);
}

var show_prgpkt_sel_is_closing=false;
var show_prgpkt_sel_interval=null;

function show_prgpkt_sel(prg_id,h_id) {
	show_prgpkt_sel_is_closing=false;
	var d = document.getElementById("prgpkt_sel");
	if (d.getAttribute("prg_id")==''+prg_id && d.getAttribute("h_id")==''+h_id) {
		d.style.display="";	return;
	}
	var i = document.getElementById("PID_"+prg_id+"_"+h_id);
	var c = abs_coord(i);
	d.setAttribute("prg_id",prg_id);
	d.setAttribute("h_id",h_id);
	d.style.left = (460+c[0])+"px";
	d.style.top = (c[1]-4)+"px";
	d.style.display = "";
	var j = document.getElementById("A_"+prg_id+"_"+h_id);
	var s = j.className.substr(5,1);
	var ud = document.getElementById("U_"+prg_id+"_"+h_id);
	var cd = document.getElementById("C_"+prg_id+"_"+h_id);
	document.getElementById("prgpkt_sel_0").checked = (s=='0');
	document.getElementById("prgpkt_sel_1").checked = (s=='1');
	document.getElementById("prgpkt_sel_2").checked = (s=='2');
	document.getElementById("prgpkt_sel_3").checked = (s=='3');
	if (ud) {
		var u = document.getElementById("U_"+prg_id+"_"+h_id).innerHTML;
		if (u=="!") u="";
		document.getElementById("prgpkt_uhrzeit").value = u;
	}
	var notiz = document.getElementById("prgpkt_notiz");
	if (cd) {
		var c = document.getElementById("C_"+prg_id+"_"+h_id).innerHTML;
		notiz.value = c;
	} else if (notiz) notiz.value = '';
}

function hold_prgpkt_sel() {
	show_prgpkt_sel_is_closing=false;
}

function leave_prgpkt_sel() {
	show_prgpkt_sel_is_closing=true;
	show_prgpkt_sel_interval = window.setInterval("leave_prgpkt_sel_now()",50);
}

function leave_prgpkt_sel_now() {
	if (show_prgpkt_sel_is_closing)	document.getElementById("prgpkt_sel").style.display='none';
	show_prgpkt_sel_is_closing=false;
	if (show_prgpkt_sel_interval) window.clearInterval(show_prgpkt_sel_interval);
}

function sel_prgpkt(stat) {
	var d = document.getElementById("prgpkt_sel");
	var prg_id=d.getAttribute("prg_id");
	var h_id=d.getAttribute("h_id");
	var i = document.getElementById("A_"+prg_id+"_"+h_id);
	i.className='rank_'+stat;
	var u = document.getElementById("U_"+prg_id+"_"+h_id);
	if (u.innerHTML=='') u.innerHTML='!';
	if (stat==0 && document.getElementById("meine_nacht_auswahl")) {do_command('del',prg_id+"_"+h_id);}	else
	document.getElementById("rq").src = http_root+"/meine/select_prg.php?prg_id="+prg_id+"&h_id="+h_id+"&s="+stat;
	document.getElementById("prgpkt_sel_"+stat).checked=true;
}

function change_prgpkt() {
	var d = document.getElementById("prgpkt_sel");
	var prg_id=d.getAttribute("prg_id");
	var h_id=d.getAttribute("h_id");
	var dz = document.getElementById("prgpkt_uhrzeit");
	var dn = document.getElementById("prgpkt_notiz");
	if (dz && dn) {
		var zeit = dz.value;
		var notiz = dn.value;
		var u = document.getElementById("U_"+prg_id+"_"+h_id);
		if (u) {
			u.innerHTML=zeit;
			if (zeit=='') u.innerHTML='!';
		}
		var n = document.getElementById("C_"+prg_id+"_"+h_id);
		if (n) {
			n.innerHTML = notiz;
		}
		document.getElementById("rq").src = http_root+"/meine/update_prg.php?prg_id="+prg_id+"&h_id="+h_id+"&u="+encodeURIComponent(zeit)+"&n="+encodeURIComponent(notiz);
	}
}

function meine_nacht_prgpkt(stat) {
	var d = document.getElementById("prgpkt_sel");
	var prg_id=d.getAttribute("prg_id");
	var h_id=d.getAttribute("h_id");
	location.href=http_root+"/meine/veranstaltungen.php#PID_"+prg_id+"_"+h_id;
}

var ThumbLoader = {
	thumb_dir: '/download/docs/resized/192x64/',
	gal: null,
	ids: [],
	curr: -1,
	min_top: 0,
	left: 0,
	start: function(gal) {
		ThumbLoader.gal = gal;
		var off = abs_coord(document.getElementById('erweiterte_Suche'));
		ThumbLoader.min_top = off[1];
		off = abs_coord(document.getElementById('main_col'));
		ThumbLoader.left = off[0];
		ThumbLoader.intervall = window.setInterval('ThumbLoader.get_ids()',200);
		ThumbLoader.append_to = document.getElementById('body');
	},
	get_ids: function() {
		window.clearInterval(ThumbLoader.intervall);
		var i = 0;
		for(i in ThumbLoader.gal.ids_records) ThumbLoader.ids.push(i);
		ThumbLoader.intervall = window.setInterval('ThumbLoader.step()',100);
	},
	step: function() {
		if (ThumbLoader.ids.length) {
			var id = ThumbLoader.ids.shift();
			if (id && id!="") {
				var rec = ThumbLoader.gal.ids_records[id][0];
				var prg = document.getElementById('PID_'+rec[3]);
				var off = abs_coord(prg);
				if (ThumbLoader.min_top < off[1]) {
					var img_div = document.createElement('DIV');
					img_div.className = 'thumb';
					var img_a = document.createElement('A');
					img_a.href = 'javascript:ThumbLoader.gal.start_gallery_for_id('+id+')';
					var img = document.createElement('IMG');
					img.src = http_root+ThumbLoader.thumb_dir+rec[1];
					img_a.appendChild(img);
					img_div.appendChild(img_a);
					ThumbLoader.append_to.appendChild(img_div);
					img_div.style.top = (off[1]+4)+'px';
					img_div.style.left = ThumbLoader.left+'px';
				}
			}
		} else {
			window.clearInterval(ThumbLoader.intervall);
		}
	}
}
