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

/*
 * Image Tooltip
 *
 */
this.screenshotPreview = function(){

	/* CONFIG */
	var xOffset = -40;
	var yOffset = 40;
	var elementBorder = 20;
	var rightX = 0;
	var rightY = 0;
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();

	$(window).bind("resize", function(){
		windowWidth  = $(window).width();
		windowHeight = $(window).height();
	});

	/* END CONFIG */
	$("area.screenshot").mouseover(function(e){
		$("body").append("<p id='screenshot'><img src='http://www.dr-kuntsch.de/images/de/praxisraeume/"+ this.id +".jpg' alt='' /></p>");

		rightX = (e.pageX + $("#screenshot").width() - xOffset + elementBorder);
		rightY = (e.pageY + $("#screenshot").height() + yOffset + elementBorder);

		if((rightY - $(window).scrollTop())  > windowHeight && (rightX - $(window).scrollLeft()) > windowWidth){

			$("#screenshot")
			.css("left",(e.pageX - $("#screenshot").width() + xOffset ) + "px")
			.css("top",(e.pageY - $("#screenshot").height() - yOffset ) + "px")
			.fadeIn("fast");

		}else if( (rightX - $(window).scrollLeft() - xOffset) > windowWidth) {

			$("#screenshot")
			.css("left",(windowWidth - $(window).scrollLeft() - $("#screenshot").width() + xOffset - elementBorder ) + "px")
			.css("top",(e.pageY + yOffset ) + "px")
			.fadeIn("fast");

		}else if((rightY - $(window).scrollTop() + yOffset) > windowHeight){

			$("#screenshot")
			.css("left",(e.pageX - xOffset ) + "px")
			.css("top",(windowHeight - $(window).scrollTop() - $("#screenshot").height() - yOffset - elementBorder ) + "px")
			.fadeIn("fast");

		} else{
			$("#screenshot")
			.css("left",(e.pageX - xOffset ) + "px")
			.css("top",(e.pageY + yOffset ) + "px")
			.fadeIn("fast");
		}
    });


	$("area.screenshot").mouseout(function(e){
		$("#screenshot").remove();
    });

	$("area.screenshot").mousemove(function(e){
		rightX = (e.pageX + $("#screenshot").width() - xOffset + elementBorder);
		rightY = (e.pageY + $("#screenshot").height() + yOffset + elementBorder);

		if((rightY - $(window).scrollTop()) > windowHeight && (rightX - $(window).scrollLeft()) > windowWidth){

			$("#screenshot")
			.css("left",(e.pageX - $("#screenshot").width() + xOffset ) + "px")
			.css("top",(e.pageY - $("#screenshot").height() - yOffset ) + "px");

		}else if( (rightX - $(window).scrollLeft() - xOffset) > windowWidth) {

			$("#screenshot")
			.css("left",(windowWidth - $(window).scrollLeft() - $("#screenshot").width() + xOffset - elementBorder ) + "px")
			.css("top",(e.pageY + yOffset ) + "px");


		}else if( (rightY - $(window).scrollTop() + yOffset)  > windowHeight){

			$("#screenshot")
			.css("left",(e.pageX - xOffset ) + "px")
			.css("top",(windowHeight + $(window).scrollTop() - $("#screenshot").height() - yOffset - elementBorder ) + "px");

		}else{
			$("#screenshot")
			.css("left",(e.pageX - xOffset ) + "px")
			.css("top",(e.pageY + yOffset ) + "px");

		}
	});
};

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

	$("#newsletteremail").clearfocus();
	$("#newslettername").clearfocus();

	$("form.callbackform input#lastname").clearfocus();
	$("form.callbackform input#email").clearfocus();
	$("form.callbackform input#phone").clearfocus();
	$("form.callbackform input#message").clearfocus();

	screenshotPreview();



});

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

function contactform(){

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

  // Processing
  $( 'form.contactform' ).find( 'ol>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();
}
