function init_leftmenu() {
	jQuery('#line_top_leftmenu ul ul').each(function(){jQuery(this).css('left',jQuery(this).parent().outerWidth(true))});
	
	jQuery('#line_top_leftmenu > ul > li').bind('mouseenter',function(){
		if(jQuery(this).find('ul').length) {
			var elcount = jQuery(this).find('ul li').length;
			var eldelay;
			for(var i=0;i<elcount;i++) {
				eldelay = i * 100;
				jQuery(this).find('ul a').eq(i).stop().delay(eldelay).animate({marginTop:0},350,'easeOutQuad');
			}
		}
	});
	
	jQuery('#line_top_leftmenu > ul > li').bind('mouseleave',function(){
		if(jQuery(this).find('ul').length && !jQuery('body').hasClass('stick_leftmenu')) {
			var elcount = jQuery(this).find('ul li').length;
			var eldelay;
			for(var i=0;i<elcount;i++) {
				eldelay = i * 100;
				jQuery(this).find('ul a').eq(i).stop().delay(eldelay).animate({marginTop:-41},350,'easeOutQuad',function(){
					if(jQuery(this).parent().hasClass('last')) {
						jQuery(this).parent().parent().find('a').css('margin-top',-42);
					}
				});
			}
		}
	});
	
	jQuery('#line_top_leftmenu > ul > li.last > a').bind('click',function(){return false;});
	jQuery('#line_top_leftmenu > ul > li.last > a').bind('click',function(){
		var elcount = jQuery(this).parent().find('ul li').length;
		var eldelay;
		for(var i=0;i<elcount;i++) {
			eldelay = i * 100;
			jQuery(this).parent().find('ul a').eq(i).stop().delay(eldelay).animate({marginTop:0},350,'easeOutQuad');
		}
		
		jQuery('body').addClass('stick_leftmenu');
	});
	
	if(jQuery('#line_top_leftmenu li li.active').length) {
		jQuery('#line_top_leftmenu > ul > li.last > a').trigger('click');
	}
}

function footer_pos() {
	var lt_h = jQuery('#line_top').outerHeight(true);
	var lh_h = jQuery('#line_header').outerHeight(true);
	var lc_h = jQuery('#line_content').outerHeight(true);
	var lb_h = jQuery('#line_bottom').height();
	var w_h = jQuery(window).height();
	
	var content_h = lt_h + lh_h + lc_h + lb_h;
	
	if(content_h < w_h) {
		jQuery('#line_bottom').css('margin-top',w_h - content_h);
	}else{
		jQuery('#line_bottom').css('margin-top',0);
	}
}

function set_class_for_submit() {
	jQuery('input[type="submit"]').each(function(){
		jQuery(this).hide().after('<span class="input-submit-btn"><span></span></span>').next('.input-submit-btn').find('span').text(jQuery(this).val()).click(function(){
			jQuery(this).parent().prev('input[type="submit"]').click();
		});
	});
	jQuery('input[type="button"]').each(function(){
		if(jQuery('#aussteller').length) {
			if(jQuery('.tx_powermail_pi1_fieldwrap_html_submit').length) {
				jQuery(this).hide().after('<span class="input-submit-btn-back-last"><span></span></span>').next('.input-submit-btn-back-last').find('span').text(jQuery(this).val()).click(function(){
					jQuery(this).parent().prev('input[type="button"]').click();
				});
			}else{
				if(jQuery('.tx-powermail-pi1_fieldset_schritt1').length) {
					jQuery(this).hide().after('<span class="input-submit-btn-back hidden"><span></span></span>').next('.input-submit-btn-back').find('span').text(jQuery(this).val()).click(function(){
						jQuery(this).parent().prev('input[type="button"]').click();
					});
				}else{
					jQuery(this).hide().after('<span class="input-submit-btn-back"><span></span></span>').next('.input-submit-btn-back').find('span').text(jQuery(this).val()).click(function(){
						jQuery(this).parent().prev('input[type="button"]').click();
					});
				}
			}
		}else{
			jQuery(this).hide().after('<span class="input-submit-btn"><span></span></span>').next('.input-submit-btn').find('span').text(jQuery(this).val()).click(function(){
				jQuery(this).parent().prev('input[type="button"]').click();
			});
		}
	});
	Cufon.refresh();
}

function transform_contentbuttons() {
	jQuery('.contentbutton').each(function(){
		jQuery(this).html('<span>'+jQuery(this).html()+'</span>');
	});
}

/**
 *  Partner-Box
 */
function init_partnerslider() {
	if(jQuery('.partner-slidebox').length) {
		var arcounter = 0;
		var imgar = new Array();
		
		// Erst einmal alle Bilder herausfischen und in ein Array speichern
		jQuery('.partner-slidebox').find('img').each(function(){
			imgar[arcounter] = jQuery('.partner-slidebox').find('img').eq(arcounter).parent().html();
			arcounter++;
		});
		
		// Das ursprüngliche csc-Konstrukt überschreiben
		jQuery('.partner-slidebox').html('');
		for(var i=0;i<arcounter;i++) { jQuery('.partner-slidebox').append(imgar[i]); }
		
		// Bildklassen setzen (Nötig für Fallback, falls JS nicht aktiviert ist)
		jQuery('.partner-slidebox').children('img').addClass('partner-logo');
		
		// Animation initialisieren
		jQuery('.partner-slidebox').children('img:first').css('left',0).addClass('pl-active');
		window.setInterval('partnerslider_next()',4000);
	}
}
function partnerslider_next() {
	jQuery('.partner-slidebox').children('.pl-active').stop().animate({left:-300},600,'easeInOutQuad',function(){ jQuery(this).css('left',300); });
	if(jQuery('.partner-slidebox').children('.pl-active').next('img').length) {
		jQuery('.partner-slidebox').children('.pl-active').removeClass('pl-active').next('img').addClass('pl-active').stop().animate({left:0},600,'easeInOutQuad');
	}else{
		jQuery('.partner-slidebox').children('.pl-active').removeClass('pl-active');
		jQuery('.partner-slidebox').children('img:first').addClass('pl-active').stop().animate({left:0},600,'easeInOutQuad');
	}
}

function transform_pm_confirmpage_headline() {
	jQuery('.tx-powermail-pi1_confirmation').prepend('<div class="clear"></div>');
	jQuery('.tx-powermail-pi1_confirmation').prepend('<h3>'+jQuery('.tx-powermail-pi1_confirmation > p').html()+'</h3>');
	jQuery('.tx-powermail-pi1_confirmation > p').remove();
}

function transform_pm_errorpage_headline() {
	jQuery('.tx-powermail-pi1_mandatory').prepend('<div class="clear"></div>');
	jQuery('.tx-powermail-pi1_mandatory').prepend('<div class="mandatory_errors_hl">'+jQuery('.tx-powermail-pi1_mandatory > strong').html()+'</div>');
	jQuery('.tx-powermail-pi1_mandatory > strong').remove();
}
/**************************************************/
/*************** Eventlisterner *******************/
/**************************************************/
if (window.addEventListener) {
	document.addEventListener("DOMContentLoaded", _cm_onload_handler, false);
	window.addEventListener("resize", _cm_onresize_handler, false);
}
else if(document.attachEvent) {	
	window.attachEvent('onload', _cm_onload_handler);
	window.attachEvent('onresize', _cm_onresize_handler);
}

function _cm_onload_handler() {
	init_leftmenu();
	transform_contentbuttons();
	set_class_for_submit();
	init_partnerslider();
	//transform_pm_confirmpage_headline();
	transform_pm_errorpage_headline();
	_cm_wait_for_cufon();
	
	
	// Formular-Elemente ersetzen
	jQuery("input[type=text], input[type=radio], input[type=checkbox], input[type=password], input[type=email], input[type=file], select").uniform({
		fileDefaultText: 'Bitte w'+unescape("%E4")+'hlen',
		fileBtnText: 'Durchsuchen'
	});
	
	// Sollte als letzte Funktion aufgerufen werden
	footer_pos();
}

function _cm_onresize_handler() {
	footer_pos();
}

function _cm_wait_for_cufon() {
	if(jQuery('cufon').length) {
		footer_pos();
	}else{
		window.setTimeout("_cm_wait_for_cufon()",20);
	}
}
