jQuery(document).ready(function($) { 	
		
		$("#header li").hover(function() { 
	
			if($(this).children('ul').length>0) {
		
				var ul = $(this).find('ul:first');
			
				ul.stop(true, true).css({height:'auto'}).fadeIn(300, function() {
			
					ul.animate({top:"43px"});
					
					ul.css({overflow:'visible', visibility:'visible'});
			
				});
		
			}
	
		}, function() {
	
			if($(this).children('ul').length>0) {
		
				var ul = $(this).find('ul:first');
			
				ul.stop(true, true).slideUp(500, function() {
					ul.animate({top:"36px"});
				});
		
			}
	
		});

		$("#testimonial").click(function() {
		
			$(".t-wrapper").text("Add your testimonial below");
		
			var ulTestimonial = $(this).find("ul");
		
			ulTestimonial.stop(true, true).fadeOut("slow", function() {
			
				$(".form-add").fadeIn();
				
				clearTimeout(timeID);
				
				
				$("#testimonial").mouseleave(function() {
				
					closeID = setTimeout(function() {
					
						$(".form-add").slideUp(1000, function() {
						
							$("#testimonial ul").slideDown(1000, function() {
							
								slideTestimonial();
							
							});
						
						});		
					
					}, 3000);
				
				}).mouseenter(function() {
				
					clearTimeout(closeID);
				
				});
					
			
			});
		
		});
		
		
		
		slideTestimonial();
		
		
		
		function slideTestimonial() {
		
			
			
				var current = $("li.show");
				var next = current.next().length ? current.next() : current.parent().children(':first');
				var nextHeight = next.height() + 8;
			
				current.removeClass("show").fadeOut(500, function() {
			
				next.fadeIn(500).addClass("show");
				
				});
			
			
			
			
			timeID = setTimeout(slideTestimonial, 15000);
		
		
		
		}
		
		var gallery = $("#header ul li a").eq(2);
		
		gallery.click(function(e) {
		
			e.preventDefault();
		
		});
		
		selectController();
		
		function selectController() {
		
			var fb = $("#control li a").eq(-4);
			var twitter = $("#control li a").eq(-3);
			var linkd = $("#control li a").eq(-2);
			var youtube = $("#control li a").eq(-1);
			
			
			fb.css({background:'#FFF'});
			
			fb.hover(function() {
			
				twitter.css({background:'#AAA'});
				fb.css({background:'#FFF'});
				
				if($(".twitter").is(":visible")) {
				
					$(".twitter").fadeOut("fast", function() {
					
						$(".facebook").fadeIn();
					
					});
				
				}
			
			});
			
			twitter.hover(function() {
			
				twitter.css({background:'#FFF'});
				fb.css({background:'#AAA'});
				
				if($(".facebook").is(":visible")) {
				
					$(".facebook").fadeOut("fast", function() {
						
						$(".twitter").fadeIn();	
					
					});
				
				}
				
			});
		
		
		}
		
		$(".contact").colorbox({inline:true,width:"auto", height:"550px"});
		
		$("#select").change(function() {
		
			if($("select option:selected").text()=="Referred by someone") {
			
				$("#referred").fadeIn();
			
			} else {
			
				$("#referred").fadeOut();
			
			}
		
		}).change();
		
		$("#testimonial").hover(function() {
		
			if($(".form-add").is(":visible")) {
			
				$(".t-wrapper").text("Add your testimonial below");
			
			} else {
			
				$(".t-wrapper").text("Click to add your testimonial");
			
			}
		
			$(".t-wrapper").stop(true, true).fadeIn("slow", function() {
			
				$(".t-wrapper").animate({top:"-36px"});
			
			});
		
		},function() {
		
			$(".t-wrapper").stop(true, true).fadeOut("slow", function() {
			
				$(".t-wrapper").animate({top:"-26px"});
			
			});
		
		});
		
		$("#testimonial-submit").click(function() {
		
			$(".fullname, .email, .addTestimonial").css({color:"#FFF"});
			
			$(".error-fullname, .error-email, .error-addTestimonial").text(' ');
		
			var url = "http://uniqueeventmgmt.com/wp-content/themes/UniqueEventMgmtTwo/includes/testimonial.php",
				name = $("#fullname").val(),
				em = $("#email").val(),
				testimonial = $("#addTestimonial").val();
				
			$("fieldset > .message").html("<div id='wait'>Sending your testimonial now...</div>");
 				
			$.post(url,{fullname: name, email: em, addTestimonial: testimonial}, 
			
			function(data) {
			
				
				$("#wait").fadeOut(1000,function() {
				
					if(data.error) {
					
						$("fieldset > .message").html("<div id='error'>"+data.error+"</div>");
						
						$.each(data, function(key, value) {
						
						
							$("."+key+"").css({color:'#A8374F'});
							
							
							$(".error-"+key+"").text(value).fadeIn();
						
						});
						
					}
					
					if(data.success) {
					
						
					
						$("fieldset > .message").html("<div id='success'>"+data.success+"</div>");
						
						
						setTimeout(function() {
						
							$("[name=fullname]").val('');
							$("[name=email]").val('');
							$("[name=addTestimonial]").val('');
							
							$("fieldset > .message").html("<div id='wait'>Now closing...</div>");
							
							$(".form-add").delay(1000).slideUp(1000, function() {
							
								$("#wait").hide();
							
								$("#testimonial ul").slideDown(1000, function() {
							
								slideTestimonial();
							
								});							
							});

						
						},1000);
		
					}
				
				});
				
							
			}, 'json');
			
			return false;
		
		});
		
});
