// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var carouselSelector = "#horizontal_carousel ul li";
var selectedCarouselSelector = "#horizontal_carousel ul li.selected";
var politicianView = "prayers";
var currentFramePoliticianId = null;
var theCarousel = null

Event.observe( window, 'load', initApp); 

function initApp(event) {
  initCarousel();
  hideFlashMessages();
  setupSamplePrayers();
  setupMessageCheckbox();
  initFacebookConnect();
  initWriteLink();
}

function hideFlashMessages() {
  if ($("flash-messages") && $$(".flash-messages .flash").size() > 0) {
    $("flash-messages").fade({duration: 5.0, from: 1, to: 0});
  }
}

function initCarousel() {
  if ($("horizontal_carousel")) {
    theCarousel = new UI.Carousel("horizontal_carousel");
    $$(carouselSelector).each(function(element) {
      Event.observe(element, 'click', setCurrentPolitician);
    })
    $$(carouselSelector)[0].addClassName('selected');
  }  
}

function setCurrentPolitician(event) {
  makePoliticianCurrent(this);
}

function makePoliticianCurrent(element) {
  $$(carouselSelector).each(function(el) {
    el.removeClassName('selected');
  })
  element.addClassName('selected');
  Hobo.ajaxRequest('/politicians/' + politicianIdFromElementId(element.identify()), ['current-politician'], {
    method: 'get', message: "loading...", onComplete: updatePageForNewPolitician});
}

function makePoliticianCurrentFromId(politicianId) {
  makePoliticianCurrent($('politician-' + politicianId));
}

function politicianIdFromElementId(elementId) {
  return elementId.gsub('politician-', '');
}

function hilightElements(elements) {
  elements.each(function(id) {
    new Effect.Highlight(id, { startcolor: '#ffff99', endcolor: '#ffffff' });
  })
}

function updatePageForNewPolitician() {
  myLytebox.initialize();
  hilightElements(['text-prayers', 'vital-stats']);
  if (politicianView == "info") {
    $('prayer-content-container').hide();
    $('vital-stats-container').show()
    $('info-tab').removeClassName("selected");
    $('prayer-tab').addClassName("selected");
  }
}

function updatePageForNewTextPrayers() {
  myLytebox.initialize();
  hilightElements(['text-prayers']);
}

function updateCurrentTextPrayers() {
  var politicianId = getCurrentPoliticianId();
  Hobo.ajaxRequest('/politicians/' + politicianId, ['current-text-prayers'], {
    method: 'get', message: "loading...", onComplete: updatePageForNewTextPrayers});
}

function getCurrentPoliticianId() {
  return politicianIdFromElementId($$(selectedCarouselSelector).first().identify());
}

function updateTextPrayersToPage(pageNumber, politicianId) {
  Hobo.ajaxRequest('/politicians/' + politicianId +'/?text_prayer_page=' + pageNumber, ['current-text-prayers'], {method: 'get', message: "loading...", onComplete: updatePageForNewTextPrayers});
}

function displayInfoPanel() {
  politicianView = "info";
  Effect.Fade('prayer-content-container', {queue: 'front', duration: 0.2});
  Effect.Appear('vital-stats-container', {queue: 'end', duration: 0.2});
  $('prayer-tab').removeClassName("selected");
  $('info-tab').addClassName("selected")
}

function displayPrayerPanels() {
  politicianView = "prayers";
  Effect.Fade('vital-stats-container', {queue: 'front', duration: 0.2});
  Effect.Appear('prayer-content-container', {queue: 'end', duration: 0.2});
  $('info-tab').removeClassName("selected")
  $('prayer-tab').addClassName("selected");
}

function textFlagComplete() {
  hilightElements(['text-prayers']);
  $('text-flagged-message').show();
  Effect.Fade('text-flagged-message', {queue: 'end', duration: 5.0});
}

function setupSamplePrayers() {
  $$("ul.sample-prayer-categories li ul.sample-prayers").each(function(el) {
    el.hide();
  })
  
  $$("ul.sample-prayer-categories span.expander").each(function(el) {
    Event.observe(el, 'click', function() {
      var self = el;
      
      $$("ul.sample-prayer-categories ul.sample-prayers").each(function(el) {
        if ((el.visible() == true) && (el != self)) {
          Effect.BlindUp(el);
        }
      })
      
      var list = el.up().down('ul.sample-prayers');
      if (list.visible() == true) {
        Effect.BlindUp(list);
      } else {
        Effect.BlindDown(list);
      }
    })
  })
}

function setupMessageCheckbox() {
  if ($('select_all')) {
    Event.observe('select_all', 'click', function(ev) {
      var self = this;
      $$('.message-checkbox').each(function(el) {
        el.checked = self.checked;
      })
    })
  
    $$('.message-checkbox').each(function(el) {
      Event.observe(el, 'click', function(ev) {
        if (this.checked == false) {
          $('select_all').checked = false;
        }
      })
    })
  }
}

function selectLyteboxPolitician(politicianId) {
  el = $(politicianId);
  makePoliticianCurrent(el)
  FirstOfAll.scrollCarousel();
}

function getCurrentPoliticianId() {
  return politicianIdFromElementId($$(selectedCarouselSelector)[0].id)
}

var FirstOfAll = {
  endOfLytebox : function() {
    if ((currentFramePoliticianId) && (currentFramePoliticianId != getCurrentPoliticianId())) {
      makePoliticianCurrentFromId(currentFramePoliticianId);
      FirstOfAll.scrollCarousel();      
    }
  },
  
  scrollCarousel : function() {
    $$(carouselSelector).each(function(el, index) {
     if (el.id == ("politician-"+currentFramePoliticianId)) {
       theCarousel.scrollTo(index);
     }
    })
  }
}

function setCurrentFramePolitician(id) {
  currentFramePoliticianId = id;
}

function initFacebookConnect() {
  if ($$('.facebook-share')) {
    $$('.facebook-share').each(function(el) {
      el.observe("click", function() {
        FB.Connect.streamPublish(fbShareMessage);
      })
    })
  }
}

function initWriteLink() {
  if ($('write-link')) {
    $('write-link').observe("click", function(ev) {
      if (politicianView != "prayers") {
        displayPrayerPanels();
      }
    });
  }
}