(function($, undefined) {
	$.fn.extend({
		cssRemove: function( key ) {
			this.each(function() {
				if (this.style.removeAttribute) { 
					this.style.removeAttribute("table-layout", true);
				}
				else if (this.style.removeProperty) { 
					this.style.removeProperty("table-layout");
				}
				else {
					$(this).css(key, null); // das könnte auch schief gehen!
				}
			});
		}
	});
	function verschiebeLogo() {
		var lo = $("#lo");
		//var lopos = lo.offset().left;
		$(".randaussen").width(1000);
		var x = $("#oi").offset().left;
		var left = Math.max(-parseInt((x + 88) / 2), -199);
		lo.css("left", left);
		var w = 604 - 4 * left;
		$(".randaussen").width(w);
		$("#aussen").width(w);
	}

	$(document).ready(function()	{
		$.retZuord.suchen = '#searchSubmit';
		$.retZuord.pass = '[name=submit]';
		$(window).resize(verschiebeLogo);
	   $('.autoSubmit').hide();
		verschiebeLogo();
		var loc = location.href;
		var actParts = loc.split('?', 2);
		var actArgs = actParts[1].split('&');
		var actMap = {};
		$.each(actArgs, function() {
			var a = this.split('=', 2);
			if (a[0] != 'err') {
				actMap[a[0]] = a[1];
			}
		});
		$('.jutyp-news').each(function() {
				var $strip = $(this);
				var stripWidth = 0;
				var containerWidth = $strip.addClass("newsticker").wrap("<div class='mask'></div>").parent().wrap("<div class='tickercontainer'></div>")
					.parent().css('background-color', $strip.css('background-color')).width();
				$strip.children("span.nt").each(function(i) {
					$(this).css('white-space', 'nowrap');
					stripWidth += $(this, i).outerWidth(true);
				});
				$strip.width(stripWidth);
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel / 0.07;
				function scrollnews(spazio, tempo){
				   $strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
						$(this).stop();
					},
					function(){
						var offset = $(this).offset();
						var residualSpace = offset.left + stripWidth;
						var residualTime = residualSpace / 0.07;
						scrollnews(residualSpace, residualTime);
					}
				);
			});
		if (actMap['edit'] == 1) {
			var setAction = function() {
				actArgs = [];
				var i = 0;
				$.each(actMap, function(key, val) {
					if (val !== undefined) {
						actArgs[i++] = key + '=' + val;
					}
				});
				actParts[1] = actArgs.join('&');
				loc = actParts.join('?');
				$('#globalForm').attr('action', loc);
				//alert($('#globalForm').attr('action'));
			}
			setAction();
			window.changeActArr = function(n, uid, art) {
				var i = 0;
				while (uid) {
					var key = n + '[' + i++ + ']';
					var val = actMap[key];
					if (art < 0 && val == uid) {
						do {
							var next = n + '[' + i++ + ']';
							val = actMap[next];
							actMap[key] = val;
							key = next;
						} while (val);
						break;
					}
					else if (art > 0) {
						actMap[key] = uid;
						uid = val;
					}
					else if (!art && !val) {
						actMap[key] = uid;
						break;
					}
				}
				setAction();
			}
			window.setAct = function(key, val) {
				actMap[key] = val;
				setAction();
			}
			window.toggleEl = function(name, rev, zus) {
				if (actMap[name] == 1) {
					if (rev) {
						$('.' + name).show();
						if (zus) {
							zus();
						}
					}
					else {
						$('.' + name).hide();
					}
					$('#' + name + 'Input').attr('src', 'fileadmin/icons/notchecked.gif');
					setAct(name, undefined);
				}
				else {
					if (rev) {
						$('.' + name).hide();
					}
					else {
						$('.' + name).show();
					}
					$('#' + name + 'Input').attr('src', 'fileadmin/icons/checked.gif');
					setAct(name, 1);
				}
			}
			var oldPlCat;
			window.recalcZusTermin = function() {
				$('.zustermin').hide();
				$('.zustermin' + oldPlCat).show();
			}
			window.initPlCat = function(pluid) {
				oldPlCat = pluid;
				$('.cbcat').removeAttr('disabled').removeAttr('checked');
				$('.zustermin').hide();
				$('.zustermin' + pluid).show();
				$.each(arguments, function(k, id) {
					$('#cbcat' + id).attr('checked', 'checked');
				});
				$('#cbcat' + pluid).attr('disabled', 'disabled');
			}
			window.changePlCat = function(pluid) {
				if (oldPlCat) {
					$('#plcat' + oldPlCat).attr('class', 'def');
					$('#cbcat' + oldPlCat).removeAttr('disabled');
					$('#cbcat' + oldPlCat).removeAttr('checked');
					$('.zustermin' + oldPlCat).hide();
					$('span.prodcat' + oldPlCat).attr('class', 'prodcat' + oldPlCat);
				}
				if (actMap['cat' + pluid]) {
					actMap['cat' + pluid] = undefined;
				}
				oldPlCat = pluid;
				actMap['plcat'] = pluid;
				$('.zustermin' + pluid).show();
				if (actMap['ab16'] == 1) {
					$('.ab16').hide();
				}
				$('#plcat' + pluid).attr('class', 'aktR');
				$('#cbcat' + pluid).attr('disabled', 'disabled');
				$('#cbcat' + pluid).attr('checked', 'checked');
				$('span.prodcat' + pluid).attr('class', 'aktPlanCat prodcat' + pluid);
				setAction();
			}
			window.setDat = function(produid) {
				var old = actMap['prod'];
				if (old) {
				}
				actMap['prod'] = produid;
				setAction();
			}
			var $ta = $('#txtakt');
			window.useText = function(s) {
				var akt = s.options[s.selectedIndex];
				var title = akt.text;
				var uid = akt.value;
				if (akt.className == 'optfirstzustxt') {
					changeActArr('txts', uid, 1);
					$('#txtakt' + uid).prependTo($ta).show();
					$('.optfirstzustxt').hide();
				}
				else {
					changeActArr('txts', uid, 0);
					$('#txtakt' + uid).appendTo($ta).show();
				}
				$('#txtopt' + uid).hide();
				s.selectedIndex = 0;
				//alert(uid+' - '+title);
			}
			window.delText = function(uid) {
				changeActArr('txts', uid, -1);
				$('#txtakt' + uid).hide();
				if ($('#txtopt' + uid).show().filter('.optfirstzustxt').size()) {
					$('.optfirstzustxt').show();
				}
			}
		}
	});

	window.closeFG = function() {
		$('#galerieCont').remove();
		$(document).unbind();
	};
	window.showFGi = function(fgdef, iakt) {
		var maxb = $[fgdef].length;
		var ivor = iakt < 2 ? maxb : iakt - 1;
		var inach = iakt >= maxb ? 1 : iakt + 1;
		var dat = $[fgdef][iakt - 1];
		var gl = $('#galerieLoading');
		$('.fgRotLinks,.fgRotRechts').removeAttr('onclick').unbind('click');
		$(document).unbind();
		if (!gl.length) {
			$('.galerieInnen').attr('class', 'galerieDyn');
			gl = $('<div id="galerieLoading"><img src="img/fg-loading.gif"></div>').appendTo('.galerieDyn');
			$('.galerieGross').click(function() { closeFG() });
			$('.ui-widget-overlay,.galerieGross').width($(document).width()).height($('body').height());
		}
		var hd = $('.galerieDyn').height();
		$('.galerieDyn').height(hd);
		$('.galerieGross').css('top', $(window).scrollTop() + 'px');
		var w = $(window).width() - 42, h = $(window).height() - 60, hi = dat.h500, wi = 500, left = $(window).scrollLeft();
		if (w >= 200 && w < wi) {
			hi = Math.round(hi * w / wi);
			wi = w;
		}
		if (wi < w) {
			left += Math.round((w - wi) / 2);
		}
		$('.galerieGross').css('left', left + 'px');
		if (h >= 200 && h < hi) {
			wi = Math.round(wi * h / hi);
			hi = h;
		}
		$('#fgBild,#fgText').hide();
		$('#fgText').html(dat.txt ? dat.txt : '&nbsp');
		$('#fgNr').html(iakt);
		gl.show();
		var iml = new Image();
		iml.onload = function() {
			iml.onload = function() {};
			$('#fgBild').attr('src', iml.src);
			$('.fgRotLinks').click(function() { showFGi(fgdef, ivor); return false });
			$('.fgRotRechts').click(function() { showFGi(fgdef, inach); return false });
			$(document).keydown(function(ev) {
				ev.stopPropagation();
				switch (ev.which) {
				case 27 : closeFG(); break;
				case 37 : showFGi(fgdef, ivor); break;
				case 39 : showFGi(fgdef, inach); break;
				}
			});
			$('#fgBild').width(wi).height(hi);
			gl.hide();
			$('#fgBild').show(600, function() { $('#fgText').show(); $('.galerieDyn').width(wi).height('auto') });
		};
		iml.src = 'uploads/tx_usergpic/img/' + dat.src500;
	};
	window.showFG = function(fgdef, nr) {
		$('body').append('<div id="galerieCont"><div class="ui-widget-overlay"></div><div class="galerieGross"><div class="galerieDyn"><a class='
			+ '"fgRotLinks" href="#" title="vorheriges Bild"><img class="middle" alt="" src="clear.gif" width="18px" height="18px"/></a>&nbsp; '
			+ '<span id="fgNr"></span> von ' + $[fgdef].length + ' &nbsp;<a class="fgRotRechts" href="#" title="nächstes Bild"><img class="middle"'
			+ ' alt="" src="clear.gif" width="18px" height="18px"/></a> &nbsp; <a class="fgClose" href="#" onclick="closeFG();return false"'
			+ ' title="schließen"><img class="middle" alt="" src="clear.gif" width="18px" height="18px"/></a><br/><img id="fgBild" alt="" title="" />'
			+ '<br/><span id="fgText"></span><div id="galerieLoading"><img src="img/fg-loading.gif"></div></div></div></div>');
		//$('.fgClose').click(function() { closeFG(); return false });
		$('.ui-widget-overlay,.galerieGross').click(function() { closeFG() });
		$('.ui-widget-overlay,.galerieGross').width($(document).width()).height($('body').height());
		showFGi(fgdef, nr);
	};
	window.changeFG = function(fgdef, wog, woo1, woo2, hv) {
		var tdg = $('#'+fgdef+wog), tdl2 = $('#'+fgdef+woo1).add('#'+fgdef+woo2);
		tdg.width(hv?120:200).find('.fgSmallPic'+hv).attr('class', 'fgMainPic'+hv);
		tdg.find('.fgtxt').show();
		tdl2.width(hv?100:120).find('.fgMainPic'+hv).attr('class', 'fgSmallPic'+hv);
		tdl2.find('.fgtxt').hide();
	};
	window.gotoFG = function(fgdef, wohin) {
		var nrg = 1 * $('#'+fgdef+wohin).find('a').attr('href').replace(/.*=-(\d+)$/, '$1');
		var maxb = $[fgdef].length;
		var set = function(nr, wo, pf) {
			var fgdat = $[fgdef][nr - 1];
			var td = $('#'+fgdef+wo);
			td.find('img').filter(':first').attr('src', 'uploads/tx_usergpic/img/'+fgdat['src200']);
			td.find('.fgtxt').html(fgdat['txt']);
			var a = td.find('a');
			a.attr('href', a.attr('href').replace(/=-\d+$/, '=-'+nr));
			if (fgdat.flv) {
				a.attr('href', a.attr('href').replace(/flvdok=\d+$/, 'flvdok='+fgdat.flv));
				a.removeAttr('onclick').unbind('click').click(function() { showFlv(fgdat.flv); return false });
			}
			else {
				a.removeAttr('onclick').unbind('click').click(function() { showFG(fgdef, nr); return false });
			}
			if (pf) {
				a = $('#'+fgdef+pf);
				a.attr('href', a.attr('href').replace(/=\d+$/, '='+nr));
			}
		}
		set(nrg, '_m', '');
		set(nrg < 2 ? maxb : nrg - 1, '_l', '_pl');
		set(nrg >= maxb ? 1 : nrg + 1, '_r', '_pr');
	};
	window.showFlv = function(flvdok) {
		$.ajax({url: "index.php?eID=user_gpicflv&flvdok=" + flvdok, dataType: 'json', success: function(data) {
			if (data && data.flvurl) {
				var flvurl = data.flvurl, title = data.title, text = data.text;
				$('body').append('<div id="galerieCont"><div class="ui-widget-overlay"></div><div class="galerieGross"><div class="flvInnen">'
					+ '<div class="flvTitle">' + title + '</div>'
					+ '<a class="closeIcon" href="#" onclick="closeFG();return false" title="schließen">'
					+ '<img class="middle" alt="" src="clear.gif" width="18px" height="18px"/></a><div class="flvBox">'
					+ '<object style="margin:0;padding:0;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="528" height="297" id="Film">'
					+ '<param name="movie" value="film-gen-16-9-g.swf?url=' + flvurl + '"/>'
					+ '<param name="wmode" value="transparent"/><param name="quality" value="high"/><!--[if !IE]>-->'
					+ '<object type="application/x-shockwave-flash" data="film-gen-16-9-g.swf?url=' + flvurl + '" width="528" height="297">'
					+ '<param name="movie" value="film-gen-16-9-g.swf?url=' + flvurl + '"/>'
					+ '<param name="wmode" value="transparent"/><param name="quality" value="high"/><!--<![endif]-->'
					+ '<a href="http://www.adobe.com/go/getflash">'
					+ '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player"/></a>'
					+ '<!--[if !IE]>--></object><!--<![endif]--></object></div>'
					+ '<div class="flvText">' + text + '</div></div></div></div>');
				prepFlv();
			}
		}});
	};
	window.prepFlv = function() {
		var flvi = $('.flvInnen').hide();
		flvi.css('margin', '10px 0 0 0');
		setTimeout(function () {
			$('.ui-widget-overlay,.galerieGross').click(function() { closeFG() });
			$('.ui-widget-overlay,.galerieGross').width($(document).width()).height($('body').height());
			$('.galerieGross').css('top', $(window).scrollTop() + 'px');
			var w = $(window).width() - 42, wi = 540, left = $(window).scrollLeft();
			if (wi < w) {
				left += Math.round((w - wi) / 2);
			}
			$('.galerieGross').css('left', left + 'px');
			flvi.show(800);
		}, 500);
	}
})(jQuery);

