var searchFocus = false,
	scrollUpButton = false;

$(document).ready( function () {
		
		/* Cufon */
		if (typeof(window.Cufon) == "function") {
		  Cufon.now();
			
			if ($('body').hasClass('dark')) {
			  $("h1").css({ fontSize: "250%" });
			  $("h2").css({ fontSize: "220%" });
		    $("h3").css({ fontSize: "150%" });
			  $("#nav li a, #meta li a").css({ fontSize: "120%" });
		    
		    Cufon.set('fontFamily', 'ColaborateLight');
  			Cufon.replace('h1', { hover: true });
  			Cufon.replace('h2', { hover: true });
  			Cufon.replace('h3', { hover: true });
  			Cufon.replace('#nav li a', { hover: true });
  			Cufon.replace('#meta li a', { hover: true });
		  }
		  else if ($('body').hasClass('leaguescript')) {
		    Cufon.set('fontFamily', 'League Script Thin');
  			Cufon.replace('h1', { hover: true });
  			Cufon.replace('h2', { hover: true });
  			
		    Cufon.set('fontFamily', 'ColaborateLight');
  			Cufon.replace('h3', { hover: true });
  			Cufon.replace('#nav li a', { hover: true });
      }
		  else if ($('body').hasClass('blackout')) {
		    Cufon.set('fontFamily', 'Blackout');
  			Cufon.replace('h1', { hover: true });
  			Cufon.replace('h2', { hover: true });
  			Cufon.replace('h3', { hover: true });
  			Cufon.replace('#nav li a', { hover: true });
  			
  			Cufon.set('fontFamily', 'Blackout 2AM');
  			Cufon.replace('#sidebar .box h3', { hover: true });
  			Cufon.replace('.read-more');
  			Cufon.replace('.older-posts a');
  			Cufon.replace('.newer-posts a');
      }
      else {
  			Cufon.replace('h1', { hover: true });
  			Cufon.replace('h2', { hover: true });
  			Cufon.replace('h3', { hover: true });
  			Cufon.replace('#nav li a', { hover: true });
  			Cufon.replace('#meta li a', { hover: true });
      }
		}
		
		/* Menu */
		$('#meta, #nav').superfish({ delay: 0 });
		
		/* Columns - Meta */
		$(".col33, .col50, .col66").hover( function () {
					$(this).find(".meta").fadeIn('slow');
				}, function () {
					$(this).find(".meta").stop(true, true).fadeOut();
			});
		
		$(".col50:odd").css({ marginRight: "0" });

		/* Give Feedback when hovering the follow-icons */
		$(".follow li a").hover( function () {
				$(this).append('<div class="overlay"></div>');
				$(this).find(".overlay").css({ opacity: 0 }).animate({ opacity: 0.3 });
			},
			function () {
		    $(this).find(".overlay").stop(true, true).animate({ opacity: 0 }, function () { $(this).remove(); });
		  });
			
		/* Give Feedback when hovering the buttons */
		$('.read-more, .older-posts a, .newer-posts a').hover( function () {
				$(this).append('<div class="overlay"></div>');
				$(this).find(".overlay").css({ opacity: 0 }).animate({ opacity: 0.3 });
			}, function () { $(this).find(".overlay").stop(true, true).animate({ opacity: 0 }, function () { $(this).remove(); }); });

		$('input[type="submit"]').hover( function () {
				if (! $(this).parents().hasClass("search")) $(this).animate({ opacity: 0.7 });
				else $(this).animate({ opacity: 0.4 });
			}, function () {
				if (! $(this).parents().hasClass("search")) $(this).stop(true, true).animate({ opacity: 1 });
				else $(this).stop(true, true).animate({ opacity: 0 });
			});
		
		/* Search: stay active if clicked */
		$('.search input[type="text"]').click( function () {
				$(this).addClass("active");
				if ($(this).val() == $(this).attr('title')) $(this).val("");
				searchFocus = true;
			});
		
		$('.search input[type="text"]').blur( function () {
				$(this).removeClass("active");
				if ($(this).val() == "") $(this).val($(this).attr('title'));
				searchFocus = false;
			});
			
			
		/* Scroll to Top */
		$("#scrollUpButton").click(function () {
				$("html, body").animate({scrollTop:0}, "slow");
			});
			
		/* Slider-Nav Position */
		$(".nivo-controlNav").css({ left: 350 - $(this).width() + "px" });
		
		/* Gallery and Images */
		if (!$.browser.msie) {
			var bg_src;
			$("#content img").each( function () {
					bg_src = $(this).attr("src");
					$(this).css("visibility", "hidden").wrap('<span class="image-wrap">');
					$(this).parents(".image-wrap").css("background", "url(" + bg_src + ") no-repeat 0 0");
				});
		}
					
        if (typeof(window.jQuery.lightBox) == "function") $(".gallery a").lightBox();
        
        /* Hovering Gallery-Close Button */
        $(".gallery a").click( function () {
				$('#lightbox-secNav-btnClose').hover( function () {
						$(this).append('<div class="overlay"></div>');
						$(this).find(".overlay").css({ opacity: 0 }).animate({ opacity: 0.3 });
					}, function () { $(this).find(".overlay").stop(true, true).animate({ opacity: 0 }, function () { $(this).remove(); }); });
        	});
        
        /* contact-form handler */
		$("#formSubmit").click(function () {
				var that = $(this);
				that.css ({ display: "none" });
				$(this).parent().append('<img src="./images/loader.gif" alt="Loading..." id="formLoad" />');
				
				/* Init variables */
				var name 	= $("#form-name").val();
				var mail	= $("#form-mail").val();
				var homepage= $("#form-homepage").val();
				var phone 	= $("#form-phone").val();
				var text 	= $("#form-message").val();
				
				/* Load form-function asynchron */
				$("#formReturn").load("ajax/contact.php", { send: true, name: name, mail: mail, homepage: homepage, phone: phone, text: text },
						function () {
							$("#formReturn p.error").animate({ marginLeft:"-220px" }, 100).animate({ marginLeft:"-180px" }, 100).animate({ marginLeft:"-220px" }, 100).animate({ marginLeft: "-200px" }, 100);
							that.parent().find("#formLoad").remove();
							that.css({ display: "block" });
					});
				
				return false;
			});
		
		/* Change backgrounds */
		$("#backgrounds a").click( function () {
				if ($(this).attr("rel") === "") $("body").attr("style", "");
				else $("body").css({ background: "url(" + $(this).attr("href") + ") " + $(this).attr("rel") });
				return false;
				
			});
			
		/* IE 6 */
		if ($.browser.msie && parseInt ($.browser.version) <= "6") {
			alert("Please update your browser immediately!");
		}
	});
	
		
$(window).scroll( function () {
		if (scrollUpButton == false) {
			if ($(document).scrollTop() > 130) {
	        	$("#scrollUpButton").fadeIn("slow");
				scrollUpButton = true;
			}
	    }
	    else {
	    	if ($(document).scrollTop() < 130) {
	        	$("#scrollUpButton").fadeOut("slow");
				scrollUpButton = false;
			}	
	        
	    }
    });
