// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var current_root = null;

function paste(url, item_id){
	new Ajax.Request(url + item_id + '?parent_id=' + current_root, {asynchronous:true, evalScripts:true}); return false;
}

var currentSubmenuPath = null;

var currentPath = new Array(32);
for (i = 0; i < currentPath.length; i++) currentPath[i] = null;

var currentAddressPath = null;

function selectionClicked(depth, path, multiselection, item_children_url) {
  //alert("depth=" + depth + " path=" + path + " multiselection=" + multiselection);
  if (multiselection) {

    checkbox = $("multiselection_" + path);
    setItemChildrenDisplay(path, item_children_url, {show:checkbox.checked});

  } else {

    setItemChildrenDisplay(currentPath[depth], item_children_url, {show:false});

    if (currentPath[depth] != path) {

        currentPath[depth] = path;

        setItemChildrenDisplay(currentPath[depth], item_children_url, {show:true});

    } else {

        currentPath[depth] = null;

    }
  }
  
}

function toggleItem(depth, path) {
    checkbox = $("multiselection_" + path);
    if (checkbox) checkbox.click();
}

function selectItem(depth, path) {
    radiobutton = $("selection_" + path);
    if (radiobutton) radiobutton.click();
}

function adjustItemChildrenDisplayIndicator(path, expanded) {
    indicator = $('item_' + path + '_indicator');

    if (expanded) 
    	indicator.src = "/images/collapse.png";
    else
	indicator.src = "/images/expand.png";
}

function toggleItemChildrenDisplay(path, item_children_url) {
  setItemChildrenDisplay(path, item_children_url, {toggle:true});
}

function setItemChildrenDisplay(path, item_children_url, options) {
  if (path != null) {

    children_id = 'item_' + path + '_children';

    children = $(children_id);

    if (children == null) {
       new Ajax.Updater(children_id + '_holder', item_children_url, 
       	   		{asynchronous:true, evalScripts:true, onComplete:function(){ adjustItemChildrenDisplayIndicator(path, true);}});
    } else {
       do_show = false;
       if (options['show'] != null)
       	  do_show = options['show'];
       else {
          if (options['toggle'] != null && options['toggle'])
	     do_show = children.style.display == 'none';
       }
       
       if (do_show)
    	  children.show()
       else
	  children.hide()
       adjustItemChildrenDisplayIndicator(path, do_show);
    }
  }
}


//---------------------------------------------------------

function switchToSubmenu(submenuPath) {

    if (currentSubmenuPath != null) {
        category_link = $('item_list_' + currentSubmenuPath)
        if (category_link) {
                category_link.className = "not-selected";
        }
    }
    Element.hide('item_' + currentSubmenuPath);

    currentSubmenuPath = submenuPath;

    if (currentSubmenuPath != null) {
        category_link = $('item_list_' + currentSubmenuPath);
        if (category_link) {
                category_link.className = "selected";
        }
    }
    Element.show('item_' + currentSubmenuPath);

    $("selection_" + currentSubmenuPath).click();

}

//--------------------------------------------------------------------------------

function switchToAddress(addressId, openForm) {
    if (currentAddressPath != null) {
        Element.hide('address_form_' + currentAddressPath);
        Element.show('address_' + currentAddressPath);
    }
    currentAddressPath = addressId;
    $('address_selection_' + currentAddressPath).click();
    if (openForm) {
        Element.show('address_form_' + currentAddressPath);
        Element.hide('address_' + currentAddressPath);
    } else {
        Element.hide('address_form_' + currentAddressPath);
        Element.show('address_' + currentAddressPath);
    }
}


//----------------------------------------------------------------------------------
/* shows and hides ajax indicator */
Ajax.Responders.register({
    onCreate: function(){
        if ($('ajax-indicator') && Ajax.activeRequestCount > 0) {
            Element.show('ajax-indicator');
        }
    },
    onComplete: function(){
        if ($('ajax-indicator') && Ajax.activeRequestCount == 0) {
            Element.hide('ajax-indicator');
        }
    }
});
