//Feb 7, 2008 Neo Wang. To tracking PM code, copied from {marketing site}/js/site.js
//Mar 26, 2009 mvaughan. Modified to work with cookies disabled, store/retrieve all values, and to return query string from cookie for url
//Apr 3, 2009 mvaughan. Added ppc_event
function getTrackingCode(){

  // first get values from stored cookie (if any)
  var IT = getCookie("IT");
  var ppc_geo = getCookie("ppc_geo");
  var ppc_land = getCookie("ppc_land");
  var ppc_kywd = getCookie("ppc_kywd");
  var ppc_lid  = getCookie("ppc_lid");
  var ppc_event  = getCookie("ppc_event");
  var referral_engine = getCookie("referral_engine");
  var referral_kywd = getCookie("referral_kywd");
  var ep = getCookie("ep");
  var first_click = getCookie("first_click");
  var furl = location.href;
  if (!is_empty(furl)) furl = furl.replace("http://", "");
  if (!is_empty(furl)) furl = furl.replace("https://", "");

  var cookieTime = new Date();

  var today = cookieTime.getDate() + "/" + (cookieTime.getMonth() + 1) + "/" + cookieTime.getFullYear();
  
  // cookie expires in 10 years (3650 days)
  cookieTime.setTime(cookieTime.getTime() + 3650 * 24 * 60 * 60 * 1000);

  // check for a query string and overwite the values if there are new ones
  if(location.search){
    var ary  = window.location.search.substr(1).split("&");
    var qStr = new Array();

    for(var i = 0; i < ary.length; i++) ary[i] = ary[i].split("=");
    for(var i = 0; i < ary.length; i++){
      ary[i][0] = ary[i][0].replace(/\+/g," ");
      ary[i][0] = unescape(ary[i][0]);
      ary[i][1] = ary[i][1].replace(/\+/g," ");
      ary[i][1] = unescape(ary[i][1]);
      qStr[ary[i][0]] = ary[i][1];
    }

    IT = qStr["IT"];
    ppc_geo = qStr["geo"];
    ppc_land = qStr["land"];
    ppc_kywd = qStr["kywd"];
    ppc_lid = qStr["lid"];
    ppc_event = qStr["event"];
    referral_engine = qStr["referral_engine"];
    referral_kywd = qStr["referral_kywd"];

    //Feb 26, 2009 Neo Wang. http://tracking.academyart.edu/browse/SEOMARKETING-22
    //use transfered first_click if current click is empty
        var first_click_transfered = qStr["first_click"];
    if (!is_empty(first_click_transfered) && is_empty(first_click)){
            first_click = first_click_transfered;
        }
    //non-null IT means PPC lead - even though IT may get value from cookie, null IT from PPC will set it to null.
    //don't set first_click cookie if it has value so we can keep the 'first' click without overwriting it
    if (!is_empty(IT)){
        
        if (is_empty(first_click)){
            first_click = "fd=" + today + ",fc=p,flid=" + ppc_lid;
        }
    }
    
    var ep_transfered = qStr["ep"];
    if (is_empty(ep_transfered)){
        ep = location.href;
    } else {
        ep = ep_transfered;
    }
  }

  if (document.referrer){
    ref = document.referrer;
    var start = ref.indexOf(":") + 3;
    var end = ref.indexOf("/",start);
    var prevPage= ref.substring(start,end); 
    if (prevPage.indexOf("art.edu") < 0 && prevPage.indexOf("academyartonline.com") < 0) {
      referral_engine = getReferralEngine(prevPage);
      var qsep = ref.indexOf("?") + 1;
      referral_kywd = getReferralKeywords(ref.substr(qsep), referral_engine);
      
      if (!is_empty(referral_engine)){
          if (is_empty(first_click)){//SEO
              first_click = "fd=" + today + ",fc=s,fr=" + referral_engine + ",fskywd=" + referral_kywd + ",furl=" + furl;
              ep = location.href;
          } else {
              if (first_click.indexOf("fc=p") >= 0 && first_click.indexOf("fr=") < 0){//PPC and not from other sites such as AON or art.edu
                  first_click = first_click + ",fr=" + referral_engine + ",fskywd=" + referral_kywd + ",furl=" + furl;
              }
          }
      }
      
    }
  }

  //Aug 5, 2010 Neo Wang. SEOMARKETING-147
  ep = document.referrer;

  // write the latest values back to the cookie
  setCookie("IT", IT, cookieTime);
  setCookie("ppc_geo", ppc_geo, cookieTime);
  setCookie("ppc_land", ppc_land, cookieTime);
  setCookie("ppc_kywd", ppc_kywd, cookieTime);
  setCookie("ppc_lid", ppc_lid, cookieTime);
  setCookie("ppc_event", ppc_event, cookieTime);
  setCookie("referral_engine", referral_engine, cookieTime);
  setCookie("referral_kywd", referral_kywd, cookieTime);
  if (!is_empty(ep)) ep = ep.replace("http://", "");
  if (!is_empty(ep)) ep = ep.replace("https://", "");
  setCookie("ep", ep, cookieTime);
  
  if (!is_empty(first_click)){
      setCookie("first_click", first_click, cookieTime);
  }
  
  return("?IT=" + IT + "&geo=" + ppc_geo + "&land=" + ppc_land + "&kywd=" + ppc_kywd + "&lid=" + ppc_lid + "&event=" + ppc_event + "&referral_engine=" + referral_engine + "&referral_kywd="+ referral_kywd + "&ep=" + escape(ep) + "&first_click=" + escape(first_click));
}

// March 26 2009 mvaughan - attach all parameters to link, not just PM code
function gotoMain(thisLink){
  thisLink.href = thisLink.href + getTrackingCode();
  return true;
}

function gotoRequestInfo(){
  newURL = 'http://www.academyart.edu/inquiry/index.jsp' + getTrackingCode();
  window.open(newURL,"Applications","toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=730,left=0,top=0,xscreen=0,yscreen=0");
}

function gotoApply(){
  newURL = 'https://apply.academyart.edu/apply/login.htm' + getTrackingCode();
  window.open(newURL,"Applications","toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=600,left=0,top=0,xscreen=0,yscreen=0");
}


function gotoArt(){
  newURL = 'http://media.academyart.edu/presentations/promoVideo/promoVideo.html';
  window.open(newURL,"tdaPopup","resizable,scrollbars=no,width=350,height=355");
}

function getQuickLink(URL){
  var selIndex = URL.selectedIndex;
  var selValue = URL.options[selIndex].value;

  if((selValue.substring(0,4) == "http" )){
   window.open(selValue,'_blank');
  }
  else window.open(selValue,'_parent');
 }

 

/**
 *  standard j-insites javascript library
 */


/*
 *
 */
// walk all elements of DOM tree at and below e, 
// run function fn for each element with parameters param
// not suitable for fn's which change a node  
function walkTree(e, fn, param) {

  fn(e, param);
  
  var eChild = e.firstChild
  while (eChild) {
    walkTree(eChild, fn, param);    
    eChild = eChild.nextSibling;
  }
}

/*
 * Crossbrowser addEvent & RemoveEvent functions
 * Courtesy of http://www.soziologie.uni-halle.de/unger/scripts/workshop_internet/fr_js_322.html
 */
 function addEvent(obj, eventType, fn, isCapture) {
    // W3C DOM
    if (obj.addEventListener) {
       obj.addEventListener(eventType, function(evt) { fn(normalizeEvent(evt)) }, isCapture);
       return true;
    }
    // Internet Explorer
    else if (obj.attachEvent) {
       return obj.attachEvent("on"+eventType, function() { fn(normalizeEvent(window.event)) });
    }
    else return false;
 }

 function removeEvent(obj, eventType, afunction, isCapture) {
    if (obj.removeEventListener) {
       obj.removeEventListener(eventType, afunction, isCapture);
       return true;
    }
    else if (obj.detachEvent) {
       return obj.detachEvent("on"+eventType, afunction);
    }
    else return false;
 }

 function normalizeEvent(evt) {
   
    /* needs to be outside evt.srcElement (IE) test to apply to onLoad event */
    if (!evt.preventDefault) evt.preventDefault = function(){ window.event.returnValue = false; }
    if (!evt.stopPropagation) evt.stopPropagation = function() { window.event.cancelBubble = true; }

    if (evt.srcElement) {
      if (!evt.target) evt.target = evt.srcElement;
      
      if (!evt.relatedTarget) {    
        if (evt.type == 'mouseover')
          evt.relatedTarget = evt.fromElement;
        else if (evt.type == 'mouseout')
          evt.relatedTarget = evt.toElement;
      }

      if(!evt.pageX){ evt.pageX = evt.clientX + (document.body.parentNode.scrollLeft || 0) }
      if(!evt.pageY){ evt.pageY = evt.clientY + (document.body.parentNode.scrollTop || 0) }
      
    } else {
      
      if(evt.pageX && !evt.offsetX) { 
        var loc = findPos(evt.target);
        evt.offsetX = evt.pageX - loc[0];
        evt.offsetY = evt.pageY - loc[1];
      }
    }
    
    return evt;
 }

/*
 * Element coordinates
 * Courtesy of PPK @ http://www.quirksmode.org/js/findpos.html
 */
function findPos(obj)
{
  var curleft = curtop = 0;
  
  try {
    if (obj.offsetParent) {
      curleft = obj.offsetLeft
      curtop = obj.offsetTop
      while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft
        curtop += obj.offsetTop
      }
    }
  } catch(err) {
    /* nothing to do */
  }
  return [curleft,curtop];
}

function getCoordinates(e) {
 
 var loc = findPos(e);
 var width = parseInt(e.offsetWidth)
 var height = parseInt(e.offsetHeight);
 
 return {x:loc[0], y:loc[1], w:width, h:height}
}

function getParentByTag(e, tag) {
  if (!e.parentNode) return false;
  
  if (!e.parentNode.tagName || e.parentNode.tagName.toUpperCase() != tag.toUpperCase()) 
    return getParentByTag(e.parentNode, tag);
    
  return e.parentNode;
}

function getParentByClass(e, className) {
  if (!e.parentNode) return false;
  
  var pattern = new RegExp ("\\b"+className+"\\b");
  if (!e.parentNode.className || -1 == e.parentNode.className.search(pattern))
    return getParentByClass(e.parentNode, className);
    
  return e.parentNode;
}

function getElementsByClass(e, className, tagName) {
  var result = new Array;
  if (!e) return result;

  if (!tagName) tagName = '*';
  
  var pattern = new RegExp ("\\b"+className+"\\b");
  var nodes = e.getElementsByTagName(tagName);
  
  for (var i=0; i<nodes.length; i++) {
    if (nodes[i].className && nodes[i].className.match(pattern)) {
      result.push(nodes[i]);
    }
  }
  
  return result;
}

function hasParent(e, parent) {
  if (!e.parentNode) return false;
  if (e.parentNode == parent) return true;
  
  return hasParent(e.parentNode, parent);
}

function array_search(needle, haystack) {
  for (var i=0; i<haystack.length; i++) {
    if (haystack[i] == needle) return i;
  }
  
  return false;
}

function toggleClass(e,c1,c2) {
  if (!e) return;
  
  e.className = (e.className == c2) ? c1 : c2;
}



/*
 *
 */
function set_link_targets() {
   var links = document.getElementsByTagName('A');
   for (var i=0; i<links.length; i++) {
     if (links[i].className && links[i].className.match(/\b_new\b/)) {
       links[i].target = '_blank';
       addEvent(links[i],'click',newWindow,false);
     }
   }
 }
 

function newWindow(evt) {
  var e = evt.target;
  if (e.tagName != "A") e = getParentByTag(e,"A");
  if (!e) return;

  // get current size of browser window (guesstimate for IE)
  var width = window.outerWidth ? parseInt(window.outerWidth) : parseInt(document.body.offsetWidth) + 20;
  var height = window.outerHeight ? parseInt(window.outerHeight) : parseInt(document.body.offsetHeight) + 100;
  
  // shrink the size slightly for the new window
  if (width >= 400) width -= 80;
  if (height >= 400) height -= 80;
  
  // offset slightly from current window
  var screenX = (window.screenX ? window.screenX : window.screenLeft) + 40;
  var screenY = (window.screenY ? window.screenY : window.screenTop - 80) + 40;
  
  var w = window.open(e.href,e.target,"status,menubar,srollbars,resizable,toolbar,titlebar,left="+screenX+",top="+screenY+",width="+width+",height="+height);

  evt.preventDefault();
  evt.stopPropagation();

  w.focus();
}


addEvent(window, 'load', init, false);

/*
 *
 */
function init(evt) {
    /* set up onclick handlers for links which want to operate in a new window */
    set_link_targets();

}

document.writeln('<style type="text/css"> body .js-only{ position: static; visibility: visible; } #bio-details .bio-item { display: none; } </style> ');
document.writeln('<style type="text/css"> #pagelinks .show ul { display: block; } #pagelinks .hide ul { display: none; } </style> ');
 
function getReferralEngine (referrer) {
  var referrers = [
    ['baidu.','baidu'],
    ['bing.','bing'],
    ['google.','google'],
    ['yahoo.','yahoo'],
    ['aol.','aol']
  ];
  for (var i=0; i < referrers.length; i++) {
    if (referrer.indexOf(referrers[i][0]) > -1){
      return referrers[i][1];
    }
  }
  return referrer; 
}

function getQuerySeparator (engine) {
  var separators = [
    ['baidu','wd'],
    ['bing','q'],
    ['google','q'],
    ['yahoo','p'],
    ['aol','q']
  ]
   for (var i=0; i < separators.length; i++) {
    if (engine == separators[i][0]) {
      return separators[i][1];
    }
  }
  return "";
}

function getReferralKeywords(referrer, engine) {
    var term;
    var separator = getQuerySeparator(engine);
    var terms = referrer.split("&");
    for (var i = 0; i < terms.length; i++) {
      term = terms[i].split("=");
      if (term[0] == separator) {
        return term[1];
      }
    }
    return null;
}

function is_empty(str){
    return str == undefined || str == null || str == "" || str.search(/^null$/i)==0;
}
