// When object is available, do function fn.
function when(obj, fn) {
  if (Object.isString(obj)) obj = /^[\w-]+$/.test(obj) ? $(obj) : $(document.body).down(obj);
  if (Object.isArray(obj) && !obj.length) return;
  if (obj) fn(obj);
}

function restripe(form_row, new_row) {
  if(!$(form_row).toggleClassName('zebra').hasClassName('zebra')) {
		$(new_row).toggleClassName('zebra');
	}
	return;
}

function clearRadioButtons(input_name) {
	var r = document.getElementsByName(input_name);
	for (var i = 0; i < r.length; i++) { r[i].checked = false; }
}

function toggleCheckboxes(input_name) {
	var r = document.getElementsByName(input_name);
	for (var i = 0; i < r.length; i++) {
		if(r[i].checked) { r[i].checked = false; }
		else { r[i].checked = true; }
	}
}


/* -- Jed's Accordion --  */
function accReveal(element) {
	$(element).removeClassName('accRevealed');
	
	$(element).siblings().each(function(el){
		if(el.hasClassName('accRevealed') && $(el).visible()) {
			new Effect.SlideUp(el,{duration:0.5}); 
			el.removeClassName('accRevealed');
		}		
	});
	
	new Effect.toggle($(element),'slide',{duration:0.5});	
	$(element).addClassName('accRevealed');
}



/* String.js ---------------------------------------------------------------*/
Object.extend(String.prototype, {
  upcase: function()
  {
    return this.toUpperCase();
  },

  downcase: function()
  {
    return this.toLowerCase();
  },
  
  toInteger: function()
  {
    return parseInt(this);
  },

  /* TODO - the replace commands here should still be optimized. */
  toSlug: function()
  {
    return this.strip().downcase()
        .replace(/[àâ]/g,"a").replace(/[éèêë]/g,"e").replace(/[îï]/g,"i")
        .replace(/[ô]/g,"o").replace(/[ùû]/g,"u").replace(/[ñ]/g,"n")
        .replace(/[äæ]/g,"ae").replace(/[öø]/g,"oe").replace(/[ü]/g,"ue")
        .replace(/[ß]/g,"ss").replace(/[å]/g,"aa")
        .replace(/[^-a-z0-9~\s\.:;+=_]/g, '').replace(/[\s\.:;=+]+/g, '_');
  }
});

// http://blog.airbladesoftware.com/2007/8/17/note-to-self-prevent-uploads-hanging-in-safari
/* A pretty little hack to make uploads not hang in Safari. Just call this
 * immediately before the upload is submitted. This does an Ajax call to
 * the server, which returns an empty document with the "Connection: close"
 * header, telling Safari to close the active connection. A hack, but
 * effective. */
function closeKeepAlive() {
  if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
    new Ajax.Request("/ping/close", { asynchronous:false });
  }
}

function zebra(row) {
	if(!row.previous() || !row.previous().hasClassName('zebra')) { row.addClassName('zebra'); }
}


document.observe('dom:loaded', function() {
	when('notice', function(notice) {
    new Effect.BlindDown(notice, {duration: 0.4});
    // new Effect.BlindUp(notice, {delay: 3, duration: 0.4});
  });

	when($$('table.doZebra tbody tr'), function(e) {
		e.each(function(r) { zebra(r) });
	});
	
	when($$('.accItem'), function(e) {
		e.each(function(el) { if(!el.hasClassName('accRevealed')) { el.hide(); } });
	});
	
	when($$("abbr,acronym,.help,.doToolTip"), function(e){
		e.each(function(el) { new Tooltip(el, {mouseFollow: false, appearDuration: .10,delay: 0, opacity: .95}); });
	});
});