var mapMarkers;
var listElements;

function changeOpenEvent(itsMe){
	//$('li', '#events-list .list-of-elements').each(function() {
	
	
	
	for (var i = 0; i < listElements.length; i++){	
		var tempElement = listElements[i];
		
		
		if (itsMe == tempElement){
			$('.event-info-box', tempElement).slideDown(300);
			tempElement.data('clicked', true);
		}else{
		
		//if (itsMe != listElements[i]){
			//$('.event-info', this).fadeOut(0);
			
			
			if (tempElement.data('clicked') == true){
				
				
				
				$('.event-info-box', tempElement).slideUp(300, function(){
					
					//alert($(this).html());
					
					
				});
				
				$('.event-info', tempElement).delay(300).fadeOut(200, function(){
						
				});
				
				tempElement.data('clicked', false);
			}
		}
	
	}
	
	//});
	
	
}

function setMarkers(id, icon, referer){
	
	var tempMarkers = new Array();
	
	$('li', id+' .list-of-elements').each(function() {
		
		var itemId = $('.event-id', this).html();
		
		var liElement = $(this);
		listElements.push(liElement);
		
		var latitude = $('.event-lat', this).html();
		var longitude = $('.event-long', this).html();
		var eventtitle = $('.event-title', this).html();
		
		$('#google-map').gmap('addMarker', { 
			'position': new google.maps.LatLng(latitude, longitude), 
			'title': eventtitle,
			'icon':new google.maps.MarkerImage(icon, new google.maps.Size(34, 38), new google.maps.Point(0, 0), new google.maps.Point(11, 30))
		}, function(map, marker){
			
			tempMarkers.push(marker);
			
			liElement.hover(function(){
				marker.setAnimation(google.maps.Animation.BOUNCE);
			}, function(){
				marker.setAnimation(null);
			});
			
			
			$(marker).click(function(){
				
				changeOpenEvent(liElement);
				/*
				liElement.data('clicked', true);
				$('.event-info-box', liElement).slideDown(300, function(){
					//alert('wasdenn');
				});
				*/
				
			});
			
			
			
			$(marker).mouseover(function(){
				$('.event-info', liElement).fadeIn(200);
			});
			$(marker).mouseout(function(){
				if (liElement.data('clicked') != true){
					$('.event-info', liElement).fadeOut(200);
				}
			});
		});
		
	});
	
	mapMarkers[id] = tempMarkers;
}

function loadList(id, url, referer, icon, page, month){
	
	var tempMarkers = mapMarkers[id];
	
	if (tempMarkers != null){
		for (var i = 0; i < tempMarkers.length; i++){
			tempMarkers[i].setMap(null);
		}
	}
	
	$('.window_content', id).html('<div class="loading">Inhalt laden...</div>');
	
	var searchTerm = $('#search-form').val();
	if ($('#search-form').hasClass('inactive')){
		searchTerm = '';
	}
	
	var kat = '';
	
	if ($('#bildende').hasClass('active')){
		kat = '3';
	}
	if ($('#darstellende').hasClass('active')){
		kat = kat + '-4'
	}
	if ($('#gesellschaft').hasClass('active')){
		kat = kat + '-2'
	}
	if ($('#sport-natur').hasClass('active')){
		kat = kat + '-1'
	}
	
	//$('.window_content', id).load(url+'?month='+month+'&page='+page+'&kat='+kat+'&search='+searchTerm, function(){
	$(id).load(url+'?month='+month+'&page='+page+'&kat='+kat+'&search='+searchTerm, function(){
		
		
		var currentPage = 1;
		var numberOfPages = 0;
		
		$('.pageination-page', id+' .pagination').each(function(key){
			
			if ($(this).hasClass('current')){
				currentPage = key+1;
			}
			
			$(this).click(function(){
				loadList(id, url, referer, icon, key+1, month);
			});
			
			numberOfPages++;
			
		});
		
		$('.previous-page', id+' .pagination').click(function(){
			if (currentPage > 1){
				loadList(id, url, referer, icon, currentPage-1, month);
			}
		});
		$('.next-page', id+' .pagination').click(function(){
			if (currentPage < numberOfPages){
				loadList(id, url, referer, icon, currentPage+1, month);
			}
		});

		setMarkers(id, icon, referer);

		$('li', id+' .list-of-elements').each(function(){
			
			$(this).hover(function(){
				$('.event-info', this).fadeIn(200);
			}, function(){
				$('.event-info', this).fadeOut(300);
				$('.event-info-box', this).slideUp();
				$(this).data('clicked', false);
			});
			
			var itemId = $('.event-id', this).html();
			
			$(this).click(function(){
				
				
				$('.event-info-box', this).slideDown();
				
				//window.location.href = referer+'?id='+itemId;
			});
			
			$('.event-info-box-close', this).click(function(){
				$('.event-info-box', this).slideUp();
			});
			
		});


		/*
		var numberOfEvents = 0;
		var sumLat = 0;
		var sumLong = 0;
		
		$('li', id+' .list-of-elements').each(function() {
			sumLat = sumLat + parseFloat($('.event-lat', this).html());
			sumLong = sumLong + parseFloat($('.event-long', this).html());
			numberOfEvents++;
		});
		sumLat = sumLat / numberOfEvents;
		sumLong = sumLong / numberOfEvents;
		*/
		
	});
	
}

function closeAllDailyList(){
	$('.list-header', '#daily-list .list-of-daily-elements').each(function(key){
		$(this).data('clicked', false);
		$(this).removeClass('active');
	});
	
	$('.list-content', '#daily-list .list-of-daily-elements').each(function(key){
		$(this).slideUp();
	});
}

function loadDailyList(selectedDay){
	
	$('.window_content', '#daily-list').html('<div class="loading">Inhalt laden...</div>');
	
		$('#daily-list').load('/windows/dailylist.php?day='+selectedDay, function(){
			
			$(this).mouseleave(function(){
				closeAllDailyList();

			});
			
			var listContents = new Array();
			var listHeaders = new Array();
			
			$('.list-content', '#daily-list .list-of-daily-elements').each(function(key){
				
				listContents.push($(this));
				
				/*
				$(this).mouseleave(function(){
					
					listHeaders[key].data('clicked', false);
					listHeaders[key].removeClass('active');
					$(this).slideUp();
					
				});
				*/
				
			});
			
			$('.list-header', '#daily-list .list-of-daily-elements').each(function(key){
			
				listHeaders.push($(this));
			
				var id = $('.event-id', this).html();
				
				
				$(this).hover(function(){

					$(this).addClass('active');
				}, function(){
					if ($(this).data('clicked') != true){
						$(this).removeClass('active');
					}
				});
				
				
				$(this).click(function(){
				
				
				
					closeAllDailyList();
				
				
					$(this).data('clicked', true);
					$(this).addClass('active');
					listContents[key].slideDown();
				
				
				});
				
			});
			
		});
}

function loadCalendar(month){
	
	$('.window_content', '#calendar').html('<div class="loading">Inhalt laden...</div>');
	
	$('#calendar').load('/windows/calendar.php?month='+month, function(){
		
		$('.next', this).click(function(){
			var nextMonth = $(this).html();
			loadCalendar(nextMonth);
		});
		
		$('.previous', this).click(function(){
			var prevMonth = $(this).html();
			loadCalendar(prevMonth);
		});
		
		var selectedDay = '';
		var firstElement = null;
		
		$('li', '#calendar .calendar-day-list').each(function(){
			
			var timestamp = $('.timestamp', this).html();
			
			if (firstElement == null && $(this).hasClass('current-month')){
				firstElement = $(this);
			}
			
			if ($(this).hasClass('current-day')){
				selectedDay = timestamp;
			}
			
			$(this).click(function(){
				
				$('li', '#calendar .calendar-day-list').each(function(){
					$(this).removeClass('current-day');
				});
				
				$(this).addClass('current-day');
				
				loadDailyList(timestamp);
			});
			
		});
		
		if (selectedDay == ''){
			firstElement.addClass('current-day');
			selectedDay = $('.timestamp', firstElement).html();
		}
		
		
		loadDailyList(selectedDay);
		
		loadList('#events-list', '/windows/eventlist.php', '/veranstaltung', '/images/marker-event.png', 1, selectedDay);
		loadList('#portraits-list', '/windows/portraitlist.php', '/portrait', '/images/marker-portrait.png', 1, '');
		
		//loadEventsList(1, selectedDay);
		//loadPortraitsList(1);
		
	});
}

$(document).ready(function(){
	
	mapMarkers = Array();
	listElements = Array();
	
	$('#search-form').focusin(function(){
		if ($(this).hasClass('inactive')){
			$(this).removeClass('inactive');
			$(this).val('');
		}
	});
	
	$('#search-form').keydown(function(event) {
		if (event.keyCode == 13){
			loadList('#events-list', '/windows/eventlist.php', '/veranstaltung', '/images/marker-event.png', 1, '');
			loadList('#portraits-list', '/windows/portraitlist.php', '/portrait', '/images/marker-portrait.png', 1, '');
		}
	});

	
	$('#search-it').click(function(){
		if ($('#search-form').hasClass('inactive')){
			alert('Bitte trage zuerst ein Suchbegriff ein.');
		}else{
			loadList('#events-list', '/windows/eventlist.php', '/veranstaltung', '/images/marker-event.png', 1, '');
			loadList('#portraits-list', '/windows/portraitlist.php', '/portrait', '/images/marker-portrait.png', 1, '');
		}
	});
	
	$('#google-map').gmap({ 
		center: new google.maps.LatLng(47.167172248, 9.3986688), 
		callback: loadCalendar(''), 
		zoom: 9,
		/*streetViewControl: false, 
		mapTypeControl: false, 
		navigationControl: false*/
	});
	
	
	katButton('#bildende');
	katButton('#darstellende');
	katButton('#gesellschaft');
	katButton('#sport-natur');
	
});

function katButton(id){
	$(id).click(function(){
		if ($(this).hasClass('active')){
			$(this).removeClass('active');
		}else{
			$(this).addClass('active');
		}
		loadList('#events-list', '/windows/eventlist.php', '/veranstaltung', '/images/marker-event.png', 1, '');
		loadList('#portraits-list', '/windows/portraitlist.php', '/portrait', '/images/marker-portrait.png', 1, '');
	});
}

