// This is the Giving Catalogs controller.
//
// The last few lines of this file cause the controller to be instantiated as
// soon as the DOM is finished loading.

var StoryController = Class.create(ViewController, {
  initialize: function($super, element) {
    $super(element);
  },
  
  storyFollow: function(followUrl) {
    if(cApp.isAuthenticated()) {
      new Ajax.Request(followUrl, { 
			method: 'get',
			onSuccess: function(res) {
			  $('storyFollowForm').hide();
			  $('storyFollowing').show();
			  $('storyFollowers').replace(res.responseText);
			},
			onFailure: function() {
			  alert("We're sorry, there was a problem processing your request.");
			}
      });
    } else { // Not authenticated; give user a chance to get shuttled through login
      window.location.href = followUrl;
    }
	return false;
  },

  storyDisplayAllFollowers: function(followersUrl) {
    new Ajax.Updater('storyFollowers', followersUrl, {method:'GET', evalScripts:true});
  }
});

document.observe('dom:loaded', function() {
  cSC = new StoryController(document);
  var box = $('addToPortfolioBox');
  if(box) {
    box.observe('change', cSC.toggleAuthSegment);
    box.next('.checkBoxBuddy').observe('click', cSC.toggleAuthSegment);
  }
  var em = $('email');
  if(em) {
    em.observe('blur', cSC.checkRegisteredEmail);
    cSC.checkRegisteredEmail();
  }
  var pwd = $('password');
  if(pwd) {
    pwd.observe('keypress', function(ev){
      if (ev.keyCode == Event.KEY_ENTER) {
        midDonationLogin();
      }
    });
  }

  // copied from widget controller
  $$('#imageChooser').each(function(e) {
    e.observe('focus', function(ev) {
      $('imageBox').checked = true;
    });
  });
  $$('#youtubeField').each(function(e) {
    e.observe('focus',  function(ev) {
      $('videoBox').checked = true;
    });
  });
});