
function highlight ()	{
	$('.interactive_map').maphilight({
		fill: true,
        fillOpacity: 0.6,
        fillColor: '993300', 
        stroke: false, 
        fade: true,  
        groupBy: "name", 
        shadow: true, 
        shadowPosition: "outside", 
        shadowRadius: 10, 
        shadowX: 4, 
        shadowY: 4
    });
}

function change_map(old_map, new_map)	{
	
	//set country cookie.
	var today = new Date();
	var expires_date = new Date( today.getTime() + (1000 * 60 * 60 * 24) );
	document.cookie = "golf18country=" + escape( new_map ) + ";expires=" + expires_date.toGMTString();
	
	if (new_map == "usa"){
		img_small = "/images/united_states_small_2.png";
		img_big = "/images/united_states_big_2.png";
		$('#select_your_state').html("Select your State <img src=\"/images/arrow.png\" />");
	}	
	if (new_map == "canada"){
		img_small = "/images/canada_small.png";
		img_big = "/images/canada_big.png";
		$('#select_your_state').html("Select your Province <img src=\"/images/arrow.png\" />");
	}
	$('.country_thumb_' + old_map).fadeIn(500);
	$('.country_thumb_' + new_map).fadeOut(500);

	$('#country_image_small').attr("usemap", "#" + new_map);
	$('#country_image_small').attr("src", img_small);
	$('#country_image_big').attr("src", img_big);
	$('.interactive_map').attr("style", 'display: block; background: url(' + img_small + ') repeat scroll 0% 0% transparent; padding: 0px; width: 489px; height: 418px;');

	highlight();
}
	
function map_simple_tooltip(target_items, name){
 	$(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);

		if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){

		$(this).removeAttr("title").mouseover(function(){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(1);
		}).mousemove(function(kmouse){
				var border_top = $(window).scrollTop();
				var border_right = $(window).width();
				var left_pos;
				var top_pos;
				var offset = 15;
				if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
					left_pos = kmouse.pageX+offset;
					} else{
					left_pos = border_right-my_tooltip.width()-offset;
					}

				if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
					top_pos = border_top +offset;
					} else{
					top_pos = kmouse.pageY-my_tooltip.height()-offset;
					}

				my_tooltip.css({left:left_pos, top:top_pos});
		}).mouseout(function(){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeOut(1);
		});

		}

	});
}
function static_tooltip(target_items, name){
 	$(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);
		
		$(this).removeAttr("title").click(function(kmouse){
			if (my_tooltip.css("opacity") == 0.9){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeOut(300);
			}
			else{
				my_tooltip.css({opacity:0.9, display:"none"}).fadeIn(300);
				my_tooltip.css({left:kmouse.pageX, top:kmouse.pageY});
			}
		});
	});
}

$(document).ready(function(){
	
	//check country cookie
	var a_all_cookies = document.cookie.split( ';' );
	for ( i = 0; i < a_all_cookies.length; i++ )	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == "golf18country" )	{
			cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			if (cookie_value == "canada")
				change_map("usa", "canada");
			else	
				change_map("canada", "usa");
		}
	}
	
	var active_map = "usa";
	
	map_simple_tooltip("area","map_tooltip");
	static_tooltip(".span_tooltip","map_tooltip_list");
	simple_tooltip("div.popupmenu","tooltip");
	highlight();

	$('area').click(function(e) {

		var zoom = 1;
		var tmp_coords = "";
		var state_code = $(this).attr('name'); 
		var div_name = "#caption_" + state_code;
		var content_div_name = "#caption_content_" + state_code;
		
		if ($('.map_item').find(div_name).attr("id")){
			if ($(this).attr('class') != '')	{
				tmp_class = "." + $(this).attr('class');
				
				$.each($(tmp_class), function(){
					tmp_coords = tmp_coords + $(this).attr('coords') + ",";
				});
				
				tmp_coords = tmp_coords.substring(0, tmp_coords.length-1);
				var coords = tmp_coords.split(",");
			}
			else
				var coords = $(this).attr('coords').split(",");
			
			var x_vals = [];
			var y_vals = [];
			
			$.each(coords, function(index, value) {
			    if ((index == 0 && index != 1) || index % 2 == 0)
			    	x_vals.push(Number(coords[index]));
			    else
			    	y_vals.push(Number(coords[index]));
			});
			max_x = Math.max.apply( Math, x_vals );
			min_x = Math.min.apply( Math, x_vals );
			width_x = max_x - min_x;
			
			max_y = Math.max.apply( Math, y_vals );
			min_y = Math.min.apply( Math, y_vals );
			width_y = max_y - min_y;
			
			var original_width = $('.interactive_map2').width();
			var original_height = $('.interactive_map2').height();
			
			if (width_y >= width_x)
				zoom = $('.interactive_map2').height() / width_y;
			else
				zoom = $('.interactive_map2').width() / width_x;
				
			zoom = zoom * .6;
			
			left = (min_x * zoom) - ((original_width - (width_x * zoom)) / 2);
			top_val = (min_y * zoom) - ((original_height - (width_y * zoom)) / 2);
			
			width = Number($('.interactive_map2').width()) * zoom;
			height = $('.interactive_map2').height() * zoom;
		
			$('.interactive_map').hide();
			$('.interactive_map2').show();
			$('.interactive_map2').animate({'top':0, 'left':0}, {duration:1});
			
			$('.country_thumb_canada').fadeOut(800);
			$('.country_thumb_usa').fadeOut(800);
			
			new_width = String(Math.round(width));
			new_height = String(Math.round(height));
			new_top_val = String(Math.round(top_val));
			new_left = String(Math.round(left));
			
			$('.interactive_map2').animate(
				{
					width: new_width, 
					height: new_height, 
					top: -new_top_val,
					left: -new_left
				}	
				, {duration:700});
				
			$('.map_item').find(div_name).stop(false,true).css('filter', 'alpha(opacity=50)');
			$('.map_item').find(div_name).stop(false,true).fadeIn(1000);
			$('.map_item').find(content_div_name).stop(false,true).fadeIn(1000);
		}
	});
});

function back_to_map (state_code) {
	
	var div_name = "#caption_" + state_code;
	var content_div_name = "#caption_content_" + state_code;
	
	$('.interactive_map2').animate({'width':$('.map_item').width(), 'height':$('.map_item').height(), 'top':'0', 'left':'0'}, {duration:700});	
	$('.map_item').find(div_name).stop(false,true).fadeOut(700);
	$('.map_tooltip_list').fadeOut(700);
	$('.map_item').find(content_div_name).stop(false,true).fadeOut(700, function() {
		$('.interactive_map2').hide();
		$('.interactive_map').show();
	});
	
	if ($('#country_image_small').attr("usemap") == "#usa")	{
		$('.country_thumb_canada').fadeIn(800);
		$('.country_thumb_usa').fadeOut(800);
	}
	else	{
		$('.country_thumb_usa').fadeIn(800);
		$('.country_thumb_canada').fadeOut(800);
	}
};
