var $j = jQuery.noConflict();

//fixes resized images in IE7, 6
	var imgSizer = {
		Config : {
			imgCache : []
			,spacer : "/wp-content/themes/Waterstone/images/spacer.gif"
		}

		,collate : function(aScope) {
			var isOldIE = (document.all && !window.opera && !window.XDomainRequest) ? 1 : 0;
			if (isOldIE && document.getElementsByTagName) {
				var c = imgSizer;
				var imgCache = c.Config.imgCache;

				//var images = (aScope && aScope.length) ? aScope : document.getElementsByTagName("img");
				var images = (aScope && aScope.length) ? aScope : $j("img.sizefix");
				for (var i = 0; i < images.length; i++) {
					images[i].origWidth = images[i].offsetWidth;
					images[i].origHeight = images[i].offsetHeight;
					
					//alert(images[i].origWidth + ", " + images[i].origHeight);

					imgCache.push(images[i]);
					c.ieAlpha(images[i]);
					images[i].style.width = "100%";
				}

				if (imgCache.length) {
					c.resize(function() {
						for (var i = 0; i < imgCache.length; i++) {
							var ratio = (imgCache[i].offsetWidth / imgCache[i].origWidth);
 							imgCache[i].style.height = (imgCache[i].origHeight * ratio) + "px";
						}
					});
				}
			}
		}

		,ieAlpha : function(img) {
			var c = imgSizer;
			if (img.oldSrc) {
				img.src = img.oldSrc;
			}
			var src = img.src;
			img.style.width = img.offsetWidth + "px";
			img.style.height = img.offsetHeight + "px";
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
			img.oldSrc = src;
			img.src = c.Config.spacer;
		}

		// Ghettomodified version of Simon Willison's addLoadEvent() -- http://simonwillison.net/2004/May/26/addLoadEvent/
		,resize : function(func) {
			var oldonresize = window.onresize;
			if (typeof window.onresize != 'function') {
				window.onresize = func;
			} else {
				window.onresize = function() {
					if (oldonresize) {
						oldonresize();
					}
					func();
				}
			}
		}
	}
	
	
	

	$j(document).ready(function(){	
		
		//fixes image risizing in IE=<7
		imgSizer.collate();		
		
		//turns masked emails into 'real' ones with links in team pages
		$j('span.maskemail').each(function() {
		    var emailPrepend = $j(this).text();
			//console.log(emailPrepend);
			
			var emailBuild = emailPrepend.replace("[at]waterstonelaw.com", "");

			$j(this).html('<a href="mailto:' + emailBuild + '@waterstonelaw.com">' + emailBuild + '@waterstonelaw.com</a>');
		  });

		
		//practice nav animation
		$j('ul#practice-nav li').hover(
			function(){
				$j(this).find('.caption').animate({bottom:"-70px", backgroundColor:"#018CC4"}, 400);						
			}, 
			function(){
				$j(this).find('.caption').animate({bottom:"-158px", backgroundColor:"#333333"}, 200);
			}
		);	
		
		
		//toggle backgrounds on form to show/hide labels
		$j('div.contact-sidebar-form .formset *').focus(
			function() {
				$j(this).addClass('active');
				var value = $j(this).val();
				
				if($j(this).attr("id") == 'name-field' && value == 'Name') $j(this).val("");
				if($j(this).attr("id") == 'email-field' && value == 'Email') $j(this).val("");				
				if($j(this).attr("id") == 'phone-field' && value == 'Phone') $j(this).val("");
				if($j(this).attr("id") == 'message-field' && value == 'Tell us about your case...') $j(this).val("");
				
			}
		);
		
		$j('div.contact-sidebar-form .formset *').blur(
			function() {
				var value = $j(this).val();
				if(value == "") {
					$j(this).removeClass('active');
					
					if($j(this).attr("id") == 'name-field') $j(this).val("Name");
					if($j(this).attr("id") == 'email-field') $j(this).val("Email");
					if($j(this).attr("id") == 'phone-field') $j(this).val("Phone");
					if($j(this).attr("id") == 'message-field') $j(this).val("Tell us about your case...");
					
				}
			}
		);
		
	
				
	});
