//Show special links for those with permission
//Look for 'layout_perm_sprout' class
//For each found, call the controller's 'perm_sprout' action
//which will replace the element found with 

function perm_sprout(min,title,tag,controller) {
    //min should be the minimum permission required to show the link
    //title should be the text title of the link to display

    //tagperm = getcookie(<%=@tag.id%>);
    tagperm = getcookie(tag);
    if (!tagperm) {
      tagperm = 0;
    }

    //Select all elements with class layout_perm_sprout
    //This is now generic enough to use embedded in a template
    perm_array = $$('.layout_perm_sprout');
    
    perm_array.each( function(e) {
      if ((tagperm == -1) || (tagperm >= min) ) {
        //e is the page element we're examining
        //update e if there's success in the Ajax call
        var div_id = e.readAttribute('id')

        //why not use /tag/controller/perm_sprout?
        //because:
        //- we would have to add perm_sprout as a member to ALL routes
        //- we would have to use the controller name versus a friendlier resource name
        //  in every case (eg nl_info versus info)
		var url = '/'+controller+'/perm_sprout';
		var ts = new Date().getTime();
        new Ajax.Request(url, {method: 'get', parameters:{tag:tag, update_id: div_id, title: title, ts: ts}});
        //{method: 'get', parameters:'update_id='+div_id+'&title='+title});
        //parameters:'update_id='+div_id+'&title='+title+'&start_value='+escape(e.innerHTML)})

        return false;
      }
    });

}

var saved_sortable = null;
function stateful_toggle(link,elementIDs,text,sortable_id) {
	try {
		elementIDs.each(Element.toggle); // toggle the elements in the passed in array
	} catch(err) {
		//don't sweat it
	}
   
   if ( link.innerHTML != text ) {
     // change link text
     link.oldInnerHTML = link.innerHTML;
     link.innerHTML = text;    
     if (sortable_id) {
		try {
			saved_sortable = Sortable.options(sortable_id);
			Sortable.destroy(sortable_id); 
		} catch(err) { } // Another don't sweat it
     }
   }
   else
   {
		// change it back
		link.innerHTML = link.oldInnerHTML;
		try {
			// FIXME: possibly should be sortable_id instead of saved_sortable.element.id
			Sortable.create(saved_sortable.element.id, saved_sortable)
		} catch(err) { }
   }
   
}

// Disable and re-enable links in elems with class c by adding an onclick("return false"), or removing same.
function disableLinks(c) { $$('.'+c).invoke('writeAttribute','onClick','return false') }
function reEnableLinks(c) { $$('.'+c).invoke('writeAttribute','onClick',false); }

function add_map_edit_link() {
  Element.insert('map_div',
	{bottom: '<div class="layout_perm_sprout" id="introduction_photo_header_notoggle_map"></div><div id="ud_introduction_photo_"></div>'}
  );
}
