function area_rollover_position(coords_str) {
    // Get a sensible position for the 'rollover' text
    var min_x=99999, max_x=0, max_y=0;
    var coords = coords_str.split(',');
    for (var i=0;i<coords.length;i+=2) {
        if (parseInt(coords[i]) < min_x) min_x=parseInt(coords[i]);
        if (parseInt(coords[i]) > max_x) max_x=parseInt(coords[i]);
        if (parseInt(coords[i+1]) > max_y) max_y=parseInt(coords[i+1]);
    }
    if (fecking_ie6) {
        return {'x' : (min_x + (max_x - min_x)/2) - 75, 'y' : max_y};
    }
    else{
        return {'x' : -225, 'y' : -128};
    }
}

function area_from_url(url) {
    bits = url.split('/');
    return bits[bits.length-2];
}

$(document).ready(function() {
    if (window.location.search || window.location.href.indexOf('new-homes') != -1) {
        $('#advanced-search-section').show();
        $('#moreDetails').remove();
    }
    $('img.searchmap-overlay').hide();
    if (!$('#area_norwich').attr('checked')) {
        $('#central-norwich-only-container').hide();
    }

    $("map#search-map area").hover(function() {
        var area;
        area = area_from_url(this.href);
        rollover = $('.searchmap-rollover-info.'+area);
        rollover_position = area_rollover_position(this.coords);
        rollover.css('left', rollover_position.x);
        rollover.css('top', rollover_position.y);
        rollover.css('zIndex', 99);
        rollover.show();
        
        $('#searchmap-imagemap').attr('src','/media/wea/img/searchmaps/'+area+'.png');
        if (fecking_ie6) { make_alphaish_in_fecking_ie6($('#searchmap-imagemap')); }    
        return false;
    },
    function() {
        $('.searchmap-rollover-info').hide();
        $('#searchmap-imagemap').attr( {'src':'/media/wea/img/searchmaps/empty.png'} );
        if (fecking_ie6) { make_alphaish_in_fecking_ie6($('#searchmap-imagemap')); }    
        return false;
    });

    $("map#search-map area").click(function() {
        var was_visible;
        area = area_from_url(this.href);
        if (area == 'sheringham-and-cromer') {
            was_visible = $('img.searchmap-overlay.sheringham-and-cromer').is(':visible');
            if (was_visible) {
                $('img.searchmap-overlay.sheringham-and-cromer').hide();
                $('input#area_cromer').attr('checked', false);
                $('input#area_sheringham').attr('checked', false);
            } else {
                $('img.searchmap-overlay.sheringham-and-cromer').show();
                $('input#area_cromer').attr('checked', true);
                $('input#area_sheringham').attr('checked', true);
            }
        } else {
            if ($('input#area_'+area).attr('checked') == true) {
                $('input#area_'+area).attr('checked', false);
                $('img.searchmap-overlay.'+area).hide();
                $('#search-areas input#area_none').attr('checked', false);
                if (area == 'norwich') {
                    $('#central-norwich-only-container').hide();
                    $('#central_norwich_only').attr('checked', false);
                }
            } else {
                $('input#area_'+area).attr('checked', true);
                $('img.searchmap-overlay.'+area).show();
                if (area == 'norwich') {
                    $('#central-norwich-only-container').show();
                }
            }
        }
        return false;
    });

    $('#central_norwich_only').click(function(){
        if (this.checked) {
            $('#search-areas input').attr('checked', false);
            $('img.searchmap-overlay').hide();
            $('#search-areas #area_norwich').attr('checked', true);
            $('img.searchmap-overlay.norwich').show();
        }
    });

    $('#search-areas input').click(function(){
        area = this.id.substr(5);
        if (area == 'none') {
            if (this.checked) {
                $('#search-areas input').attr('checked', true);
                $('img.searchmap-overlay').show();
            } else {
                $('#search-areas input').attr('checked', false);
                $('img.searchmap-overlay').hide();
            }
            return;
        }
        if (this.checked) {
            if (area == 'sheringham' || area == 'cromer') {
                $('img.searchmap-overlay.sheringham-and-cromer').show();
            } else {
                $('img.searchmap-overlay.'+area).show();
                if (area == 'norwich') {
                    $('#central-norwich-only-container').show();
                }
            }
        } else {
            if ((area == 'sheringham' && !$('input#area_cromer').attr('checked')) ||
                (area == 'cromer' && !$('input#area_sheringham').attr('checked')) ) {
                    $('img.searchmap-overlay.sheringham-and-cromer').hide();
            } else {
                $('img.searchmap-overlay.'+area).hide();
                if (area == 'norwich') {
                    $('#central-norwich-only-container').hide();
                    $('#central_norwich_only').attr('checked', false);
                }
            }
            $('#search-areas input#area_none').attr('checked', false);
        }
    });

    $('#moreDetails').click(function() {
        if ($('#advanced-search-section').is (':visible')) {
            $('#advanced-search-section').hide();
            $('#search-headline').text('Quick Search');
        } else {
            $('#advanced-search-section').show();
            $('#search-headline').text('Advanced Search');
            $('#moreDetails').remove();
        }
        return false;
    });
});
