var dealerSearch = {
    prefix : "ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_",
    init : function(){
        dealerSearch.map.init();
        dealerSearch.box.init();
    },
    map : {
        regions : { // this appears to be unused, probably is in here for reference
            "ireland"   : [55.44, -8.97, 53.86, -5.30],
            "scotland"  : [58.91, -7.08, 54.45, -0.44],
            "wales"     : [53.55, -5.65, 51.29, -2.64],
            "north"     : [55.11, -5.30, 53.90, -0.38],
            "yorks"     : [54.86, -3.32, 52.75, 0.63],
            "midlands"  : [53.57, -2.77, 51.27, 0.63],
            "anglia"    : [53.08, -1.10, 51.25, 2.00],
            "southeast" : [51.89, -1.31, 50.45, 1.67],
            "southwest" : [52.11, -6.18, 49.86, -0.87]
        },
        init : function(){
            var $mapArea = $('#map area');
            var $panelsDiv = $("#panels>div");
            var $panelsDivLast = $("#panels>div:last");

            var test = document.getElementById("map");
            if(test){
                // setup event handling for the regions
                $mapArea.mouseover(function(){
                    var a = $(this);
                    $('#' + a.attr('id').replace('region_', '')).show();
                }).mouseout(function(){
                    var a = $(this);
                    $('#' + a.attr('id').replace('region_', '')).hide();
                }).click(function(){
                    var a = $(this);
                    //window.alert("#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_panel_"+a.attr("id").split("region_")[1]);
                    $panelsDiv.fadeOut();
                    $("#" + dealerSearch.prefix + "panel_" + a.attr("id").split("region_")[1]).fadeIn();
                    $panelsDivLast.fadeIn(0);
                    return false;
                });

                // setup click event for 'back to map' button
                $("#panels .close").click(function(){
                    var a = $(this);
                    a.parent().fadeOut();
                    $panelsDivLast.fadeOut(0);
                    return false;
                });

                // event handlers for the markers
                $panelsDiv.find(">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();
                });
            }
        }
    },
    box : {
        init : function(){
            // makes the postcode box work with 'enter'
            $("#" + dealerSearch.prefix +" txtPostcode").keypress(function(e){
                if(e.keyCode === 13) {
                    $("#" + dealerSearch.prefix + "searchPostCode").click();
                    return false;
                }
            });
            dealerSearch.box.setChecked();
        },
        setChecked : function(){
            var filter = window.location.href.split("?filter=");
            if(filter.length > 1){
                if(filter[1].split("&").length > 1){
                    var filters =  filter[1].split("&");
                    for(var i = 0; i < filters.length; i++){
                        document.getElementById(dealerSearch.prefix +  + "check"+filters[i]).checked=true;
                    }
                }
                else{
                    document.getElementById(dealerSearch.prefix + "check"+filter[1]).checked=true;
                }
            }
        }
    }
}

$(document).ready(function() {
    dealerSearch.init();
});
