var timeout;
String.prototype.repeat = function(num) {
    return new Array(isNaN(num)? 1 : ++num).join(this);
}
	
$.fn.infiniteCarousel = function () {
	function repeat(str, num) {
		return new Array( num + 1 ).join( str );
	}
  
	return this.each(function () {
		var $wrapper = $(this).children('div'),
			$slider = $wrapper.children('#slider'),
			$items = $slider.children('li'),
			$single = $items.filter(':first'),
			singleWidth = $single.outerWidth(), 
			visible = 1, // note: doesn't include padding or border
			currentPage = 1,
			pages = 3;   
		$(this).append('<ul class="slider-icons" />');
		$items.each(function(i) {
			$('.slider-icons').append('<li><div style="margin-left: 16px;"></div></li>');
		});
		$('.slider-icons').children(':first').addClass('active').children().css('margin-left', '-1px');
		
		
		// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
		$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
		$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
		$items = $slider.find('> li'); // reselect
		
		// 3. Set the left position to the first 'real' item
		$wrapper.scrollLeft(singleWidth * visible);
		
		function point(interval) {
			if(interval == 0) {
				return;
			}
				
			var active = $('.slider-icons > .active');
			active.children('div').animate({
				"margin-left" : (interval > 0) ? '-16px' : '16px'
			}, 250);
			window.setTimeout(function() {
				var next;
				if(interval > 0) {
					if(active.parent().children(':first').hasClass('active')) {
						next	= active.nextAll(':last');
					} else {
						next	= active.prevAll(':eq('+(Math.abs(interval) - 1)+')');
					}
					next.children().css('margin-left', '16px');
				}
				else if(interval < 0) {
					if(active.parent().children(':last').hasClass('active')) {
						next	= active.prevAll(':last');
					} else {
						next	= active.nextAll(':eq('+(Math.abs(interval) - 1)+')');
					}
					next.children().css('margin-left', '-16px');
				}
				
				active.removeClass('active')
				next.addClass('active').children().animate({
					"margin-left" : '-1px'
				}, 250);
			}, 175);
		}			
		// 4. paging function
		function gotoPage(page) {
			window.clearTimeout(timeout);
			var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				left = singleWidth * dir * visible * n;
			point(currentPage - page);
			$wrapper.filter(':not(:animated)').animate({
				scrollLeft : '+=' + left
			}, 500, function () {
				if (page == 0) {
					$wrapper.scrollLeft(singleWidth * visible * pages);
					page = pages;
				} else if (page > pages) {
					$wrapper.scrollLeft(singleWidth * visible);
					// reset back to start position
					// reset back to start position
					page = 1;
				} 

				currentPage = page;
			});                
			
			
			timeout = window.setTimeout(function() {
				return gotoPage(currentPage + 1);                
			}, 7000);
			
			return false;
		}
			
		timeout = window.setTimeout(function() {
			return gotoPage(currentPage + 1);                
		}, 7000);

		
		// 5. Bind to the forward and back buttons
		$('.slider-icons > li').click(function(e) {
			if($wrapper.is(':not(:animated)'))
				return gotoPage($(this).prevAll().length + 1);                
		});		
	});  
};

(function($){$.Placeholder={settings:{color:"rgb(169,169,169)",dataName:"original-font-color"},init:function(c){if(c){$.extend($.Placeholder.settings,c)}var d=function(a){return $(a).val()};var e=function(a,b){$(a).val(b)};var f=function(a){return $(a).attr("placeholder")};var g=function(a){var b=d(a);return(b.length===0)||b==f(a)};var h=function(a){$(a).data($.Placeholder.settings.dataName,$(a).css("color"));$(a).css("color",$.Placeholder.settings.color)};var i=function(a){$(a).css("color",$(a).data($.Placeholder.settings.dataName));$(a).removeData($.Placeholder.settings.dataName)};var j=function(a){e(a,f(a));h(a)};var k=function(a){if($(a).data($.Placeholder.settings.dataName)){e(a,"");i(a)}};var l=function(){if(g(this)){k(this)}};var m=function(){if(g(this)){j(this)}};var n=function(){if(g(this)){k(this)}};$("textarea, input[type='text']").each(function(a,b){if($(b).attr("placeholder")){$(b).focus(l);$(b).blur(m);$(b).bind("parentformsubmitted",n);$(b).trigger("blur");$(b).parents("form").submit(function(){$(b).trigger("parentformsubmitted")})}});return this}}})(jQuery);

jQuery(document).ready(function ($) {
	$.Placeholder.init();
	$('#teaser').infiniteCarousel();
	if($.browser.msie && $.browser.version < 8)
		$('#mainnav > :last, .submenu > :last').addClass('last');
		
	$('a.fancybox-iframe').fancybox({
		'width': 800,
		'height': 650,
		'type': 'iframe',
		'href': $(this).attr('href'),
		'title': '<a href="http://maps.google.de/maps?oe=utf-8&amp;client=firefox-a&amp;q=Hauptstr.+23+Bad+Nenndorf&amp;ie=UTF8&amp;hq=&amp;hnear=Hauptstra%C3%9Fe+23,+Bad+Nenndorf+31542+Bad+Nenndorf,+Schaumburg,+Niedersachsen&amp;gl=de&amp;ll=52.337883,9.383054&amp;spn=0.022131,0.066047&amp;t=m&amp;z=14&amp;vpsrc=6&amp;source=embed" target="_blank">Größere Kartenansicht</a>'
	});
});
