/* $Id: ga.js 552 2011-10-07 09:11:41Z lusl0338 $ */
addGaEvent = function(link, category, action, label) {
  link.click(function() {
    try {
      _gaq.push(['_trackEvent', category, action, label]);
    } catch(error) {
    }
  });
}
processLink = function(link) {
  var href = link.attr('href');
  var text = link.text();
  var interniIS = /https?:\/\/(verso|obd|obd-demo|fis-web|iskam|portal|katalog|www|studenti|dokumenty|zamestnanci|projekty|e-shop)\.upce\.cz.*/;
  var outText = null;
  if (interniIS.test(href)) {
    var is = href.replace(interniIS, '$1');
    if (text == 'detail') {
      if (is == 'verso') {
        var versoModul = /.*fname=([a-z_]*).*/;
        var modul = href.replace(versoModul, '$1');
        addGaEvent(link, 'Odchod', is+': '+modul, href);
        return;
      }
      if (is == 'katalog') {
        addGaEvent(link, 'Odchod', 'katalog: vypujcky', href);
        return;
      }
    }
    addGaEvent(link, 'Odchod', is+': '+text, href);
    return;
  }
  var helpdesk = /https:\/\/idp\.upce\.cz.*helpdesk\.upce\.cz/;
  if (helpdesk.test(href)) {
    addGaEvent(link, 'Odchod', 'helpdesk', href);
    return;
  }
  var eiz = /knihovna\/eiz/;
  if (eiz.test(document.location.pathname)) {
    addGaEvent(link, 'Odchod EIZ', text, href);
  }
  addGaEvent(link, 'Odchod', text, href);
}
processDownload = function(link) {
  var href = link.attr('href');
  var text = link.text();
  var viewChange = /.*\.html\?level=([0-9])/;
  if (viewChange.test(href)) {
    var level = href.replace(viewChange, '$1');
    addGaEvent(link, 'Přepnutí úrovně', 'Link', level);
    return;
  }
  var common = /^\/(index|lide-vizitka|akce|aktuality)\.html/;
  if ((href=='/') || common.test(href)) {
    return;
  }
  var email = /^mailto:/;
  if (email.test(href)) {
    addGaEvent(link, 'Email', text, href);
    return;
  }
  var monitoring = /^\/monitoring\//;
  if (monitoring.test(href)) {
    addGaEvent(link, 'Download', 'monitoring:'+text, href);
    return;
  }
  addGaEvent(link, 'Download', text, href);
}
$(document).ready(function() {
  if ((document.location.hostname != 'zamestnanci.upce.cz') &&
      (document.location.hostname != 'studenti.upce.cz') &&
      (document.location.hostname != 'www.upce.cz')) {
    return;
  }
  try {
    var links = $('a');
    var startHttp = /^https?:\/\//;
    var endHtml = /.{3}\.html$/;
    for (var i=0; i<links.length; i++) {
      var link = $(links[i]);
      var href = link.attr('href');
      if (startHttp.test(href)) {
        processLink(link);
      } else {
        if (!endHtml.test(href)) {
          processDownload(link);
        }
      }
    }
  } catch(error) {
  }
});

