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

$(document).ready(function(){

	$("a").focus(function() {
		$(this).blur();
	});

	$("#password").clearfocus();

	$("footer#pageinformation").css({position: "absolute"});
	$("div#wrapper").css({ "background-image" : "url('http://www.draisin.de/images/project-elements/wrapper-bg.png')" });
	$("<div id='white-decoration'>&nbsp;</div><div id='curve-decoration'><img src='http://www.draisin.de/images/project-elements/curve-decoration.png' width='100%' style='position: absolute; left: 0px; top: 0px;' height='280' alt='' /></div>").appendTo("body");
	$("<div id='font-sizer'> Schriftgrösse: <a href='javascript:;' class='standard'>Normal</a> <a href='javascript:;' class='middle'>Medium</a> <a href='javascript:;' class='big'>Gross</a></div>").appendTo("div#branding");

	observeResizing();
	buildFontResizer();

	$('.jswarning').remove();
	$.get("/token/",function(txt){
	  $("form.secured").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});

});

// Cookie functions
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) + ";path=/" +
	((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1)
				c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function buildFontResizer(){

	// Schriftgröße Normal
	$("a.standard").click(function() {
		$("div#subcontent p, div#subcontent h3, div#maincontent p, div#maincontent h3, div#maincontent h4, label,, nav#subnavigation ul li a").css({'font-size' : '0.81em'});
		$("div#maincontent h2").css({'font-size' : '1.25em'});
		$("footer ul li a, nav#mainnavigation ul li a, nav#mainnavigation ul li span, nav#metanavigation ul li a").css({'font-size' : '0.88em'});
		setCookie('fontsize', $(this).attr('class') ,30);
		observeResizing();
	});

  	// Schriftgröße Mittel
	$("a.middle").click(function() {
		$("div#subcontent p, div#subcontent h3, div#maincontent p, div#maincontent h3, div#maincontent h4, label").css({'font-size' : '1.00em'});
		$("div#maincontent h2").css({'font-size' : '1.38em'});
		$("footer ul li a, nav#mainnavigation ul li a, nav#mainnavigation ul li span, nav#metanavigation ul li a, nav#subnavigation ul li a").css({'font-size' : '0.94em'});
		setCookie('fontsize', $(this).attr('class') ,30);
		observeResizing();
	});

  // Schriftgröße Groß
	$("a.big").click(function() {
		$("div#subcontent p, div#maincontent h4, div#subcontent h3, div#maincontent p, label").css({'font-size' : '1.13em'});
		$("div#maincontent h2").css({'font-size' : '1.50em'});
		$("div#maincontent h3").css({'font-size' : '1.06em'});
		$("footer ul li a, nav#mainnavigation ul li a, nav#mainnavigation ul li span, nav#metanavigation ul li a, nav#subnavigation ul li a").css({'font-size' : '1.00em'});
		setCookie('fontsize', $(this).attr('class') ,30);
		observeResizing();
	});

	if (getCookie('fontsize') != "") {
		var c = getCookie('fontsize');
		$("a." + c).trigger('click');
	}

}

function observeResizing(){
	setPageLayout();
	$(window).resize(function() {
		setPageLayout();
	});

	$(window).scroll(function() {
		setPageLayout();
	});
}

function setPageLayout(){



	var contentHeight = $("div#container").height();

	var windowheight = $(window).height();
	var alarmheight = windowheight - 280;

	if(alarmheight > (contentHeight - 240)){
		$("div#white-decoration,div#wrapper,html,body").height(windowheight);
		stickyFooterPosition = ($(window).scrollTop() + windowheight - $("#curve-decoration").height());
		$("#curve-decoration").css({position: "absolute",top: stickyFooterPosition + "px"});
	}else{

		$("div#white-decoration,div#wrapper,html,body").height(contentHeight + 40);
		stickyFooterPosition = contentHeight - 240;
		$("#curve-decoration").css({position: "absolute",top: stickyFooterPosition + "px"});
	}

	$("footer#pageinformation").css({left: "40px", top: (stickyFooterPosition + 210 ) + "px"});

	if($(window).width() < 1060){
		$("div#white-decoration").css({ left : "0px", "margin-left" : "0px" });
		$("div#curve-decoration img").css({ width : "1060px" });
	}else{ 
		$("div#white-decoration").css({ left : "50%", "margin-left" : "-530px" });
		$("div#curve-decoration img").css({ width : "100%" });
	}
}

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', contactform, false );

function feedbackform(){

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

  // Processing
  $( 'form.feedbackform' ).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.feedbackform' ).show().end();
}

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

function loginform(){

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

  // Processing
  $( 'form.loginform' ).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.loginform' ).show().end();

}
