$(document).ready(function() {
  $("div.eventThumbnail").each(function() {
    if ( $(this).find("img").attr('src') == '?w=110' ) {
      $(this).hide();
    }
  });
  $("div.eventLocation").each(function() {
    if ( $(this).text() ) {
      var flag = false;
      if ( $(this).find("span.LocationPhone").text() ) {
        flag = true;
      }
      if ( $(this).find("span.LocationZip").text() ) {
        if ( flag ) $(this).find("span.LocationZip").append(', ');
        flag = true;
      } else if ( $(this).find("span.LocationState").text() ) {
        if ( flag ) $(this).find("span.LocationState").append(', ');
        flag = true;
      }
      if ( $(this).find("span.LocationCity").text() ) {
        if ( flag ) $(this).find("span.LocationCity").append(', ');
        flag = true;
      }
      if ( $(this).find("span.LocationAddress1").text() ) {
        if ( flag ) $(this).find("span.LocationAddress1").append(', ');
        flag = true;
      }
      if ( $(this).find("span.Location").text() ) {
        if ( flag ) $(this).find("span.Location").append(', ');
      }
    } else {
      $(this).hide();
    }
  });
  /*
  var pageTypeId, fieldId;
  pixelsilk.getPageTypes(function(json) {
    $.each(json, function() {
      if ( this.name == 'RecurringEvents' ) {
        pageTypeId = this.id;
        //alert(pageTypeId);
      }
    });
    if ( pageTypeId ) {
      pixelsilk.getPageTypeFields(pageTypeId, function(json) {
        $.each(json, function() {
          if ( this.name == 'Category' ) {
            fieldId = this.id;
            //alert(fieldId);
          }
        });
        if ( fieldId ) {
          pixelsilk.getPageTypeFieldValues(pageTypeId, fieldId, function(json) {
            getItemsIntoSelect(json, 'filter1');
          });
        }
      });
    }
  });
  */

  if ( !$("#eventItems").text() ) {
    $("div.pagination").hide();
  }
  
  $("form#eventFilterForm").submit(function() {
    //var filter = $("#filter1").val();
    if ( $("#filter1").val() ) {
      $("#filter1").val('RecurringDate,gt,' + $("#filter1").val());
    } else {
      $("#filter1").val('RecurringDate,gt,' + getFormattedDate(0));
    }
    if ( $("#filter2").val() ) {
      $("#filter2").val('RecurringDate,lt,' + getFormattedDate(1, $("#filter2").val()));
    } else {
      $("#filter2").val('RecurringDate,lt,20991231');
    }
    return true;
  });
});

function getItemsIntoSelect(data, id) {
  var select = document.getElementById(id);
  var i, str;

  clearSelectItems(id);

  // Add default value
  select.options[select.options.length] = new Option('View All', 'Category,ne,ZZZ');
  for (i = 0; i < data.length; i++) {
    str = 'Category,eq,' + data[i].listValue;
    if ( location.search && (location.search.indexOf(data[i].listValue) > -1) ) {
      select.options[select.options.length] = new Option(data[i].listText, str, true, true);
    } else {
      select.options[select.options.length] = new Option(data[i].listText, str);
    }
  }
}

function clearSelectItems(id) {
  var select = document.getElementById(id);
  var options = select.getElementsByTagName('option');
  var i;

  for (i = options.length - 1; i > 0; i--) {
    select.removeChild(options[i]);
  }
}

function getFormattedDate( daysOffset, ds ) {
  if ( ds ) {
    var d = new Date(ds.substr(0,4), parseInt(ds.substr(4,2), 10) - 1, parseInt(ds.substr(6,2), 10) + daysOffset);
  } else {
    var today = new Date();
    var d = new Date(today.getFullYear(), today.getMonth(), today.getDate() + daysOffset);
  }

  var month = d.getMonth() + 1;
  if ( month < 10 ) {
    month = '0' + month;
  }
  
  var day = d.getDate();
  if ( day < 10 ) {
    day = '0' + day;
  }
  
  return '' + d.getFullYear() + month + day;
}
