/* swijs.js >= 0.03 needs to be loaded before this */
/* FIXME: A+ e A- não funcionam se a página recarregada pegar informações do cookie */

var swiPrefsLoaded = false;

//This is the size of main-content tag
//in the future start it with e$('main-content').style.fontSize; in swiSetUserOptions


var swiDefaultFontSize = 100;
//How much to multiply to have the same size in % (from pt or px)
var swiMaxSize = 250; // max %
var swiMinSize = 60; // Min %
var swiFontStep = 20;
var swiResizeCounter = 0;
var swiFontOriginal = new Object();
var swiFontOriginalMainContent = 0;
var swiFirstTime = 0;
//This has to be load because we have to wait for external csses to load before applying 
swiAddEvent(window, "load", swiSetUserOptions, false);
swiAddEvent(window, "unload", swiSaveSettings, false);

function swiRevertStyles()
{
	swiResizeCounter = 0;
	swiChangeFontSize(0);
}

function swiChangeFontSize(sizeDifference)
{
    if (((swiResizeCounter + sizeDifference) * swiFontStep + 100 <= swiMaxSize) &&
	((swiResizeCounter + sizeDifference) * swiFontStep + 100 >= swiMinSize)
       ){
	    swiResizeCounter =  parseInt(swiResizeCounter) + parseInt(sizeDifference);
	}
	swiSetFontSize(swiResizeCounter);
};

function swiSetFontSize(fontSize)
{
//    percentualSize = fontSize * swiFontStep + swiDefaultFontSize;
//    e$('main-content').style.fontSize = percentualSize + '%';
    
    //Arruma o main-content primeiro
    
    
    mainContentSize = swiGetElementStyle('main-content', 'font-size');
    sizeType = mainContentSize.substring
		    ( mainContentSize.length-1
		    , mainContentSize.length
		    );
    if (sizeType == '%')
    {//é %  ?
	size = parseInt( mainContentSize.substring(0, mainContentSize.length-1) );
	percentualSize = fontSize * swiFontStep + size;
	e$('main-content').style.fontSize = percentualSize + '%';
    }
    else
    {//pt or px only
	sizeType = mainContentSize.substring
		    ( mainContentSize.length-2
	    	    , mainContentSize.length
	    	    );
	
	
	if ( sizeType == "pt" || sizeType == "px" )
	{
	    size = parseInt( mainContentSize.substring(0, mainContentSize.length-2) );
	    
	    if (swiFirstTime == 0)
	    {
 		swiFontOriginalMainContent = size;
 	    }
	    resize = swiFontOriginalMainContent * ((fontSize * swiFontStep + 100)/100);
	    e$('main-content').style.fontSize = resize + sizeType;
	}
    }
    
	
    docBody = e$('main-content');
    
    // Tags é a array de tags permitidas
    // 'div','td','tr','p','b','table', 'strong','em','a','h1','h2',
    // 'h3','pre','sub','sup','i','th','cp','ul','ol','li','dt','dd','input', 'span'
    tags = new Array( '*' );
	
    for (i = 0 ; i < tags.length ; i++ ) 
    {
	allTags = docBody.getElementsByTagName(tags[i]);
	for (k = 0 ; k < allTags.length ; k++) 
	{
 	    if ( allTags[k].style.fontSize )
 	    {
 		//pt or px only
		sizeType = allTags[k].style.fontSize.substring
		    ( allTags[k].style.fontSize.length-2
		    , allTags[k].style.fontSize.length);
		    
 		if ( sizeType == "pt" || sizeType == "px" )
 		{
 		    size = parseInt( allTags[k].style.fontSize.substring(0, allTags[k].style.fontSize.length-2) );
 					
 		    if (swiFirstTime == 0)
 		    {
 			if (typeof(swiFontOriginal[i]) == 'undefined') 
 			{
 			    swiFontOriginal[i] = new Object();
 			}
 			    swiFontOriginal[i][k] = size;
 		    }
 					
 		    resize = swiFontOriginal[i][k] * ((fontSize * swiFontStep + 100)/100);
 					
 		    allTags[k].style.fontSize = resize + sizeType;
		}
 	    }
	}
    }
    swiFirstTime = 1;
    
    //Call external events if needed
    //Have to make sure this is cross-browser supported
    if ( window._swiFontSizerEvent )
    	_swiFontSizerEvent();
}


function swiCreateCookie(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+"="+value+expires+"; path=/";
}

function swiReadCookie(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 swiSetUserOptions()
{
	if(!swiPrefsLoaded){

		cookie = swiReadCookie("swiResizeCounter");
		swiResizeCounter = cookie ? cookie : swiResizeCounter;
		
		swiSetFontSize(swiResizeCounter);
		
		swiPrefsLoaded = true;
	}
}



function swiSaveSettings()
{
  swiCreateCookie("swiResizeCounter", swiResizeCounter, 365);
}
