/*
 * Sitewide javascript goes here
 */

$(document).ready(function() {

  $("div.collapsed").each(function() {
    $(this).attr('data-height', $(this).height());
    $(this).css({height: '330px'});
    var elink = $(document.createElement('a')).attr('href','#')
      .html('Expand to read full itinerary &darr;').addClass('expander')
      .click(function(e) {
        var target = $(this).prev('.collapsible');
        if(target.hasClass('collapsed')) {
          var new_height = parseInt(target.attr('data-height')) + $(this).height() + 10;
          target.animate({height: new_height}, 250);
          target.removeClass('collapsed').addClass('expanded');
          $(this).html('Hide full itinerary &uarr;')
        } else {
          target.animate({height: '330px'}, 250);
          target.removeClass('expanded').addClass('collapsed');
          $(this).html('Expand to read full itinerary &darr;')
        }
        return false;
      });
    $(this).after(elink);
  })

  if ($.datepicker) {
    $('.dateinput').datepicker({
      dateFormat: 'dd/mm/yy',
      showOn: 'both',
  	 buttonImage: '/static_media/swoop/img/icons/calendar.gif',
  	 buttonImageOnly: true
    });
  }

  $("#search input").focus(function() {
    $(this).css('background-position', '-200px -200px');
  }).blur(function() {
    if($(this).val()=='') {
      $(this).css('background-position', '10px 50%');
    } else {
      $(this).css('background-position', '-200px -200px');
    }
  }).blur();

  // Change all telephone links to tel: for iPhone. Note that
  // tel: is actually the standard...!
  if (navigator.userAgent.match(/iPhone/)) {
    $('.telephone a').each(function() {
      var existing = $(this).attr('href');
      $(this).attr('href', existing.replace('callto:', 'tel:'));
    });
  }

  $('.more-trips').click(function(){
     // toggle the next hidden trips sibling
     $(this).nextAll(".hidden-trips").toggle();

     //toggle the link text
     if($(this).text() == 'Hide trips'){
         // get the dynamic link text from the store next door
         $(this).text($(this).next(".store").text());
         $(this).addClass('more-closed');
         $(this).removeClass('more-open');
     } else {
         // store the dynamic title in a sibling span
         $(this).next(".store").text($(this).text());
         $(this).text('Hide trips');
         $(this).addClass('more-open');
         $(this).removeClass('more-closed');
     }

     return false;
  });

  // add class to empty submenus so we can hide borders
  $('ul.sub').each(function(){
      items = $(this).find('li');
      if(items.length == 0){
          $(this).addClass('empty');
      }
  })

  // add class to last item in nav lists
  $('nav.contextual ul.sf-menu>li:last').addClass('lastitem');

  doWhenGoing();

});

// for chrome/webkit - dimensions not set until img has loaded
$(window).load(function() {
   autoCaption();
});

// display contents of alt attribute over bottom of photos
function autoCaption(){
    $('.auto-caption').each(function(){
        var img = $(this).find('img');
        $(this).append('<span class="caption">' + img.attr('alt') + '</span>');
        caption = $(this).find('.caption');
        img.removeAttr("width");
        img.removeAttr("height");
        p_left = parseInt(caption.css('padding-left').replace('px',''));
        p_right = parseInt(caption.css('padding-right').replace('px',''));
        caption_width = 280; // hardcode for now!!
        caption_width = (img.width() - (p_left + p_right));
        caption.width(caption_width);
        caption.show('slow');
    });
}

// used to populate depature date field in modal form
function setDepartureDate(sDate){
    departureDate = sDate;
}

// take contents of definition list and
// create select list with switching
function doWhenGoing(){
    //alert('ok');
    $('#when-going dl').hide();
    var headings = $('#when-going dt');
    var texts = $('#when-going dd');
    var select_html = '<select>';
    headings.each(function(){
        select_html += '<option value="' + $(this).html() + '">' + $(this).html() + '</option>';
    });
    select_html += '</select>';
    $('#when-going .select-wrapper').html(select_html);

    // put first dd content in the text wrapper
    var first_text = texts.eq(0);
    $('#when-going .text-wrapper').html(first_text.html());

    // set up listener on select
    $('#when-going select').change(function(){
        var item = $(this).attr("selectedIndex");
        var item_text = texts.eq(item);
        $('#when-going .text-wrapper').html(item_text.html());
    });

}

var departureDate = '';

