

var has_map;
var map;
var markers = [];
var cluster;
var properties = {};

// ======================
// = Toggle street view =
// ======================
var streetInit = false;
function showMap() {
    $('#street_link').html('Show street');
    $('#street').hide();
    $('#map').show();
}
function showStreet(lat,lng) {
    $('#street_link').html('Show map');
    $('#map').hide();
    $('#street').show();
    	        
    if(!streetInit) {
        var options = { 
            latlng: new GLatLng(lat,lng)
        };
        var myPano = new GStreetviewPanorama(document.getElementById("street"), options);	        
        streetInit = true;
    }
}
function on_street_link_click(lat,lng) {
    if($('#street').css('display') == 'none') {
        showStreet(lat,lng);
    }
    else {
        showMap();
    }    
    return false;
}


var iconHouse = new GIcon(G_DEFAULT_ICON);
iconHouse.iconSize = new GSize(16, 18);
iconHouse.image = '/lettings/images/house.gif';
iconHouse.iconAnchor = new GPoint(8, 18);
iconHouse.shadow = false;

var iconCluster = new GIcon(iconHouse);
iconCluster.image = '/lettings/images/cluster.gif';   

function getMarkerContent(marker) {
    var suffix = parseInt(marker.info.price) < 5000 ? ' pcm' : ''
    return '' + 
    '<div class="infowindow">'+
        (marker.info.image ? '<img src="'+marker.info.image+'"/>' : '') +
        '<div class="infowindowtext">' +
            '<h2>'+marker.info.title+' <span class="gray"> - &#163;'+marker.info.price + suffix + '</span></h2>' + 
            '<p>'+marker.info.desc+'</p>' +
            '<p class="readmore"><a href="' + marker.info['uri'] + '">More Details</a></p>' + 
        '</div>'
    '</div>';
}

function addInfoWindow(marker) {
    GEvent.addListener(marker, "click", function() {
        var opts = {
            maxWidth: 300
        }
        marker.openInfoWindowHtml(getMarkerContent(marker), opts);
    });        
}


function onClusterMarkerClick(args) {
    var tabs = [];
    $.each(args.clusteredMarkers, function(i, marker) {
        if(i == 3 && args.clusteredMarkers.length > 4) {
            var tab = new GInfoWindowTab(i + 1, '<p>Found more than 4 properties in this spot, please zoom in further.</p>');
            tabs.push(tab);
            return false    
        }
        else {
            var tab = new GInfoWindowTab(i+1, getMarkerContent(marker));
            tabs.push(tab);            
        }

    })

    args.clusterMarker.openInfoWindowTabsHtml(tabs, {maxWidth: 350});
    
}

function on_submit_search(form) {
    form.t.value = window.t
    return true
}

function print(loc) {
    window.open(loc,'print','width=700,height=500,scrollbars=yes,resizable=yes')
}

$(function() {
    has_map = document.getElementById("map") ? true : false
    if(has_map) {
        map = new GMap2(document.getElementById("map"));        
        map.setUIToDefault();        
        map.removeMapType(G_HYBRID_MAP)
        map.removeMapType(G_SATELLITE_MAP)
        map.removeMapType(G_PHYSICAL_MAP)              
        map.removeMapType(G_NORMAL_MAP)        
        
        GEvent.addListener(map, "moveend", function() {
            cluster.refresh()
        });
        GEvent.addListener(map, "zoomend", function() {
            cluster.refresh()
        });        
    }
    

    if($('.property .images')[0]) {
        $('.property .images').popbox();
    }


    
        
    $("table.sortable").each(function() {
        var sortList = [];
        if(this.id && $.cookie('tablesorter_' + this.id)) {
            var sortList = eval($.cookie('tablesorter_' + this.id))
        }
        else {
            // sort on price, desc, as default
            var sortList = [[3,0]]
        }
        $(this).tablesorter({
            sortList: sortList
        }).bind('sortEnd', function(e) {
            if(e.target.id) {
                var sortList = '[['+e.target.config.sortList[0]+']]';  // [col_num, sort_order 0|1]
                $.cookie('tablesorter_' + e.target.id, sortList)
            }
        })
    })        

    
    
    
    
    
})	   
