$(document).ready(function() {
	
	var activeMainMenu=null;
	
	transformInputs = function() {
		$('[rel=defaultlegend]').each(function() {
			var that=$(this);
			that.data('value',that.val());
			that.focus(function() {
				if ($(this).val()==$(this).data('value')) $(this).val('');
				$(this).removeClass('blurred');
			});
			that.blur(function() {
				if ($(this).val()=='') $(this).val($(this).data('value'));
				$(this).addClass('blurred');
			});
		});
	}

	$('a[rel=ajax]').each(function() {
		
		var myParent=$(this).parent();
		if (myParent.hasClass('active')) activeMainMenu=myParent;
		
		var href=this.href;
		
		if (href.match(/\.(52|53|54|55|62|14|43)\.0\.html/)) return; 
			
		switch ($(this).text()) {
			case 'per Post':
				type = 307; break;
			case 'E-Mail':
				type = 303; break;
			case 'Rückruf':
				type = 305; break;
			case 'Ansprechpartner':
				type = 301; break;
			case 'PDF-Download':
				type = 309; break;
			default:
				type = 42; break;
				
		}
		$(this).data('type',type);
		
		this.href="javascript:void(0);";
		
		var id=href.match(/index\.php\?id=(\d+)/);
		if (id) {
			$(this).data('href',href+'&type='+type);
		} else if (href.match(/\d+\.html$/)) {
			$(this).data('href',href.replace(/\d+\.html$/,type+'.html'));
		};
		
		$(this).click(function() {
			if (activeMainMenu) activeMainMenu.removeClass('active');
			$(this).parent().addClass('active');
			
			titlePrefix=''
			switch ($(this).data('type')) {
				case 42: 
					xt_click(this,'F', '', $(this).attr('title'));
					break;
				case 307:
					titlePrefix = 'per Post';
				case 303:
					titlePrefix = titlePrefix || 'E-Mail';
				case 305:
					titlePrefix = titlePrefix || 'Rückruf';
				case 301:
					titlePrefix = titlePrefix || 'Ansprechpartner';
					title = titlePrefix + " "+window.document.title;
					xt_click(this,'F', '', title);
					if (window.console && console.log) console.log(title);
					break;					
			};
			
			var that=this;
			$.blockUI({message: '', css: {}});
			new $.ajax({
				url: $(this).data('href'), 
				success: function(html) {
					$.unblockUI();
					$('#message').remove();
					var menuoffset=$('#menu').offset();
					
					$('#overlaynavi').remove();
					$('body').append('<div id="overlaynavi" />');
					var container=$('#overlaynavi');
					container.html(html);
					container.append('<div id="closer"><img src="fileadmin/template/images/closer.png" border="0" id="closeoverlay"/></div>')
					container.css('left',(menuoffset.left+200)+'px');
					container.css('display','none');
					container.css('top',menuoffset.top+'px');
					
					if ($(that).data('type')==42) {
						if ($.browser.msie && $.browser.version.substr(0,1)=='6') 
						container.css('background-image','url(fileadmin/template/images/subnavi-background_top.gif)')
						else
						container.css('background-image','url(fileadmin/template/images/subnavi-background_top.png)')
					};
					//container.slideDown();
					$.blockUI({
						message: container, 
						centerX: false, 
						centerY: false, 
						css: {
							top: 0, 
							left: 0, 
							cursor: 'pointer',
							backgroundColor: 'transparent',
							border: 0,
							textAlign: 'left'
						},
						baseZ: 2000,
						fadeIn: 0,
						bindEvents: false
					});
					//container.replaceTextWithElement('plus', redsupplus);
					
					$('.blockUI.blockOverlay,#closeoverlay').click(function() {
						$(that).parent().removeClass('active');
						$.unblockUI();
						if (activeMainMenu) activeMainMenu.addClass('active');
					});
					
					transformInputs();
				}
			})
		});
		
	});
	
	$('marquee').marquee();
	
	var redsupplus = $(document.createElement('span'))
		.addClass('redsupplus')
	  	.html('plus');

		var dummy=0;
			
	if (dummy) $('body').replaceTextWithElement('plus', redsupplus);
	
	$('div.csc-frame-invisible').each(function() {
		
		var headline=$('.csc-header h1,.csc-header h2',this);
		var content=$('.sectioncontent',this);
		
		headline.data('content',content);
		headline.data('html',headline.html());
		headline.data('hidden',true);
		
		headline.html(headline.html()+' [+]');
		content.hide();
		
		headline.css('cursor','pointer');
		headline.click(function() {
			var headline=$(this);
			
			headline.data('content').slideToggle();
			
			var state=headline.data('hidden');
			if (state) {
				headline.data('hidden',false)
				headline.html(headline.data('html')+' [-]');
			} else {
				headline.data('hidden',true)
				headline.html(headline.data('html')+' [+]');
			};
		})
		
	})
	
	transformInputs();
	
	
	$('ul[rel=downloadtracking] a').each(function() {
		
		var that=$(this);
		that.data('href',this.href);
		
		that.click(function() {
			return xt_click(this, 'C', 'Level 2 ID', that.text(), 'T');
		});
		
		
	});
	
	
	
});

overlaysubmit = function(mode,form) {
	$('#ajax',form).val(1);
	$('[rel=defaultlegend]').each(function() {
		if ($(this).val()==$(this).data('value')) $(this).val('');
	});
	
	var query=$(form).serialize();
	$('[rel=defaultlegend]').each(function() {
		if ($(this).val()=='') $(this).val($(this).data('value'));
	});
	
	$.post(
		'form_dispatcher_ct.php',
		query, 
		function(data, textStatus)  {
			if (data.success) {
				$('.frage').hide();
				$('.antwort').show();
				$('#closerbutton').click(function() {$('#closeoverlay').click()});
			} else {
				$('*',form).each(function() { $(this).removeClass('error') });
				var errorfields=data.errors.split(/,/);
				$(errorfields).each(function() {
					$('[name='+this+']').parent().addClass('error');
				})
			};
		}
   	);
	return false;
}

function kontakt(wert){
    if (wert=='Kunde'){
        document.getElementById('kontakt_typ_label').firstChild.data='Nachname';
        document.getElementById('kontakt_typ_help').firstChild.data='Geben Sie bitte Ihren Nachnamen ein.';
    } else if (wert=='Interessent'){
        document.getElementById('kontakt_typ_label').firstChild.data='Nachname';
        document.getElementById('kontakt_typ_help').firstChild.data='Geben Sie bitte Ihren Nachnamen ein.';
    } else if (wert=='Arbeitgeber'){
        document.getElementById('kontakt_typ_label').firstChild.data='Betriebsnummer';
        document.getElementById('kontakt_typ_help').firstChild.data='Tragen Sie bitte hier Ihre Betriebsnummer ein.';
    } else if (wert=='Krankenhaus'){
        document.getElementById('kontakt_typ_label').firstChild.data='Krankenhausnummer';
        document.getElementById('kontakt_typ_help').firstChild.data='Bitte geben Sie Ihre Krankenhausnummer ein.';
    }

}
