function getViewport(){

  var e = window, a = 'inner';
  
  if(!('innerWidth' in e)){
    var t = document.documentElement
    e = t && t.clientWidth ? t : document.body 
    a = 'client';
  }
  
  return e[a+'Width'];
}

function getCookie(c_name){
	if(document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return false;
}

function setCookie(c_name,value,expiredays) {

   var exdate=new Date();
   exdate.setDate(exdate.getDate()+expiredays);
   var	cookie_value = c_name + "=" +escape(value) 
      + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
   document.cookie=cookie_value;
   //alert('I set the cookie' + cookie_value);
}


function setDefaultFontSize() {
 
   var fontSize = getCookie("fontSize");

//alert('set1');

   var body = document.getElementsByTagName('body')[0];

   if ( fontSize ) {
      body.style.fontSize = fontSize;
   } else {
      //autoZoom();
      body.style.fontSize = '1em';
   }
//alert('set2');

   var zoom = document.getElementById('zoom');

   if ( zoomAdjusted() ) {
      zoom.style.visibility = 'hidden';
   } else {
      zoom.style.visibility = 'visible';
   }

}



function zoomAdjusted() {
//alert('za?');
   var body = document.getElementsByTagName('body')[0];
   //var adjusted_size = Math.round(getViewport() * 1000 / 1096 / 0.75 ) / 1000;
   var adjusted_size = Math.round(getViewport() * 1000 / 1096 ) / 1000;
   var current_size = body.style.fontSize;
   current_size = current_size.replace( 'em', '' );
   difer = Math.abs( adjusted_size - current_size )
   //alert ( "curr " + current_size + " adj " + adjusted_size + "difer " + difer );
   return ( difer < 0.1 ); // 10%
}


function autoZoom() {

   var body = document.getElementsByTagName('body')[0];
   //var adjusted_size = Math.round( getViewport() * 1000 / 1096 / 0.75 ) / 1000;
   var adjusted_size = Math.round( getViewport() * 1000 / 1096 ) / 1000;

   
   // Preference to 100%
   if ( Math.abs( 1 - adjusted_size ) < 0.1 ) {
      adjusted_size = 1;
   }

   //adjusted_size = adjusted_size * 0.75;
   adjusted_size = adjusted_size + "em";

   //alert ( "current " + body.style.fontSize + " change to " + adjusted_size );

   body.style.fontSize = adjusted_size;

   var zoom = document.getElementById('zoom');
   zoom.style.visibility = 'hidden';

   //setCookie( "fontSize", body.style.fontSize, 5);
   setCookie( "fontSize", body.style.fontSize );
}



window.onresize = function () {

   var zoom = document.getElementById('zoom');
   if ( zoomAdjusted() ) {
      zoom.style.visibility = 'hidden';
   } else {
      zoom.style.visibility = 'visible';
   }
}

/*
   Load before page is rendered
*/

function init() {
       // quit if this function has already been called
       if (arguments.callee.done) return;

       // flag this function so we don't do the same thing twice
       arguments.callee.done = true;

       setDefaultFontSize();
};

if (document.addEventListener) {
   // for Mozilla 
   document.addEventListener("DOMContentLoaded", init, false);
} else {
   // for other browsers 
   window.onload = init;
}

