$(window).ready(function() {

	// Forms
	
	$(".text input, textarea").each(function() {
		if($(this).attr('value')!='') {
			$(this).parent().children("label").fadeTo(0, 0).hide();
		}
	});
	$(".text label, .textarea label").click(function() {
		$(this).stop().fadeTo(250, 0).delay(250).hide();
		$(this).parent().children()[0].focus();
	});
	$(".text input, textarea").focus(function() {
		$(this).parent().children("label").stop().fadeTo(250, 0).delay(250).hide();
	}).blur(function() {
		if($(this).attr('value')=='') {
			$(this).parent().children("label").stop().show().fadeTo(500, 1);
		}
	});
	$("#contact_form .submit").hover(function() {
		$(this).find(".out").stop().animate({"opacity": "0"}, 250);
	},
	function() {
		$(this).find(".out").stop().animate({"opacity": "1"}, 500);
	}).click(function() {
		$(this).parents("form").submit();
	});
	
	// Main menu 
	
	$(".menu_bar li.depth_0.active").each(function() {
		var end = $(this).attr('id').replace('main_menu', '');
		$(this).append('<div class="menu_corners_bottom" id="menu_corners_bottom' + end + '"></div>');
		$(this).children(".menu_corners_top").stop().animate({ "top": "-10", "height": "10" }, 0);
		$(this).find(".out").remove();
	});
	$(".menu_bar li.depth_0").hover(function() {
		$(".menu_corners").each(function() {
			$(this).stop().animate({ "height": "0" }, 250).children(".menu_over").stop().animate({ "top": "-30" }, 50, 'linear');		
			$(this).parents("li.depth_0").find(".out").stop().animate({"opacity": "1"}, 500);
		});
		$(this).find(".out").stop().animate({"opacity": "0"}, 250);
		var height = $(this).find("li.depth_1").size()*29+15;
		$(this).children(".menu_corners").stop().animate({ "height": height }, 250);
		$(this).children(".menu_corners").children(".menu_over").stop().animate({ "top": "-30" }, 50, 'linear');
		if(!$(this).hasClass('active')) {
			$(this).children(".menu_corners_top").stop().animate({ "top": "-10", "height": "10" }, 250);
		}
	},
	function() {
		$(this).find(".out").stop().animate({"opacity": "1"}, 500);
		$(this).children(".menu_corners").stop().animate({ "height": "0" }, 250).children(".menu_over").stop().animate({ "top": "-30" }, 250, 'linear');		
		if(!$(this).hasClass('active')) {
			$(this).children(".menu_corners_top").stop().animate({ "top": "0", "height": "0" }, 250);
		}
	});
	$(".menu_bar li.depth_1 a").mouseover(function() {
		var top_pos = $(this).parent().position().top+2;
		$(this).parents(".menu_corners").children(".menu_over").stop().animate({ "top": top_pos }, 250);
	});
	$(".main_menu_contact .main_menu_link").click(function() {
		return false;
	});
	
	// Artciles menu
	
	$("#articles_menu .closed").animate({ "height": "0" }, 0);
	$("#articles_menu a.depth_0").click(function() {
		var size = $(this).parent().find("li.depth_1").size();
		if(size>0) {
			var height = size*25+16;
			$("#articles_menu .open").addClass("closed").removeClass("open").animate({ "height": "0" }, 250);
			$(this).parent().children("ul.closed").addClass("open").removeClass("closed").stop().animate({ "height": height }, 250);
			return false;
		}
	});
	$("#articles_menu a").hover(
	function() {
		$(this).stop().animate({ "padding-left" : "30" }, 150);
	},
	function() {
		$(this).stop().animate({ "padding-left" : "20" }, 150);
	});
	$(".home_box a").hover(
	function() {
		$(this).parents(".home_box").find(".photo_text_bar a").stop().animate({ "padding-left" : "10" }, 150);
	},
	function() {
		$(this).parents(".home_box").find(".photo_text_bar a").stop().animate({ "padding-left" : "0" }, 150);
	});
	
	// Articles table
	
	$(".more_link").hover(function() {
		$(this).find(".out").stop().animate({"opacity": "0"}, 250);
	},
	function() {
		$(this).find(".out").stop().animate({"opacity": "1"}, 500);
	});
	var td_count = $(".articles_td_center").size();
	var td_width = 660/td_count-2;
	var p_width = 660/td_count-40;
	$(".articles_td_left").css('width', td_width + 'px');
	
	// Footer menu
	
	$("#footer_menu li.depth_0").hover(
	function() {
		var size = $(this).find("li.depth_1").size();
		var height = size*24;
		$(this).children("ul").stop().animate({ "height": height }, 250);
	},
	function() {
		$(this).children("ul").stop().animate({ "height": "0" }, 250);
	}).children("a").click(function() {
		var size = $(this).parent().find("li.depth_1").size();
		if(size!=0) {
			return false;
		}
	});
	
	// Contact
	
   $("#contact_form form").submit(function() {
		$(this).ajaxSubmit({
			success: function(responseStatus, responseCode) {
				var responseText = responseStatus.substr(1);
				responseStatus = responseStatus.substr(0, 1);
				if(responseStatus=='0') {
					$("#contact_form form").clearForm();
					$("#contact_form label").fadeTo(0, 0).hide();
				}
				$('#contact_form .ajax_response').html(responseText).show();
				setTimeout(function(){
					$('#contact_form .ajax_response').fadeOut();
				}, 5000);
			}
		});
		return false;
   })
   
   // Weird bug fix
	
});
