var slideSwitchHomeInterval;

jQuery(document).ready(function(){
	
	startUp();
	
	$('#partner ul li').live('click',function(){
		
		if ($(this).hasClass('current')) return;
		
		var tmp = $(this).index()+1;
		
		if ( $('#partner ul li.current').length > 0 ){
			$('#partner ul li.current div.vertrieb_content').slideUp(500,function(){
				$('#partner ul li').removeClass('current');
			});
		}
		
		$('#partner ul li div.vertrieb_content').each(function(){
			if ($(this).css('display') == 'block'){
				$(this).slideUp(500);
			}
		});
		
		$('#partner ul li.n_'+tmp+' div.vertrieb_content').delay(500).slideDown(750, function(){
			$('#partner ul li.n_'+tmp).addClass('current');
			
			if ( GBrowserIsCompatible() ) {
				var map_detail  = new GMap2(document.getElementById("gmap_detail_" + tmp ));
				
				if (!checkBrowserName('msie 6')) {
					map_detail.addOverlay(new GMarker(new GLatLng($('#partner ul li.n_'+tmp).attr('lat'),$('#partner ul li.n_'+tmp).attr('lon')), markerOptions));				
				} else {
					map_detail.addOverlay(new GMarker(new GLatLng($('#partner ul li.n_'+tmp).attr('lat'),$('#partner ul li.n_'+tmp).attr('lon'))));	
				}
				
				map_detail.setCenter(new GLatLng($('#partner ul li.n_'+tmp).attr('lat'),$('#partner ul li.n_'+tmp).attr('lon')), 4);
				
			}
			
		});
		
	});	
	
	if ($('.js').length > 0) {
		$('.js').show();
	}

	$('#sidebar-right a.callback').live('click',function(){
		
		if ( $('form#callback').css('display') == 'block' ) {
			$('form#callback').slideUp(500);
		} else {
			$('form#callback').slideDown(500);
		}
		
	});
	
	$('#home a.callback').live('click',function(){
		
		if ( $('#home div.first_boxholder').css('marginBottom') == "20px" ) {
			
			$('div.callback_form').fadeOut(750,function(){
				$('#home div.first_boxholder').animate({marginBottom: '0px'},250);
			});
						
		} else {
			
			$('#home div.first_boxholder').animate({marginBottom: 20+'px'},250, function() {
				$('div.callback_form').fadeIn(750);
			});
		}
		
		return false;
		
	});	
	
	
	if ($('div#slideshow div.desc').length > 0) {
		$('div#slideshow div.desc').hide();
		$('div#slideshow div.current div.desc').show();
	}
	
	$('div.slideshow div.next a').live('click',function(){
		
		$('div#slideshow div').stop(true,true);
		
		clearInterval(slideSwitchHomeInterval);
		
		var $active = $('div#slideshow div.current');
		if ( $active.length == 0 ) $active = $('div#slideshow div.item:last');
		var $active_desc = $('div#slideshow div.current div.desc');
		 
    	var $next = $active.next().length ? $active.next() : $('div#slideshow div.item:first');		 
		 
	    $active.addClass('last-active');
		
		$active_desc.slideUp(500, function(){
			
			$active_desc.fadeOut(250,function(){
				
				$active.animate({opacity: 0.0},1250);
	
			    $next.css({opacity: 0.0}).show().addClass('current').animate({opacity: 1.0}, 1250, function() {
			    	$active.removeClass('current last-active');
					$('div#slideshow div.current div.desc').slideDown(500);
			    });		
								
			})
			
		});
			
		return false;

	});		
	
	$('div.slideshow div.prev a').live('click',function(){
		
		$('div#slideshow div').stop(true,true);
		
		clearInterval(slideSwitchHomeInterval);
		
		var $active = $('div#slideshow div.current');
		if ( $active.length == 0 ) $active = $('div#slideshow div.item:first');
		var $active_desc = $('div#slideshow div.current div.desc');
		 
    	var $next = $active.prev().length ? $active.prev() : $('div#slideshow div.item:last');		 
		 
	    $active.addClass('last-active');
		
		$active_desc.slideUp(500, function(){
			
			$active_desc.fadeOut(250,function(){
				
				$active.animate({opacity: 0.0},1250);
	
			    $next.css({opacity: 0.0}).show().addClass('current').animate({opacity: 1.0}, 1250, function() {
			    	$active.removeClass('current last-active');
					$('div#slideshow div.current div.desc').slideDown(500);
			    });		
								
			})
			
		});
			
		return false;

	});	
	
	if ($('div#slideshow').length > 0) {
		slideSwitchHomeInterval = setInterval("slideSwitchHome()", 5000);
	}
		
	
	$("a.iframe").fancybox({
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	600, 
		'speedOut'			:	200, 
		'scrolling'			:	false,
		'autoScale'			:	false,
		'hideOnContentClick': 	false,
		'enableEscapeButton': 	true,
		'overlayShow'		: 	true,
		'width'				: 	500,
		'height'			: 	325,
		'margin'			:   0,
		'padding'			:   0,
		'overlayShow'		:	true 
	});	

	if ($('div.content_slideshow').length > 0) {
		var slideSwitchInterval = setInterval("slideSwitch()", 10000);
	}
});


function slideSwitchHome() {
	
	$('div#slideshow div').stop(true,true);
	
	var $active = $('div#slideshow div.current');
	if ( $active.length == 0 ) $active = $('div#slideshow div.item:last');
	var $active_desc = $('div#slideshow div.current div.desc');
	 
	var $next = $active.next().length ? $active.next() : $('div#slideshow div.item:first');		 
	 
    $active.addClass('last-active');
	
	$active_desc.slideUp(500, function(){
		
		$active_desc.fadeOut(250,function(){
			
			$active.animate({opacity: 0.0},1250);

		    $next.css({opacity: 0.0}).show().addClass('current').animate({opacity: 1.0}, 1250, function() {
		    	$active.removeClass('current last-active');
				$('div#slideshow div.current div.desc').slideDown(500);
		    });		
							
		})
		
	});
		
	return false;

}


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

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

    var $next =  $active.next().length ? $active.next()
        : $('.content_slideshow img:first');

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


function startUp(){

	$(".itemlist a").live("click",function(){

		$(".itemlist").addClass("loader");
		
		$(".ajaxcontent").load($(this).attr('href')+"?ajax=true", function(){
			
			$(".ajaxcontent").slideUp(500,function(){
				
				$(".ajaxcontent").animate({ opacity: 1 }, 500, function(){
				
					$(".ajaxcontent").slideDown(500,function(){
						$(".itemlist").removeClass("loader");
					});		
				
				});		
			})

		});		
		
		return false;
		
	});
	
	$(".month_nav a").bind("click", function(e){
		var link = $(this);

		$(".ajax_calendar").addClass("load");
		$(".ajaxcontent").fadeOut(150);
					
		$(".ajax_calendar").load(link.attr('href'), function(){
			$(".ajax_calendar").removeClass("load");
			startUp();
		});
		return false;
	});	
	
}

