// METHOD :: Add listener 'type' to 'obj' with result 'func'
var addListener = function() {
   if (window.addEventListener) {
      return function(obj, type, func) {
         obj.addEventListener(type, func, false);
      };
   }
   else if (window.attachEvent) {
      return function(obj, type, func) {
         var f = function() {
            func.call(obj, window.event);
         };

         obj.attachEvent('on' + type, f);
      };
   }
   else {
      return function(obj, type, func) {
         element['on' + type] = func;
      }
   }
}();

// METHOD :: Returns the width of the viewport
var getViewportWidth = function() {
   if (document.documentElement && document.documentElement.clientWidth) {
      return document.documentElement.clientWidth;
   }
   else if (document.body && document.body.clientWidth) {
      return document.body.clientWidth;
   }
   else if (window.innerWidth) {
      return window.innerWidth - 18;
   }

   return 0;
};

// METHOD :: Returns the height of the viewport
var getViewportHeight = function() {
   if (document.documentElement && document.documentElement.clientHeight) {
      return document.documentElement.clientHeight;
   }
   else if (document.body && document.body.clientHeight) {
      return document.body.clientHeight;
   }
   else if (window.innerHeight) {
      return window.innerHeight - 18;
   }

   return 0;
};

// METHOD :: Handles special (external / pdf) links
function xLinks(){
   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";
      }

      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "pdf") {
         anchor.pdfLink = anchor.getAttribute("href");
         addListener(anchor, 'click', pdfLink);
      }

      if (anchor.getAttribute("href") && anchor.getAttribute("href").substr(anchor.getAttribute("href").length - 3) == "pdf") {
         anchor.pdfLink = anchor.getAttribute("href");
         addListener(anchor, 'click', pdfLink);
      }
   }
}
addListener(window, 'load', xLinks);

// METHOD :: Opens a PDF link
function pdfLink(e) {
   var e = e ? e : event;
   var eObj = e.target ? e.target : e.srcElement;

   if (navigator.appName == "Microsoft Internet Explorer") {
      e.returnValue = false;
   }
   else {
      e.preventDefault();
   }

   while (eObj.tagName != 'A' && eObj.parentNode) {
      eObj = eObj.parentNode;
   }

   // Show notice only once
   if (readCookie("pdfNotice")) {
      return window.open(baseURL + eObj.pdfLink);
   }

   createCookie("pdfNotice", 1);
   XPopup.show("index.php?content=com_helper&task=pdf_notice&pdf_link=" + encodeURIComponent(eObj.pdfLink), 450, 210, true);
}

// METHOD :: Creates cookie 'name' with 'value' which expires after 'expireTime' seconds
function createCookie(name, value, expireTime) {
   if (expireTime) {
      var date = new Date();
      date.setTime(date.getTime() + expireTime);
      expireTime = "; expires=" + date.toGMTString();
   }
   else {
      expireTime = "";
   }

   document.cookie = name + "=" + value + expireTime + "; path=/";
}

// METHOD :: Returns the value of cookie 'name'
function readCookie(name) {
   name = name + "=";

   var cValues = document.cookie.split(';');
      for(var i = 0; i < cValues.length; i++) {
         var cValue = cValues[i];
         while (cValue.charAt(0) == ' ') {
            cValue = cValue.substring(1, cValue.length);
         }

         if (cValue.indexOf(name) == 0) {
            return cValue.substring(name.length, cValue.length);
         }
      }

   return null;
}