$(document).ready(function() {

    var outFront = new OutFront();
    
    //load City Tabs
    $('.city #tabs').tabs({
      cache: false,
      load: function(event, ui) {
        outFront.installMaps();
      },
      cookie: {
        expires: 1
      },
      select: function(event, ui) {
        var tabIndex;
        tabIndex = $('.city #tabs').tabs('option', 'selected');
        return $.ajax({
          url: _base_path + 'ajax/write_to_session/tab/' + tabIndex + '/',
          success: function(data) {},
          error: function() {
            return false;
          }
        });
      },
      create: function(event, ui) {
        if(typeof window.filter == 'function') {
            window.filter(); //run this here so it would only work on cities pages
        }
        return $('.city #tabs').show(100);
      }
    });

    //Load Apartment tabs
    $('.apartment #tabs').tabs({
      cache: false,
      load: function(event, ui) {

        outFront.installMaps();
      },
      cookie: { 
        expires: 1
      },
      create: function(event, ui) {
        return $('.apartment #tabs').show(100);
      }
    });

    $('.apartment-details .map-tab').click(function(){
        google.maps.event.trigger(outFront._map, 'resize');
        outFront._map.setCenter(outFront._latlng);

		var thisZoom = outFront._zoom;
		if (parseInt(thisZoom) < 15) {
			 thisZoom = 21;
		}
		outFront._map.setZoom(thisZoom);
    });

	$('.city .map-tab').click(function() {
        if ($('#osgoode-map').length < 1) {
            google.maps.event.trigger(outFront._map, 'resize');
            outFront._map.fitBounds(outFront._bounds);
        }
	});

    //click here to view apartments link
    $('#osgoode-map-instructions a').bind('click', function(event) {
      return $('#tabs').tabs('select', 0);
    });

    //erase from session when user clicks on any links in any nav
    $('nav ul li a, section.footer-links ul li a').bind('click', function(event) {
      var nextUrl, url;
      url = window.location.href;
      nextUrl = $(this).attr('href');
      if (url.indexOf("apartments-for-rent") !== -1 || url.indexOf("apartmentsforrent") !== -1) {
        $.ajax({
          url: _base_path + 'ajax/erase_from_session/min_bed/order/regions/tab',
          success: function(data) {
            return window.location = nextUrl;
          },
          error: function() {
            return false;
          }
        });
      }
      return true;
    });


    order = 'asc';

    $('div[data-sortable="true"] article.apartment').tsort({
      attr: 'data-min-rate',
      order: order,
      place: 'end'
    });

    $('[data-filter]').bind('change', function(event) {
      return window.filter();
    });

    window.filter = function() {
      var min_bed, numVisible, regions;
      numVisible = 0;
      min_bed = $('input:radio[name=bed]:checked').val();
      order = $('input:radio[name=price]:checked').val();
      $('div[data-sortable="true"] article.apartment').tsort({
        attr: 'data-min-rate',
        order: order,
        place: 'end'
      });
      regions = new Array();
      $('input:checkbox[name=region]:checked').each(function() {
        return regions.push($(this).val());
      });
      $.ajax({
        url: _base_path + 'ajax/write_to_session/min_bed/' + min_bed + '/order/' + order + '/regions/' + regions + '/tab/' + $('#tabs').tabs('option', 'selected') + '/',
        context: document.body,
        success: function(data) {},
        error: function() {
          return false;
        }
      });
      $('article.apartment').each(function() {
        var aRegion, bed, beds, region, show, stillShow, _fn, _fn2, _i, _j, _len, _len2;
        show = false;
        beds = $(this).attr('data-beds').split(',');
        aRegion = $(this).attr('data-region_id');
        if (!beds) {
          show = false;
        } else if (min_bed < 0) {
          show = true;
        } else {
          _fn = function(bed) {
            bed = $.trim(bed);
            if (min_bed <= bed) {
              return show = true;
            }
          };
          for (_i = 0, _len = beds.length; _i < _len; _i++) {
            bed = beds[_i];
            _fn(bed);
          }
        }
        stillShow = false;
        if (show) {
          if ($('input:checkbox[name=region]').length > 0) {
            _fn2 = function(region) {
              if (region === aRegion) {
                return stillShow = true;
              }
            };
            for (_j = 0, _len2 = regions.length; _j < _len2; _j++) {
              region = regions[_j];
              _fn2(region);
            }
          } else {
            stillShow = true;
          }
        }
        if (show && stillShow) {
          $(this).show();
          return numVisible = numVisible + 1;
        } else {
          return $(this).hide();
        }
      });
      $('span.apartments-count').html(numVisible);
      if (numVisible !== 1) {
        $('span.apartments-count-label').html($('span.apartments-count-label').attr('data-multiple') + '<br>' + $('span.apartments-count-label').attr('data-found'));
      } else {
        $('span.apartments-count-label').html($('span.apartments-count-label').attr('data-singular'));
      }
      return $('div.marker').each(function() {
        var aRegion, bed, beds, region, show, stillShow, _fn, _fn2, _i, _j, _len, _len2;
        show = false;
        beds = $(this).attr('data-beds').split(',');
        aRegion = $(this).attr('data-region_id');
        if (!beds) {
          show = false;
        } else if (min_bed < 0) {
          show = true;
        } else {
          _fn = function(bed) {
            bed = $.trim(bed);
            if (min_bed <= bed) {
              return show = true;
            }
          };
          for (_i = 0, _len = beds.length; _i < _len; _i++) {
            bed = beds[_i];
            _fn(bed);
          }
        }
        stillShow = false;
        if (show) {
            if ($('input:checkbox[name=region]').length > 0) {
              _fn2 = function(region) {
                if (region === aRegion) {
                  return stillShow = true;
                }
              };
              for (_j = 0, _len2 = regions.length; _j < _len2; _j++) {
                region = regions[_j];
                _fn2(region);
              }
          } else {
            stillShow = true;
          }
        }
        if (show && stillShow) {
          return $(this).show();
        } else {
          return $(this).hide();
        }
      });
    };

    if ($('html.ie7').length >= 1) {
        //IF IE7 run filter
        // modern browser run filter in osgoode map script
        if(typeof window.filter == 'function' && $('.city').length >= 1) {
            window.filter(); //run this here so it would only work on cities pages
        }
    }

    $('html.ie6 .disable-ie6').hide();
    $('html.ie7 .disable-ie7').hide();

});
