// Homepage JavaScripts

$(document).ready(function() {
  
  var loadedImages = {};
  var preloadImage = function (filename) {
    if (!loadedImages[filename]) {
      var loadImage = new Image();
      loadImage.src = filename;
      loadedImages[filename] = true;
    }
  };
  
  var actionIds = ['see', 'eat', 'sleep', 'shop', 'see-more'];
  var ie6 = $.browser.msie && $.browser.version=="6.0";
  var gallery = $('.homepage_marquee_images');
  var detail = $('.homepage_marquee_images .detail');
  var destinations = $('.homepage_marquee_list.listing li a');
  var currentDestination = $('.homepage_marquee_list.listing li a')[0];
  var lastRandomIndex = 0;
  var index = -1;
	if (destinations.length > 0) {
	  do {
	    index = Math.floor(destinations.length*Math.random());
	  } while (lastRandomIndex == index);
	  preloadImage(destinations[index].name);
	} else {
		index = 0;
	}
	lastRandomIndex = index;

  var shouldOmitFragment = function(destination, action) {
    var omissions = { "seattle":{ sleep: true, 
                                  shop:  true }}
    return omissions[destination] && omissions[destination][action];
  }
  
  var showDestination = function (destination) {
    gallery.css({ 'background': 'url('+destination.name+') no-repeat'});
    //if (!ie6)
      gallery.hide().show('slide', { easing: 'easeOutQuad', direction: 'left' }, 100);
    
    $('.homepage_marquee_images .detail .caption h3 a').html(destination.innerHTML);
    $('.homepage_marquee_images .detail .caption h3 a').attr('href', destination.href);
    $('.homepage_marquee_images .detail .actions p').html(destination.title);
    
    for (var i = 0; i < actionIds.length; i++) {
      $('.homepage_marquee_images .detail .actions a.'+actionIds[i]).each(function (j, link) {
        var action    = actionIds[i];
        var fragment  = (shouldOmitFragment($(destination).attr('id'), action) ? '' : '#/' + action);
        link.href     = destination.href + fragment;
      }) 
    }

    $(destination).parent().addClass('selected');
    if (currentDestination != null && $(currentDestination) != null)
      $(currentDestination).parent().removeClass('selected');
    currentDestination = destination;

    //if (!ie6)
      detail.hide().show("slide", { easing: 'easeOutQuad', direction: 'left', callback:function() {
        
      }}, 100);
      // $(document).stopTime("slidePhotoIn");
      // $(document).oneTime(500, "slidePhotoIn", function() {
      //   detail.hide().show("slide", { easing: 'easeOutQuad', direction: 'down' }, 550);
      // })
  }
  
  $('.homepage_marquee_list.listing li a').each(function (index, destination) {
    $(destination).click(function (e) {
      $(document).stopTime("refresh");
      if (currentDestination && currentDestination.id == e.target.id)
        return true;
      else
        showDestination(e.target);
      return false;
    }).hover(function (e) {
      if (currentDestination && currentDestination.id != e.target.id)
        $(e.target).parent().addClass('selected')
    }, function (e) {
      if (currentDestination && currentDestination.id != e.target.id)
        $(e.target).parent().removeClass('selected')
    });
  });
  
  $(document).everyTime(6000, "refresh", function (i) {
    if (destinations.length <= 1) {
      $(document).stopTime("refresh");
      return;
    }
    showDestination(destinations[lastRandomIndex]);
    var index = -1;
    do {
      index = Math.floor(destinations.length*Math.random());
    } while (lastRandomIndex == index);
    preloadImage(destinations[index].name);
    lastRandomIndex = index;
  });
  
  $('div.destinations-browser').click(function() {
    $(document).stopTime("refresh");
  });
  
  
  $('.dropped').pngfix();

  $("#DestinationSearch").hinty();

  var citySelect = [];
  $("#DestinationSearch").autocomplete('/destinations/autocomplete',
    {
      width: 374,
      max: 10,
      minChars: 3,
      formatItem: function(row, i, max) {
        return $('<div/>').append($('<a/>')
          .attr('class', 'ac_item'+(row[2] == "true" ? ' ac_featured' : ''))
            .append($('<span/>').attr('class', 'feat')
              .append($('<span/>').attr('class', 'arrow')
                .append(row[0])))).html();
      }
    }).result(function(e, data, formatted) {
      citySelect = data;
    });

  $(".search_destinations_form").submit(function() {
    if (citySelect.length > 0) {
      s.prop35 = 'TravelPlanning:SearchByLocation';
  		$.omnitureTracking('SearchByLocation', citySelect[0]);
  		window.location = "/destinations/" + citySelect[1];
    }
    return false;
  })
});

