// asynchronously call the undo action for the given activity 
function undo_activity(activity_id) {
    new Ajax.Request('/activities/' + activity_id + '/undo', {
        onSuccess: function(response) {
            Effect.SlideUp('activity-' + activity_id, {duration: 0.25, queue:'end'});
            notice(response.responseText);
        },
        onFailure: function(response) { failure(); }
    });
}

// generic error message
function failure() {
  alert('There seems to be some trouble with our server or with your internet connection. Please try again later. Sorry for the hassle.')
}

function toggleCheckbox(elem) {
	elem.checked = !elem.checked;
}

/* this function is used to make the size of the bookmarklet dymanic.
   called after load and when anything changes */ 
function updateBookmarkletSize() {
  var dimension = $('bookmarklet_wrapper').getDimensions();
  if (dimension.height < 100)
    dimension.height = 100;
  window.resizeTo(dimension.width, dimension.height+20);
}

// clear the excerpt (i.e., the selected text) from the bookmarklet
function clearExcerpt(container_id, input_id) {
    alert(container_id);
    alert(input_id);
    
    $(input_id).value = null;
    Effect.SlideUp($(container_id), {duration:0.2, queue:'end', afterFinish:function() {
        updateBookmarkletSize();
    }});
}
 
/* the following two functions are for enabling and disabling a form
based on whether or not the user has entered some data */

function enableWhenContentAdded(submit, input) {
  input.observe('keyup', function(e) {
    if (input.value.strip() != '')
  		submit.enable();
  	else
  		submit.disable();
	});
	
	// this is to handle the situation of when a user uses the memu to
	// cut all of the data
	input.observe('blur', function(e) {
	  if (input.value.strip() != '')
  		submit.enable();
  	else
  		submit.disable();
  });
}

function default_text(element, text) {
    element = $(element);
    if (element) {    
        // put default text in place
        if (element.value == '') {
            element.value = text;
            element.addClassName('noterms');
        }
        
        // add focus listener to clear text
        element.observe('focus', function(e) {
            if (element.hasClassName('noterms')) {
        		element.value = '';
        		element.removeClassName('noterms');
        	}
    	});
	
        // add blur listener to re-add text
        element.observe('blur', function(e) {
    		if (element.value == '') {
    			element.addClassName('noterms');
    			element.value = text;
    		}
    	});
    }
}

// function clearOnFocus(input, submit) {
//   submit.enable();
// }
// function resetOnBlur(input, submit) {
//   if (input.value == '') {
//     submit.disable();
//   }
// }

function hide(elem) {
  var element = $(elem);
  if (element) Effect.Fade(elem, {duration:0.2, queue:'end'});
}

// adds support for using two or more links to toggle corresponding "tools"
function toggle_tools(link_element, container_id) {
 var container = $(container_id);
 link_element = $(link_element);
     
     $$('.active_toggle_link').each(function (e) {
     e.removeClassName('active_toggle_link');
     });
 
 if (container.visible()) {
     Effect.Fade(container, {duration:0.5, queue:'end'});
 } else {
     $$('.toggle_tool_element').each(function (e) {
         if (e.visible() && e != container) {
             Effect.Fade(e, {duration:0.5, queue:'end'});
         }
     });
     
     link_element.addClassName('active_toggle_link');
     
     Effect.Appear(container, {duration:0.5, queue:'end', afterFinish:function() {
         if (container_id == 'new_code_application_wrapper') {
                resize_code_list();
         }
     }});
 }
 
 return false;
}

// functions for controlling the notice bar (current) at the top of all pages

function notice(text) {
    $('notice_content').update(text);
    show_notice();
}

function hide_notice() {
	if ($('notice') && $('notice').visible()) {
    	Effect.SlideUp($('notice'), { duration:0.4, queue:'end' });
	}
}

function show_notice() {
	var notice = $('notice');
	if (notice) {
		if (!notice.visible()) {
			Effect.SlideDown($('notice'), { duration:0.5, queue:'end' });
		}
		
		setTimeout("hide_notice()", 5000);
	}
}

function set_cookie(key, value, days) {
	if (typeof key != 'string') {
	    throw "Invalid key";
	}
	if (typeof value != 'string' && typeof value != 'number') {
	    throw "Invalid value";
	}
	if (days && typeof days != 'number') {
	    throw "Invalid expiration time";
	}
	var setValue = key+'='+escape(new String(value));
	if (days) {
	    var date = new Date(); 
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var setExpiration = "; expires="+date.toGMTString();
	} else var setExpiration = "";
	var cookieString = setValue+setExpiration;
	document.cookie = cookieString;
}

Event.observe(window, 'load', function(event) {
	if ($('notice') && $('notice').visible())
        setTimeout("hide_notice()", 5000);
	set_cookie('tzoffset', (new Date()).getTimezoneOffset(), 5);
	
	$$('form').each(function(f) {
	    f.observe('submit', function(event) {
	        
	    });
	});
	
	default_text('query', 'Search');
	default_text('memo_content', 'Memo');
	default_text('code_name', 'Code');
});

