/* global variables */
var TIMEOUT = false;

var CX_FADE = 2000;
var CX_WAIT = 4000;
var CX_HEIGHT = 200;

function op_lang(l) {
	var href = location.href;
	location.href = '/' + l + '/' + href.replace(/https?:\/\/[^\/]+\/((de|en|fr|pl)\/?)?/, '');
	return false;
}

function ap_crossfade() {
	$clone = $('img.crossfade.clone').stop();
	$clone.animate({ opacity: $clone.css('opacity') == 0 ? 1 : 0 }, CX_FADE, "swing", function() { setTimeout(ap_crossfade, CX_WAIT); });
	
	//$next = $orig.siblings(':first');
	/*
	if ($orig.is(':visible')) {
		$next.css({ marginTop: '-' + CX_HEIGHT + 'px', padding: 0 }).fadeIn(CX_FADE);
		$orig.fadeOut(CX_FADE, function() { $next.css({ marginTop: 0 }); setTimeout(ap_crossfade, CX_WAIT); });
	}
	else {
		$next.css({ marginTop: '-' + CX_HEIGHT + 'px', padding: 0 }).fadeOut(CX_FADE);
		$orig.fadeIn(CX_FADE, function() { $next.css({ marginTop: 0 }); setTimeout(ap_crossfade, CX_WAIT); });
	}*/
	//$orig.stop().animate({ opacity: 1 });
}

function ap_deactivate() {
	$('#jobs li.active').removeClass('active').next().slideUp(function() { $(this).removeClass('active-content') }).queue('fx');
}

function ap_activate() {
	$this = $(this);
	if ( ! $this.hasClass('active')) {
		ap_deactivate();
		$this.addClass('active').next().addClass('active-content').slideDown().queue('fx');
	}
	if (TIMEOUT) { clearTimeout(TIMEOUT); TIMEOUT = false; }
}

$(function() { // executed when DOM is ready
	
	// set correct style-xxx class
	$body = $('body');
	bclass = $body.attr('class');
	if (bclass.match(/_pr$/)) $body.addClass('style-pr');
	else if (bclass.match(/_aim$/)) $body.addClass('style-aim');
	else if (bclass.match(/_es$/)) $body.addClass('style-es');
	else if (bclass.match(/_ng$/)) $body.addClass('style-ng');
	else if ($body.hasClass('about') && LAST_BLOG) {
		if (LAST_BLOG.match(/_pr$/)) $body.addClass('style-pr');
		else if (LAST_BLOG.match(/_aim$/)) $body.addClass('style-aim');
		else if (LAST_BLOG.match(/_es$/)) $body.addClass('style-es');		
		else if (LAST_BLOG.match(/_ng$/)) $body.addClass('style-ng');
	}
	
	// fix sidebar
	$('#sidebar li:last-child').addClass('last');
	
	// disable ac
	$('#quick_query').attr('autocomplete', 'off');
	
	// setup meta-dropdowns
	$('.meta-options').hide();
	$('#meta-navigation .meta-label a[rel]').click(function() {
		var p = $(this).parent().position();
		var w = $(this).parent().width();
		$meta = $('#meta-' + $(this).attr('rel'));
		
		$('#meta-navigation .meta-dropdown').each(function() {
			if ($(this).find('#' + $meta.attr('id')).length > 0) {
				$(this).toggleClass('active');
			}
			else {
				$(this).removeClass('active').find('.meta-options').hide();
			}
		})
		
		$meta.css({ top: (p.top-1) + 'px', left: (p.left+w+1-$meta.width()) + 'px'}).slideToggle(250).queue('fx-meta');
		return false;
	});

	// only applied to index/startpage
	if ($body.hasClass('index')) {	
		// speed-up animations -> include pre-calculated height
		$('#jobs dl').each(function() { $(this).css({ height: $(this).height() + 'px' }); });
	
		// setup accordion
		$('#jobs li:even:not(.ignore)').hide();
		$('#jobs li.head').mouseover(ap_activate);		
		$('#jobs').mouseleave(function() { TIMEOUT = setTimeout(ap_deactivate, 750); });
		
		// fix line-height for .loc
		$('#sidebar li .loc').each(function() { $(this).css({ lineHeight: $(this).parent().height() + 'px' }); });

		// setup crossfade
		$('img.crossfade').each(function() {
			$this = $(this).addClass('original');
			$this.clone().css({ position: 'absolute', top: 0, opacity: 0 }).addClass('clone').attr('src', $this.attr('src').replace(/_1\.jpg$/, '_2.jpg')).appendTo($this.parent());
		});
		setTimeout(ap_crossfade, CX_WAIT);
		
		// fix height of three .box'es
		var mheight = 0;
		$('.box').each(function() { $this = $(this); if ($this.height() > mheight) mheight = $this.height(); });
		$('.box').css({height:mheight + 'px'});
	}
	else {
		$('#sidebar li').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
	}
});

