$(document).ready(function() {
	
	// target simulation
	$('a')
		.filter(function() {
			return ! /^http:\/\/blog\.(local|olafschneider\.de)(\/|$)/.test(this.href);
		})
		.addClass('external')
		.click(function() {
			window.open(this.href);
			return false;
		})
	;

	// toggle sticky posts and save state in cookie
	$('.sticky').each(function() {
		var key = $(this).attr('id') + '-is-collapsed';
		if ($.cookie(key)) {
			$(this).addClass('collapsed');
		}

		$(this).find('span.hide-icon').click(function() {
			$(this).parent('.sticky').addClass('collapsed');
			$.cookie(key, true, {expires: 1000 });
		});
		
		$(this).find('span.show-icon').click(function() {
			$(this).parent('.sticky').removeClass('collapsed');
			$.cookie(key, null);
		});
	});
	
	// rewrite mail addresses
	$('.email').each(function() {
		var href = $(this).text().replace(' (at) ', '@').replace(' (dot) ', '.');
		$(this).html('<a href="mailto:' + href + '">' + href + '</a>');
	});
	
	// rewrite skype addresses
	$('.skype').each(function() {
		var txt = $(this).html();
		$(this).html('<a href="skype:' + txt + '">' + txt + '</a>');
	});

	// highslide

	hs.graphicsDir = '/wp-content/themes/nordwaerts/js/highslide/graphics/';
	hs.align = 'center';
	hs.transitions = ['expand', 'crossfade'];
	hs.outlineType = 'rounded-white';
	hs.fadeInOut = true;
	hs.numberPosition = 'caption';
	hs.dimmingOpacity = 0.75;
	hs.captionEval = '$(this.thumb).data("title")';
	
	hs.lang = {
	   cssDirection:     'ltr',
	   loadingText :     'Lade …',
	   loadingTitle :    'Klick zum Abbrechen',
	   focusTitle :      'Klick um nach vorn zu bringen',
	   previousText :    'Voriges Bild',
	   previousTitle :   'Voriges Bild (Pfeiltaste links)',
	   nextText :        'Nächstes Bild',
	   nextTitle :       'Nächstes Bild (Pfeiltaste rechts)',
	   closeText :       'Schließen',
	   closeTitle :      'Schließen (Esc)',
	   playText :        'Abspielen',
	   playTitle :       'Slideshow abspielen (Leertaste)',
	   pauseText :       'Pause',
	   pauseTitle :      'Pausiere Slideshow (Leertaste)',
	   number :          'Bild %1 von %2',
	   restoreTitle :    'Klick um das Bild zu schließen, klick und ziehe um zu verschieben. Benutze Pfeiltasten für vor und zurück.'
	};

	// Add the controlbar
	if (hs.addSlideshow) {
		hs.addSlideshow({
			interval: 5000,
			repeat: false,
			useControls: true,
			fixedControls: 'fit',
			overlayOptions: {
				opacity: .75,
				position: 'bottom center',
				hideOnMouseOut: true
			}
		});
	}
	
	hs.registerOverlay({
		html: '<div class="closebutton" onclick="return hs.close(this)" title="' + hs.lang.closeText + '"></div>',
		position: 'top right'
	});
	
	$(document).ready(function() {
	   $('a:has(img), a[rel=lightbox]').addClass('highslide').each(function() {
	      this.onclick = function() {
	         return hs.expand(this);
	      };
	   });
	});
	
	$().addClass('highslidelink').live('click', function(e) {
		e.preventDefault();
		hs.expand(this);
	});
	
	// pairs
	
	var output = $('.pairs-output:eq(0)');
	
	$(document).bind('pairs:start', function(e, data) {
		output.text('Um mit dem Spielen anzufangen, einfach auf die Quadrate klicken und alle Paare finden.');
	});

	$(document).bind('pairs:end', function(e, data) {
		output.html('Hurra, alle Lebensmittel sind aufgedeckt. Guten Appetit!');
	});

	$(document).bind('pairs:active', function(e, data) {
		var img = data.item.children('img');
		output.text(img.attr('title'));
	});

	$('.pairs:eq(0)').pairs();
	
	// panorama
	
	$('.panorama img').panorama();
	
	// twitter
	
	$('.twitter-box div').tweet({
		username: 'nordwaerts',
		count: 5,
		loading_text: 'lade Tweets …'
	});
	
	// better tooltip (alpha stage)
	// @todo lazy
	
	$('img[title]').each(function() {
		var $this = $(this);
		var timeout;
		var tooltip = $('<p />')
			.addClass('tooltip')
			.text(this.title)
			.css('width', $this.width() - 20) // 20 = padding
			.hide()
			.appendTo('body')
		;
		tooltip.css('top', $this.offset().top + $this.height() - tooltip.height());
		$this.data('title', this.title); // for highslide
		this.title = '';
		
		$this.add(tooltip).hover(function(e) {
			if (timeout) {
				window.clearTimeout(timeout);
			}
			if (this.nodeName == 'IMG') {
				tooltip.css('left', $this.offset().left).fadeIn();
			}
		}, function(e) {
			timeout = window.setTimeout(function() {
				tooltip.fadeOut();
			}, 500);
		});
	});
	
	// easter egg: style switch
	
	var headerVersion = parseInt($('#header').attr('class').replace('v', ''), 10);
	var currentHeaderVersion = headerVersion;
	
	$('#home-link').click(function(e) {
		if (e.metaKey) {
			currentHeaderVersion--;
			if (currentHeaderVersion < 0) {
				currentHeaderVersion = headerVersion;
			}
			$('#header').attr('class', 'v' + currentHeaderVersion);
			e.preventDefault();
		}
	});

});

