﻿(function($){
	// Anti Spam Links!
	function decodeAntiSpamLink(txt){
		return txt.replace(/#1#/g, '.').replace(/#2#/, '@').split('').reverse().join('');
	};
	
	var antiSpamImages = [
		{name: 'kontaktadresse.jpg', encodedlink: 'ed#1#si-grc#2#tkatnok'}
	];

	$('img.antispam')
		.each(function(){
			for ( var i=0; i<antiSpamImages.length; i++ )
			{
				if ( this.src.indexOf(antiSpamImages[i].name) != -1 )
				{
					var m = decodeAntiSpamLink(antiSpamImages[i].encodedlink);
					$(this).replaceWith('<a href="mailto:' + m + '" title="' + m + '">' + m + '</a>');
				};
			};
		});
	
	$('a[rel*=#2#]')
		.each(function(){
			var m = decodeAntiSpamLink( $(this).attr('rel') );
			$(this)
				.attr('href', 'mailto:' + m)
				.attr('title', m)
				.removeAttr('rel')
				.text(m);
		});
	
	/*
	* Bearbeitungsmodus
	*/
	CRGHomepage.editmode = false;
	if ( $('#MSOZone').length > 0 || $('div.ms-SPZone:first').length > 0 ) CRGHomepage.editmode = true;

		
	// Tab functionality
	var $tabs = $('.ms-rteCustom-tabsNav a[href^=#]')
		.each(function(){
			var hrefId = $(this).attr('href');
			var $tabContent = $(hrefId).addClass('tabsPanel').hide();
			if ( !$tabContent.children('div:empty').length ) $(this).closest('li').show(); //$(this).closest('li').hide();
		})
		.click(function(){
			$('.tabsPanel').hide();
			var hrefId = $(this).attr('href');
			$(hrefId).show();
			$(this).parents('li:first').addClass('tabs-selected').siblings().removeClass('tabs-selected');
			
			var date = new Date();
            date.setTime(date.getTime() + (10 * 60 * 1000)); // Cookie should expire in 10 minutes
            var expires = '; expires=' + date.toUTCString();
	        document.cookie = ['TabSelected', '=', encodeURIComponent(hrefId), expires].join('');
			return false;	
		});
		
	// Get selected tab from cookie and show content of tab (only in edit mode)
	var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = $.trim(cookies[i]).split('=');
            if ( cookie[0] == 'TabSelected' ){
            	cookieValue = decodeURIComponent(cookie[1]);
            };
        }
    }
    if ( CRGHomepage.editmode && cookieValue && $(cookieValue).is('.tabsPanel') ) $tabs.filter('[href=' + cookieValue + ']').click();
	else $tabs.eq(0).click();
	
	// Carousel & Cycle functionality
	if ( $('table.ms-WPAddButton').length == 0 ){ // Not in EditMode
		// Carousel functionality
		$('div.carousel2D').each(function(ix){
			var $this = $(this);
			var startFn = function(){
				$this.jMyCarousel({
					auto: true,
					speed: 3000,
					visible: '484px', //'3',
					eltByElt: true,
					easing: 'linear'
				});
			};
			var $img = $this.find('img:last');
			if ( $.browser.msie )
				window.setTimeout(startFn, 100);
			else
				window.setTimeout(function(){
					startFn();
				}, 100);

			if (ix == 0) $('div.topNavFlyOuts').css('zIndex', '100');
		});
		// Cycle functionality
		$('.slideshow')
			.css({
				display: 'block',
				overflow: 'hidden'
			})
			.each(function(){
				var $this = $(this),
					maxH = $(this).attr('minheight') ? parseInt($(this).attr('minheight'), 10) : 130,
					timeout = $(this).attr('timeout') ? parseInt($(this).attr('timeout'), 10) : 4000;
				
				$(window)
					.load(function(){
						$this.children().each(function(){
							var h = $(this).height();
							if ( h > maxH ) maxH = h;
						});
						$this.height(maxH);
					});
				
				$(this)
					.height(maxH)
					.cycle({
						timeout: timeout
					});
			});
	} else {
		$('div.carousel2D li').show();
		$('.slideshow').show().children().show();
	};

	
})(jQuery);