$(function() {
    // setupTabbedPropertyContent();
    grabRatesAndAvailability()
    initializePropertyMap();
    setupVirtualTourLinks();
    var postalCode = $('#postal_code').html() + '';
    var feedUrl = 'http://weather.yahooapis.com/forecastrss?p=' + postalCode;
    //getFeed(feedUrl);
  }
);

function getFeed(feed)
{
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  newScript.src = 'http://pipes.yahoo.com/pipes/9oyONQzA2xGOkM4FqGIyXQ/run?&_render=json&_callback=piper&feed=' + feed;
  document.getElementsByTagName('head')[0].appendChild(newScript);
}

function piper(feed) {
  var html = '';
  for (var i =0 ; i < feed.value.items.length; i++) {
    html += '<h3>';
    html += feed.value.items[i].title;
    html += '</h3>';
    if (feed.value.items[i].description) {
       html += feed.value.items[i].description;
    }
  }
  $('#weather').html(html);
}

var map_initialized = false;
function initializePropertyMap() {
  if (map_initialized) return;

  map_initialized = true;
  setTimeout(function() {
    $('#google_map').mapController('#property_data', '#property_data');
  }, 50);
}

// function setupTabbedPropertyContent() {
//   $('#property_content_tabs')
//     .tabs({
//       cookie: { expires: 30 },
//       cache: true,
//       spinner: '<img src="/images/spinner.gif"/>'
//     })
//     .bind('show.ui-tabs', function(e, ui) {
//       if (ui.panel.id == 'property_map') {
//         initializePropertyMap();
//       }
//     });
// 
// 
//   //select programatically since the cookie will override the selected tabs option
//   if (/#review_/.test(location.hash))
//     $('#property_content_tabs').tabs('select', 3);
// 
//   var selected = $('#property_content_tabs li').index($('#property_content_tabs li.ui-tabs-selected')[0]);
//   if (selected == 2)
//     initializePropertyMap();
// 
// }

function setupVirtualTourLinks() {

  var myWidth = 800;
  var myHeight = 600;
  var myTop = 75;
  var myLeft = 100;

  $('.virtual_tour_link').click(
      function() {
        window.open($(this).attr('href'), 'mediaWin', 'menubar=no,toolbar=no,resizable=yes,directories=no,scrollbars=yes,status=no,width='+ myWidth +',height='+ myHeight +',left=' + myLeft + ',top=' + myTop)
        return false;
      }
    )
  return true;
}
function grabRatesAndAvailability(){
  prop_id = $('input#property_id').val()
  if(prop_id != undefined){
    $.ajax({
       type: "GET",
       url: '/properties/'+prop_id+'/availabilities',
       dataType: 'html',
       success: function(data){
         $('#rates_and_availability div.main').html(data)
       }
    });
  }

}