page = 1;
map = null;
refresh_details = false;


jQuery(document).ready(function(){
  jQuery("#listingscont").load("/properties");
  //jQuery("div#content").load("/home/home");
  loadMap();
});


jQuery("select#parish").livequery("change", function(event) {
  page = 1;
  filterListings();
});

jQuery("select#pricerange").livequery("change", function(event) { 
  page = 1;
  filterListings();
});

jQuery("select#landtype").livequery("change", function(event) { 
  page = 1;
  filterListings();
});

jQuery("select#currency").livequery("change", function(event) { 
  page = 1;
  refresh_details = true;
  filterListings();
});

jQuery("#listingscont div.pagination a").livequery("click", function(event) {
  page = jQuery(this).attr("id");
  filterListings();
  return false;
});

jQuery("#content div.pagination a").livequery("click", function(event) {
  jQuery("#content").load(this.href)
  return false;
});

jQuery("a.show-map").livequery("click", function(event){
	resetMap();
  jQuery("div#map").toggle();
  jQuery("div#listing_properties").toggle();
  jQuery("img#hide-map").toggle();
  jQuery("img#show-map").toggle();
});

jQuery(".listings a").livequery("click", function(event) {
  jQuery("#content").load(this.href);
  return false;
});

jQuery("table.profile td a").livequery("click", function(event) {
  jQuery("#content").load(this.href);
  return false;
});

jQuery("div#property-links a").livequery("click", function(event) {
  jQuery("#content").load(this.href);
  return false;
});


jQuery(".inquire").livequery("click", function(event) {
  jQuery("#content").load(this.href);
  return false;
});


jQuery("ul.listing").livequery("click", function(event) {
  var link = jQuery(this).attr("link");
  jQuery("#content").load(link);
  return false;
});

jQuery("li.most_viewed a").livequery("click", function(event){
  jQuery("#content").load(this.href);
  return false;
});

function setupGallery() {
  jQuery(function(jQuery) { 
    jQuery("ul.gallery").galleria({
      clickNext: true,
      insert : '#main_image',
      onImage : function(image, caption, thumb) {
        if(! (jQuery.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) {
          image.css('display','none').fadeIn(1000);
        }
        caption.css('display','none').fadeIn(1000);
        var _li = thumb.parents('li');
        _li.siblings().children('img.selected').fadeTo(500,0.3);
        thumb.fadeTo('fast',1).addClass('selected');
        image.attr('title','Next image >>');
      },
      onThumb : function(thumbnail) { 
        var _li = thumbnail.parents('li');
        var _fadeTo = _li.is('.active') ? '1' : '0.3';
        thumbnail.css({ display:'none', opacity:_fadeTo }).fadeIn(1500);
        thumbnail.hover(
          function() { thumbnail.fadeTo('fast',1); },
          function() { _li.not('.active').children('img').fadeTo('fast',0.3); }
        )
      }
    }); 
  });
}

function filterListings() {
  var priceRange = jQuery("select#pricerange").val();
  var parish = jQuery("select#parish").val();
  var landType = jQuery("select#landtype").val();
  var currency = jQuery("select#currency").val();
  jQuery.get("/properties", { 
        "parish" : parish,
        "pricerange" : priceRange,
        "landtype" : landType, 
        "currency" : currency, 
        "page" : page, 
        "reset" : refresh_details }, 
        function(data) {
          jQuery("#listingscont").html(data);
          if (refresh_details) {
            refreshPriceRanges();
            var url = jQuery("input#property_url").val();
            jQuery("div#content").load(url);
            refresh_details = false;
          }
        }
  );
}

function refreshPriceRanges() {
  jQuery("span#priceranges").load("/home/price_ranges");
  jQuery("select#pricerange").val(0);
}

function addPropertyMarker(map, lat, longitude, link) {
  var latlng = new GLatLng(lat, longitude);
  var marker = new GMarker(latlng);
  GEvent.addListener(marker, 'click', function() {
    var request = jQuery.get(link + '/mapshow', function(data) {
      marker.openInfoWindowHtml(data);
    });
    var center = new GLatLng(lat + 0.0005, longitude);
    map.setCenter(center, 18, G_SATELLITE_MAP);
    jQuery("div#content").load(link);
  });
  map.addOverlay(marker);
}

function loadMap() {
  if (GBrowserIsCompatible()) {
    map_zoom = 11;
    map = new GMap2(document.getElementById("map"), { size: new GSize(450, 500)});
    map.addControl(new GLargeMapControl);
    map.addControl(new GMapTypeControl);
    resetMap();
  }
}

function resetMap() {
  if (map) {
    var map_center = new GLatLng(13.188, -59.55);
    var map_zoom = 11;
    var map_type = G_NORMAL_MAP;
    map.setCenter(map_center, map_zoom, map_type);
    // to clear an info window if present and still allow more info windows to be shown
    map.disableInfoWindow();
    map.enableInfoWindow();
  }
}
