jQuery.fn.extend({
	scrollV : function(speed, margin, easing) {
		var speed = speed || 1000;
		var margin = margin || 0;
		return this.each(function() {
			var targetOffset = $(this).offset().top;
			$('html, body').animate({scrollTop: targetOffset}, speed, easing);
		});
	},
	
	SearchTopic : function(callback,ref) {
		var ref = ref || document.referrer;
		
		var Search = {
			engines: [
			    [/^http:\/\/(www\.)?google\./i, /q=([^&]+)/i],                            // Google
			    [/^http:\/\/(www\.)?search\.yahoo\./i, /p=([^&]+)/i],                     // Yahoo
			    [/^http:\/\/(www\.)?search\.msn\./i, /q=([^&]+)/i],                       // MSN
			    [/^http:\/\/(www\.)?search\.live\./i, /query=([^&]+)/i],                  // MSN Live
			    [/^http:\/\/(www\.)?search\.aol\./i, /userQuery=([^&]+)/i],               // AOL
			    [/^http:\/\/(www\.)?ask\.com/i, /q=([^&]+)/i],                            // Ask.com
			    [/^http:\/\/(www\.)?altavista\./i, /q=([^&]+)/i],                         // AltaVista
			    [/^http:\/\/(www\.)?feedster\./i, /q=([^&]+)/i],                          // Feedster
			    [/^http:\/\/(www\.)?search\.lycos\./i, /q=([^&]+)/i],                     // Lycos
			    [/^http:\/\/(www\.)?alltheweb\./i, /q=([^&]+)/i],                         // AllTheWeb
			    [/^http:\/\/(www\.)?technorati\.com/i, /([^\?\/]+)(?:\?.*)$/i]            // Technorati
			    ],
			
			decodeURL: function(URL,reg) {
				URL = decodeURIComponent(URL);
				var query = null;
				try {
					$.each(reg,function(i,n){
						if(n[0].test(URL)) {
							var match = URL.match(n[1]);
							if(match) {
								query = match[1].toLowerCase();
								return false;
							}
						}
					});
					
					if (query) {
						query = query.replace(/(\'|")/, '\$1');
						query = query.split(/[\s,\+\.]+/);
					}
				} catch (e) {}
				
				return query;
			}
		};
		
		var topics = Search.decodeURL(ref,Search.engines);
		for (i=0; i<this.length; i+=1) {
			callback(this[i],topics);
		}
		
		return this;
	}
});

var contains = function(src,keys) {
	var check = false;
	if (keys) {
		for (j=0; j<keys.length; j +=1) {
			if (con.indexOf(keys[j]) != -1) {
				check = true;
			}
		}
	}
	return check;
};

function slideSwitch() {
  var active = $('#ref img.active');

  if ( active.length == 0 ) active = $('#ref img:last');

  // use this to pull the images in the order they appear in the markup
  var next =  active.next().length ? active.next() : $('#ref img:first');
  // check if only one image
  if (next.attr('src') == active.attr('src')) {
    return;
  }

  // uncomment the 3 lines below to pull the images in random order
  //var $sibs  = $active.siblings();
  //var rndNum = Math.floor(Math.random() * $sibs.length );
  //var $next  = $( $sibs[ rndNum ] );

  active.addClass('last-active');

  next.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 2000, function() {
          active.removeClass('active last-active');
    	});
}

$(function() {
	$("#head").find("a").append("<span></span>");
	
	var elm = '#fxt';
	if ($.browser.msie && $.browser.version < 8 ) {
		elm = elm + ', #ref h3';
	}
	$('#ref h3').prepend('<span id="fx" style="display:none"><span></span></span><span id="fxt"></span>');
	$('#fx').height(0).fadeTo(0,0);
	$(elm)
	.hover(function(){
		$('#fx').stop().animate({ height: '214px', opacity: '1' }, 2000);
	},     function(){
		$('#fx').stop().fadeTo(2000,0,function(){$(this).height(0);});
	})
	.click(function() {
		$('#ref a:first').click();
	});
	
	$('#ref a').lightBox({
		overlayBgColor: '#465058',
		overlayOpacity: 0.9,
		txtImage: '',
		txtOf: '/',
		imageLoading: img + 'loading.gif',
		imageBtnPrev: img + 'prev.png',
		imageBtnNext: img + 'next.png'
	});
	
	//Bestehende RF LÃ¶sung mit 2 Bildern
	//$('#ref').prepend('<div id="change" style="display:none"></div>');
	//setInterval("$('#change').animate({ opacity: 'toggle'},3000)", 5000);
	
	//tgrund: Slideshow Skript eingefÃ¼gt
  setInterval( "slideSwitch()", 5000 );
	
	if ($.browser.safari) { //fix f&uuml;r Safari 2.0.4
		$("#sub dd").prepend("<span> </span>");
	}
	
	$('#sub')
	.addClass("shrink")
	.find('dt')
	.hover(function(){
		$(this).addClass("over");
	},     function(){
		$(this).removeClass("over");
	})
	.click(function() {
		$(this).toggleClass("open")
		.next().animate( { height: 'toggle', opacity: 'toggle' }, 'slow');
	})
	.next().hide()
	.end()
	.find('dd').SearchTopic(function(elm,t){
		var con = elm.innerHTML.toLowerCase().replace(/<\/?[^>]+>/gi, '');
		if (contains(con,t)) {
			$(elm).show().prev().addClass("open");
		}
	}).end();
	
	//tgrund: das erste dt Element im Kontakt wird gleich ge&ouml;ffnet
	$('#p-kontakt').find("#sub").find("dt:first").addClass("open").next().show();
	
	//tgrund: Im Downloadbereich werden alle Links gleich aufgeklappt
	$('#p-download').find("#sub").find("dt").addClass("open").next().show();

	$('#teaser').find("a").click(function(){/*
		if (window.location.pathname.indexOf(this.pathname) != -1) {
			$('#sub dt').removeClass("open")
			.next().stop().animate({ height: 'hide', opacity: 'hide' }, 'slow');
			$('#' + this.search.slice(1)).addClass("open")
			.next().stop().animate({ height: 'show', opacity: 'show' }, 'slow');
			return false;
		}
	*/})

	.each(function(){
		this.href = this.href.replace("#","?");
	});
	
	if (window.location.search) {
		var art = window.location.search.slice(1);
		$('#' + art).addClass("open").next().show();
		//if ($('#main :first').attr("id") != art) {
		//	$('#' + art).scrollV(1500);
		//}
	}
});