// ----------------------------------------------
// StyleSwitcher functions written by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -
// For the details, visit ALA:
// http://www.alistapart.com/stories/alternate/
// ----------------------------------------------


/* head:

    <link rel="alternate stylesheet" type="text/css"
          media="screen"
          href="TextSmall.css"
          title="Small Text" />
    <link rel="alternate stylesheet" type="text/css"
          media="screen"
          href="TextLarge.css"
          title="Large Text" />

    body:

    <li id="siteaction-small_text"><a
    href="javascript:setActiveStyleSheet('Small Text', 1);"
    title="Small Text">Small Text</a></li>
    <li id="siteaction-normal_text"><a
    href="javascript:setActiveStyleSheet('', 1);"
    title="Normal Text">Normal Text</a></li>
    <li id="siteaction-large_text"><a
    href="javascript:setActiveStyleSheet('Large Text', 1);"
    title="Large Text">Large Text</a></li>
*/


function setActiveStyleSheet(title, reset) {
  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;
    }
  }
  if (reset == 1) {
  createCookie("wstyle", title, 365);
  }
}

function setStyle() {
  var style = readCookie("wstyle");
  if (style != null) {
    setActiveStyleSheet(style, 0);
  }
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+escape(value)+expires+"; path=/;";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
  }
  return null;
}

function registerOnLoad(func){
  if (window.addEventListener) window.addEventListener("load",func,false);
  else if (window.attachEvent) window.attachEvent("onload",func);
}

registerOnLoad(setStyle);

// --------------------------------------------------------------------------
// Fra Plone:

var W3CDOM = (document.createElement && document.getElementsByTagName);

function getContentArea() {
    if (W3CDOM) {
        var node=document.getElementById('content'); 
        return node;
    }
    return false;
}
function wrapNode(node, wrappertype, wrapperclass){
    /* utility function to wrap a node in an arbitrary element of type "wrappertype"
     * with a class of "wrapperclass" */
    var wrapper = document.createElement(wrappertype)
    wrapper.className = wrapperclass;
    var innerNode = node.parentNode.replaceChild(wrapper,node);
    wrapper.appendChild(innerNode);
};

function scanforlinks() {
    // terminate if we hit a non-compliant DOM implementation
    if (!W3CDOM) { return false; }

    contentarea = getContentArea();
    if (!contentarea) { return false; }

    links = contentarea.getElementsByTagName('a');
    for (i=0; i < links.length; i++) {
        if ( (links[i].getAttribute('href'))
             && (links[i].className.indexOf('link-plain')==-1) ) {
            var linkval = links[i].getAttribute('href');
            var hostname = window.location.host;

            // check if the link href is a relative link, or an absolute link to
            // the current host.
            if (linkval.toLowerCase().indexOf(window.location.protocol
                                              + '//'
                                              + hostname) == 0) {
                // absolute link internal to our host - do nothing
            } else if (linkval.toLowerCase().indexOf(window.location.protocol
                                              + '//'
                                              + 'www.' + 
                                              hostname) == 0) {
                // fremdeles hjemme, men med "www" foran
            } else if (linkval.indexOf('http:') != 0) {
                // not a http-link. Possibly an internal relative link, but also
                // possibly a mailto or other protocol add tests for relevant
                // protocols as you like.
                protocols = ['mailto', 'ftp', 'news', 'irc', 'h323', 'sip',
                             'callto', 'https', 'feed', 'webcal'];
                // h323, sip and callto are internet telephony VoIP protocols
                for (p=0; p < protocols.length; p++) {
                    if (linkval.indexOf(protocols[p]+':') == 0) {
                        // if the link matches one of the listed protocols, add
                        // className = link-protocol
                        wrapNode(links[i], 'span', 'link-'+protocols[p]);
                        break;
                    }
                }
            } else {
                // we are in here if the link points to somewhere else than our
                // site.
                if ( links[i].getElementsByTagName('img').length == 0 ) {
                    // we do not want to mess with those links that already have
                    // images in them
                    wrapNode(links[i], 'span', 'link-external');
                    links[i].setAttribute('target', '_blank');
                }
            }
        }
    }
};

registerOnLoad(scanforlinks);
