var state_var = null;

$(function() {	
	$("#usa_map").maphilight({
		fillColor: '000000',
		stroke: false,
	});
		
	$("area").click(function() {
		
		$("div.locations").fadeOut();
		state_var = $(this).attr('alt');
		$.post(
			'/stores/request/state:'+state_var, 
			null,
			function(data) {
				$("div.locations").html(data).fadeIn();
				attach_actions();				
			}
		);
		
		return false;
	});
	
	$("a.nonus").click(function() {
		$("div.locations").fadeOut();
		state_var = 'nonus';
		$.post(
			'/stores/nonus/',
			{},
			function(data) {
				$("div.locations").html(data).fadeIn();
				attach_actions();				
				
			}
		);
		
		return false;
	});
	
	$("a.tdhover").click(function() {
		update_dimensions();
		var self = this;
		tb_show(null, $(self).attr('href')+"?width="+thickWidth+"&height="+thickHeight, null);
		return false;		
	});
	
});

tb_callback = function() {
	attach_site_actions();
	attach_contact_actions();
}

function attach_contact_actions() {
	var options = {
		beforeSubmit: function() {
			var blank=true;
			$(":text").each(function() {
				if ($(this).attr('value')) blank=false;
			})
			if (blank) {
				alert('Please enter information before submitting');
				return false;
			}
			
			var sign_up = $("#ContactFormEntrySignUp").attr('checked');
			var email = $("#ContactEmail").attr('value');
			
			if(sign_up && !email) {
				alert('Please enter an e-mail address in order to sign up for the newsletter.');
				return false;
			}
			
			$("div.talktous").fadeOut();
		},
		success: function(responseText) {
			$("div.talktous").before('<h2>Thank you for contacting us!</h2>');
			setTimeout(function() {
				tb_remove();
			}, 1500)
		}
	};
	$("#TB_window form").ajaxForm(options);

}
function attach_actions() {
	var self = this;
	$("div.locations div.paging a").click(function() {
		var self = this;
		$("div.locations").fadeOut();
		var link = $(self).attr('href');
		$.post(
			link, 
			null,
			function(data) {
				$("div.locations").html(data).fadeIn();
				attach_actions();				
			}
		);
		
		return false;
	});
}

