function addEvent(element, type, handler)
{
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else
	{
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers)
		{
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
	
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler)
{
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event)
{
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers)
	{
		if (!Object.prototype[i])
		{
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}

	if (this.$$handler) this.$$handler = null;

	return returnValue;
}

function fixEvent(event)
{
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function()
{
	this.returnValue = false;
}
fixEvent.stopPropagation = function()
{
	this.cancelBubble = true;
}

var g_available_pages = ["local_upload", "remote_upload", "text_paste", "manage"];

function switch_to(id)
{
  for (var i = 0; i < g_available_pages.length; i++)
  {
    var cur_id = g_available_pages[i];
    
    if (cur_id != id)
    {
      Element.hide(cur_id);
      $(cur_id + "_menu").className = "";
    }
  }
  
  Element.show(id);
  $(id + "_menu").className = "selected";
}

var g_upload_ids = new Array();

function start_upload_updater()
{
  //var file_ids = g_upload_ids.join(",");
  //new Ajax.PeriodicalUpdater("latest_files_updater", "/latest_updater.php", {evalScripts: true, parameters: "file_ids=" + file_ids + "&alt_class=" + first_latest_file_alt_class(), frequency: 5});
}

function do_upload_updater(req)
{
  //alert(req.responseText);
}

function upload_updater_failure(req)
{
  //alert(req.responseText);
}

function in_array(haystack, needle)
{
  for (var i = 0; i < haystack.length; i++)
    if (haystack[i] == needle)
      return(true);
  
  return(false);
}

function remove_from_file_ids(id)
{
  for (var i = 0; i < g_upload_ids.length; i++)
    if (g_upload_ids[i] == id)
    {
      g_upload_ids.splice(i, 1);
      break;
    }
}

function first_latest_file()
{
  try
  {
    return($("latest_file_" + g_upload_ids[0]));
  }
  catch (e)
  {
    
  }
  
  return(null);
}

function first_latest_file_alt_class()
{
  var obj = first_latest_file();
  
  if (!obj)
    return("alt_two");
  
  try
  {
    var arr = obj.className.split(" ");
    return(arr[1]);
  }
  catch (e)
  {
    return("alt_two");
  }
}

function update_rss_label(label_id, check_elm)
{
  if (check_elm.checked)
    $(label_id).innerHTML = "This file is public.";
  else
    $(label_id).innerHTML = "This file is private.";
}

