var sMenu = {};
var sHightlight;

function checkMenuClose() {
	for (var i in sMenu) {
		if (!sMenu[i]) {
			$('#subnav-'+i).css({ display: 'none' });
			$('#nav-link-'+i).removeClass('hover');
			if (i == sHighlight)
				$('.nav-hover[rel='+i+']').mouseleave()
		}
	}
}

var headerFadeIndex;
var headerImages;

function headerFade() {
	headerFadeIndex = (headerFadeIndex + 1) % headerImages.length;
	var newImg = headerImages.eq(headerFadeIndex);
	var destCol = newImg.hasClass('h1white') ? 'FFFFFF' : '000000';
	var colorMap = {
		'rgb(0, 0, 0)': '000000',
		'rgb(255, 255, 255)': 'FFFFFF',
		'rgb(0,0,0)': '000000',
		'#000000': '000000',
		'rgb(255,255,255)': 'FFFFFF'
	};
	$('h1').jFade({
		property:'color', 
		start:colorMap[$('h1').css('color')],
		end:destCol, 
		duration:50,
		steps:30
	});
	newImg
		.css('opacity', 0)
		.appendTo($('#headerrotation'))
		.animate({opacity:1}, 1500);
}

// http://james.padolsey.com/javascript/jquery-delay-plugin/
$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

function mybuild() {
	//var as = $('a', $(this));
	var as = $(this).find('.subnav-fg').children();
	as.css('opacity', 0);
	as.each(function(i) {
		$(this).delay(i*50).animate({opacity: 1}, 300);
	});
}

var currentTab;

jQuery(document).ready(function() {
	
	$('.subnav').each(function() { this.mybuild = mybuild; });
	
	$('#nav-links div[class!="lang"]').mouseenter(function() {
		$('.nav-hover').mouseleave();
		var $a = $('a', $(this));
		var $div = $(this);
		$a.addClass('hover');
		var cleanID = $a.attr('id').replace(/nav\-link\-/, '');
		
		// insert hover hightlight
		var p = $div.position();
		var hov = $('<div class="nav-hover" rel="'+cleanID+'"></div>')
			.insertAfter('#nav-bg')
			.css({
				left: p.left,
				width: $div.width() + 2 * parseInt($div.css('paddingLeft').substr(0, 2))
			})
			.mouseleave(function() { $(this).remove(); });
		sHighlight = cleanID;
		
		// show subnav if appropriate
		if (cleanID) {
			var s = $('#subnav-'+cleanID);
			if (s.length) {
				if (cleanID == 'links') {
					s.css({
						display: 'block',
						left: p.left + hov.width() - s.width()
					}).get(0).mybuild();
				} else {
					s.css({
						display: 'block',
						left: p.left
					}).get(0).mybuild();
				}
			}
		}
		
		sMenu[cleanID] = sMenu[cleanID] ? sMenu[cleanID] + 1 : 1;
	}).mouseleave(function() {
		sMenu[sHighlight]--;
		window.setTimeout('checkMenuClose()', 10);
		$('a', $(this)).removeClass('hover');
	});

	$('.subnav').mouseenter(function() {
		var cleanID = $(this).attr('id').replace(/subnav\-/, '');
		sMenu[cleanID]++;
	})
	.mouseleave(function() {
		var cleanID = $(this).attr('id').replace(/subnav\-/, '');
		sMenu[cleanID]--;
		window.setTimeout('checkMenuClose()', 10);
	});
	
	// initialize headerrotation
	headerImages = $('#headerrotation img');
	headerFadeIndex = headerImages.length - 1;
	if (headerImages.length > 1)
		window.setInterval('headerFade()', 6000);
	
	// set tab handlers
	$('.tabs .t').mouseenter(function() {
		$(this).addClass('hover');
	}).mouseleave(function() {
		$(this).removeClass('hover');
	}).click(function() {
		var idx = $(this).prevAll().length;
		
		// on "bewertungen", follow the Google link if clicked
		if (idx == 0 && $(this).closest('.tabs').hasClass('bewertungen')) {
			return true;
		}
		
		$('.tabs .t').removeClass('active').eq(idx).addClass('active');
		currentTab.animate({opacity:0}, {duration:500, complete:function(){ $(this).css('display', 'none');}});
		currentTab = $('.tab').eq(idx);
		currentTab.css('display', 'block').animate({opacity:1}, 500);
		
		// replace a with iframe, if necessary
		var link = currentTab.find('a.placeholder');
		if (link.length) {
			link.replaceWith($('<iframe />').attr({src: link.attr('href'), style: link.attr('style')}));
		}

		// display loading animation, if present
		$('#tabcontainer .loading').show().delay(3000).fadeOut();
		
		// modify header image, if required
		if ($('body.career').length) {
			var newImg = new Image();
			newImg.src = [
				"/images/karriere/karriere_dental.jpg",
				"/images/karriere/karriere_dental.jpg",
				"/images/karriere/karriere_zmf.jpg",
				"/images/karriere/karriere_zahn.jpg"
			][idx];
			$(newImg)
				.appendTo($('#headerrotation'))
				.css('opacity', 0)
				.animate({opacity:1}, 1500);
		}
		
		$('#tabcontainer').css('height', currentTab.height()+'px');
		
		var pics = $('.pics img');
		if (pics.length)
			pics.fadeOut(500).eq(idx).fadeIn(500);
			
		return false;
	});
	$('.tab').css('position', 'absolute');
	currentTab = $('#tabcontainer .tab-active');
	$('.tab').not('#tabcontainer .tab-active').css('display', 'none');
	$('#tabcontainer').css('height', currentTab.height());
	
	// gallery
	if ($('a.colorbox').length) {
		$('a.colorbox').colorbox({
			speed: 1000,
			opacity:0.7,
			slideshow: true,
			slideshowAuto: false,
			current: '',
			previous: '',
			next: '',
			close: ''
		});
		
		// IE6 PNG patch
		$('#cboxPrevious').append('<div></div>');
		$('#cboxNext').append('<div></div>');
		$('#cboxClose').append('<div></div>');
	}
});

function colorbox_close() {
	$.fn.colorbox.close();
}

