isIE6 = false;

function initFilter(o) {
	o._up = false;
	o.len = o.selection.length;
	o.block = function() {
		return false;
	}
	o.clearAll = function() {
		this.selection = [];
		this.reset();
	}
	o.reset = function() {
		this._selected = {};
		this.count = 0;
		this.first = null;
		var ci = $(this.categoryItems,this.c);
		for(var i=0;i<this.selection.length;i++) {
			var t = "t_"+this.selection[i];
			this._selected[t] = true;
			this.count++;
		}
		var sel = this._selected;
		ci.find(".item").each(function() {
			var f = sel[this.name] || false;
			sel[this.name] = f;
		});
		this.updateSelection();
		this.first = $(this.categories,this.c).find(".item.has:first");
		if(this.first.length==0) this.first = $(this.categories,this.c).find(".item:first");
	}
	o.changeCategory = function(e) {
		$(o.categoryItems,o.c).find(".item").hide();
		for(var i=0;i<this.items.length;i++) {
			var t = "[name=t_"+this.items[i]+"]";
			$(o.categoryItems,o.c).find(t).css("display","block");
		}
		$(this).addClass("cur").siblings(".item").removeClass("cur");
		return false;
	}
	o.selectItem = function(e) {
		var f = o._selected[this.name] || false;
		f = !f;
		if(f && o.count==15) {
			$(".filterInfo",o.c).fadeIn();
			return false;
		} else if(o.count==15) {
			$(".filterInfo",o.c).hide();
		}
		o.count += (f ? 1 : -1);
		o._selected[this.name] = f;
		o.updateSelection();
		return false;
	}
	o.updateSelection = function() {
		var ca = $(this.categories,this.c);
		var si = $(this.selectedItems,this.c);
		var ci = $(this.categoryItems,this.c);
		ca.find(".has").removeClass("has");
		si.empty();
		for(var n in this._selected) {
			var el = ci.find("[name="+n+"]");
			var e = "[name=c_"+el.get(0).rel+"]";
			if(this._selected[n]) {
				si.append(el.clone(true).removeClass("cur").css("display","block"));
				el.addClass("cur");
				ca.find(e).addClass("has");
			} else {
				el.removeClass("cur");
			}
		}
	}
	o.submit = function() {
		var ids=[];
		$(o.selectedItems,o.c).find("[name]").each(function() {
			ids.push(parseInt(this.name.split("_")[1]));
		});
		writeCookie("hs_themenfilter",ids.join(","));
		var f = ids.length > 0 ? 1 : 0;
		writeCookie("hs_themen_on",1);
		$(o.bgnd).hide();
		$(o.sheet).hide();
		setTimeout(function() {	location.reload() },100);
	}
	o.close = function() {
		if(!o._up) return false;
		o._up = false;
		$(o.bgnd).fadeOut("fast");
		$(o.sheet).hide();
		return false;
	}

	var c = $(o.sheet).get(0);
	o.c = c;
	$(o.submitButton,c).click(o.submit);
	$(o.closeButton,c).click(o.close);
	$(o.resetButton,c).click(function() {o.clearAll(); o.first.click() } );
	$(o.bgnd).click(o.close);
	$(o.viewButton).click(function() {
		window.scrollTo(0,0);
		var h = Math.max($(document).height(),$(window).height());
		o.reset();
		if(o.first) o.first.click();
		o._up = true;
		$(o.sheet).show();
		$(o.bgnd).css({opacity:0.0,height:h,top:"0px",left:"0px"}).show().animate({opacity:0.6},500);
		return false;
	});
	$(o.switchButton).click(function() {
		var sw = o.switchStatus > 0 ? 0 : 1;
		o.switchStatus = sw;
		writeCookie("hs_themen_on",sw);
		if(o._up) {
			o.submit();
		} else {
			if(o.selection.length==0) {
				$(o.viewButton).click();
			} else {
				location.reload();
			}
		}
	}).toggleClass("active",(o.switchStatus > 0));
	$(o.categories,c).find(".item").each(function() { 
		this.items = this.rel.split(",");
	}).click(o.changeCategory);

	$(o.categoryItems,c).find(".item").click(o.selectItem);
	$(".item",c).bind("mousedown",o.block);
	if(isIE6) {
		$(".item",c).attr("href","#");
	}
}
function initMap(m) {
	var d = document.getElementById(m.mapContainer);
	if(!d) return;
	m.div = d;
	m.map = new GMap2(m.div);
	m.map.enableScrollWheelZoom();
	m.map.addControl(new GSmallMapControl());
	m.geo = new GClientGeocoder();
	m.focusLoc = null;
	m.focusZoom = 12;
	m.center = null;
	m.locked = false;
	m.cls = "hs";

	//Loc eintragen
	m.setMarker = function(pt,o) {
		var marker = new GMarker(pt);
		if(!marker) return;
		var mp = this.map;
		if(o.text && o.text != "") {
			GEvent.addListener(marker,"mouseover",function() {
				marker.openExtInfoWindow(mp,"googleInfo",o.text,{beakOffset:0});
			});
			GEvent.addListener(marker,"mouseout",function() {
				mp.closeExtInfoWindow();
			});
		}
		if(o.aid != "0" && o.aid != "") {
			GEvent.addListener(marker,"click",function() {
				window.location.href = "index.php?article_id="+o.aid;
			});
		}
		mp.addOverlay(marker);
	}
	//Loc anfordern
	m.setLocation = function(o) {
		var me = this;
		var lat = o.lat;
		var lng = o.lng;
		if(lat+lng > 0) {
			var pt = new GLatLng(lat,lng);
			this.setMarker(pt,o);
		} else {
			this.geo.getLatLng(o.adr,function(pt) {
				if(pt) me.setMarker(pt,o);
			});
		}
	}
	//Loc setzen (wg. Refs in eig. Methode)
	m.setLocations = function() {
		for(var i=0;i<this.locs.length;i++) {
			this.setLocation(this.locs[i]);
		}
	}
	m.setLock = function(f) {
		if(f) {
			$(this.lockSwitch).addClass("active");
		} else {
			$(this.lockSwitch).removeClass("active");
		}
		this.locked = f;
		writeCookie("geoLock",(f ? 1 : 0));
	}
	m.setFocus = function() {
		var z = this.map.getZoom();
		var c = this.map.getCenter();
		var lt = c.lat();
		var lg = c.lng();
		this.focusLoc = c;
		this.focusZoom = z;
		writeCookie("geo",lt+"|"+lg+"|"+z);
	}
	m.showFocus = function() {
		if(this.locked) {
			var pt = this.focusLoc;
			var z = this.focusZoom;
		} else {
			var pt = this.center;
			var z = this.zoom;
		}
		this.map.setZoom(z);
		this.map.panTo(pt);
	}
	m.check = function(oz) {
		if(this.locked) {
			this.setLock(false);
		}
	}
	//Events, dragging schaltet Fokus aus
	GEvent.addListener(m.map,"dragstart",function() {
		m.check();
	});
	//Buttons
	//Doppelklick-Auswahl unterbinden
	$(m.mapButtons).bind('mousedown',function() { return false});
	$(m.mapHeader).bind('mousedown',function() {return false});

	//Geofilter an/aus
	$(m.lockSwitch).click(function() {
		if(!m.focusLoc) {
			var ok = confirm("Noch kein Fokus gesetzt. Fokus hier setzen?");
			if(ok) m.setFocus();
			else return false;
		}
		m.setLock(!m.locked);
		m.showFocus();
		return false;
	});
	//Geofilter setzen
	$(m.lockButton).click(function() {
		m.setFocus();
		m.setLock(true);
		m.showFocus();
		return false;
	});
	//Zoom ein/aus
	$(m.zoomButton).toggle(
		function() {
			var c = m.map.getCenter();
			$(m.mapHolder).addClass("zoomed");
			m.map.checkResize();
			m.map.setCenter(c);
			$(this).html(m.zoomButtonCaption[1]);
		},
		function() {
			var c = m.map.getCenter();
			$(m.mapHolder).removeClass("zoomed");
			m.map.checkResize();
			m.map.setCenter(c);
			$(this).html(m.zoomButtonCaption[0]);
		}
	);
	//Geofilter oder Eintrag
	//Geofilter via cookies
	var g = readCookie("geo");
	var f = parseInt(readCookie("geoLock")) == 1;
	if(g && g.indexOf("|") > 0) {
		var a = g.split("|");
		var lt = parseFloat(a[0]);
		var lg = parseFloat(a[1]);
		var z = parseInt(a[2]);
		var pt = new GLatLng(lt,lg);
		m.focusLoc = pt;
		m.focusZoom = z;
		m.setLock(f);
	}
	//Oder evtl. Seite / Uni als Vorauswahl
	var pt = null;
	var fo = m.focus;
	//Vorauswahl naeher zoomen
	if(fo > -1) {
		m.zoom = Math.max(m.zoom,11);
	} else {
		fo = 0;
		var pt = new GLatLng(m.defaultLocation.lat,m.defaultLocation.lng);
		m.zoom = m.defaultLocation.zoom;
	}
	//Eintrag
	if(!pt) {
		var o = m.locs[fo];
		m.geo.getLatLng(o.adr,function(pt) {
			if(!pt) {
				//Punkt nicht gefunden
				var pt = new GLatLng(m.defaultLocation.lat,m.defaultLocation.lng);
				m.zoom = m.defaultLocation.zoom;
			}
			m.center = pt;
			//Fokus an/aus
			if(m.locked) {
				m.map.setCenter(m.focusLoc,m.focusZoom);
			} else {
				m.map.setCenter(pt,m.zoom);
			}
			//Alle Marker setzen
			m.setLocations();
		});
	} else {
		m.center = pt;
		if(m.locked) {
			m.map.setCenter(m.focusLoc,m.focusZoom);
		} else {
			m.map.setCenter(pt,m.zoom);
		}
		m.setLocations();
	}
	m.div.style.visibility = "visible";
}
function readCookie(n) {
	return $.cookie(n);
}
function writeCookie(n,v) {
	$.cookie(n,v,{path:"/"});
}
function deleteCookie(n) {
	$.cookie(n,null);
}

function trace(s) {
	if(window.console) console.log(s);
}
function value(v) {
	try {
		var r = v.toString();
	} catch(e) {
		var r = "n.a.";
	}
	return r;
}
//Cookie
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// NOTE Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};
function ieFix() {
	isIE6 = true;
	$("img[src$=.png]").each(function() {
		var w = this.width;
		var h = this.height;
		var s = this.src;
		this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+s+"',sizingMethod='crop'";
	});
	$(".mapSwitch,.filterSwitch").wrapInner("<div class='onoff'></div>");
}
function initBubbles() {
	hs_popups = {};
	var cnt = 1;
	if(jQuery.browser.msie) {
		var cso = {background:"none",filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/bubble_top.png',sizingMethod='scale')"};
		$(".popup_top").css(cso);
		cso.filter = cso.filter.replace(/_top/,'_middle');
		$(".popup_content").css(cso);
		cso.filter = cso.filter.replace(/_middle/,'_bottom');
		$(".popup_bottom").css(cso);
		$(".popup_inner").css("opacity",1);
	}
	$(".popup").each(function() {
		this.fx = 0;
		this.id = "p_"+cnt++;
		this.hide2=function() {
			if(this.fx==2) return;
			this.fx = 2;
			hs_popups[this.id] = false;
			$(this).animate({
				opacity:0,
				top:"+=20px"
			},300,"swing",function() {
				this.fx=0;
				this.style.display="none";
			});
		}
	});
	$(".popup_block").hover(function() {
		var p = $(this).find(".popup");
		var h = p.get(0);
		if(h.fx==1) return;
		if(h.fx==2) {
			p.stop(true,true);
		}
		h.fx = 1;
		for(var pp in hs_popups) {
			var op = hs_popups[pp];
			if(op) {
				$("#"+pp).get(0).hide2();
			}
		}
		hs_popups[h.id] = true;
		p.css({top:-30,opacity:0,display:'block'}).animate({
			top:-20,
			opacity:1
		},500,"swing",function() {
			h.fx=0;
		});
		
	},function() {
		var p = $(this).find(".popup");
		var h = p.get(0);
		if(h.fx==2) return;
		if(h.fx==1) {
			p.stop(true,true);
		}
		h.hide2();
	});
}
function initPanes(o) {
	var p = $(o.panes);
	var b = $(o.tabs);
	if(b.length==0) return;
	o.cur = {n:"",r:"",h:""};
	o.block = function() {return false};
	o.filter = function() {
		var n = this.name;
		$(this).siblings().removeClass("active");
		$(this).addClass("active");
		var off = this.rel=="0";
		o.cur[n] = off ? "" : this.rel;
		o.update();
	}
	o.update = function() {
		var cn = this.cur.n;
		var cr = this.cur.r;
		var ch = this.cur.h;
		//alle aus
		if(cn=="" && cr=="" && ch=="") {
			this.entries.show();
			return;
		}
		//str-suche (schneller als array)
		cr = ","+cr+",";
		
		this.entries.each(function() {
			var n = this.name;
			var r = this._r;
			var sh = r==",0," || cr==",," || cr.indexOf(r) > -1;
			if(sh) {
				if(cn != "") {
					sh = cn.indexOf(n) > -1;
				}
				if(sh && ch != "") {
					sh = ch == this._h;
				}
			}
			$(this).toggle(sh);
		});
	}
	//tabs
	if(isIE6) {
		//ie6 verkraftet max. 2 css-Klassen
		b.find(".tab").click(function() {
			$(this).siblings().css("background-color","#808080");
			this.style.backgroundColor = "#ee9601";
			var r = this.rel;
			if(r=="pane_2") {
				p.find(".filters").hide();
				b.css("background-color","#ee9601");
			} else {
				b.css("background-color","#808080");
				p.find(".filters").show();
			}
			p.find(".pane").removeClass("active");
			p.find("#"+r).addClass("active");
			return false;
		}).attr("className","tab");
		b.find(".tab:first").css("background-color","#ee9601");
		p.find(".entry").hover(
			function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
		});
	} else {
		b.find(".tab").click(function() {
			//tab
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			var r = this.rel;
			//klinke
			if(r=="pane_2") {
				b.addClass("t2");
				p.find(".filters").hide();
			} else {
				b.removeClass("t2");
				p.find(".filters").show();
			}
			//pane
			p.find(".pane").removeClass("active");
			p.find("#"+r).addClass("active");
			return false;
		}).bind("mousedown",o.block);
	}
	//filter buttons
	p.find(".filter").click(o.filter).bind("mousedown",o.block);
	//alle eintraege
	o.entries = p.find(".entry[rel]").each(function() {
		this.name = $(this).attr("name");
		var r = $(this).attr("rel");
		var rr = r.split("|");
		this._n = $(this).attr("name");
		this._r = ","+rr[0]+",";
		this._h = rr[1];
		this.rel = rr[0];
	});
	b.find(".filter.active").click();
}
