$(document).ready( function() {
   
   PEPS.rollover.init();
   
   var $ml = "-39px"; var $mr = "35px";
   var $mlh = "-29px"; var $mrh = "25px";

// Hide Gap and current Submenu on Entering the Menu, Show them again on leaving it
   $("div#menu > ul").hoverIntent( function() {
		//$("#submenu > ul").hide();
		//$("#menu_cut").animate( { height: "34px" }, 500);
	}, function() {
		$("#menu_cut").animate( { height: "152px" }, 200, 0);
		$("div.current > ul").slideDown(200);
	});

   $("#menu > ul > li:not(li.current)").hoverIntent( function() {
		$("div.current > ul").hide();
		$("#menu_cut").css( "height", "34px");
		$(this).children("div.sub_hover").slideDown(200);
		$ml = "-24px"; $mr = "20px";
		$mlh = "-14px"; $mrh = "10px";
	}, function() {
		$(this).children("div.sub_hover").stop(true, true).hide();
		$ml = "-39px"; $mr = "35px";
		$mlh = "-29px"; $mrh = "25px";
	});
   
   $("#menu > ul > li.current").hoverIntent( function() {
		$("#menu_cut").animate( { height: "152px" }, 200, 0);
		$("div.current > ul").slideDown(200);
	}, function() {
		//$("#submenu > ul").hide();
		//$("#menu_cut").animate( { height: "34px" }, 500);
	});
   
   $("li.current_page_item > a > img").css("margin", "0 " + $mrh + " 0 " + $mlh);
   $("li.current_page_item > a > img").css("opacity", "1");
   $("li.current_page_item > a > img").show();
   
   $("li.current_page_item > a").hover(
		function() { 
			$(this).children("img").css("margin", "0 " + $mr + " 0 " + $ml);
			$(this).children("img").css("opacity", "0");
			$(this).children("img").animate( { marginRight: $mrh, marginLeft: $mlh, opacity: "1" }, 300);
		}, function() { 
			$(this).children("img").css("margin", "0 " + $mrh + " 0 " + $mlh);
			$(this).children("img").css("opacity", "1");
	});
   
   $("li.page_item:not(li.current_page_item) > a").hover(
		function() { 
			$(this).children("img").show();
			$(this).children("img").animate( { marginRight: $mrh, marginLeft: $mlh, opacity: "1" }, 200);
		}, function() { 
			$(this).children("img").stop(true, true).hide();
			$(this).children("img").css("margin", "0 " + $mr + " 0 " + $ml);
			$(this).children("img").css("opacity", "0");
	});
   
   $("a.flink").hover(
		function() { $("div.fhover").slideDown(300); },
		function() { $("div.fhover").slideUp(300); }
	);
   
   $("#kontakt").click( function() {
		if ($(this).hasClass("clicked")) {
			$("#info_kontakt").slideUp(200, function() {
				$("#info_begin").hide();
				$("#footer").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_bl.png) left no-repeat');
				$("#footer_br").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_br.png) right no-repeat');
				$("#kontakt").css('background', 'none');
				$("#info_end").hide();
			});
			$(this).removeClass("clicked");
		} else {
			$(this).css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/f_kontakt_bg.png)');
			if ($("#impressum").hasClass("clicked")) {
				$("#impressum").css('background', 'none')
					.removeClass("clicked");
				$("#info_impressum").slideUp(200, function() { $("#info_kontakt").slideDown(1000); });
			} else {
				$("#info_begin").show();
				$("#footer").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_bl_info.png) left no-repeat');
				$("#footer_br").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_br_info.png) right no-repeat');
				$("#info_end").show();
				$("#info_kontakt").slideDown(1000);
			}
			$(this).addClass("clicked");
		}
	});
   
   $("#impressum").click( function() {
		if ($(this).hasClass("clicked")) {
			$("#info_impressum").slideUp(200, function() {
				$("#info_begin").hide();
				$("#footer").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_bl.png) left no-repeat');
				$("#footer_br").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_br.png) right no-repeat');
				$("#impressum").css('background', 'none');
				$("#info_end").hide();
			});
			$(this).removeClass("clicked");
		} else {
			$(this).css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/f_impressum_bg.png)');
			if ($("#kontakt").hasClass("clicked")) {
				$("#kontakt").css('background', 'none')
					.removeClass("clicked");
				$("#info_kontakt").slideUp(200, function() { $("#info_impressum").slideDown(1000); });
			} else {
				$("#info_begin").show();
				$("#footer").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_bl_info.png) left no-repeat');
				$("#footer_br").css('background', 'url(http://localhost/ispor/wp-content/themes/ispor/images/foot_br_info.png) right no-repeat');
				$("#info_end").show();
				$("#info_impressum").slideDown(1000);
			}
			$(this).addClass("clicked");
		}
	});
   
   $(".field").hover( 
		function() { $(this).css("border", "1px solid #7ca8bc"); },
		function() { $(this).css("border", "1px solid #eeeeee"); }
	);
   
   $('.top').click( function () {
		$.scrollTo('#header', 1000);
   });
  
});

PEPS = {};

PEPS.rollover = {
   init: function() {
      this.preload();
     
      $(".ro").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function() {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src ) {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_h' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src ) {
      return src.replace(/_h\./, '.');
   }
};

function clearText(thefield) {
	if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
	if (thefield.value=="") { thefield.value = thefield.defaultValue }
}

$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}