var $ = jQuery;

$(document).ready(function() {



	$("#header .nav a").hover(function() {
		$(this).parent().addClass("active");
	}, function() {
		$(this).parent().removeClass("active");	
	});
	
	
	// state changer
	$("#state_selector").change(function() {
	 if($(this).val()!="") location.href = "/states/"+ $(this).val() + "/"; 
	});
	
	// add a span to things with class .ruled so that the rule doesn't show up behind the actual text
	$(".ruled").each(function() {
	 $(this).html("<span>"+$(this).html()+"</span>");
	});
	
	$("#choose-year").change(function() {
		window.location = '/reports/'+$(this).val();		
	});
	
	
	// Check if map exists
	if($('#imgmap')) {
		// Loop through each AREA in the imagemap
		$('#imgmap area').each(function() {
	
			// Assigning an action to the mouseover event
			$(this).mouseover(function(e) {
				var country_id = $(this).attr('alt');
				$('#usmap .overlay').attr('src','/cms/wp-content/themes/solo/graphics/map/'+country_id+'.png');
			});
			
			// Preload Image
			$.preLoadImages('/cms/wp-content/themes/solo/graphics/map/'+$(this).attr('alt')+'.png');
			
			// Assigning an action to the mouseout event
			$(this).mouseout(function(e) {
				$('#usmap .overlay').attr('src','/cms/wp-content/themes/solo/graphics/map/empty.png');
			});
			
			// Assigning an action to the click event
			/*$(this).click(function(e) {
				e.preventDefault();
				var country_id = $(this).attr('alt');
				alert('You clicked ' + country_id);
			});*/
		
		});
	}

	$(".gbox img").hover(function(e) {
	  var pos = $(this).position();
	  $("#ghov").css("left",pos.left+160).css("top",pos.top+520);
	  $("#ghov img").attr("src",$(this).attr("alt"));
	  $("#ghov").removeClass("ghost");
	}, function() {
	  // do nothing	  
	});
	
	$("#ghov img").hover(function(e) {
	}, function() {
	 $("#ghov").addClass("ghost"); 
	});
	
	$(".thumbbuttons span").hover(function() {
	 $(this).find(".bg").removeClass("ghost");
	}, function() { 
	 $(this).find(".bg").addClass("ghost");
	});
		
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
