function strip(string) {
  return string.replace(/^\s+/, '').replace(/\s+$/, '');
}

function downcase(string) {
  return string.toLowerCase();
}

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
});

$(document).ajaxSend(function(event, request, settings) {
  if (settings.type == 'GET' || settings.type == 'get' || typeof(AUTH_TOKEN) == "undefined"){ return; }
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

// Adding missing indexOf for Arrays - Thanks, IE!  (From: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays#indexOf)
if(!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(elt /*, from*/) {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0) ? Math.ceil(from) : Math.floor(from);
    if (from < 0) {
      from += len;
    }

    for (; from < len; from++) {
      if (from in this && this[from] === elt) {
        return from;
      }
    }
    return -1;
  };
}

$(function() {
  swapValues = [];
  $(".swap_value").each(function(i){
    swapValues[i] = $(this).val();
    $(this).focus(function(){
        if ($(this).val() == swapValues[i]) {
            $(this).val("");
        }
    }).blur(function(){
        if ($.trim($(this).val()) == "") {
            $(this).val(swapValues[i]);
        }
    });
  });
});

function addToItinerary(activityID, description) {
  $.ajax({
    url: '/itineraries/queue',
    type: 'POST',
    data: {'id': activityID},
    complete: function(){
      $('#add-to-itinerary-' + activityID).css("display","none");
      $('#remove-from-itinerary-' + activityID).css("display","block");
      $('#remove-from-itinerary-' + activityID + ' a').effect('highlight', 1000);
      $("#create-your-trip-button img").qtip({
        content: "This destination has been added to your list. Ready to finish planning your trip? Click Create your Trip below.",
        position: {
          corner: {
            target: "topMiddle",
            tooltip: "bottomMiddle"
          }
        },
        style: {
          name: "cream",
          tip: "bottomMiddle",
          border: {
            width: 3,
            radius: 3,
            color: "#c0a877"
          }
        }
      });
      $("#create-your-trip-button img").mouseover();
      // Omniture
      s = s_gi('azguidedev');
      s.linkTrackEvents="event13";
      s.linkTrackVars="events,eVar11";
      s.events = "event13";
      s.eVar11 = description;
      s.tl(this, 'o', 'Add To Itinerary');
    }
  });
}

function removeFromItinerary(activityID) {
  $.ajax({
    url: '/itineraries/remove',
    type: 'POST',
    data: {'id': activityID},
    complete: function(){
      $('#remove-from-itinerary-' + activityID).css("display","none");
      $('#add-to-itinerary-' + activityID).css("display","block");
      $('#add-to-itinerary-' + activityID + ' a').effect('highlight', 1000);
      existingQTip = $("#create-your-trip-button img");
      if(existingQTip != null) {
        $(existingQTip).qtip("destroy");
      }
    }
  });
}

$(document).ready(function(){
  $("a.travel-deal").click(function(){
    s = s_gi("azguidedev");
    s.linkTrackVars="events,eVar6";
    s.events = "event11";
    s.eVar6 = "Travel Deals";
    s.tl(this, 'o', "Travel Deals");
  });
  $("a.partner").click(function(){
    r = $(this).attr('rel');
    s = s_gi("azguidedev");
    s.linkTrackVars = "events,eVar5";
    s.events = "event9";
    s.eVar5 = $(this).attr(r);
    s.tl(this, 'o', r);
  });
});
