﻿/*global font*/

function createCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
        document.cookie = name+"="+escape(value)+expires+"; path=/;";
    }
}

var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1);

var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && document.getElementsByTagName && document.createElement);

function setActiveStyleSheet(title, reset) {
  
  if (!W3CDOM){return false;}
    var i, a, main;

    if (reset == 1) {
        createCookie("wstyle", title, 365);
        jQuery('.activefont').removeClass('activefont');
    } 
      
    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;
                jQuery('.activefont').removeClass('activefont');
                if (title == "Large font")
                    jQuery('.largefontlink').addClass('activefont');
                else
                    jQuery('.mediumfontlink').addClass('activefont');
            }
        }
    }
}

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 setStyle() {

    var style = readCookie("wstyle");
    if (style !== null) {
        setActiveStyleSheet(style, 0);
    }
}

function registerEventListener(elem, event, func) {
    if (elem.addEventListener) {
        elem.addEventListener(event, func, false);
        return true;
    } else if (elem.attachEvent) {
        var result = elem.attachEvent("on"+event, func);
        return result;
    }

    return false;
}

function registerPloneFunction(func) {
     registerEventListener(window, "load", func);
}

registerPloneFunction(setStyle);

/* Code to hide element with javascript before element is loaded */
function createStyleRule(selector, declaration) {
    if (!document.getElementsByTagName ||
      !(document.createElement || document.createElementNS)) return;
    var agt = navigator.userAgent.toLowerCase();
    var is_ie = ((agt.indexOf("msie") != -1) &&  (agt.indexOf("opera") == -1));
    var is_iewin = (is_ie &&  (agt.indexOf("win") != -1));
    var is_iemac = (is_ie &&  (agt.indexOf("mac") != -1));
    if (is_iemac) return; // script doesn't work properly in IE/Mac
    var head = document.getElementsByTagName("head")[0]; 
    var style = (typeof document.createElementNS != "undefined") ?
      document.createElementNS("http://www.w3.org/1999/xhtml", "style") :
      document.createElement("style");
    if (!is_iewin) {
        var styleRule = document.createTextNode(selector + " {" + declaration + "}");
            style.appendChild(styleRule); // bugs in IE/Win
    }
        style.setAttribute("type", "text/css");
    style.setAttribute("media", "screen"); 
    head.appendChild(style);
    if (is_iewin &&  document.styleSheets &&  document.styleSheets.length > 0) {
        var lastStyle = document.styleSheets[document.styleSheets.length - 1];
        if (typeof lastStyle.addRule == "object") {
            lastStyle.addRule(selector, declaration);
        }
    }
}

/* Minislideshow */
function HorizontalSlideShow(outerimagelist, innerimagelist, altlist, imageholder0, imageholder1, imageholder2)
{
    this.previndex = outerimagelist.length-1;
    this.focusindex = 0;
    this.nextindex = 1;
    this.ih0 = document.getElementById(imageholder0);
    this.ih1 = document.getElementById(imageholder1);
    this.ih2 = document.getElementById(imageholder2);

    function GetNext()
    {
        if(this.previndex == outerimagelist.length-1)
        {
            this.previndex = 0;
        }
        else
        {
            this.previndex++;
        }
        if(this.focusindex == outerimagelist.length-1)
        {
            this.focusindex = 0;
        }
        else
        {
            this.focusindex++;
        }
        if(this.nextindex == outerimagelist.length-1)
        {
            this.nextindex = 0;
        }
        else
        {
            this.nextindex++;
        }
        this.ih0.src = outerimagelist[this.previndex];
        this.ih0.alt = altlist[this.previndex];
        this.ih0.title = altlist[this.previndex];
        this.ih1.src = innerimagelist[this.focusindex];
        this.ih1.alt = altlist[this.focusindex];
        this.ih1.title = altlist[this.focusindex];
        this.ih2.src = outerimagelist[this.nextindex];
        this.ih2.alt = altlist[this.nextindex];
        this.ih2.title = altlist[this.nextindex];
     }

     function GetPrev()
     {
        if(this.previndex === 0)
        {
            this.previndex = outerimagelist.length-1;
        }
        else
        {
            this.previndex--;
        }
        if(this.focusindex === 0)
        {
            this.focusindex = outerimagelist.length-1;   
        }     
        else
        {
            this.focusindex--;
        }
        if(this.nextindex === 0)
        {
            this.nextindex = outerimagelist.length-1;
        }
        else
        {
            this.nextindex--;
        }
        this.ih0.src = outerimagelist[this.previndex];
        this.ih0.alt = altlist[this.previndex];
        this.ih0.title = altlist[this.previndex];
        this.ih1.src = innerimagelist[this.focusindex];
        this.ih1.alt = altlist[this.focusindex];
        this.ih1.title = altlist[this.focusindex];
        this.ih2.src = outerimagelist[this.nextindex];
        this.ih2.alt = altlist[this.nextindex];
        this.ih2.title = altlist[this.nextindex];
    }
    
    this.GetNext = GetNext;
    this.GetPrev = GetPrev;
}

function VerticalSlideShow(imagelist, altlist, imageholder4, countlabel)
{
      this.focusindex = 0;
      this.ih4 = document.getElementById(imageholder4);
        
      function GetNext()
      {
          if (this.focusindex < imagelist.length-1)
          {
              this.focusindex++;
              this.ih4.src = imagelist[this.focusindex];
              this.ih4.alt = altlist[this.focusindex];
              this.ih4.title = altlist[this.focusindex];
              var label = document.getElementById(countlabel);
              if (label !== null)
              {
                if (document.all)
                {
                    label.innerText = this.focusindex+1;
                }
                else
                {
                    label.textContent = this.focusindex+1;   
                } 
              }
          }
      }

    function GetPrev()
    {
        if (this.focusindex > 0)
        {
            this.focusindex--;
            this.ih4.src = imagelist[this.focusindex];
            this.ih4.alt = altlist[this.focusindex];
            this.ih4.title = altlist[this.focusindex];
            var label = document.getElementById(countlabel);
            if (label !== null)
            {
                if (document.all)
                {
                    label.innerText = this.focusindex+1;
                }
                else
                {
                    label.textContent = this.focusindex+1;
                }
            }
        }
    }
    this.GetNext = GetNext;
    this.GetPrev = GetPrev;
}