if (typeof(Prototype) !== 'undefined') {
  var HeroPageMediaAttachment = Class.create({
    initialize: function() {
      this.element = $('hero_media_attachment');
	    this.queue = this.element.select('ul li');
	    
      var navigate = new Element('div', {id: 'navigate'});
      navigate.insert(new Element('a', {id: 'previous_img', href: '#'}).update('&larr; Previous').observe('click', this.previous.bindAsEventListener(this)));
      navigate.insert(new Element('a', {id: 'next_img', href: '#'}).update('Next &rarr;').observe('click', this.next.bindAsEventListener(this)));
      this.element.insert({top: navigate});
      
      this.update(this.currentIndex = 0);
    },
    update: function(index) {
      this.currentIndex = index;
      this.queue.each(function(element, i) {
        element[i == index ? 'show' : 'hide']();
      });
      $('next_img')[this.currentIndex < this.queue.length - 1 ? 'show' : 'hide']();
      $('previous_img')[this.currentIndex > 0 ? 'show' : 'hide']();
    },
    previous: function(event) {
      Event.stop(event);
      this.update(Math.max(this.currentIndex - 1), 0);
      return false;
    },
    next: function(event) {
      Event.stop(event);
      this.update(Math.min(this.currentIndex + 1), this.queue.length - 1);
      return false;
    }
  });

  var BuildHeroPageMediaAttachment = Class.create({
    initialize: function() {
      $('upload_button').observe('click', function(event) {
        openWindow(this.href);
        Event.stop(event);
        return false;
      });
    },

    showHeroPageMediaAttachment: function(id) {
      new Ajax.Request('/hero_page_media_attachments/show/' + id + '.json', 
        { 
          method: 'get',
          onSuccess: function(transport) {
            $('hero_page_media_attachment').update(transport.responseJSON.html);
          }
        }
      );
    }
  }); 
}

function openWindow(id) {
	var w=650;
	var h=500;
	var leftPos=(screen.width)?(screen.width-w)/2:100;
	var topPos=(screen.height)?(screen.height-h)/2:100;
	var settings='width='+w+',height='+h+',top='+topPos+',left='+leftPos+',location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars,resizable=no,dependent=no';
	var sUrl=id;
	var ccWindow=window.open(sUrl,'EverydayHero',settings);
	ccWindow.focus();
}

function CreateBookmarkLink(title,url) {
	if (window.sidebar) {                      // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, '');
	} else if ( window.external ) {            // IE Favorite
		window.external.AddFavorite( url, title); 
	} else if (window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

var FormHelper = {
  disableSubmitButtonByEvent: function(event) {
    var theForm = Event.element(event);
    var submitButton = theForm.commit;  
    this.disable(submitButton);
  },

  disableSubmitButton: function(theForm) {
    var submitButton = theForm.commit;  
    this.disable(submitButton);
  },

  disable: function(submitButton) {
    submitButton.value = 'Processing...';
    $(submitButton).disable();
  }
}

function addFormHelperListener() {
  var formsInPage = document.forms;
  for (var i = 0; i <= formsInPage.length; i++) {        
    Event.observe(formsInPage[i], 
                  'submit',
                  FormHelper.disableSubmitButtonByEvent.bindAsEventListener(FormHelper));
  }     
}    

function addListeners(e) {
  addFormHelperListener(); 
}
