// LINKS to press releases
function pr_linkto(xml_address) {
  var xhr = new XHR({method: 'get', onSuccess: pr_linkto_execute, onFailure: pr_linkto_failure}).send('/xhrproxy.php?' + xml_address);
}

function pr_linkto_execute(obj) {
  try {
    document.location = this.response.xml.getElementsByTagName('file')[0].getElementsByTagName('location')[0].getAttribute('href');
  } catch (e) {
    document.location = '/pressreleaser';
  }
}

function pr_linkto_failure() {
  alert('Sorry, I could not load that press release.')
}



// LOAD a press release
function pr_fetch(xml_address, target_id) {
  var target_object = $(target_id);
  if (target_object.is_xhr_populated) {
    target_object.animator.toggle();
    return;
  }
  
  target_object.innerHTML = '<div class="press-main">Laddar...</div>';
  
  xhr = new XHR({method: 'get', onSuccess: pr_fetch_execute, onFailure: pr_fetch_failure});
  xhr.target_id = target_id;
  xhr.send('/xhrproxy.php?' + xml_address);
}

function pr_fetch_execute(obj) {
  try {
    var target_object = $(this.target_id);
    var main_div = new Element('div');
    var inner_html = '';
    
    inner_html = '<div class="press-main">';
    var main_tag = this.response.xml.getElementsByTagName('main')[0];
    
    if (main_tag.textContent != null) {
      inner_html += main_tag.textContent;
    } else if (main_tag.nodeTypedValue != null) {
      inner_html += main_tag.nodeTypedValue;
    } else {
      inner_html += 'Sorry, your web browser does not seem to be supported :-(.<br/>Recommended browsers: Firefox, Internet Explorer, Safari';
    }
    
    var files = this.response.xml.getElementsByTagName('file');
    if (files.length > 0) {
      inner_html += '<br/><br/>';
      for (var i = 0; i < files.length; i++) {
        var headline = files[i].getElementsByTagName('file_headline')[0].textContent;
        if (headline == null) {
          headline = files[i].getElementsByTagName('file_headline')[0].nodeTypedValue;
        }
        inner_html += '<a href="' + files[i].getElementsByTagName('location')[0].getAttribute("href") + '" class="press-link" target="_blank">' + headline + '</a><br/>\n';
      }
    }
    
    inner_html += '</div>';
    target_object.is_xhr_populated = true;
    
    target_object.animator = new Fx.Slide(target_object);
    target_object.animator.hide();
    target_object.innerHTML = inner_html;
    target_object.animator.slideIn();
    
     // = this.response.xml.getElementsByTagName('file')[0].getElementsByTagName('location')[0].getAttribute('href');
  } catch (e) {
    target_object.innerHTML = '<div class="press-main">[Sorry, a parsing error has occurred.]</div>';
  }
}

function pr_fetch_failure() {
  $(this.target_id).innerHTML = '<div class="press-main">[Sorry, I couldn\'t load this press release.]</div>';
}
