function searchMenu(current_obj, menu_obj){
  var current = $(current_obj);
  var menu = $(menu_obj);	
  var display = menu.getStyle('display');
  if (display == "none") {	
  	var position = current.viewportOffset();
  	var left = position[0];
  	var top = position[1];

	menu.absolutize();
  	menu.setStyle({
	  position: 'absolute',
	  top: top+'px',
  	  left: left+'px'
  	});
	menu.show();
  } else {
	menu.hide();
  }
}

function filterSearch(form_field_obj, switched_id) {
	var form_field = $(form_field_obj);
	form_field.value = switched_id;
	$('search-form').submit();
}

function filterSearchLocation(type_field_obj, switched_type, id_field_obj, switched_id) {
	var id_form_field = $(id_field_obj);
	id_form_field.value = switched_id;
	var type_form_field = $(type_field_obj);
	type_form_field.value = switched_type;
	$('search-form').submit();
}


function searchMenuSwitch(menu_link_obj, form_field_obj, link_label, switched_id, switched_name, current_obj, menu_obj) {
	var menu_link = $(menu_link_obj);
	var form_field = $(form_field_obj);
	form_field.value = switched_id;
	if (menu_link_obj == 'event_tags_menu_link') {
		//$('venue_tags_menu_link').update("<em>"+link_label + ':</em> ' + switched_name);
	} else if (menu_link_obj == 'venue_tags_menu_link') {
		//$('event_tags_menu_link').update("<em>"+link_label + ':</em> ' + switched_name);
	} else {
		menu_link.update("<em>"+link_label + ':</em> ' + switched_name);
		searchMenu(current_obj, menu_obj);
	}
}

function set_class_name(element, class_name) {
  new Element.ClassNames(element).set(class_name);
}
function auto_complete_on_select(element, selectedElement)
{
  var entityParts = selectedElement.id.split('::');
  var entityIdField = entityParts[0];
  var entityId   = entityParts[1];
  var entityClassField   = entityParts[2];
  var entityClass   = entityParts[3];
  document.getElementById(entityIdField).value = entityId;
  document.getElementById(entityClassField).value = entityClass;
}

function updateMe() {
//function updateMe(post_id) {
		//new Ajax.Request('/sms_messages/send_data/'+post_id, { method: 'get' });
		new Ajax.Request('/sms_messages/send_data/', { method: 'get' });
		// NEED TO ADD Callbacks
};

function updateGuideActivityList(guide_id) {
	new Ajax.Request('/guides/send_data/'+guide_id, { method: 'get' });
};

function updateFairActivityList(fair_id) {
	new Ajax.Request('/fairs/send_data/'+fair_id, { method: 'get' });
};

/// ///Truncate w/js thru prototype (but we are going to do it the old fashioned way)
/// var TruncateIt = Class.create({
///   initialize: function(sTruncate) {
///     var toTruncate = (typeof sTruncate != 'undefined') ? $$(sTruncate) : $$('.truncate');
///     var len = 300;
///     for (var i = 0; i < toTruncate.length; i++) {
///       toTruncate[i].innerHTML=toTruncate[i].innerHTML.truncate(len);
///     }
///   }
/// });
/// 
/// document.observe('dom:loaded', function(){
///   var truncate = new TruncateIt('.truncate');
/// });

// gets an array of all elements with `classname`
function getElementsByClass(classname)
{
    var elements = new Array();
    var inc = 0;
    var alltags = document.all ? document.all : document.getElementsByTagName('*');
    for (var z = 0; z < alltags.length; z++)
    {
        if (alltags[z].className == classname)
        {
            elements[inc++] = alltags[z];
        }
    }
    return elements;
}


// loads tag highlighting if we need it
window.onload = function()
{
    el = document.getElementById('tag_input');
    if (el)
    {
        highlight_tags(el);
    }
}

// adds or removes a tag
function tag_swap(el, name)
{
    if (!find_tag(el, name))
    {
        var normalized = el.value.replace(/^\s*|\s*$/g,'');
        el.value = (normalized == '' ? '' : normalized + ' ') + name;
        highlight_tags(el);
    }
    else
    // tag already exists, remove it
    {
        tags = el.value.split(' ');
        new_tags = new Array();
        for (var i = 0; i <= tags.length; i++)
        {
            if (tags[i] != name)
            {
                new_tags[new_tags.length] = tags[i];
            }
        }
        el.value = new_tags.join(' ');
        clear_highlight();
        highlight_tags(el);
    }
}

// see if a tag is already in the block
function find_tag(el, name)
{
    tags = el.value.split(' ');
    for (var i = 0; i <= tags.length; i++)
    {
        if (tags[i] == name)
        {
            return true;   
        }
    }
    return false;
}

// highlights tags that are already there
function highlight_tags(el)
{
    tags = el.value.split(' ');
    clear_highlight();
    for (var i = 0; i <= tags.length; i++)
    {
        temp_tag = document.getElementById('tag_' + tags[i]);
        if (temp_tag)
        {
            temp_tag.setAttribute('class', 'tag_selected');
        }
    }
}

// clears all highlighted tags
function clear_highlight()
{
    highlighted_tags = getElementsByClass('tag_selected');
    for (var x = 0; x < highlighted_tags.length; x++)
    {
        highlighted_tags[x].setAttribute('class', 'tagging');
    }
}

function MM_jumpMenu(targ,selObj,restore){ 
			eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
			if (restore) selObj.selectedIndex=0; }

function closeKeepAlive() {
  if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
    new Ajax.Request("/photo/close", { asynchronous:false });
  }
}

function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();
}

function remove_fields_for_existing(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".existing_image").hide();
}

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).up().insert({
    before: content.replace(regexp, new_id)
  });
}