$.fn.clearfocus = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

/**
 * File Input Fix
 *
 * @author Jann Glasmachers  <glasmachers@gimmixx.de>
 * @copyright 2007 Jann Glasmachers
 */
fixFileInputSize = function (element, reachableWidth){
	if(element.style.width != 'auto'){
		element.style.width = 'auto';
	}

	while (element.offsetWidth < reachableWidth){
		element.size += 1;
	}

	element.size -= 1;
};

$(document).ready(function(){
	$("a").focus(function() {
		$(this).blur();
	});
	
	Cufon.now();
	Cufon.replace('h2');
	Cufon.replace('#notruf');
	Cufon.replace('h3');

	$('a.printbutton').click( function() {
        window.open( $(this).attr('href'),this,'width=655,height=700,scrollbars=yes' );
        return false;
    });

    jQuery("#movie-unternehmen").tubeplayer({
		width: 640,
		height: 457, 
		showControls: 1, 
		showRelated: 0, 	
		allowFullScreen: "true", 
		initialVideo: "AsX6fgbzALU", 
		preferredQuality: "default"
	});
		
	$('#movie-unternehmen a.playmovie').click(function(event){
		event.preventDefault();
		$('a.playmovie').hide();
		jQuery("#movie-unternehmen").tubeplayer("play");
	});
	
	jQuery("#movie-fahrradpimp").tubeplayer({
		width: 950,
		height: 567, 
		showControls: 1, 
		showRelated: 0, 	
		allowFullScreen: "true", 
		initialVideo: "FZ9YiN3jV7Q", 
		preferredQuality: "default"
	});
	
	jQuery("#movie-marketing").tubeplayer({
		width: 950,
		height: 567, 
		showControls: 1, 
		showRelated: 0, 	
		allowFullScreen: "true", 
		initialVideo: "NupSTUXIhmM", 
		preferredQuality: "default"
	});
	
	$('#movie-fahrradpimp a.playmovie').click(function(event){
		event.preventDefault();
		$('a.playmovie').hide();
		jQuery("#movie-fahrradpimp").tubeplayer("play");
	});
	
	jQuery("#movie-nanoversiegelung").tubeplayer({
		width: 640,
		height: 390, 
		showControls: 1, 
		showRelated: 0, 	
		allowFullScreen: "true", 
		initialVideo: "WKPRpaAGxa8", 
		preferredQuality: "default"
	});
		
	$('#movie-nanoversiegelung a.playmovie').click(function(event){
		event.preventDefault();
		$('a.playmovie').hide();
		jQuery("#movie-nanoversiegelung").tubeplayer("play");
	});

    $('.jswarning').remove();
	$.get("/token/",function(txt){
	  $("form.secured").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
    
    var parentnode = $("nav#mainnavigation > ul > li.foldout");

		
		parentnode.hover(
			function(){
				
				$(this).children("ul").show();
				if( !parentnode.hasClass("active") ){
	
					if(parentnode.hasClass("leistungen")){
						$(this).find("a.leistungen").css({ "background-position" : "-337px -45px" });
					}
					
					if(parentnode.hasClass("aktuelles")){
						$(this).find("a.aktuelles").css({ "background-position" : "-235px -45px" });
					}
				}
			},
			function(){
				
				$(this).children("ul").hide();
				
				if( !parentnode.hasClass("active") ){
					if(parentnode.hasClass("leistungen")){
						$(this).find("a.leistungen").css({ "background-position" : "-337px 0px" });
					}
					
					if(parentnode.hasClass("aktuelles")){
						$(this).find("a.aktuelles").css({ "background-position" : "-235px 0px" });
					}
				}
				
			}
		);
		
	

    $("form.callbackform input#clastname").clearfocus();
	$("form.callbackform input#cemail").clearfocus();
	$("form.callbackform input#cphone").clearfocus();
	$("form.callbackform input#cmessage").clearfocus();

	// Fix File Upload Length except safari
	if((!$.browser.webkit) && (!$.browser.safari)){
		for (var x = 0; x < document.forms.length; ++x){
			/*
			document.forms[x].onsubmit = function (){
				showLoading();
			}
			*/
			// Alle Form-Elemente des jeweiligen Form-Tags
			for (var y = 0; y < document.forms[x].elements.length; ++y) {
			// Nur Form-Elemente mit dem Type="file"
				if (document.forms[x].elements[y].type == 'file'){
					// Fix
					
					fixFileInputSize(document.forms[x].elements[y], 680);
				}
			}
		}
	}

});

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', contactform, false );

function contactform(){

  // Hide forms
  $( 'form.contactform' ).hide().end();

  // Processing
  $( 'form.contactform' ).find( 'ol.blockstyle>li>label' ).not( '.nogx' ).each( function( i ){
    var labelContent = this.innerHTML;


    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );

    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = "";
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( 'form.contactform' ).show().end();
}

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', callbackform, false );

function callbackform(){

  // Hide forms
  $( 'form.callbackform' ).hide().end();

  // Processing
  $( 'form.callbackform' ).find( 'ol.blockstyle>li>label' ).not( '.nogx' ).each( function( i ){
    var labelContent = this.innerHTML;


    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );

    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = "";
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( 'form.callbackform' ).show().end();
}
