function move_to_portlet(section, position) {
	var from = $('from').value;
	var from_array = from.split("_");
	var from_section = from_array[0];
	var update_page = from_section == section ? section : "page";
	var target = section + '_' + (position+1);
	var above_target = section + '_' + position;
	if (from == undefined || from == '' || from == target) { 
		alert('You must select the article to move here first.'); 
		return false; 
	}	
	new Ajax.Updater(update_page, '/layout/move?from=' + from + '&to=' + target, 
										{onLoading:function(request){show_wait_for('portlet_' + above_target)}, 
										 onComplete:function(request){hide_wait_for('portlet_' + above_target, 'portlet_' + target)},
										 asynchronous:true}); 
}

function toggle_edit(id) {
	 Toggle.display(id);
	 Toggle.display(id + '_edit');
	/*if($(id + '_edit').firstChild.nextSibling.summary){
   $(id + '_edit').firstChild.nextSibling.summary.focus();
 }
	if($(id + '_edit').firstChild.nextSibling.caption){
   $(id + '_edit').firstChild.nextSibling.caption.focus();
   $(id + '_edit').firstChild.nextSibling.caption.select();
 }
if($(id + '_edit').firstChild.nextSibling.headline){
   $(id + '_edit').firstChild.nextSibling.headline.focus();
   $(id + '_edit').firstChild.nextSibling.headline.select();
 }
if($(id + '_edit').firstChild.nextSibling.topic){
   $(id + '_edit').firstChild.nextSibling.topic.focus();
 } */

  if($(id + '_form').summary){
    $(id + '_form').summary.focus();
    $(id + '_form').summary.select();
  }

  if($(id + '_form').headline){
    $(id + '_form').headline.focus();
    $(id + '_form').headline.select();
  }

  if($(id + '_form').topic){
    $(id + '_form').topic.focus();
    $(id + '_form').topic.select();
  }

  if($(id + '_form').caption){
    $(id + '_form').caption.focus();
    $(id + '_form').caption.select();
  }
  
  if($(id + '_form').author_last_name){
    $(id + '_form').author_last_name.focus();
    $(id + '_form').author_last_name.select();
  }
}

function selected_photo(photo_id, popup) {
	re = /portlet_(\w+)_(\d+)_(\w+)_(\d+)_(\w*)/i
	data = re.exec(popup.name);
	section = data[1];
	position = data[2];
	type = data[3];
	article_id = data[4];
	solo = data[5] == 'solo';
	setTimeout("update_photo(" + article_id + "," + photo_id + 
													 ",'" + section + "'," + position + ",'" + type + "'," + 
													 solo + ")",10);
}

function update_photo(article_id, photo_id, section, position, type, solo)
{
	portlet_id = 'portlet_' + section + '_' + position;
	url = '/layout/';
	
	if (solo) 
	{ 
		container = 'page';
		url += 'set_solo';
	}	else { 
		container = portlet_id + '_container';
		url += 'update_photo';
	}
		
	url += '/' + article_id + '?photo_id=' + photo_id + 
				'&type=' + type + '&section=' + section + '&position=' + position

	new Ajax.Updater(container, url,
										{onLoading:function(request){show_wait_for(portlet_id)}, 
	                  onComplete:function(request){hide_wait_for(portlet_id)},
	                  asynchronous:true}); 
}

function toggle_controls() {
	var elements = document.getElementsByTagName('b');
	for (var j = 0; j < elements.length; j++) {
		var display = elements[j].style.display;
		elements[j].style.display = display == 'none' ? '' : 'none'
	}
}

function handleAccelerator(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
        if (evt.keyCode == 120) {
            toggle_controls();
        }
    }
}

document.onkeyup = handleAccelerator;

function show_wait_for(portlet) {
	$(portlet + '_wait').style.display = '';
}

function hide_wait_for(portlet, highlight) {
	if (highlight == undefined) highlight = portlet;
	$(portlet + '_wait').style.display = 'none';
	new Effect.Highlight(highlight + '_container');
}

