/* PSA
---------------------------------------------------------------------------*/
$.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++){
    	$("<img>").attr("src", arguments[i]);
	}
}

$(function(){
	/* cta form
	---------------------------------------------------------------------------*/
	$("#newsletter input").focus(function(){
		$(this).val("");
		$(this).css("color","#555");
	});
	$("#sub a").click(function(){
		$("#newsletter").attr("action",r+"pages/newsletter.php");
		$("#newsletter").submit();
	});

	// rel externals
	$("a[rel=external]").attr("target","_blank");

	// sIFR
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:r+"swf/HelveticaNeue45Light.swf", sColor:"#6e317a", sWmode:"transparent"}));
	};

	/* subnavs
	---------------------------------------------------------------------------*/
	if ($("#subcos").is("div") || $("#statues").is("div")){

		// preload images
		$.preloadImages(r+"images/bg_sub_cosmetic.gif", r+"images/bg_sub_surgical.gif");

		$("#nav a, .hassub").mouseover(function(){
			// turn things on from links
			if ($(this).attr("rel")){
				$(this).parent(".imglnk").addClass("stayon"); // make nav link stay on
				$("#" + $(this).attr("rel")).fadeIn("fast"); // turn box on
			}

			// turn things off from links
			$(this).parent(".imglnk").siblings(".imglnk").each(function(){
				$(this).removeClass("stayon"); // turn off other active links
				var me = $(this).attr("id") + " > a"; // is there an associated submenu
				if($("#"+me).attr("rel")){
					$("#" + $("#"+me).attr("rel")).fadeOut("fast");
				}
			});

			// turn submenu off from rollout
			$(".sub").mouseleave(function(){
				$(this).fadeOut("fast");
			});
		});
	}

	/* statue head sub sub navs
	---------------------------------------------------------------------------*/
	if ($("#statues").is("div")){
		var hideDelay;
		
		// preload images
		$.preloadImages(
			r+"images/statues_heads.jpg",
			r+"images/statues_heads_non.jpg",
			r+"st_face2.gif",
			r+"st_ears.gif",
			r+"st_grooves.gif",
			r+"st_skin.gif",
			r+"st_lips.gif",
			r+"st_wrinkles.gif",
			r+"st_eyelids.gif",
			r+"st_nose.gif",
			r+"st_jowls.gif",
			r+"st_chin.gif"
		);

		// show and hide submenus (where applicable)
		$(".statwrap .slink > a").mouseover(function(){
			var me = $(this).parent(".slink").attr("id");
			if ($("#"+me+" > a").attr("href") == "#"){
				$("#"+me).css("zIndex",30);
				$("#"+me).find("ul").slideDown("fast", function(){
					$(".statwrap .slink:not(#"+me+") ul").each(function(){
						$(this).slideUp("fast",function(){
							if ($(this).parent("#substat")){
								$(this).css("zIndex",20); // is in head zoom
							} else {
								$(this).css("zIndex",5); // is not in head zoom
							}
						});
					});
				});
			} else {
			
				$(".statwrap .slink:not(#"+me+") ul").each(function(){
					$(this).slideUp("fast",function(){
						if ($(this).parent("#substat")){
							$(this).css("zIndex",20); // is in head zoom
						} else {
							$(this).css("zIndex",5); // is not in head zoom
						}
					});
				});
			
			}
			
		});
		
		$(".statwrap .slink > a[href=#]").click(function(){
			return false;
		});
		
		// hide submenu on mouse out
		$(".statwrap ul").mouseout(function(){
			var me = $(this);
			hideDelay = setTimeout(function(){
				$(me).slideUp("fast");
			},"500");
		});
		
		$(".statwrap ul, .statwrap a").mouseover(function(){
			clearTimeout(hideDelay);
		});
	}

	/* consultants
	---------------------------------------------------------------------------*/
	if ($(".consultant").is("div")){
		$(".consultant").css("cursor","pointer");
		$(".consultant").click(function(){
			location.href = $(this).find(".more a").attr("href");
		});
		$(".consultant").hover(
			function(){
				var me = "#" + $(this).attr("id");
				$(me + " .cname span").css("backgroundPosition","right");
				$(me + " p").css("color","#0b4459");
				$(me + " .more a").css("color","#ffffff");
				
			},
			function(){
				var me = "#" + $(this).attr("id");
				$(me + " .cname span").css("backgroundPosition","left");
				$(me + " p").css("color","#16576f");
				$(me + " .more a").css("color","#54a5c3");
			}
		);
	}

	/* forms
	---------------------------------------------------------------------------*/
	if ($("#appt").is("form")){
		$("#enqservices span").hover(
			function(){
				$(this).css("backgroundColor","#fff")
			},
			function(){
				$(this).css("backgroundColor","transparent")
			}
		);
		$("#apptsub a").click(function(){
			var t = $("#trudest").val();
			$("#appt").attr("action",r+"pages/" + t + ".php");
			$("#appt").submit();
			return false;
		});
	}

});
