function format_time_to_12h(time){
  if(parseInt(time.split(":")[0]) > 12){
    time_show = (parseInt(time.split(":")[0]) - 12);
    if(parseInt(time.split(":")[1]) == "30"){
      time_show +=".30";
    }
    time_show +="pm";
  }else{
    if (parseInt(time.split(":")[0]) == 0){
      time_show = 12;
      if(parseInt(time.split(":")[1]) == "30"){
        time_show +=".30";
      }
      time_show +="pm";
    }else{
      time_show = parseInt(time.split(":")[0]);
      if(parseInt(time.split(":")[1]) == "30"){
        time_show +=".30";
      }
      time_show +="am";
    }
  }
  return time_show;
}

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return linkify(title);
}

$(document).ready(function() {
    
  $("a.fancybox").fancybox({
    'transitionIn'  :  'fade',
    'transitionOut'  :  'fade',
    'speedIn'    :  600, 
    'speedOut'    :  200,
  });  
  
  $('a.cakebox').fancybox({
    'titlePosition' : 'over',
    'titlePosition' : 'inside',
    'titleFormat' : formatTitle
  });
  
  var clearMePrevious = "";

  // clear input on focus
  $('.clearMeFocus').focus(function() {
    if($(this).val()==$(this).attr('title')) {
      clearMePrevious = $(this).val();
      $(this).val("");
    }
  });

  // if field is empty afterward, add text again
  $('.clearMeFocus').blur(function() {
    if($(this).val()=="") {
      $(this).val(clearMePrevious);
    }
  });
  $('a.inline_log_in').click(function() {
    $('form.search').css('display', 'none');
    $('form.signIn').css('display', 'block');
    return false;
  });
  $('a.inline_log_in_close').click(function() {
    $('form.search').css('display', 'block');
    $('form.signIn').css('display', 'none');
    return false;
  });

  // Timeago
  $("abbr.timeago").timeago();

  //TV Guide
  $(".tv-guide-nav").live("click", function(){
    week_day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    hour = parseInt($(this).attr("data-hour"));
    if(hour < 24 && hour >= 0){
      $.getJSON("/tvguide_time/"+hour, function(data) {
        legend_current = hour + " am";
        legend_next =  hour+1 + " am";
        if(hour > 12){ legend_current = (hour - 12) + " pm"; }
        if(hour+1 > 12){ legend_next =  hour+1-12 + " pm"; }
        if(hour == 0){ legend_current = "12 pm"; }
        $("td.tslot1").find("span.tv-guide-time").html(legend_current);
        $("td.tslot1").find("span.tv-guide-nav").attr("data-hour",hour-1);
        $("td.tslot2").find("span.tv-guide-time").html(legend_next);
        $("td.tslot2").find("span.tv-guide-nav").attr("data-hour",hour+1);
        $("table.tv-guide tbody").html("");
        $.each(data, function(key, value) {
          d = key.split("-");
          date = new Date(parseInt(d[0]), (parseInt(d[1]) - 1), parseInt(d[2])).getDay();
          line = "<tr><td class=\"days\">"+  week_day[date] + "</td>";
          $.each(value, function() {
            time_start = parseInt(this.start_time.split(":")[0]) + (parseInt(this.start_time.split(":")[1])/60);
            time_end   = parseInt(this.end_time.split(":")[0]) + (parseInt(this.end_time.split(":")[1])/60);
            colspan = ((time_end - time_start) == 0.5) ? "" : " colspan=\"2\"";
            line += "<td class=\"shows\""+colspan+">";
            line += "         <span>"+ this.epg.title+" <span>"+format_time_to_12h(this.start_time)+" - "+format_time_to_12h(this.end_time)+"</span></span>";
            line += "        <div>";       
            line += "           <div>";
            line += "             <p>"+this.epg.programmeEPGSynopsis+"</p>";
            line += "             <a href=\""+ this.url +"\">Watch Online</a>";
            line += "           </div>";
            line += "        </div>";              
            line += "      </td>";
          });
          line +="</tr>";
          $("table.tv-guide tbody").append(line); 
        });
      }, 'json');
    }
  });
  
});

$(function(){

  // Gift lists suggestions
  function clear_feedback_form() {
      $('#feedback_name').val('');
      $('#feedback_email').val('');
      $('#feedback_message').val('');
      $('#gift-list-suggestion .form_flash').html('');
  }
  $('#suggest-gift-list-link').fancybox({'onClosed':function(){
    clear_feedback_form();
  }});
  $('#gift-list-suggestion').submit(function() {
    $.ajax({
     type: "POST",
     dataType: "json",
     url: "/feedbacks",
     data: $(this).serialize(),
     success: function(resp){
       if (resp['errors']==undefined) {
        clear_feedback_form();
        message = '<p class="flash notice">Thank you for your suggestion!</p>'
        $('#gift-list-suggestion .form_flash').html(message);
        $.fancybox.center();
       }
       else {         
         error_messages = '<ul class="flash alert"><li>'+resp['errors'].join('</li><li>')+'</li></ul>'
         $('#gift-list-suggestion .form_flash').html(error_messages);
         $.fancybox.center();
       }
     }
   });
    return false;
  });


  // Wedding PA form
  function clear_pa_feedback_form() {
      $('#feedback_name').val('');
      $('#feedback_email').val('');
      $('#feedback_phone').val('');
      $('#wedding-pa-form .form_flash').html('');
  }
  $('#wedding-pa-form').submit(function() {
    $.ajax({
     type: "POST",
     dataType: "json",
     url: "/feedbacks",
     data: $(this).serialize(),
     success: function(resp){
       if (resp['errors']==undefined) {
        clear_pa_feedback_form();
        message = '<p class="flash notice">Thank you! We will contact you shortly!</p>'
        $('#wedding-pa-form .form_flash').html(message);
        $('#wedding-pa-form-fields').hide();
       }
       else {
         error_messages = '<ul class="flash alert"><li>'+resp['errors'].join('</li><li>')+'</li></ul>'
         $('#wedding-pa-form .form_flash').html(error_messages);
       }
     }
   });
    return false;
  });

});
