$(function() {
    $('#branch-map').jmap('init', {'mapZoom':8, 'mapEnableType':true, 'mapCenter':[54.5, -2.96215103656439]});
    
    var markers = [];
    var icon = new GIcon();
    icon.shadow = '/assets/theme/icons/marker-shadow.png';
    icon.iconSice = new GSize(12, 20);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    
    $('.branch-selector').click(function() {
        $('.branch-selector').removeClass('selected');
        $(this).addClass('selected');
        
        $.getJSON(
            '/about/branch-finder/data.json?',
            {'type':$(this).attr('rel')},
            function(data) {
                var map = $('#branch-map');
                var list = $('#branch-list').html(''); 

                $.each(markers, function(i, marker) {
                    map.jmap('RemoveMarker', marker);
                });
                
                $.each(data, function(i, branch) {
                    
                    switch(branch.type) {
                        case 'bs':
                            icon.image = '/assets/theme/icons/marker-green.png';
                            break;
                        case 'ea':
                            icon.image = '/assets/theme/icons/marker-red.png';
                            break;
                        case 'ag':
                            icon.image = '/assets/theme/icons/marker-yellow.png';
                            break;
                        case 'all':
                        default:
                            icon.image = '/assets/theme/icons/marker-blue.png';
                            break;
                    }
                    
                    map.jmap('AddMarker', {
                        'pointLatLng': [branch.latitude, branch.longitude],
                        'pointIcon': icon
                        //'pointHTML': '<div class="branch-marker-frame '+branch.type+'"><iframe src="/about/branch-finder/branch-info?slugs[]='+branch.slugs[0]+'slugs[]='+branch.slugs[1]+'type='+branch.type+'"></iframe></div>'
                        
                    }, function(marker, options) {
                        markers.push(marker);
                        GEvent.addListener(marker, options.pointOpenHTMLEvent, function() {
                            $('<iframe src="/about/branch-finder/branch-info?slugs[0]='+branch.slugs[0]+'&slugs[1]='+branch.slugs[1]+'&slugs[2]='+branch.slugs[2]+'&type='+branch.type+'" class="'+(branch.isCommunity == true ? 'communityBranch' : '')+'" frameborder="no" border="no"></iframe>')
                                .modal({'containerId':'branch-map-modal-'+branch.slugs.length});
                        });
                    });    
                    
                    var link = document.createElement('a');
                    $(link).attr('href', '#').text(branch.address).addClass('type-'+branch.type).click(function() {
                        $('<iframe src="/about/branch-finder/branch-info?slugs[0]='+branch.slugs[0]+'&slugs[1]='+branch.slugs[1]+'&slugs[2]='+branch.slugs[2]+'&type='+branch.type+'" class="'+(branch.isCommunity == true ? 'communityBranch' : '')+'" frameborder="no" border="no"></iframe>')
                            .modal({'containerId':'branch-map-modal-'+branch.slugs.length});
                        return false;
                    });
                    $(list).append(link);
                });
            }
        );
        
        return false;
    });
    
    $('body.site-CBSMain #branch-bs-selector, '+
      'body.site-CBSBusiness #branch-bs-selector, '+
      'body.site-CBSFinancial #branch-bs-selector, '+
      'body.site-CBSProperty #branch-ea-selector').click();
});