/**
 *
 * Javascript library used by Mosquito CMS
 *
 * @version $Id: lib.js 30 2005-08-03 17:21:30Z root $
 * @author $
 *
 */

/**
 * Cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
 *
 * @param elm object Target element
 * @param evType string Event type (ie. 'load', 'click' ...)
 * @param fn string Function to be attached
 * @param useCapture boolean UseCapture feature
 * @author Scott Andrew
 * @since 2.0
 */
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    } else {
        elm['on' + evType] = fn;
    }
}

/**
 * Cookie
 * Set, get, and delete cookies
 *
 * @author Travis Beckham | squidfingers.com
 *
 */
var Cookie = {
    set : function(name, value, days) {
        if(days) {
          var date = new Date();
          date.setTime(date.getTime() + (days*24*60*60*1000));
          var expires = "; expires=" + date.toGMTString();
        } else {
            var expires = "";
        }
            document.cookie = name + "=" + value + expires + "; path=/";
        },
    get : function(name) {
        name += "=";
        var s = document.cookie.split("; ");
        for(var i = 0; i < s.length; i++) {
            var c = s[i];
            if(c.indexOf(name) == 0){
                return unescape(c.substring(name.length,c.length));
            }
        }
        return null;
    },
    erase : function(name) {
        this.set(name,"",-1);
    },
    enabled : function() {
        this.set("cookietest","cookietest");
        return this.get("cookietest") != null;
    }
};

/**
 * Blur Anchors
 *
 * Remove anchor outlines from all links in the document
 *
 * @author Travis Beckham
 * @see http://www.squidfingers.com
 *
 */
function blurAnchors() {
  if(document.getElementsByTagName) {
    var a = document.getElementsByTagName("a");
    for(var i = 0; i < a.length; i++){
      a[i].onfocus = function(){this.blur()};
    }
  }
}

/**
 * Print
 */
function printWindow() {
  bV = parseInt(navigator.appVersion);
  if (bV >= 4) window.print();
}

/**
 * Javascript implementation of base64 encode
 * Taken from http://www.jan-winkler.de/hw/artikel/art_j02.htm
 *
 */
function base64_encode(decStr) {
    var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    var bits, dual;
    var i = 0;
    var encOut = '';
    while(decStr.length >= i + 3) {
        bits = (decStr.charCodeAt(i++) & 0xff) <<16 | (decStr.charCodeAt(i++) & 0xff) <<8 |
                decStr.charCodeAt(i++) & 0xff;
        encOut += base64s.charAt((bits & 0x00fc0000) >>18) + base64s.charAt((bits & 0x0003f000) >>12) +
                  base64s.charAt((bits & 0x00000fc0) >> 6) + base64s.charAt((bits & 0x0000003f));
    }
    if(decStr.length -i > 0 && decStr.length -i < 3) {
        dual = Boolean(decStr.length -i -1);
        bits = ((decStr.charCodeAt(i++) & 0xff) <<16) |
               (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
        encOut += base64s.charAt((bits & 0x00fc0000) >>18) + base64s.charAt((bits & 0x0003f000) >>12) +
                  (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') + '=';
    }
    return(encOut);
}

/**
 *
 *
 * inspired by http://verens.com/demos/linktracker/linktracker.js
 */
function initLinkTracker() {
    var localserver = document.location.toString().replace(/^[^\/]*\/+([^\/]*)(\/.*)?/, '$1');
    var els = document.getElementsByTagName('a');
    for(var i=0; i < els.length; i++) {
        var href = els[i].href;
        if(href.match(eval('/^(http(s)?:\\/\\/)?'+localserver+'/'))) continue;
        els[i].href = '/exit.php?url=' + base64_encode(href);        
        els[i].title = els[i].title + ' (' + _extURL + ')';
    }
}

/**
 * Stylesheet switcher
 */
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

/**
 * Dom
 *
 **/


/**
 * Popup links
 *
 * Inits popup links within document
 *
 * @author Michi
 *
 */
function initPopupLinks() {
  if(document.getElementsByTagName) {
    var a = document.getElementsByTagName("a");
    for(var i = 0; i < a.length; i++){
      if(a[i].getAttribute("class") != 'popup') continue;
      alert('found');
      a[i].onclick = function(){ alert('foo') };
    }
  }
}

function windowPopup() {
    alert('popup');
}

