var searchFocus = false,
	sidebar = true,
	that;

$(document).ready( function () {
		$("#content ul, #content ol").each( function () { $(this).find("li:first").addClass("first"); $(this).find("li:last").addClass("last"); });
		
		/* Scrolling Menu & Sidebar */
			$(window).scroll(function () {
					var offset1 = parseInt ($(document).scrollTop()) - parseInt ($("#content").offset().top) + 20  + "px";
					var offset2 = parseInt ($(document).scrollTop()) + 20  + "px";

					if (sidebar) {
						$("#sidebar").css({ top: $("#content").offset().top + "px" });
						sidebar = false;
					}

					if ($(document).scrollTop() > parseInt ($("#content").offset().top)) {
						$("#nav").animate({ top: offset1 }, { duration: 500, queue: false });
						$("#sidebar").animate({ top: offset2 }, { duration: 500, queue: false });
					}
					else {
						$("#nav").animate({ top: "0" }, { duration: 500, queue: false });
						$("#sidebar").animate({ top: $("#content").offset().top  + "px" }, { duration: 500, queue: false });
					}
				});
		
		/* Navigation */
		$("#nav ul li").hover( function () {
				$(this).find("ul").stop(true, true).slideDown("slow");
			}, function () {
				if (!($(this).hasClass("current-menu-item") || $(this).hasClass("current-menu-parent") || $(this).hasClass("current_page_parent") || $(this).hasClass("current_page_item"))) $(this).find("ul").stop(true, true).delay(3000).slideUp("slow");
			});
	
		$("#nav .wrap").hover( function () {
				$(this).children(".inner").stop(true, true).slideDown("slow");
				$(this).css({ display: "block" });
			}, function () {
				$(this).children(".inner").stop(true, true).delay(3000).slideUp("slow");
				$(this).css({ display: "inline-block" });
			});
		
		/* Sidebar */
		$("#sidebar .wrap").hover( function () {
				var a = false;
				if ($(this).parent().hasClass("search")) {
					$(this).addClass("active");
					$(this).children(".inner").stop(true, true).animate({ width: "104px" }, "slow");
				}
				else {
					if (!$(this).parent().hasClass("open")) {
						$(this).children(".inner").stop(true, true).slideDown("slow");
						$(this).css({ display: "block" });
					}
				}
			}, function () {
				if ($(this).parent().hasClass("search")) {
					that = $(this);
					if (searchFocus == false)
						$(this).children(".inner").stop(true, true).delay(3000).animate({ width: "0px" }, "slow", function () { that.removeClass("active"); });
				}
				else {
					if (!$(this).parent().hasClass("open")) {
						$(this).children(".inner").stop(true, true).delay(3000).slideUp("slow");
						$(this).delay(3000).css({ display: "inline-block" });
					}
				}
			});
		
		/* Search: stay active if clicked */
		$(".searchfield").click( function () {
				$(this).addClass("active");
				if ($(this).val() == "Keyword") $(this).val("");
				searchFocus = true;
			});
		
		/* Show entry-info @ blog-page */
		$("#content .article").hover( function () {
				$(this).children(".shadow.info-shadow").stop(true, false).delay(500).animate({ width: "152px", paddingLeft: "12px" }, "slow");
				$(this).find(".img a").append('<div class="overlay"></div>');
				$(this).find(".overlay").css({ opacity: 0 }).animate({ opacity: 0.1 });
			}, function () {
				$(this).find(".overlay").stop(true, true).animate({ opacity: 0 }, function () { $(this).remove(); });
				$(this).children(".shadow.info-shadow").stop(true, false).animate({ width: "0px", paddingLeft: "0" }, "slow");
			});
		
		/* Show entry-info @ entry-page */
		$("#content .header").hover( function () { $(this).children(".inner-shadow").stop(true, false).delay(500).animate({ height: "56px" }, "slow"); },
				function () { $(this).children(".inner-shadow").stop(true, false).animate({ height: "0px" }, "slow"); });
		
		/* Show info @ portfolio */
		$(".portfolio").hover( function () {
				$(this).find(".info-shadow").stop(true, false).animate({ height: "70px", paddingBottom: "39px" }, "slow");
				$(this).children("a").append('<span class="overlay"></span>');
				$(this).find(".overlay").css({ opacity: 0 }).animate({ opacity: 0.5 });
			}, function () {
				$(this).find(".overlay").stop(true, true).animate({ opacity: 0.1 }, function () { $(this).remove(); });
				$(this).find(".info-shadow").stop(true, false).animate({ height: "0px", paddingBottom: "0" }, "slow");
			});
		
		/* Give Feedback when hovering the follow-icons */
		$(".follow li").hover( function () {
				$(this).children("a").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" style="margin: 10px;" />');
				
				/* Init variables */
				var pre 	= $("#formPre").val();
				var name 	= $("#formName").val();
				var mail	= $("#formMail").val();
				var phone = $("#formPhone").val();
				var text 	= $("#formText").val();
				
				/* Load form-function asynchron */
				$("#formReturn").load("ajax/contact.php", {send: true, prename: pre, name: name, mail: mail, phone: phone, text: text },
						function () {
							$("#formReturn p.error").animate({ top:"-=10px" }, 100).animate({ top:"+=10px" }, 100).animate({ top:"-=10px" }, 100).animate({ top:"+=10px" }, 100);
							that.parent().find("#formLoad").remove();
							that.css({ display: "block" });
					});
				
				return false;
			});
		
		// Background-color-fade for tables
		$("#content table tr").hover( function() { $(this).css({backgroundColor: "#f1f1f1"})}, 
			function(){ if ($(this).hasClass("odd")) $(this).animate({backgroundColor: "#fafafa"}, 750); else $(this).stop(true, false).animate({backgroundColor: "#fff"}, 750); });
		
		/* Resize portfolio-entry on window resizing to keep square */
		$(".portfolio").each( function () {
				$(this).css({ height: $(this).width() });
			});
		
		var newPic;
		$(".gallery a").each ( function () {
				newPic = jQuery(this).find("img").attr("src");
				newPic = str_replace("-138x138", "", newPic);
				$(this).attr("href", newPic);
			});
			$('.gallery a').lightBox({fixedNavigation:true});
		
		function str_replace(search, replace, subject) {
			return subject.split(search).join(replace);
		}
		
		/* IE 6 Warning */
		if ($.browser.msie && $.browser.version < 7) alert("You are using an old browser. Please update.");
		
		$("#content .article .info .tags a").wrap("<div />")

	});

$(window).resize( function () {
		/* Resize portfolio-entry on window resizing to keep square */
		$(".portfolio").each( function () {
				$(this).css({ height: $(this).width() });
			});
	});
