jQuery.easing.def = "easeOutExpo";
$.ajaxSetup({cache: false});

var actualArticleIndex = 0;
var loader = $('<img class="loader" src="/site/images/loaders/ldr-black-blue.gif" alt="" />');
var loaderPage = $('<img id="loader-page" src="/site/images/loaders/ldr-page.gif" alt="" />');
var calArr = new Object();
var actualDate;
var homeAgendaIndex = 0;
var scrollPane;
var ytArray = new Array();
var vimeoArray = new Array();
var addressType = '';
var firstLoad = true;

$(document).ready(function(){
    $(document).scroll(function(){
        $(this).scrollLeft(0);
    });
    
    $(document, window).css('overflow', 'hidden');
});

function initValues() {
	actualArticleIndex = 0;
	loader = $('<img class="loader" src="/site/images/loaders/ldr-black-blue.gif" alt="" />');
	//calArr = new Object();
	actualDate = null;
	homeAgendaIndex = 0;
	scrollPane = null;
	ytArray = new Array();
	vimeoArray = new Array();
	addressType = '';
}


function showAddress(address, text, zoom, controlType, mapType, showBubble)
	{
		var div = document.getElementById("map_canvas");
		var geocoder = new GClientGeocoder();
		
		var map = new GMap2(div);
		map.addControl(controlType);
		
		geocoder.getLatLng(address,
			function(point)
			{
				if (!point)
				{
					div.style.visibility = 'hidden';
					alert(address + " nicht gefunden");
				}
				else
				{
					map.setCenter(point, zoom);
					var marker = new GMarker(point);
					
					if(showBubble) {
						GEvent.addListener(marker, "click", function(){
							marker.openInfoWindowHtml(text);
						});
						
						map.addOverlay(marker);
						marker.openInfoWindowHtml(text);
					} else {
						map.addOverlay(marker);
					}
				}
			}
		);
		map.setMapType(mapType);
		div.style.visibility = 'visible';
	}
	
	

function initIEHacks() {
	if(!$('ul li:first-child').hasClass('first-child')) {
		$('ul li:first-child').addClass('first-child');
	}
	
	if(!$('ul li:last-child').hasClass('last-child')) {
		$('ul li:last-child').addClass('last-child');
	}
	
	$('.column table tr::nth-child(odd)').each(function() {
		if(!$(this).find('td').hasClass('odd')) {
			$(this).find('td').addClass('odd');
		}
	});
	
	if(!$('.ui-datepicker-calendar tr td:last-child').hasClass('right-border')) {
		$('.ui-datepicker-calendar tr td:last-child').addClass('right-border');
	}
	
	if(!$('.ui-datepicker-calendar tr:first-child td').hasClass('top-border')) {
		$('.ui-datepicker-calendar tr:first-child td').addClass('top-border');
	}
	
	if(!$('.home-element.home2column:nth-child(2n)').hasClass('margin')) {
		$('.home-element.home2column:nth-child(2n)').addClass('margin');
	}
	
	
}

function initBackground() {
	$(window).resize(function() {
	
		if($('#background img').width() > 0 && $('#background img').height() > 0) {
			var windowDim = $(window).width() / $(window).height();
			var backgroundDim = $('#background img').width() / $('#background img').height();
			
			$('#background img').removeAttr('style');
			
			if(windowDim > backgroundDim) {
				$('#background img').css('width', '100%');
				$('#background img').css('height', 'auto');
			} else {
				$('#background img').css('height', '100%');
				$('#background img').css('width', 'auto');
			}
		
		}
	});
		
	$('#background img').load(initBackground);
	$(window).trigger('resize');
}

function initGalleries() {
	$('div.slider').each(function() {
		$(this).find('.slider-wrapper').cycle({ 
		    fx:     'scrollHorz', 
		    prev:   '#'+$(this).attr('id')+'-prev', 
		    next:   '#'+$(this).attr('id')+'-next', 
		    pause:	true,
		    random: true,
		    timeout: 4000,
		    speed: 800,
		    before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
		    	$(currSlideElement).removeClass('active');
		    	$(nextSlideElement).addClass('active');
		    }
		});
		
		var zIndex = -1;
		var index = -1;
		
		$('#'+$(this).attr('id')+'-lbox').bind('click', function() {
			$(this).parents('.slider-container').find('.slider-wrapper a.active').trigger('click');
			
			return false;
		});
		
		if($(this).hasClass('hasLightbox')) {
			$(this).find('.slider-wrapper a').fancybox({
				'padding'		:	0,
				'cyclic'		:	true,
				'overlayShow'	:	true,
				'overlayOpacity':	0.8,
				'overlayColor'	:	'#000000',
				'titleShow' 	:	false	
			});
		} else {
			$(this).find('.slider-wrapper a').bind('click', function() {
				return false;
			});
		}
		
		
		$(this).find('a.btn-prev').bind('mouseover', function() {
			$(this).animate({ width: '31px', paddingRight: '0px' }, 100);
			return false;
		});
		
		$(this).find('a.btn-prev').bind('mouseleave', function() {
			$(this).animate({ width: '20px', paddingRight: '2px' }, 100);
			return false;
		});
		
		$(this).find('a.btn-next').bind('mouseover', function() {
			$(this).animate({ width: '31px', paddingLeft: '0px' }, 100);
			return false;
		});
		
		$(this).find('a.btn-next').bind('mouseleave', function() {
			$(this).animate({ width: '20px', paddingLeft: '2px' }, 100);
			return false;
		});
		
		$(this).find('a.btn-prev, a.btn-next').trigger('mouseleave');
		
				
		$(this).find('a.btn-prev, a.btn-next, a.btn-lightbox').css('opacity', 0);
		
		$(this).bind('mouseover', function() {
			$(this).find('a.btn-prev, a.btn-next, a.btn-lightbox').stop().animate({ opactiy: 1 }, 400, 'easeOutExpo', function() {
				$(this).css('opacity', 1);
			});
		});
		
		$(this).bind('mouseleave', function() {
			$(this).find('a.btn-prev, a.btn-next, a.btn-lightbox').stop().animate({ opactiy: 0 }, 200, 'easeOutExpo', function() {
				$(this).css('opacity', 0);
			});
		});
		
		
	});
	
}



function initLighboxes() {
		
		
		$('a[rel=lightbox]').fancybox({
			'padding'		:	0,
			'cyclic'		:	true,
			'overlayShow'	:	true,
			'overlayOpacity':	0.8,
			'overlayColor'	:	'#000000',
			'titleShow' 	:	false	
		});
	
}


function initLinkBehaviors() {


	$('a[href^="http:"]').live('click', function() {
		
		var a = new RegExp('/' + window.location.host + '/');
		
		if(!a.test(this.href)) {
			window.open($(this).attr('href'), '_blank');
			return false;
		}
	});
	
	$('a[rel=external]').live('click', function() {
		window.open($(this).attr('href'), '_blank');
		return false;
	});
	
	$('#content-view').live('click', function() {
	
		if(!$(this).hasClass('hidden')) {
			$(this).addClass('hidden');
			$('.content').animate({ opacity: 0 }, 1000);
			$('#article-slider-prev, #article-slider-next').animate({ opacity: 0 }, 200);
		} else {
			$(this).removeClass('hidden');
			$('.content').animate({ opacity: 1 }, 1000);
			$('#article-slider-prev, #article-slider-next').animate({ opacity: 1 }, 200);
		}
		
		return false;
	});
	
	$('#agenda-list a').live('click', function() {

		if(!$(this).parent('li').hasClass('active')) {
			$(this).parent('li').parent('ul').find('li').removeClass('active');
			$(this).parent('li').addClass('active');
			startLoading($(this).parent('li'));
		}
	});
	
	
	$('#activities-list a').live('click', function() {

		if(!$(this).parent('li').hasClass('active')) {
			$('#activities-list li').removeClass('active');
			$(this).parent('li').addClass('active');
			startLoading($(this).parent('li'));
		}
	});
	
	
	$('input[type=text]').live('click', function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});

	$('input[type=text]').live('blur', function() {
		if (this.value == '') {
			this.value = this.defaultValue;
			$(this).focus().select();
		}
	});
	$('textarea').live('click', function() {
			$(this).focus().select();
	});
	
	
	$('#agenda-navigation .action-bar a.list').live('click', function() {
		$('#agenda-list').show(0);
		$('#agenda-calendar').hide(0);
		
		$('#agenda-navigation .action-bar a').removeClass('active');
		$(this).addClass('active');
		
		$('#agenda-navigation').css('width', 'auto');
		
		return false;
	});
	
	$('#agenda-navigation .action-bar a.calendar').live('click', function() {
		$('#agenda-list').hide(0);
		$('#agenda-calendar').show(0);
		
		$('#agenda-navigation .action-bar a').removeClass('active');
		$(this).addClass('active');
		
		$('#agenda-navigation').css('width', '253px');
		
		return false;
	});
	
	
	$('#facebook-like .fb-lash a').live('click', function() {
		
		if($(this).hasClass('open')) {
			$(this).removeClass('open');
			$('#facebook-like').animate({ right: '-436px' }, 200);
		} else {
			$(this).addClass('open');
			$('#facebook-like').animate({ right: '0px' }, 200);

		}
		
		return false;
	});
}


function initArticleSlider() {
		
		$('#article-slider-prev').bind('click', function() {
			prevArticle();
			return false;
		});
		
		$('#article-slider-next').bind('click', function() {
			nextArticle();
			return false;
		});
		
		$('#article-slider-prev').bind('mouseover', function() {
			$(this).find('span').animate({ marginLeft: '0px' }, 100);
			$(this).find('img').animate({ marginRight: '0px' }, 100);
			return false;
		});
		
		$('#article-slider-prev').bind('mouseleave', function() {
			$(this).find('span').animate({ marginLeft: '-20px' }, 100);
			$(this).find('img').animate({ marginRight: '-10px' }, 100);
			return false;
		});
		
		$('#article-slider-prev').trigger('mouseleave');
		
		$('#article-slider-next').bind('mouseover', function() {
			$(this).find('span').animate({ marginLeft: '0px' }, 100);
			$(this).find('img').animate({ marginLeft: '0px' }, 100);
			return false;
		});
		
		$('#article-slider-next').bind('mouseleave', function() {
			$(this).find('span').animate({ marginLeft: '20px' }, 100);
			$(this).find('img').animate({ marginLeft: '-10px' }, 100);
			return false;
		});
		
		$('#article-slider-next').trigger('mouseleave');
		
		$('#subnavigation a').live('click', function() {
			actualArticleIndex = $(this).parent('li').index();
			moveToArticle();
			
			return false;
		});
	/*
	
		$('#article-slider .column').live('click', function() {
			actualArticleIndex = $(this).index();
			moveToArticle();
			
			return false;
		});
*/
		
}

function updateArticleSlider() {
	if($('#article-slider').length > 0) {
		$('#article-slider-prev, #article-slider-next').css('display', 'block');
		
		setArticleIndexStart();
		moveToArticle();
		updateSubnavigation();
	} else {
		$('#article-slider-prev, #article-slider-next').css('display', 'none');
	}
}

function setArticleIndexStart() {
	if($('#article-slider .column.start').length > 0) {
		actualArticleIndex = $('#article-slider .column.start').index();
	}
}

function moveToArticle() {

	var columns = $('#article-slider .column');
	var coordX = 0;
	
	for(var i = 0; i < columns.length; i++) {
		if(i < actualArticleIndex) {
			coordX += $(columns[i]).width();
			coordX += 30;
		}
		
		if(i == actualArticleIndex) {
			$(columns[i]).animate({ opacity: 1 }, 500, 'easeInExpo');
			$(columns[i]).find('.black-bg, .white-bg, .slider').children().animate({ opacity: 1 }, 500, 'jswing');
			$(columns[i]).find('.slider-wrapper').cycle('resume');
		} else {
			$(columns[i]).animate({ opacity: 0.4 }, 500, 'easeInExpo');
			$(columns[i]).find('.black-bg, .white-bg, .slider').children().animate({ opacity: 0.8 }, 500, 'jswing');
			$(columns[i]).find('.slider-wrapper').cycle('pause');
		}
	}
	
	$('#article-slider-list').animate({
		left: -coordX+'px'
	}, 500, 'easeInOutExpo');
	
	if(actualArticleIndex == 0) {
		$('#article-slider-prev').fadeOut(500);
	} else {
		$('#article-slider-prev').fadeIn(500);
	}
	
	if(actualArticleIndex+1 == columns.length) {
		$('#article-slider-next').fadeOut(500);
	} else {
		$('#article-slider-next').fadeIn(500);
	}
	
	updateSubnavigation();
}

function prevArticle() {
	if(actualArticleIndex > 0) {
		actualArticleIndex--;
		moveToArticle();
	}
	
	

}

function nextArticle() {
	if(actualArticleIndex+1 < $('#article-slider .column').length) {
		actualArticleIndex++;
		moveToArticle();
	}
}

function updateSubnavigation() {
	$('#subnavigation li').removeClass('active');
	$($('#subnavigation li').get(actualArticleIndex)).addClass('active');
}

function initCustomScrollbar() {
	//$('#agenda-list').qvivoScroll();
	if($('#agenda-list').length > 0) {
		scrollPane = $('#agenda-list').jScrollPane().data('jsp');
	}
	
	if($('#activities-list').length > 0) {
		scrollPane = $('#activities-list').jScrollPane().data('jsp');
	}
}


function updateScrollPane() {
	//console.log(scrollPane);
	scrollPane.reinitialise();
}


function initAddresses() {
	$.address.change(function(event) { 
	
		if($('#facebook-like .fb-lash a').hasClass('open')) {
			$('#facebook-like .fb-lash a').trigger('click');
		} 
    	// do something depending on the event.value property, e.g.  

    	if(event.value != '/') {

    	/*
$('#languages li a').each(function() {
    		var href = '/'+$(this).parent('li').attr('rel');
    		
    		for(var i = 1; i < $.address.pathNames().length; i++) {
    			href = href+'/'+$.address.pathNames()[i];
    		}
    		
    		$(this).attr('href', href);
    	});
*/
    	
    	
    /*
	if(addressType == '') {
    	
    		if($.address.pathNames().length > 2) {
    			addressType = 'content';
    		} else {
    			addressType = 'page';
    		}
    	}
*/

			try {
				_gaq.push(['_trackPageview', $.address.path()]);
			} catch(error) {
			
			}

			if($.address.pathNames().length > 2) {
				if($('#ajax-container').length > 0) {
	    			addressType = 'content';
	    		} else {
	    			addressType = 'page';
	    		}
    		} else {
    			addressType = 'page';
    		}
    		

    		
    		//$('#language-ajax').load(event.value + ' #language-ajax-container');
    		
    		
    		if(addressType == 'page' || addressType == '') {
			    $('#ajax-wrapper-page').load(event.value + ' #ajax-container-page', updateInitsPage);  
			} else if(addressType == 'content-multiple') {
				$('#ajax-wrapper').animate({ opacity: 0.5 }, 400);
			    $('#ajax-wrapper').load(event.value + ' #ajax-container', {'multiple':1},  updateInits);  
			} else {
				$('#ajax-wrapper').animate({ opacity: 0.5 }, 400);
			    $('#ajax-wrapper').load(event.value + ' #ajax-container', updateInits);
			}
			
			addressType = '';
			
			$('#navigation li').each(function() {
				if($(this).find('a').attr('href') == event.value) {
					$('#navigation li').removeClass('active');
					$(this).addClass('active');
				}
			});
		}
		
		if(firstLoad) {
			firstLoad = false;
			updateInitsPage();
		}
	});  

	$('a[rel=ajax]').live('click', function() {  
		addressType = 'content';
    	$.address.value($(this).attr('href'));
    	$(this).blur(); 
    	return false;
	});
	
/*
	$('a[rel=ajax-page]').live('click', function() {  
    	var href = $(this).attr('href');
    	
    	startPageLoading(href);
    	$(this).blur();
    	
    	return false;
	}); 
*/
/*
	
	$('#navigation ul li a').live('click', function() { 
	
		if(!$(this).parent('li').hasClass('active')) { 
			var href = $(this).attr('href');
		
			$('#navigation ul li').removeClass('active');
			$(this).parent('li').addClass('active');
		
			startPageLoading(href);
    		$(this).blur(); 
    	}
    	
    	return false;
	}); 
*/
}

function initActives() {
	var articleRel = $('#ajax-container div').attr('rel');

	if(articleRel != '' || articleRel != undefined) {
		$('#agenda-list').each(function() {
			$(this).find('li').removeClass('active');
			$(this).find('li[rel='+articleRel+']').addClass('active');
		});
	}
	
	$('#activities-search-form p.text-input a.search-delete').bind('click', function() {
	
		$(this).parent('p').find('input').val('');
		$(this).parent('p').find('input').trigger('change');
	
		return false;
	});
}

function initCalendar() {
	
	if(!$('#agenda-calendar').hasClass('hasDatepicker')) {

		$('#agenda-calendar').hide(0);
		$('#agenda-calendar').datepicker({ 
			firstDay: 1,
			dateFormat: 'dd-mm-yy',
			altFormat: 'dd-mm-yy',
			onSelect: onSelectDate,
			onChangeMonthYear: onChangeMonth
		});
		
		setCalendarDate();
		updateCalendar();
	}
}

function setCalendarDate() {
	$('#agenda-calendar').datepicker('setDate', $('#ajax-container h2 span.date').attr('rel'));
	actualDate = $('#ajax-container h2 span.date').attr('rel');// $('#agenda-calendar').datepicker('getDate');
}


function resetCalenderObject() {
	calArr = new Object();
}
function addToCalendar(dateAsText, titleStr, linkStr) {

	if(calArr[dateAsText] == undefined) {
		calArr[dateAsText] = new Array();
	}
	
	calArr[dateAsText].push({ title:titleStr, link:linkStr});
}

function updateCalendar(month, year) {
    window.setTimeout('updateCalendarDelayed('+month+', '+year+');', 10);
}

function updateCalendarDelayed(month, year) {
	//console.log("updateCalendarDelayed");
	var actualMonth = month;
	var actualYear = year;
	
	if(month == undefined || year == undefined) {
		var actualInstanceDate = new Date($('#agenda-calendar').datepicker('getDate'));
		actualYear = actualInstanceDate.getFullYear();
		actualMonth = actualInstanceDate.getMonth()+1;
	}
	
	var tmpArr = new Array();
	
	for(var dateAsText in calArr) {
		tmpArr = dateAsText.split('-');
		//console.log(tmpArr, actualYear, actualMonth);
		if(tmpArr[1] == actualMonth && tmpArr[2] == actualYear) {
			
			$('.ui-datepicker td').filter(function() {
            	return $(this).find('a').text() === String(Number(tmpArr[0]));
        	}).find('a').addClass('has-event');
		}

 
	}
}
	
	

function onSelectDate(dateAsText, datePickerInstance) {

	//console.log(formatDate($('#agenda-calendar').datepicker('getDate')), actualDate);
	var selectedDate = formatDate($('#agenda-calendar').datepicker('getDate'));
	//console.log(selectedDate, actualDate, dateAsText);
	
	updateCalendar();
	
	if(selectedDate == actualDate) {
		return;
	}
	
	if(calArr[dateAsText] != undefined) {
		if(calArr[dateAsText].length == 1) {
			addressType = 'content';
		} else {
			addressType = 'content-multiple';
		}
		
		if(calArr[dateAsText][0]['link'] != $.address.value()) {
			$.address.value(calArr[dateAsText][0]['link']);
		} else {
			$.address.update();
		}
		
		actualDate = dateAsText;
		
	} else {
		$('#agenda-calendar').datepicker('setDate', actualDate);
	}
}

function onChangeMonth(year, month, datePickerInstance) {
	//alert(year + ' - ' + month);
	updateCalendar(month, year);
}



function formatDate(date) {
	var day = date.getDate();
	var month = date.getMonth()+1;
	var year = date.getFullYear();
	
	if(day < 10) {
		day = "0"+day;
	}
	
	if(month < 10) {
		month = "0"+month
	}
	
	return day+"-"+month+"-"+year;
}


function updateInits() {
	$('#ajax-wrapper').animate({ opacity: 1 }, 400);
	endLoading();
	initIEHacks();
	initGalleries();
	initLighboxes();
	initActives();
	initInlineScripts();
	
	if(!$('#agenda-calendar').hasClass('hasDatepicker')) {
		setCalendarDate();
		updateCalendar();
	}
}

function updateInitsPage() {
	endPageLoading();
	initValues();
	initIEHacks();
  	initBackground();
  	initGalleries();
  	initLighboxes();
  	updateArticleSlider();
  	initCustomScrollbar();
  	initCalendar();
	initHomeAgenda();
	initActivities();
	initInlineScripts();
	
	$('.content').css('margin-top', $(window).height()+'px');
	$('.content').css('opacity', 0);
	//$('.content').fadeIn(0);
	$('.content').animate({ 'margin-top': '55px', opacity: 1 }, 300);

}


function startLoading(el) {
	loader.fadeOut(0);
	el.append(loader);
	loader.fadeIn(200);
}

function endLoading() {
	loader.fadeOut(200, function() {
		$(this).remove();
	});
}

function startPageLoading(href) {
	loaderPage.fadeOut(0);
	$('body').addClass('loading').append(loaderPage);
	loaderPage.fadeIn(200);
	
	$('.content').animate({ 'margin-top': -$('#background').height()+'px', opacity: 0 }, 300, 'jswing', function() {
		addressType = 'page';
		$.address.value(href);
	});
}

function endPageLoading() {
	loaderPage.fadeOut(200, function() {
		$(this).remove();
		$('body').removeClass('loading');
	});
}

function initInlineScripts() {
	eval($('.hidden-script').text());
}

function initHomeAgenda() {
	$('#home-agenda-wrapper .btn-prev').bind('click', function() {
	
		if(homeAgendaIndex > 0) {
			homeAgendaIndex--;
			moveHomeAgenda();
		}
		
		checkArrowVisibility();
	
		return false;
	});
	
	$('#home-agenda-wrapper .btn-next').bind('click', function() {
	
		if(homeAgendaIndex+1 < $('#home-agenda-wrapper ul li.slider-element').length) {
			homeAgendaIndex++;
			moveHomeAgenda();
		}
		
		checkArrowVisibility();
		
		return false;
	});
	
	$('#home-agenda-wrapper .slider-element').css('display', 'none');
	$('#home-agenda-wrapper .slider-element:first-child').css('display', 'block');
	$('#home-agenda-wrapper .agenda-slider').addClass('active');
	checkArrowVisibility();
	
	
	
	/*
$('#home-agenda-wrapper .btn-prev').bind('mouseover', function() {
			$(this).animate({ width: '31px', paddingRight: '0px' }, 100);
			return false;
		});
		
	$('#home-agenda-wrapper .btn-prev').bind('mouseleave', function() {
			$(this).animate({ width: '20px', paddingRight: '2px' }, 100);
			return false;
		});
		
	$('#home-agenda-wrapper .btn-next').bind('mouseover', function() {
			$(this).animate({ width: '31px', paddingLeft: '0px' }, 100);
			return false;
		});
		
	$('#home-agenda-wrapper .btn-next').bind('mouseleave', function() {
			$(this).animate({ width: '20px', paddingLeft: '2px' }, 100);
			return false;
		});
		
	$('#home-agenda-wrapper .btn-prev, #home-agenda-wrapper .btn-next').trigger('mouseleave');
*/
}

function checkArrowVisibility() {
	if(homeAgendaIndex > 0) {
		$('#home-agenda-wrapper .btn-prev').fadeIn(0);
	} else {
		$('#home-agenda-wrapper .btn-prev').fadeOut(0);
	}
		
	if(homeAgendaIndex+1 < $('#home-agenda-wrapper ul li.slider-element').length) {
		$('#home-agenda-wrapper .btn-next').fadeIn(0);
	} else {
		$('#home-agenda-wrapper .btn-next').fadeOut(0);
	}
}

function moveHomeAgenda() {
	$('#home-agenda-wrapper .slider-element').css('display', 'block');
	
	var singleWidth = parseInt($('#home-agenda-wrapper .agenda-slider').width());
	$('#home-agenda-wrapper .agenda-slider ul').animate({ 'margin-left': -(homeAgendaIndex*singleWidth) + 'px'}, 800, 'easeInOutExpo');
	$('#home-agenda-wrapper .agenda-slider').animate({ 'height': $($('#home-agenda-wrapper .slider-element').get(homeAgendaIndex)).height()+'px' }, 800, 'easeInOutExpo');
}


function initActivities() {
	$('#activities-list .activities-category').bind('click', function() {
		if($(this).hasClass('closed')) {
			$(this).removeClass('closed');
			$($(this).attr('href') + ' ul').slideDown(200, updateScrollPane);
		} else {
			$(this).addClass('closed');
			$($(this).attr('href') + ' ul').slideUp(200, updateScrollPane);
		}
		return false;
	});
	
	$('#activities-list .activities-category').trigger('click');
	
	$('#activities-navigation a.search').bind('click', function() {
		return false;
	});
	
	
	
	
	
/*	
	$('#activities-navigation a.search').bind('mouseover', function() {
		
		if(!$('#activities-search-form').hasClass('open')) {
			$('#activities-search-form').stop().show(200);
			$('#activities-search-form').addClass('open');
		}
		return false;
	});
	
	
$('#activities-search-form').bind('mouseleave', function() {
		$('#activities-search-form').hide(200);
		$('#activities-search-form').removeClass('open');
	});

	
	//$('#activities-search-form').hide(0);
*/	
	$('#activities-search-form input').bind('change', updateActivities);
	$('#activities-search-form input[type=text]').bind('keyup', updateActivities);
	
	$('#activities-navigation .search-delete').bind('click', function() {
		$('#activities-search-form input[type=text]').val('');
		$('#activities-list .activities-category:not(.closed)').trigger('click');
		updateActivities();
		return false;
	});
}

function updateActivities() {
	//console.log($('#activities-search-form .checkboxes.categories input'));
	//console.log($('#activities-search-form .checkboxes.seasons input'));
	//console.log($('#activities-search-form .text-input input').val());


	var searchText = $('#activities-search-form .text-input input').val();
	$('#activities-list li').removeClass('selected');

	
	var seasons = '';
	var seasonArray = new Array();
	var seasonList = $('#activities-search-form .checkboxes.seasons input');
	
	if($('#activities-search-form .checkboxes.seasons input:checked').length == 0) {
		$('#activities-search-form .checkboxes.seasons input').attr('checked','checked');
	}
	
	for(var i = 0; i < seasonList.length; i++) {
		if($(seasonList[i]).is(':checked')) {
			if(seasons != '') {
				seasons += ', ';
			}
			seasons += ('.'+$(seasonList[i]).attr('name'));
			seasonArray.push($(seasonList[i]).attr('name'))
		}
	}
	
	//console.log('li.'+seasonArray.join(', li.'));
	
	$('#activities-list li.'+seasonArray.join(', li.')).addClass('selected');
	$('#activities-list li:not('+seasons+')').removeClass('selected');
	
	if(searchText.length > 0) {
		$('#activities-list .activities-category.closed').trigger('click');
		updateActivitiesList(searchText);
		$('#activities-search-form p.text-input a.search-delete').css('display', 'block');
	} else {
		$('#activities-search-form p.text-input a.search-delete').css('display', 'none');
	}
	
	
	if($('#activities-list li.selected').is(":hidden")) {
      	$('#activities-list li.selected').show();
      	updateScrollPane();
	} else {
    	$('#activities-list li.selected').slideDown(200, updateScrollPane);
	}
	
	if($('#activities-list li:not(.selected)').is(":hidden")) {
      	$('#activities-list li:not(.selected)').hide();
      	updateScrollPane();
	} else {
    	$('#activities-list li:not(.selected)').slideUp(200, updateScrollPane);
	}
	
	

	//$('#activities-list li.selected').slideDown(200, updateScrollPane);
	//$('#activities-list li:not(.selected)').slideUp(200, updateScrollPane);
	
	
	
	var categoryList = $('#activities-search-form .checkboxes.categories input');
	var elName = '';
	
	for(var i = 0; i < categoryList.length; i++) {
		elName = $(categoryList[i]).attr('name');
		
		if($(categoryList[i]).is(':checked')) {
			$('#activities-list a.activities-category.'+elName).show(200, updateScrollPane);
			$('#activities-list div.category-articles.'+elName).show(200, updateScrollPane);
	
		} else {
			$('#activities-list a.activities-category.'+elName).hide(200, updateScrollPane);
			$('#activities-list div.category-articles.'+elName).hide(200, updateScrollPane);
		}
	}

}

function updateActivitiesList(searchText) {

	var searchArray = searchText.split(' ');
	
	$('#activities-list li').each(function() {
	
		if(hasText(searchArray, $(this).text().toLowerCase())) {
			$(this).addClass('selected');
			//console.log('found');
		} else {
			$(this).removeClass('selected');
		}
		
	});
}

function hasText(searchArray, findInText) {
	//console.log(searchArray, findInText);

	for(var i = 0; i < searchArray.length; i++) {
		//console.log(findInText, searchArray[i]);
		if(findInText.indexOf(searchArray[i].toLowerCase()) != -1) {
			return true;
		}
	}
	
	return false;
}

$(document).ready(function(){
  //initIEHacks();
  //initBackground();
  //initGalleries();
  //initLighboxes();
  initLinkBehaviors();
  initArticleSlider();
  //initCustomScrollbar();
  initAddresses();
  //initCalendar();
  //initHomeAgenda();
  //initActivities();
  
  
  
  
	  
});

