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

// used to make external URL's open in a new window and also be XHTML valid
function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors .length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
      anchor.target = "_blank";
      anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
      anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
    }
  }
}
window.onload = externalLinks;

var TreeState = Class.create({
                               initialize: function(reorder_url) {
                                 this.reorder_url = reorder_url;
                               },
                               update: function(element, direction, dropon) {
                                 this.element = element;
                                 this.direction = direction;
                                 this.dropon = dropon;
                               },
                               sendRequest: function () {
                                 new Ajax.Updater('status', this.reorder_url, {
                                                    parameters: 'element=' + this.element.id.replace(/^\D+/,'') + '&direction=' + this.direction + '&dropon=' + this.dropon.id.replace(/^\D+/,'')
                                                  });
                               }});

function mark_for_destroy(element, hide) {
  $(element).next('.should_destroy').value = 1;
  $(element).up('.' + hide).hide();
}

function toggle_and_save(element) {
  element = $(element);
  element.toggle();
  document.cookie = element.id + '-open=' + element.visible() + '; path=/';
}

function widgets_checkall(frm) {
  all_checkboxes = $(frm).getInputs('checkbox');
  all_checkbox = all_checkboxes.first();
  elements = all_checkboxes.reject(function(n) { return n.getAttribute('id') == 'all_pages'})
  if (all_checkbox.checked) {
    elements.invoke('setAttribute', 'checked', true);
    $$('.placement').invoke('removeAttribute', 'disabled');
  }
  else {
    elements.invoke('removeAttribute', 'checked');
    $$('.placement').invoke('setAttribute', 'disabled', 'disabled');
  }
}
function mark_for_destroy(element, hide) {
    $(element).next('.should_destroy').value = 1;
    $(element).up('.' + hide).hide();
}
function toggle_disable(elem) {
  elem = $(elem);
  if (elem.getAttribute('disabled')) {
    elem.removeAttribute('disabled');
  }
  else {
    elem.setAttribute('disabled', true);
  }
}

function toggleDetailedDescriptionLink(ddId, ddIdToggle) {
   if (ddId.style.display == "none") {
       ddIdToggle.innerHTML = "Detailed product information";
   } else {
      ddIdToggle.innerHTML = "hide";
   }
}

