jQuery.fn.toggleanimate = function (cb, duration, css_over, css_out, ease)
{
	if (!ease) ease = 'linear';
	for(var i = 0; i < this.length; i++)
	{
		jQuery(this).css(css_out);
		var reset =	function ()
		{
			jQuery(this).data('toggleanimate').start = 0;
		}
		var over = jQuery.proxy(function ()
		{

			var now = new Date();
			var object = this.data('toggleanimate');
			var started = object.start; object.start = now;

			if (started)
			{
				this.stop(true, false);
				var elapsed = now.getTime() - started.getTime();
				object.progress -= elapsed;
			}
			else { object.progress = 0; }

			this.animate(css_over, duration - object.progress, ease, reset);

		}, jQuery(this[i]));

		var out = jQuery.proxy(function () {

			var now = new Date();
			var object = this.data('toggleanimate');
			var started = object.start; object.start = now;

			if (started)
			{
				this.stop(true, false);
				var elapsed = now.getTime() - started.getTime();
				object.progress += elapsed;
			}
			else { object.progress = duration; }

			this.animate(css_out, object.progress, ease, reset);

		}, jQuery(this[i]));

		jQuery(this[i]).data('toggleanimate', { 'over' : over, 'out' : out });

		cb.call(this[i], over, out);

		return jQuery(this[i]).data('toggleanimate');

	}

};

var animation_time = 300;

jQuery(function(){

	jQuery("body").removeClass("no-js");
			
	var enable = jQuery('.coda-slider .panel').length > 1;

	var settings = {
		autoSlide : jQuery('.coda-slider DIV.panel').length > 1,
		autoSlideInterval : 3000,
		autoSlideStopWhenClicked : false,
		autoSlidePauseWhenMouseOver : true,
		dynamicArrows : true,
		dynamicArrowLeftText : '<img src="/fileadmin/templates/global/img/arrow-left.png" width="25" height="25">',
		dynamicArrowRightText : '<img src="/fileadmin/templates/global/img/arrow-right.png" width="25" height="25">',
		dynamicTabs : false,
		continous : true,
		crossLinking : false
	};

	document.sliders = jQuery('.coda-slider').codaSlider(settings);

	// fade in/out function 
	var fader = function(opacity, duration) {
		var left = jQuery(".coda-nav-left a",this);
		var right = jQuery(".coda-nav-right a",this);
		if(left) left.stop(true, false);
		if(right) right.stop(true, false);
		if(left) left.animate({ opacity:opacity }, duration);
		if(right) right.animate({ opacity:opacity }, duration);
	}

	fader.call(this, 0, 0);

	if(enable) { jQuery("div.coda-slider-wrapper").hover(
		// fade navigation in and out on hover
		function() { fader.call(this, 0.7, animation_time); },
		function() { fader.call(this, 0, animation_time); }
	); } else {
		// disable navigation elements if only a single slide
		jQuery("div.coda-slider-wrapper").find('.coda-nav-left a, .coda-nav-right a').hide();
	}

	var titleFormat = function(title, currentArray, currentIndex, currentOpts) {
		if (!title) return false;
		html = '<span id="fancybox-title-wrap">';
		html +=  '<span id="fancybox-title-left"></span>';
		html +=  '<span id="fancybox-title-main">';
		html +=   title;
		html +=  '</span>';
		html +=  '<span id="fancybox-title-right"></span>';
		html += '</span>';
	return html; }

	var selector = 'DIV.gallery TABLE.gallery-thumbnail TR TD A';

	if (jQuery.fn.colorbox) {
	
		jQuery(selector).colorbox({
			rel : 'gallery',
			slideshow : true,
			slideshowAuto : false,
			slideshowStart : 'Slideshow Starten',
			slideshowStop : 'Slideshow Stoppen',
			current : '{current} von {total}',
			close : 'Schliessen',
			previous : 'Zurück',
			next : 'Vorwärts',
			preloading : true,
			scalePhotos : true,
			maxWidth : "100%",
			maxHeight : "100%"
		});

	} else if (jQuery.fn.fancybox) {

		jQuery(selector).fancybox({
			cyclic : true,
			speedIn : 400, 
			speedOut : 200, 
			transitionIn : 'fade',
			transitionOut : 'fade',
			easingIn : 'easeOutBack',
			easingOut : 'easeInBack',
			titleFormat : titleFormat,
			titlePosition : 'outside',
			overlayShow : true,
			showNavArrows : true,
			hideOnContentClick: true,
			enableEscapeButton : true
		});

	} else if (jQuery.fn.lightBox) {

		jQuery(selector).lightBox({
			overlayBgColor: '#FFFFFF',
			overlayOpacity: 0.6,
			imageLoading: '/fileadmin/templates/global/img/lightbox/loading.gif',
			imageBtnClose: '/fileadmin/templates/global/img/lightbox/close.gif',
			imageBtnPrev: '/fileadmin/templates/global/img/lightbox/prev.gif',
			imageBtnNext: '/fileadmin/templates/global/img/lightbox/next.gif',
			imageBlank: '/fileadmin/templates/global/img/lightbox/blank.gif',
			containerResizeSpeed: 350,
			containerBorderSize: 10,
			keyToClose: 'x',
			keyToPrev: 'p',
			keyToNext: 'n',
			txtImage: 'Bild Nr.',
			txtOf: 'von'
		});

	}
	
	jQuery('FORM.searchbox').each(function()
	{

		var text = jQuery('INPUT.text', this);
		var clear = jQuery('SPAN.clear', this);
		var submit = jQuery('INPUT.submit', this);
		var images = clear.find('IMG');

		var update_ui = function()
		{

			if(text.val() != "")
			{ clear.css('cursor', 'pointer'); images.attr('src', '/fileadmin/templates/global/img/search_right_clear.gif') }
			else { clear.css('cursor', 'default'); images.attr('src', '/fileadmin/templates/global/img/search_right.gif') }

		}

		var sbmt_on = function(){ this.src = this.src.replace(/(?:_hover)?\.gif$/, '_hover.gif') };
		var sbmt_off = function(){ this.src = this.src.replace(/(?:_hover)*\.gif$/, '.gif') };

		images.hover(sbmt_on, sbmt_off)

		submit.hover(sbmt_on, sbmt_off)
			.focus(sbmt_on).blur(sbmt_off)

		text.toggleanimate
		(

			function(over, out)
			{
	  		// attach to hover events
	  		jQuery(this).blur(jQuery.proxy(out, jQuery(this)));
	  		jQuery(this).focus(jQuery.proxy(over, jQuery(this)));
			},
			200,
			{ 'width' : '140px' },
			{ 'width' : '40px' }

		);

		text
			.keyup(update_ui)
			.keydown(update_ui)
			.keypress(update_ui)
			.change(update_ui)
			.change()

		clear.click(function()
		{

			text.val('').keyup();

		})

	});


});

