
// Runs when the DOM has been loaded
$(document).ready(function() {

    // regions in an array
    var regions = [];
    regions["ireland"]   = [55.44, -8.97, 53.86, -5.30];
    regions["scotland"]  = [58.91, -7.08, 54.45, -0.44];
    regions["wales"]     = [53.55, -5.65, 51.29, -2.64];
    regions["north"]     = [55.11, -5.30, 53.90, -0.38];
    regions["yorks"]     = [54.86, -3.32, 52.75, 0.63];
    regions["midlands"]  = [53.57, -2.77, 51.27, 0.63];
    regions["anglia"]    = [53.08, -1.10, 51.25, 2.00];
    regions["southeast"] = [51.89, -1.31, 50.45, 1.67];
    regions["southwest"] = [52.11, -6.18, 49.86, -0.87];

    var prefix = "ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_";

	// Check if map exists
	if($('#map')) {
		// Loop through each AREA in the imagemap
		$('#map area').each(function() {
	
			// Assigning an action to the mouseover event
			$(this).mouseover(function(e) {
				var country_id = $(this).attr('id').replace('region_', '');
				$('#'+country_id).show();
			});
			
			// Assigning an action to the mouseout event
			$(this).mouseout(function(e) {
				var country_id = $(this).attr('id').replace('region_', '');
				$('#'+country_id).hide();
			});
			
			// Assigning an action to the click event
			$(this).click(function(e) {
				e.preventDefault();
				var country_id = $(this).attr('id').replace('region_', '');
				//alert('You clicked ' + country_id);
			});
		
		});
		
		$("#map area").click(function(){
		    var a = $(this);
		    //window.alert("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_panel_"+a.attr("id").split("region_")[1]);
		    $("#panels>div").fadeOut();
		    $("#"+prefix+"panel_"+a.attr("id").split("region_")[1]).fadeIn();
		    $("#panels>div:last").fadeIn(0);
		    return false;
		});
		
	    $("#panels .close").click(function(){
	        var a = $(this);
	        a.parent().fadeOut();
	        $("#panels>div:last").fadeOut(0);
	        return false;
	    });
	
	    $("#panels>div>span").click(function(){
	        var a = $(this);
	        location.href="results.aspx?postcode="+a.attr("class").split("_")[1].replace(" ","").toUpperCase();
		}).hover(function(){
		    var a = $(this);
		    a.css({ "cursor":"pointer" });
		    $("#panel_dealer"+a.attr("class").split("_")[2]).stop(true, true).fadeIn();
		},function(){
		    var a = $(this);
		    a.css({ "cursor":"default" });
		    $("#panel_dealer"+a.attr("class").split("_")[2]).stop(true, true).fadeOut();
		});
		$("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_txtPostcode").keypress(function(e){
		    if(e.keyCode == 13) {
                $("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_searchPostCode").click();
                return false;
            }
		});
		
		
		/*$("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_txtPostcode").keypress(function(){
		    var a = $(this);
		    if(a.val().length < 6){
		        $("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_lblError").html("<br />Please enter a full postcode.");
		    }
		    else{
		        $("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_lblError").html("");
		    }
		});*/
	}
});