var maptop = -6;
var mapleft = -6;
var images_dir = "http://wwtl.s3.amazonaws.com/images";
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages(images_dir + "/states/WA.png",
 				images_dir + "/states/OR.png",
				images_dir + "/states/CA.png",
				images_dir + "/states/NV.png",
				images_dir + "/states/ID.png",
				images_dir + "/states/UT.png",
				images_dir + "/states/AZ.png",
				images_dir + "/states/MT.png",
				images_dir + "/states/ND.png",
				images_dir + "/states/WY.png",
				images_dir + "/states/SD.png",
				images_dir + "/states/CO.png",
				images_dir + "/states/NM.png",
				images_dir + "/states/TX.png",
				images_dir + "/states/OK.png",
				images_dir + "/states/NE.png",
				images_dir + "/states/KS.png",
				images_dir + "/states/MN.png",
				images_dir + "/states/IA.png",
				images_dir + "/states/MO.png",
				images_dir + "/states/AR.png",
				images_dir + "/states/LA.png",
				images_dir + "/states/WI.png",
				images_dir + "/states/IL.png",
				images_dir + "/states/MI.png",
				images_dir + "/states/MS.png",
				images_dir + "/states/IN.png",
				images_dir + "/states/OH.png",
				images_dir + "/states/ME.png",
				images_dir + "/states/NH.png",
				images_dir + "/states/VT.png",
				images_dir + "/states/NY.png",
				images_dir + "/states/MA.png",
				images_dir + "/states/RI.png",
				images_dir + "/states/CT.png",
				images_dir + "/states/PA.png",
				images_dir + "/states/NJ.png",
				images_dir + "/states/DE.png",
				images_dir + "/states/MD.png",
				images_dir + "/states/WV.png",
				images_dir + "/states/VA.png",
				images_dir + "/states/KY.png",
				images_dir + "/states/TN.png",
				images_dir + "/states/SC.png",
				images_dir + "/states/AL.png",
				images_dir + "/states/GA.png",
				images_dir + "/states/FL.png",
				images_dir + "/states/AK.png",
				images_dir + "/states/HI.png",
				images_dir + "/states/NC.png",
				images_dir + "/states/NY-mask.png",
				images_dir + "/states/AK-mask.png",
				images_dir + "/states/FL-mask.png",
				images_dir + "/states/HI-mask.png",
				images_dir + "/states/LA-mask.png",
				images_dir + "/states/MA-mask.png",
				images_dir + "/states/MI-mask.png"
				);
				
$(document).ready(function(){
	$('.state').hover(
		function(){
			$('.state-copy').remove();
			$('.state-mask').remove();
			var img = "<img class=\"state-copy\" id=\"" + $(this).attr("id") + "-copy\" src=\"" + images_dir + "/states/" + $(this).attr("id") + ".png\" />";
			if($(this).attr("id") == 'NY' || 
			   $(this).attr("id") == 'AK' || 
			   $(this).attr("id") == 'FL' || 
			   $(this).attr("id") == 'HI' || 
			   $(this).attr("id") == 'LA' || 
			   $(this).attr("id") == 'MA' || 
			   $(this).attr("id") == 'MI'){
				var imgmask = "<img class=\"state-mask\" id=\"" + $(this).attr("id") + "-mask\" src=\"" + images_dir + "/states/" + $(this).attr("id") + "-mask.png\" />";
			}else{
				var imgmask = "<img class=\"state-mask\" id=\"" + $(this).attr("id") + "-mask\" src=\"" + images_dir + "/spacer.png\" />";
			}
			
			$("#usmap").prepend(img);
			$("#usmap").prepend(imgmask);
			positionimage();
			$(".state-copy").each(function(){
				var width = $(this).width();
				var height = $(this).height();
				
				var zoomheight = height * 1.2;
				var zoomwidth = width * 1.2;
				
				var	centerheight = (zoomheight - height)/2;
				var	centerwidth = (zoomwidth - width)/2;
			
				$(this).animate({
					top: '-=' + centerheight,
					left: '-=' + centerwidth,
					width: zoomwidth,
					height: zoomheight
				}, 100);
	
			});
			
		},
		function(){
			$('.state-copy').remove();
			$('.state-mask').remove();
		}
	);
});

function positionimage(){
	$("#IL-copy").css({'top':maptop + 123,'left':mapleft + 352});
	$("#IN-copy").css({'top':maptop + 127,'left':mapleft + 391});
	$("#WI-copy").css({'top':maptop + 65,'left':mapleft + 330});
	$("#IA-copy").css({'top':maptop + 113,'left':mapleft + 297});
	$("#MO-copy").css({'top':maptop + 154,'left':mapleft + 307});
	
	$("#AR-copy").css({'top':maptop + 208,'left':mapleft + 323});
	$("#LA-copy").css({'top':maptop + 256,'left':mapleft + 332});
	$("#LA-mask").css({'top':maptop + 256,'left':mapleft + 332});
	$("#TX-copy").css({'top':maptop + 217,'left':mapleft + 188});
	$("#OK-copy").css({'top':maptop + 205,'left':mapleft + 230});
	$("#KS-copy").css({'top':maptop + 165,'left':mapleft + 241});
	
	$("#NE-copy").css({'top':maptop + 126,'left':mapleft + 219});
	$("#SD-copy").css({'top':maptop + 84,'left':mapleft + 220});
	$("#ND-copy").css({'top':maptop + 42,'left':mapleft + 220});
	$("#NM-copy").css({'top':maptop + 207,'left':mapleft + 159});
	$("#CO-copy").css({'top':maptop + 152,'left':mapleft + 164});
	
	$("#WY-copy").css({'top':maptop + 96,'left':mapleft + 149});
	$("#MT-copy").css({'top':maptop + 35,'left':mapleft + 110});
	$("#ID-copy").css({'top':maptop + 33,'left':mapleft + 88});
	$("#UT-copy").css({'top':maptop + 133,'left':mapleft + 109});
	$("#AZ-copy").css({'top':maptop + 202,'left':mapleft + 93});
	
	$("#WA-copy").css({'top':maptop + 25,'left':mapleft + 30});
	$("#OR-copy").css({'top':maptop + 58,'left':mapleft + 14});
	$("#CA-copy").css({'top':maptop + 116,'left':mapleft + 9});
	$("#NV-copy").css({'top':maptop + 124,'left':mapleft + 50});
	$("#MN-copy").css({'top':maptop + 34,'left':mapleft + 285});
	
	$("#MI-copy").css({'top':maptop + 48,'left':mapleft + 345});
	$("#MI-mask").css({'top':maptop + 48,'left':mapleft + 345});
	$("#KY-copy").css({'top':maptop + 162,'left':mapleft + 380});
	$("#TN-copy").css({'top':maptop + 192,'left':mapleft + 375});
	$("#OH-copy").css({'top':maptop + 113,'left':mapleft + 417});
	$("#MS-copy").css({'top':maptop + 227,'left':mapleft + 365});
	
	$("#AL-copy").css({'top':maptop + 222,'left':mapleft + 398});
	$("#FL-copy").css({'top':maptop + 269,'left':mapleft + 411});
	$("#FL-mask").css({'top':maptop + 268,'left':mapleft + 409});
	$("#GA-copy").css({'top':maptop + 217,'left':mapleft + 427});
	$("#AK-copy").css({'top':maptop + 265,'left':mapleft + 2});
	$("#AK-mask").css({'top':maptop + 265,'left':mapleft + 2});
	$("#HI-copy").css({'top':maptop + 316,'left':mapleft + 152});
	$("#HI-mask").css({'top':maptop + 316,'left':mapleft + 150});
	
	$("#PA-copy").css({'top':maptop + 101,'left':mapleft + 461});
	$("#NY-copy").css({'top':maptop + 55,'left':mapleft + 465});
	$("#NY-mask").css({'top':maptop + 53,'left':mapleft + 463});
	$("#NJ-copy").css({'top':maptop + 107,'left':mapleft + 517});
	$("#WV-copy").css({'top':maptop + 137,'left':mapleft + 450});
	$("#VA-copy").css({'top':maptop + 144,'left':mapleft + 444});
	
	$("#SC-copy").css({'top':maptop + 209,'left':mapleft + 451});
	$("#NC-copy").css({'top':maptop + 176,'left':mapleft + 437});
	$("#ME-copy").css({'top':maptop + 8,'left':mapleft + 538});
	$("#NH-copy").css({'top':maptop + 44,'left':mapleft + 534});
	$("#VT-copy").css({'top':maptop + 45,'left':mapleft + 518});
	
	$("#MD-copy").css({'top':maptop + 133,'left':mapleft + 478});
	$("#MA-copy").css({'top':maptop + 76,'left':mapleft + 529});
	$("#MA-mask").css({'top':maptop + 76,'left':mapleft + 529});
	$("#DE-copy").css({'top':maptop + 130,'left':mapleft + 514});
	$("#CT-copy").css({'top':maptop + 89,'left':mapleft + 530});
	$("#RI-copy").css({'top':maptop + 88,'left':mapleft + 546});
}
