/**
* add_link.js
*
*/

//--------------------------------------------------------------------
function get_link_path() {
    var view = getCookie("view");
    var inst = getCookie("inst");
    if( is_valid( "view", view ) && is_valid( "inst", inst ) ) {
        var instpath = inst.split(".")[1];
        if( instpath == "guest" ) return "/";
        return "/"+view+"/"+instpath+"/";
    }
    return "/";
}

//--------------------------------------------------------------------
function get_link_match( str ) {

  var re = /\/cgi\/link\?(.*)/;
  var a = str.match(re);  // a = ['/cgi/link?link=....','....']
  if( a && a.length ) return a[1];

  re = /express\?(.*)/;
  a = str.match(re);
  if( a && a.length ) return a[1];
}

//--------------------------------------------------------------------
function get_reflect_match( str ) {
  var tsu = /(sedna|andromeda).galib.uga.edu/;
  var tsg = /gal(fe1|fe2|db1|sw1).gsu.edu/;
  var us = /(www|beta|prod|prep|demo|maint|dev|betatest).galileo.usg.edu/;
  var w3 = /(validator|jigsaw).w3.org/;
  var ms = /^(javascript:|mailto:)/;
  if( str.match(tsu) || str.match(tsg) || str.match(us) || str.match(w3) || str.match(ms) ) return false;
  return true;  // i.e., external link we want to reflect
}

//--------------------------------------------------------------------
function add_linker() {
  var linkpath = get_link_path();
  var a = document.body.getElementsByTagName('a');

  for(var i=0;i<a.length;i++) {
    if( a[i].getAttribute('class') != "smoothbox" && a[i].getAttribute('onClick') == null ) {
      if( get_link_match( a[i].href ) ) {
        a[i].onclick = function () { var query=get_link_match(this.href); window.location.href = linkpath+'link/?'+query; return false; }
      }
      else if( get_reflect_match( a[i].href ) ) {
        a[i].onclick = function () { window.location.href = linkpath+'reflect/?'+this.href; return false; }
      }
    }
  }
}

//--------------------------------------------------------------------
function add_iframe_linker() {
  var linkpath = get_link_path();
  var a = document.body.getElementsByTagName('a');

  for(var i=0;i<a.length;i++) {
    if( a[i].getAttribute('onClick') == null ) {
      if( get_link_match( a[i].href ) ) {
        a[i].onclick = function () { var query=get_link_match(this.href); window.parent.location.href = linkpath+'link/?'+query; return false; }
      }
      else if( get_reflect_match( a[i].href ) ) {
        a[i].onclick = function () { window.parent.location.href = linkpath+'reflect/?'+this.href; return false; }
      }
    }
  }
}

