﻿// Heinle's function for retrieving a cookie.
function getCookie(name){
  var cname = name + "=";
  var dc = document.cookie;
  if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    }
  }
  return null;
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) +
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
    var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
    return newnumber;
}

var firstCall = true;
var changes_count = 0;
var change_size = '1';

function changeFontsize(fSize, increment) {

  if (firstCall) {
    firstCall = false;
    if (increment != "")
        changeFontsize("1", "0");
  }

  if (fSize == "0") {

      if (changes_count != 0) {
          var temp = changes_count.toString().indexOf("-");
          var number_sign;

          if (temp < 0) { number_sign = "-"; }
          else { number_sign = "+"; }

          changes_count = Math.abs(changes_count);

          var i;
          for (i = 0; i < changes_count; i++) {
          
              changeFontsize('1', number_sign + change_size);
          }
          changes_count = 0;
       }
    
    return;
  
  }



  if (document.getElementsByTagName) {

      if (fSize != "1") {
          
          if (increment.indexOf("-") < 0 && increment != "0") { changes_count += 1; }
          else if (increment.indexOf("-") >= 0 && increment != "0") { changes_count -= 1; }
      }
  
    tags = new Array ( "p", "h1", "h2", "h3", "h4", "h5", "div", "span" );
    for (j=0; j<tags.length; j++) {
      var getElement = document.getElementsByTagName(tags[j]);
      var eachElement, currentFontSize, fontIncrease, newFontSize;
      var cont;

      for (i=0; i<getElement.length; i++) {
          eachElement = getElement[i];
          cont = eachElement.id;

          if ((cont.indexOf("HeaderMenuLower") < 0) && (cont.indexOf("nav_main_horisontal") < 0) && (cont.indexOf("clearfix") < 0))
          {

              if (increment != "0") {

                  currentFontSize = eachElement.style.fontSize;
              }
              else if (increment == "0") {

                  if (window.getComputedStyle) {
                      // FF, Opera
                      currentFontSize = document.defaultView.getComputedStyle(eachElement, null).getPropertyValue('font-size');
                  }
                  else if (eachElement.currentStyle) {
                  // IE
                  // the default font-size of most of the browsers is 16px, we can take this fot base
                  // when transformation from % and em is made
                  // 16px = 100% = 12pt

                     var base = 16;
                     var base_for_pt = roundNumber((base / 12), 3) ;
                     var oneEM;
                     var documentfontsize = document.body.currentStyle.fontSize;
                     
                     if (documentfontsize.indexOf("pt") >= 0){

                         documentfontsize = roundNumber((parseFloat(documentfontsize) * 100) / 12, 2);
                         documentfontsize = documentfontsize + "%";
                     }

                     if (parseFloat(documentfontsize) < 100) {
                        oneEM = parseFloat(documentfontsize);
                        oneEM = parseInt(base * oneEM)/100;
                     }
                     else {
                        oneEM = base;
                     }
                     
                     currentFontSize = eachElement.currentStyle.fontSize;
                     
                     if (currentFontSize.indexOf("em") >= 0) {

                         currentFontSize = oneEM * roundNumber(parseFloat(currentFontSize), 2);   
                         currentFontSize = currentFontSize + "px";
    	             }
    	             else if (currentFontSize.indexOf("%") >= 0) {

     	                 currentFontSize = base * roundNumber(parseFloat(currentFontSize),2)/100 ;     
    	                 currentFontSize = currentFontSize + "px";
    	             }
    	             else if (currentFontSize.indexOf("pt") >= 0) {

    	                 currentFontSize = roundNumber(base_for_pt * parseFloat(currentFontSize), 2);
    	                 currentFontSize = currentFontSize + "px";
    	             }
                     
                  } 
               }

  
               if (currentFontSize.indexOf("px") >= 0) {
                   currentFontSize = parseInt(currentFontSize) + parseInt(increment);
                   newFontSize = currentFontSize + "px";
                   eachElement.style.fontSize = newFontSize;
                   setCookie('fontSize', newFontSize);
               }
        	   
          }
      }
    }
  }
}