jQuery.noConflict();
(function($) {
	$.latToY = function(lat) {
		if (lat > 90) lat = lat - 180;
		if (lat < -90) lat = lat + 180;

		// conversion degree => radians
		phi = Math.PI * lat / 180;
		return 0.5 * Math.log((1 + Math.sin(phi)) / (1 - Math.sin(phi)));
	};
	$.latLngToXY = function(lat, lng) {
		var x = ((lng + 180) / 360) * 384;
		var y = 174 - (($.latToY(lat) + Math.PI) / (Math.PI)) * 194;
		return {'x':x, 'y':y};
	};
	$(document).ready(function() {
		//Afhandeling external link
		$('a[rel=external]').bind('click', function(evt){
			evt.preventDefault();
			window.open($(this).attr('href'), '_blank');
		});
		
		$('.tx-tfegallery-main-controller .albums .album').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('album-hover');
		});
		$('.news-list-item').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('news-list-item-hover');
		});
		
		
		$('#calendar-organizer-backlink a').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('hover');
		});
		$('#calendar-location-backlink a').bind('mouseenter mouseleave', function(){
			$(this).toggleClass('hover');
		});
		
		
		$('.print').bind('click', function(e){
			e.preventDefault();
			window.print();
		});
		var initSearchValue = $('label[for="sword"]').html();
		$('form#searchform').bind('submit', function() {
			if ($('input#sword', this).val() == initSearchValue) {
				$('input#sword', this).val('');
			}
		});
		$('form#searchform input#sword').bind('focus', function(evt) {
			if ($(this).val() == initSearchValue) {
				$(this).val('');
			}
		}).bind('blur', function(evt) {
			if ($(this).val() == '') {
				$(this).val(initSearchValue);
			}
		});
		if ($('input#sword').val() == '') {
			$('input#sword').val(initSearchValue);
		}
		
		Cufon.replace('h1');
		if ($('body').hasClass('home')) {
			if ($("#row1 .right > div").size() > 1) {
				$('#row1 .right').append('<a class="next" href="#"></a><a class="prev" href="#"></a>').cycle({
					slideExpr: '.csc-default',
					prev: '#row1 .right a.prev',
					next: '#row1 .right a.next',
					speed: 1000,
					timeout: 4000,
					pause: 1
				});
			}
			Cufon.replace('#row2 .left h2', {hover:true, hoverables:{h2:true}});
			
//			/* Plot some points on the map */
//			var offsetY = 149 - 10;
//			var offsetX = -13 - 6;
//			var points = [
//				[0, 0],
//				[52.486794,13.42163], // Berlin
//				[51.489593,-0.183106], // London
//				[48.766328,2.358397], // Paris
//				[40.390071,-3.742677], // Madrid
//				[40.702297,-74.029541] // New York
//			];
//			$(points).each(function() {
//				var latLng = $.latLngToXY(this[0], this[1]);
//				var point = $('<div class="point"></div>').appendTo('#row1 .points');
//				point.css({'top':latLng.y + offsetY, 'left':latLng.x + offsetX});
//			});
		}
		Cufon.replace('#content h2', {hover:true, hoverables:{h2:true}});
		Cufon.replace('.tx-tfeisbt-banner-controller .banner-home .more a', {hover:true});
		Cufon.replace('.tx-tfeisbt-banner-controller .banner-right .more a', {hover:true});
		
		// Home tabs
		$('#row2 .left .csc-header h2').bind('click', function(evt) {
			$('#row2 .left .csc-header').each(function() {
				$(this).next().hide();
				$('h2', this).removeClass('active');
			});
			$(this).parent().next().show();
			$(this).addClass('active');
			Cufon.refresh('h2');
		});
		$('#row2 .left .csc-default:first .csc-header h2').click();
		
		// home gallery
		$('.tx-tfegallery-main-controller .home-gallery .dir .photo a img').bind('mouseover', function(e){
			$(this).css('top', '-59px');
		});
		$('.tx-tfegallery-main-controller .home-gallery .dir .photo a img').bind('mouseout', function(e){
			$(this).css('top', '0px');
		});
		
		// Calendar
		if ($('.tx-cal-controller .month-small').length > 0) {
			$('.tx-cal-controller .month-small td.eventDay').bind('mouseenter', function() {
				$(this).addClass('hover');
			}).bind('mouseleave', function() {
				$(this).removeClass('hover');
			}).find('span a').bind('click', function(evt) {
				evt.preventDefault();
			});
		}
		
		// IE6 stuff
		if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
			$('.news-latest-container').hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});
		}

		//Archive periods
		$('.archive-filter ul.periods li a.inactive').click(function() {
			var id = $(this).attr("id");
			id = id.replace("period_","");
			$('.archive-filter #archive_period').val(id);
			$('.archive-filter').submit();
			return false;
		});
		$('.archive-filter ul.periods li a.active').click(function() {
			$('.archive-filter #archive_period').val('');
			$('.archive-filter').submit();
			return false;
		});

		//Archive periodgroups
		$('.archive-filter ul.periodgroups li a.closed').click(function() {
			$(this).toggleClass('closed');
			$(this).toggleClass('open');
			$(this).toggleClass('active');
			$('ul',$(this).parent()).toggle();
			return false;
		});
		$('.archive-filter ul.periodgroups li a.open').click(function() {
			$(this).toggleClass('active');
			$(this).toggleClass('open');
			$(this).toggleClass('closed');
			$('ul',$(this).parent()).toggle();
			return false;
		});
		$('.archive-filter ul.periodgroups li a.force-open').click(function() {
			return false;
		});

		//Archive categories
		$('.archive-filter ul.categories li a.inactive').click(function() {
			var id = $(this).attr("id");
			id = id.replace("category_","");
			$('.archive-filter #archive_category').val(id);
			$('.archive-filter').submit();
			return false;
		});
		$('.archive-filter ul.categories li a.active').click(function() {
			$('.archive-filter #archive_category').val('');
			$('.archive-filter').submit();
			return false;
		});
		
		
	});
})(jQuery);
