$(document).ready(function(){
	$('.state').usamap();
	
	//This is for the color change, you can delete this function
	$('#colors a').click(function(){
		var color = $(this).attr('href');
		$('.states').removeClass('red green').addClass(color);
		return false;
	});
	//This is for the color change, you can delete this function
});



$('.states').click(function(){
	alert('id:'+$(this).attr('class'));
 $('html, body').animate({ 
      scrollTop: $('#load_states').offset().top 
  }, 3000);
});

//This is the map function, you dont need to do anything about that.
(function($){
	$.fn.usamap = function(options) {
		var defaults = {
			tooltip: true
		};
		var options = $.extend(defaults, options);
		return this.each(function(i) {
			obj = $(this);
			
			
			var name = obj.attr('id');
			var title = obj.attr('title');
			var name_split = name.split('_');
			var state_class = name_split[1];
			
			obj.attr('href','#'+state_class);
			obj.click(function(){
				$.ajax({
		  			url: '/ajaxgetpost.php',
					data: 'category='+state_class,
          			type: "POST",
  					success: function(data) {
    					if(data!=''){
							$('#load_states').html(data).hide().slideDown('normal');
							}
						}
					
					});
				
				
				
 				$('html, body').animate({ 
     			 	scrollTop: $('#load_states').offset().top 
  					},1000);
				});

			
			
			
			var tooltipname = "tooltip";
	
	
	
			var p = $('#'+state_class+'_smokerate').html();
			if(p != 'undefined' && p != '' && p != 'null' && p != '0' && p != null && p != 0){
				var smokerate = '<div class="tooltip_per">Smoking Prevalence: '+p+'%</div>';
				}
			else var smokerate = '';	
	
	
	
			var p = $('#'+state_class+'_percentage').html();
			if(p != 'undefined' && p != '' && p != 'null' && p != '0' && p != null && p != 0){
				var percentage = '<div class="tooltip_per">AA Population: '+p+'%</div>';
				}
			else var percentage = '';	
			
			
			
			
			
			
			
			if (options.tooltip) {
				$("body").append('<div class="tooltip" id="tooltip'+i+'"><p>'+title+'</p>'+smokerate+percentage+'</div>');	
			}
			obj.removeAttr("title");
			obj.mouseover(function(){
				$('#tooltip'+i).css({opacity:0.8, display:"none"}).stop().fadeIn('fast');
      				$('.states .'+state_class).addClass('hover').css('opacity','0.3').animate({opacity: 1.0},'fast'); 
						$('.states .'+state_class+' .map_icon').addClass("map_icon_over");				
					
			}).mousemove(function(kmouse){
				$('#tooltip'+i).css({left:kmouse.pageX+10, top:kmouse.pageY+10});
			}).mouseout(function(){
				$('#tooltip'+i).hide();
				$('.states .'+state_class).removeClass('hover');
				$('.states .'+state_class+' .map_icon').removeClass("map_icon_over");				

			});
		});
	};
})(jQuery);
