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