var cookieInit = readCookie("textsizestyle2");

if(cookieInit && cookieInit != ""){
  if(cookieInit > 0){
    for(j=0; j < cookieInit; j++){
      increaseFontSize(true);
    }
  }

  if(cookieInit < 0){
    for(j=0; j > cookieInit; j--){
      decreaseFontSize(true);
    }
  }
}



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 var expires = "";
  document.cookie = name+"="+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 c.substring(nameEQ.length,c.length);
  }
  return null;
}


function increaseFontSize(fromCookie) {
  var pmax=18;
  var h1max=30;
  var navmax=15;
  var nmax=15;
  var changed = false;
  $("p, a").each(function(){

    if(!$(this).parent().is("li") && !$(this).parent().is("p") && !$(this).parent().is("h4")){

      var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
      if(fontSize <= pmax){
        fontSize += 1;
        $(this).css('fontSize', fontSize+"px");
        $(this).css('lineHeight', fontSize+(fontSize/2)+"px");
        changed = true;
      }
    }
  });

  $("#subnav a").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize <= navmax){
      fontSize += 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $("#navigation a").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize <= nmax){
      fontSize += 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $(".policies").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize <= pmax){
      fontSize += 1;
      $(this).css('fontSize', fontSize+"px");
      $(this).css('lineHeight', fontSize+(fontSize/2)+"px");
      changed = true;
    }

  });

  $("h4").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize <= pmax){
      fontSize += 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $("h1").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize <= h1max){
      fontSize += 2;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $("#news_listing a, #start_links li.nolink, #start_links li a").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize <= nmax){
      fontSize += 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  var cookie = readCookie("textsizestyle2");
  if(changed){
    if(!fromCookie){
      if(cookie && cookie != ""){
        createCookie("textsizestyle2", parseInt(cookie) + 1, 365);
      }else{
        createCookie("textsizestyle2", -1, 365);
      }
    }
  }
}


function decreaseFontSize(fromCookie) {
  var pmin=12;
  var h1min=20;
  var navmin=12;
  var nmin=12;
  var changed = false;
  
  $("p, a").each(function(){
    if(!$(this).parent().is("li") && !$(this).parent().is("p") && !$(this).parent().is("h4")){
      var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
      if(fontSize >= pmin){
        fontSize -= 1;
        $(this).css('fontSize', fontSize+"px");
        $(this).css('lineHeight', fontSize+(fontSize/2)+"px");
        changed = true;
      }
    }
  });

  $("#subnav a").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","").replace("em", "")));
    if(fontSize >= navmin){
      fontSize -= 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $("#navigation a").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize > nmin){
      fontSize = fontSize-1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $(".policies").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize >= pmin){
      fontSize -= 1;
      $(this).css('fontSize', fontSize+"px");
      $(this).css('lineHeight', fontSize+(fontSize/2)+"px");
      changed = true;
    }

  });

  $("h4").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize >= pmin){
      fontSize -= 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $("h1").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize >= h1min){
      fontSize -= 2;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  $("#news_listing a, #start_links li.nolink, #start_links li a").each(function(){
    var fontSize = (parseInt($(this).css('fontSize').replace("px","")));
    if(fontSize >= nmin){
      fontSize -= 1;
      $(this).css('fontSize', fontSize+"px");
      changed = true;
    }

  });

  if(changed){
    if(!fromCookie){
      var cookie = readCookie("textsizestyle2");
      if(cookie && cookie != ""){
        createCookie("textsizestyle2", parseInt(cookie) - 1, 365);
      }else{
        createCookie("textsizestyle2", -1, 365);
      }
    }
  }
}
