﻿// JavaScript Document

  function getElement(e,f) {
    if(document.layers) {
      f=(f)?f:self;
      if(f.document.layers[e]) {
        return f.document.layers[e];
        }
      for(W=0;i<f.document.layers.length;W++) {
        return(getElement(e,fdocument.layers[W]));
        }
      }
    if(document.all) {
      return document.all[e];
      }
    return document.getElementById(e);
    }

  function setHeight(objID,objHeight) {
    getElement(objID).style.height = objHeight;
    }

  function setWidth(objID,objWidth) {
    getElement(objID).style.width = objWidth;
    }
    
  function setBookmark() {
    var title = 'Dio Dopo Internet - la predica on-line dal 1995' //document.title
    var url = 'http://www.diodopointernet.it/'  //location.href

    if (window.sidebar) window.sidebar.addPanel(title, url,"");

    else if( window.opera && window.print )
    {
      var mbm = document.createElement('a');
      mbm.setAttribute('rel','sidebar');
      mbm.setAttribute('href',url);
      mbm.setAttribute('title',title);
      mbm.click();
    }

    else if( document.all ) window.external.AddFavorite( url, title);

    //alert('La pagina \350 stata salvata tra i Preferiti del tuo Browser');
  }
  
  function compareHeight(objA,objB) {
    if (getElement(objA).style.display == 'block' || getElement(objA).style.display == '') {
      //alert('> ?: ' + (getElement(objA).offseteight > getElement(objB).offsetHeight));
      if (getElement(objA).offsetHeight > getElement(objB).offsetHeight) {
        getElement(objA).style.display = 'none';
        }
      }
    else {
      //alert('height = ' + getElement(objA).height);
      //alert('<= ?: ' + (getElement(objA).height <= getElement(objB).offsetHeight));
      if (131 <= getElement(objB).offsetHeight) {
        getElement(objA).style.display = 'block';
        }
      }
    }
    
  function adjustHeight(objA,objB) {
    if (getElement(objA).offsetHeight < getElement(objB).offsetHeight) {
        //alert('casoA');
        setHeight(objA,getElement(objB).offsetHeight + 'px');
      } else {
        //alert('casoB');
        setHeight(objB,getElement(objA).offsetHeight + 'px');
      }
    }
    
    function trimStr(newstr)
    {
    	newstr = newstr + ""; //cast di tipo su String
    	newstr = lTrimStr(newstr);
    	newstr = rTrimStr(newstr);
    	return newstr;
    }

    function lTrimStr(newstr)
    {
    	newstr = newstr + "";
    	while(newstr.charAt(0) == " ")
    		newstr = newstr.substring(1, newstr.length);
    	return newstr;
    }

    function rTrimStr(newstr)
    {
    	newstr = newstr + "";
    	while(newstr.charAt(newstr.length - 1) == " ")
    		newstr = newstr.substring(0, newstr.length - 1);
    	return newstr;
    }

/*
 * <p>Title: Style Managemet</p>
 * <p>Description: JavaScript for css and font-size management based on cookies. Tested on Win (IE, Moz, Opera) and Mac (Safari)</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: http://www.mac4.it</p>
 * @author m.casati@mac4.it
 * @version 1.1
 */

/**************************************************************************\
	STRING PROTOTYPE
\**************************************************************************/

function replaceText(pattern, substitute)
{
	return this.split(pattern).join(substitute);
}
String.prototype.replaceText = replaceText;
function trim()
{
	var newstr = this + "";
	newstr = newstr.lTrim();
	newstr = newstr.rTrim();
	return newstr;
}
String.prototype.trim = trim;
function lTrim()
{
	var newstr = this + "";
	while(newstr.charAt(0) == " ")
		newstr = newstr.substring(1, newstr.length);
	return newstr;
}
String.prototype.lTrim = lTrim;
function rTrim()
{
	var newstr = this + "";
	while(newstr.charAt(newstr.length - 1) == " ")
		newstr = newstr.substring(0, newstr.length - 1);
	return newstr;
}
String.prototype.rTrim = rTrim;

/**************************************************************************\
	COOKIES
\**************************************************************************/
function setCookie(key, value, expirationdate)
{
	var c = key + "=" + escape(value) + ";";
	c += "path=/;";
	if(expirationdate)
		c += "expires=" + expirationdate.toUTCString() + ";";
  //response.setHeader('p3p','cp=\u0022NOI DSP COR NID TAIo OUR NOR STA\u0022');
	document.cookie = c;
}
function getCookieList()
{
	var cl = document.cookie.split(";");
	var c = new Array();
	for(var i = 0; i < cl.length; i++)
	{
		var a = cl[i].split("=");
		if(a.length == 2)
			c[a[0].trim()] = escape(a[1].trim());
	}
	return c;
}
function getCookie(key)
{
	return getCookieList()[key];
}

/**************************************************************************\
	CSS
\**************************************************************************/
var COOKIE_CSS_KEY = "style";
var CSS_LIST =
{
	"default"			: "../stili/stili_txt.css",
	"relax"				: "../css/relax.css",
	"highcontrast"		: "../css/highcontrast.css"
};
var DEFAULT_CSS = "default";
function setCSS(css)
{
	setCookie(COOKIE_CSS_KEY, css, getCookieExpirationDate());
	document.location.reload();
}
function initCSS()
{
	if(!document.getElementsByTagName)
		return;
	var css = getCookie(COOKIE_CSS_KEY);
	if(css)
	{
		var links = document.getElementsByTagName("link");
		for(var i = 0; i < links.length; i++)
		{
			if(links[i].media == "screen" && links[i].title == DEFAULT_CSS && CSS_LIST[css])
				links[i].href = CSS_LIST[css];
		}
	}
}

/**************************************************************************\
	TEXT SIZE
\**************************************************************************/
var COOKIE_TEXT_SIZE_KEY = "ddi-textsize";
var TEXT_SIZE_LIST =
{
	"m"				: "1em",
	"l"				: "1.5em"
};
var DEFAULT_TEXT_SIZE = "m";

function getkey(e)
{
	if (window.event)
   		return window.event.keyCode;
	else if (e)
   		return e.keyCode;
	else
   		return null;
}

function setTextSize(s)
{
	setCookie(COOKIE_TEXT_SIZE_KEY, s, getCookieExpirationDate());
	document.location.reload();
}

function initTextSize()
{
	if(!document.getElementsByTagName)
		return;
	var cs = getCookie(COOKIE_TEXT_SIZE_KEY);
	s = (cs) ? cs : DEFAULT_TEXT_SIZE;
	document.getElementsByTagName("html")[0].style.fontSize = TEXT_SIZE_LIST[s];
}

/**************************************************************************\
	COMMONS
\**************************************************************************/
function getCookieExpirationDate()
{
	var dt = new Date();
	dt.setFullYear(dt.getFullYear() + 1);	// cookie expires after 1 year
	return dt;
}

/**************************************************************************\
	EVENT HANDLER
\**************************************************************************/
function scriptError(msg, url, line)
{
	//window.status = "ERROR: " + msg + " @ line " + line;
	return true;
}
window.onerror = scriptError;

function initPage(e)
{
	initCSS();
	initTextSize();
}
if(navigator.platform.toLowerCase().indexOf("mac") != -1)	// MAC OS: use event handler
	window.onload = initPage;
else		// NOT MAC OS (Win, Unix, Solaris, ...): call function directly to prevent page reload effect
	initPage();
