function highlight(div, color) {
	new Effect.Highlight(div, {
		startcolor: color,
		duration : 1.5
	});
}

function toggle_div(div_id){
	new Effect.toggle(div_id, 'slide', { duration: 2.0 });
}



function flash_divs(request){
	
	if(request.request != null){
		in_div = "div#"+request.request.container.success + " ";
	} else{
		in_div = "";
	}
	var els = $$(in_div + "div.message");
	els.each( function(el) {
		highlight(el, "#00FF00");
	});

	var els = $$(in_div + "div.error");
	els.each( function(el) {
		highlight(el, "#FF0000");
	});
	
	var els = $$(in_div + "div.todo");
	els.each( function(el) {
		highlight(el, "#FFFF00");
	});
	
	
	
	

	
	
}

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

Event.observe(window, 'load', function() {
    $$('a[rel="external"]').each(function(link){
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
            link.writeAttribute('target','_blank');
        }
    });
});



function loader_callback(div_id) {
	$(div_id).update("<img class = 'loader' src='/epetition_core/public/images/loader.gif'/>");

}

function close_div(div_id) {
	new Effect.SlideUp($(div_id), {
		duration : .5,
		afterFinish : function(obj) {
			obj.element.update("");
			obj.element.show();
		}
	});
	// new Effect.SlideUp($(div_id), {duration:1.5});
	return false; // stops link from being followed.
}

function open_div(div_id) {
	new Effect.SlideDown(div_id, {
		duration : .5
		//beforeStart : function(obj) {
		//	obj.element.show();
		//}
	});
	// new Effect.SlideUp($(div_id), {duration:1.5});
	return false; // stops link from being followed.
}

function check_serialize(form) {

	alert($(form).serialize());
}


function close_subforms()
{
	subforms_with_content = $$('div.subform').findAll(function(el) { return el.innerHTML.length > 0; });
	/* Returns true if:
	 * 1) None have content
	 * 2) Some have content but form not modified (content cleared before return)
	 * 3) Some have content and user confirms they can be closed (content cleared before return)
	 **/
	return (subforms_with_content.size() == 0 ||
			((subforms_with_content.findAll(function(el) { return el.hasClassName('modified'); }).size()==0 || confirm(get_close_subform_confirm_msg(subforms_with_content)))
					&& subforms_with_content.each(function(e){e.removeClassName('modified').innerHTML = ''})));
}

function get_close_subform_confirm_msg(subforms_with_content)
{
	// TODO: Currently relying on the first <legend> element containing a suitable title for subform
	title = subforms_with_content.first().select('legend').first().innerHTML;
	return close_modified_subform_msg.replace('%s', title);
}

function listen_for_change(form_div_id)
{
	$$('#'+form_div_id+' input,#'+form_div_id+' textarea','#'+form_div_id+' select').each(
			function(form_div_id){
				return function(el)
				{
					el.observe('change', function(e){$(form_div_id).addClassName('modified');});
					$(form_div_id).observe('submit', function(){$(form_div_id).removeClassName('modified')})
				};
			}(form_div_id)
		);
}
