// *****************************************************************************************************
/** 
* Get the ISO week date week number 
*/  
Date.prototype.getWeek = function () {  
	var target = new Date(this.valueOf());  
	var dayNr = (this.getDay() + 6) % 7;  
	target.setDate(target.getDate() - dayNr + 3);  
	var jan4 = new Date(target.getFullYear(), 0, 4);  
	var dayDiff = (target - jan4) / 86400000;   
	var weekNr = 1 + Math.ceil(dayDiff / 7);   
	return weekNr;   
}  


function GoTo(url) {
	if (url != '')
		window.location.href = url;
}

function getObject(obj) {
	if(document.all) {
		if(typeof obj=="string") {
			return document.all(obj);
		} else {
			return obj.style;
		}
	} 
	if(document.getElementById) {
		if(typeof obj=="string") {
			return document.getElementById(obj);
		} else {
			return obj.style;
		}
	}
	return null;
}

function count(obj, maxlength, counterobj)
{
	var a=getObject(obj).value.length;
	var b=maxlength-a;
	if (b<0)
	{
		b=0;
		getObject(obj).value=getObject(obj).value.substr(0, maxlength);
	}
	getObject(counterobj).innerHTML=b + "/" + maxlength + " Zeichen";
}
function count_tags(obj, maxtags, counterobj, length, name)
{
	var data = getObject(obj).value;
	
	var splitdata = data.split(",");
	var tagcounter = 0;
	var tooshort = false;
	var doubleentry = false;
	var info_txt = "";

	if (length == null){
		length = 3;
	}	
	if (name == null){
		name = 'Themen';
	}	
	
	for (var i = 0; i < splitdata.length; i++) 
	{
		dud = splitdata[i].replace(/\s/g, "");
		if(dud != "")
			tagcounter = tagcounter + 1;
		if((dud.length < length) && (i < (splitdata.length-1)))
		{
			tooshort = true;
		}
		for (var j = 0; j < i; j++) 
		{
			if((dud === splitdata[j].replace(/\s/g, "")) && (i < (splitdata.length-1)))
			{
				doubleentry = true;
			}
		}
	}
	if((tagcounter>0) && (tagcounter<=maxtags))
		 info_txt = tagcounter + ' von ' + maxtags + ' ' + name;
	else
	{
		if(tagcounter>maxtags)
			info_txt = '<span style="color: #FF0000;">Bitte gib maximal ' + maxtags + ' ' + name + ' ein. Es werden nur die ersten ' + maxtags + ' ' + name + ' verwendet.</span>';
	}
	if(doubleentry)
		info_txt = info_txt + '<br />' + "Du hast " + name + " doppelt oder mehrfach eingegeben. Doppelte " + name + " werden nur einmal gespeichert.";
	if(tooshort)
		info_txt = info_txt + '<br />' +  name + " m&uuml;ssen aus "+ length + " oder mehr Zeichen bestehen. K&uuml;rzere " + name + " werden nicht ber&uuml;cksichtigt";
	if(info_txt != "")
		getObject(counterobj).innerHTML = info_txt;

}
function addtag(obj, tag, maxtags, counterobj, length, name)
{
	var data = getObject(obj).value;
	var splitdata = data.split(",");
	var add = true;
	var j = 0;
	if (length == null){
		length = 3;
	}	
	if (name == null){
		name = 'Themen';
	}	
	
	for (var i = 0; i < splitdata.length; i++) 
	{
		if(splitdata[i].replace(/\s/g, "") != "")
		{
			if(tag == splitdata[i].replace(/\s/g, ""))
				add = false;
			else
			{
				if(j > 0)
					data_new = data_new + ', ' + splitdata[i].replace(/\s/g, "");
				else
					data_new = splitdata[i].replace(/\s/g, "");
				j++;
			}
		}
	}

	if(add)
	{	
		if(data != "")
			data_new = data_new + ', ' + tag;
		else
			data_new = tag;
	}

	getObject(obj).value = data_new;
	count_tags(obj, maxtags, counterobj, length, name);
	if(name == "Regionen") {
		change_nachbarn(obj, maxtags, counterobj, length, name);
	}
}

function toggleView(open, button, picset)
{
  var op=document.getElementById(""+open);
  var bu = document.getElementById(""+button);
  
  
  if(op.style.display=="none")
  {
    op.style.display="block";
    bu.src='http://bilder.webpool.de/_bilder/silkicons/'+picset+'_delete.png';
  }
  else
  {
    op.style.display="none";
    bu.src='http://bilder.webpool.de/_bilder/silkicons/'+picset+'_add.png';
  }
  return true;
}

/* *********************** Browsergröße etc. ******************************************** */

function posLeft() 
{
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 

function posTop() 
{
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageWidth();
} 

function pageWidth() 
{
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function pageHeight()
{
	return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?       document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
} 

/* ********************** Heartbeat / Benachrichtigungen ******************************************** */

timerStartVal=20;
var timer;
var url1 = "/benachrichtigungen.html";
var heartbeat;

if (userIssOnline) // START DES HEARTBEAT
{
	$(document).ready(function(){
			window.setTimeout("refresh(timerStartVal)", timerStartVal*1000);
			
			$.ui.dialog.defaults.bgiframe = true;
			
			$("[name=counter]").css("display","inline");
			
			$(".ticket_div_class").dialog({autoOpen:false, title:"Melden", height:410, width: 600});
			$(".ticket_div_class_msg").dialog({autoOpen:true, title:"Melden", height:410, width: 600});
			
			$("[name=ticket_melden_img]").click(function(){
				var ticket_id="#"+ $(this).attr("t_melden");
				$(ticket_id).dialog("open");
			});
			
			$("[name=info_box_img]").click(function(){
				if($(this).parent().next("[name=info_box]").length > 0)
				{
					$(this).parent().next("[name=info_box]").dialog({height:400, width:400, title:"Hilfe"}); 
				}
				else
				{
					var o = "#"+$(this).attr("id").substring(4);
					$(o).dialog("open");
				}
			});
			$("[name=logfile_img]").click(function(){
				if($(this).parent().next("[name=logfile]").length > 0)
				{
					$(this).parent().next("[name=logfile]").dialog({height:400, width:600, title:"Logfile"}); 
				}
				else
				{
					var o = "#"+$(this).attr("id").substring(4);
					$(o).dialog("open");
				}
			});
			$("[name=plz_karte_img]").click(function(){
				if($(this).parent().next("[name=plz_karte]").length > 0)
				{
					$(this).parent().next("[name=plz_karte]").dialog({height: 700, width: 470, title:"Plz-Karte"}); 
				}
				else
				{
					var o = "#plz_karte";
					$(o).dialog("open");
				}
			});
			$("[name=plz_karte_img]").click(function(){
				if($(this).parent().next("[name=plz_karte]").length > 0)
				{
					$(this).parent().next("[name=plz_karte]").dialog({height: 700, width: 470, title:"Plz-Karte"}); 
				}
				else
				{
					var o = "#plz_karte";
					$(o).dialog("open");
				}
			});
			
 	});
}
$(document).ready(function(){
	$("ul.sf-menu").superfish().find('ul').bgIframe({opacity:false});
});

function refresh(timer) 
{
	$.get(url1,
		{b_cmd: "heartbeat", zz:Math.random()},
	  	function(response)
	  	{
	  		if (response=="true")
			{
				msg_refresh();
				timer = timerStartVal;
			}
			else
			{
				if (timer<300)
				{ timer = timer + 20; }
			}
			window.setTimeout("refresh("+timer+")", timer*1000);
		}
	);
}

function msg_refresh()
{
	$.getJSON(url1, { b_cmd: "nachrichten_refresh", zz: Math.random() }, function(jsonObj){
 		if (document.getElementById("g_id"))
		{
			for (var i = 0; i < jsonObj.absender.length; ++i)
  			{
  				if (($("#g_id") == jsonObj.absender[i]) && document.getElementById("log_res"))
  				{
  					setPostEvent();
					refresh2(0);
  				}
  			}
  		}
		$("#newsListe").html(jsonObj.html);
	});
}

// ############# Suckerfish, Superfish und noch so'n paar Fische #################

(function($){
	/* hoverIntent by Brian Cherne */
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
	
})(jQuery);

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-06-19 20:25:28 -0500 (Tue, 19 Jun 2007) $
 * $Rev: 2111 $
 *
 * Version 2.1
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&parseInt($.browser.version)<=6){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};if(!$.browser.version)$.browser.version=navigator.userAgent.toLowerCase().match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)[1];})(jQuery);


/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

;(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'current',
		pathLevels	: 1,
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 'normal',
		autoArrows	: false,
		dropShadows : true,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op,
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});
})(jQuery);


