document.observe("dom:loaded", function() {
  highlightFieldRow();
  fadeFlashes();
  initCorners();  
  initEndpointEntries();
  Feedback.init();
  
  $$('a[rel=external]').each(function(e) {
    // TODO: figure out why "window.open(this.href); return false;" isn't working...
    $(e).observe('click', function() { $(this).writeAttribute('target', '_blank') });
  });
});

var Feedback = {
  init: function() {
    var default_feedback = $$('#feedback input#default')[0]
    if (default_feedback) {
      default_feedback.observe('focus', function() {
        Feedback.toggle(this)
      });
    }
    
    var feedback_submit = $$('#feedback input[type=submit]')[0]
    if (feedback_submit) {
      feedback_submit.observe('click', function() {
        Feedback.submit()
      })
    }
  },
  toggle: function(e) {
    $(e).hide();
    $('feedback-fields').show();
    $$('#feedback textarea')[0].focus();
  },
  submit: function() {
    $('feedback-loading').show()
    setTimeout('Feedback.reset()', 5000)
  },
  reset: function() {
    $$('#feedback input#default')[0].show()
    $('feedback-fields').hide()
    $('feedback_result').update('')
  }
}

function highlightFieldRow() {
  // TODO: make this work better
  // var css_class = 'yellow';
  // $$('.field input').each(function(e) {
  //   $(e).observe('focus', function() {
  //     e.up().addClassName(css_class);
  //   });
  //   $(e).observe('blur', function() {
  //     e.up().removeClassName(css_class);
  //   })
  // });
}

function initEndpointEntries() {
	$$('#endpoints-list .endpoint-entry .clicktarget').each(function(div) {
		$(div).observe('click', function(event) {
		  var element = Event.element(event);
		  if ('A' != element.tagName) {
  			window.location = div.parentNode.getAttribute('edit_path');
		  }
		})
	})
}

function fadeFlashes() {
  setTimeout(fadeMessages, 10000);
  function fadeMessages() {
    $$('p.notice, p.warning, p.error').each(function(e) {
      new Effect.Fade(e, { duration: 2.5 });
    });
  }
}

function pinOption(cb) {
  if (cb.checked) {
    new Effect.BlindDown('pin-option', { duration: 0.25 });
    $('pin-option').show();
  } else {
    new Effect.BlindUp('pin-option', { duration: 0.25 });
    $('pin-option').hide();
  }
}

function initCorners() {
	if (Prototype.Browser.IE) { return; }
	
	settings = {
		tl: { radius: 10 },
		tr: { radius: 10 },
		bl: { radius: 10 },
		br: { radius: 10 },
		antiAlias: true,
		autoPad: false
	}

	$$('.curvy').each(function(e){
		var e_ = new curvyCorners(settings, e);
		e_.applyCornersToAll();	
	});
}

// TODO: combine corner functions to accept a radius
function initSmallCorners() {
  //if (Prototype.Browser.IE) { return; }

	settings = {
		tl: { radius: 5 },
		tr: { radius: 5 },
		bl: { radius: 5 },
		br: { radius: 5 },
		antiAlias: true,
		autoPad: false
	}

	$$('.curvy-small').each(function(e){
		var e_ = new curvyCorners(settings, e);
		e_.applyCornersToAll();	
	});
}

function showManagerPassword(target) {
  if (target.secret_visible) {
    target.innerHTML = target.hidden_text;
    target.secret_visible = false;
  } else {
    target.hidden_text = target.innerHTML;
    target.innerHTML = target.getAttribute('secret');
    target.secret_visible = true;
  }
}

var CodePreview = {
  init: function() {
    this.language = null;
    this.active = null;
    this.currentCSS = 'current';
    
    $$('span.glider').each(function(e) {
      e.observe('click', this.handle.bindAsEventListener(this, e));
    }.bind(this));
    
    $($$('div.languages span')[0]).addClassName(this.currentCSS);
  },
  handle: function(event, element) {
    this.language = $(element).readAttribute('id');
    this.active = $(element)
    this.showCode();
    this.setCurrent();
  },
  hideAllLanguages: function() {
    $$('div.lang').each(function(e) { $(e).hide(); });
  },
  showCode: function() {
    this.hideAllLanguages();
    $(this.language).show();
  },
  resetLinks: function() {
    $$('span.glider').each(function(e) { $(e).setAttribute('class', 'glider'); });
  },
  setCurrent: function() {
    this.resetLinks();
    this.active.addClassName(this.currentCSS);
  }
}