// Site tools - version 1.2
// Copyright (c) 2006 duechiacchiere - http://www.duechiacchiere.it/
// 
// Feel free to use this script into your website, please send an email
// to info@duechiacchiere.it to keep us aware of this.

// Default position for the sliding menu
// These values should match ones in the html style definition (left and top)
var myTargetX = 600; 
var myTargetY = 120;

// Specify the ID of div to move, shelf, handle, etc
myObjectToMove = "smallNavigator";
myObjectShelf = "accessibleTools";
myObjectHandle = "accessibleTab";
myObjectDescriptionBox = "textNavigator";
myIconDescriptionBox = "toggleDescriptionIcon";

// Specify the ID of text container (for resizing and justifying font)
myContentID = "container";

// How many pixels is the distance from upper border?
var myPaddingTop = 116;

// Justify text on page load? Good values: left or justify
var justifyText = "left";

// Move menu by default?
var isRecursive = "true";

// YOU DO NOT NEED TO MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING // 

// Initialization for scrolling menu
var myCurrentScrollTop = 0;
var myDeltaScrollTop = 0;
var myDx = 0;
var myDy = 0;
myInterval = 25;

// Initialization for font size switch
var myNewSize = 0;

// Initializazion for open/close accessibility shelf and description box
var isDescriptionBoxOpen = false;
var objShelf;
var objDescriptionBox;
var isFadingIn = false;
var isFadingOut = false;

// Function: fontSize
// Input parameters: difference percentage for resizing
// Output parameters: none
// Side effects: changes font size percentage and writes new value into the cookie
//
function fontSize( sizeDiff ) {

  // Get the text container, and its font size
  stObj = (document.getElementById) ? document.getElementById(myContentID) : document.all(myContentID);

  myNewSize = ( myNewSize ? myNewSize : currentFontSizeInEM ) + sizeDiff ;
  
  // Round to two decimals
  myNewSize = Math.round( myNewSize * Math.pow( 10, 2 ) ) / Math.pow( 10, 2 );
  
  // Limit values to max and min allowed
  if ( myNewSize > maxSizeInEM ) {
    myNewSize = maxSizeInEM;
  } else if ( myNewSize < minSizeInEM ) {
    myNewSize = minSizeInEM;
  } 

  stObj.style.fontSize =  myNewSize + 'em';
  stObj.style.lineHeight =  (myNewSize + 0.5) + 'em';
  createCookie("fontSize", myNewSize, 365);
}

// Function: stripTags
// Input parameters: a string to clean
// Output parameters: the string where html tags were removed
// Side effects: none
//
function stripTags (t) {
  while (t.match(/<.*>/)) t = t.replace(/<[^>]*>/, "");
  return t;
}

// Function: justifySwitch
// Input parameters: none
// Output parameters: none
// Side effects: changes text align property for the main content, and writes new value into the cookie
//
function justifySwitch() {
  stObj = (document.getElementById) ? document.getElementById(myContentID) : document.all(myContentID);
  if (stObj.style.textAlign == null || 
      stObj.style.textAlign == "" || 
	  stObj.style.textAlign == undefined || 
	  stObj.style.textAlign == "left") {
	justifyText = "justify";
  }
  else {
    justifyText = "left";
  }
  
  stObj.style.textAlign = justifyText;
  createCookie("justifyText", justifyText, 365);
}

// Function: createCookie
// Input parameters: name of the cookie, value to store, expiration days
// Output parameters: none
// Side effects: create a new cookie for storing values
//
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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// Function: readCookie
// Input parameters: name of the cookie to read
// Output parameters: value stored
// Side effects: none
//
function readCookie(name) {
  var nameEQ = name + "=";
  var cookieValues = document.cookie.split(';');
  for(var i=0; i < cookieValues.length; i++) {
    var aValue = cookieValues[i];
    while (aValue.charAt(0)==' ') aValue = aValue.substring(1, aValue.length);
    if (aValue.indexOf(nameEQ) == 0) return aValue.substring(nameEQ.length, aValue.length);
  }
  return null;
}

// Function: setOpacity
// Input parameters: object, opacity to set
// Output parameters: none
// Side effects: changes visibility property of the object
//
function setOpacity(obj, opacity) {
  opacity = (opacity >= 99)?99.999:opacity;
 
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
	
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
	
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

// Function: fadeIn
// Input parameters: object to show, opacity to set, increasing opacity step, opacity upper limit
// Output parameters: none
// Side effects: changes visibility property of the object
//
function fadeIn(objectToFade, objectName, opacity, step, limit) {
  if ( (opacity < limit) && !isFadingOut ) {
	isFadingIn = true;
	setOpacity(objectToFade, opacity);
	opacity += step * ( 1 - (opacity/100.0));
	window.setTimeout("fadeIn("+objectName+", '"+objectName+"', "+opacity+", "+step+", "+limit+")", 200);
  }
  else {
	isFadingIn = false;
  }
}

// Function: fadeOut
// Input parameters: object to show, opacity to set, decreasing opacity step, opacity lower limit
// Output parameters: none
// Side effects: changes visibility property of the object
//
function fadeOut(objectToFade, objectName, opacity, step, limit) {
  if ( (opacity > limit) && !isFadingIn ) {
	isFadingOut = true;
	setOpacity(objectToFade, opacity);
	opacity -= step * ( 1 - (opacity/100.0));
	window.setTimeout("fadeOut("+objectName+", '"+objectName+"', "+opacity+", "+step+", "+limit+")", 200);
  }
  else { // menu is already fading-in, wait for completion
    isFadingOut = false;

	if (!isFadingIn) {
      obj.style.zIndex = 0;
      objectToFade.style.visibility = "hidden";
	}
  }
}

// Function checkLocation
// Input parameters: none
// Output parameters: none
// Side effects: updates position of given object on scrolling up/down the page
//
function checkLocation(obj) {
  if (navigator.appName.indexOf("Netscape")!= -1) {
    myCurrentScrollTop = window.pageYOffset;
  }
  else {
    if (document.documentElement && document.documentElement.scrollTop) {
      myCurrentScrollTop = document.documentElement.scrollTop;
    }
    else {
      myCurrentScrollTop = document.body.scrollTop;
    }
  }

  myDeltaScrollTop = myScrollTop - myCurrentScrollTop;

  myStepY = Math.round(myDeltaScrollTop / 10); 
  myScrollTop -= myStepY;

  if (myScrollTop >= myTargetY - myPaddingTop) {
    obj.style.top = (myScrollTop + myPaddingTop) + "px";
  }
  
  if (isRecursive == "true") {
    setTimeout("checkLocation(obj)", myInterval);
  }
}

// Function switchRecursion
// Input parameters: none
// Output parameters: none
// Side effects: set to true/false isRecursive variable, to start/stop checkLocation execution, 
//               and writes new value into the cookie
//
function switchRecursion() {

  if (isRecursive == "false") {
    isRecursive = "true";
	createCookie("isRecursive", isRecursive, 365);
	checkLocation(obj);
  }
  else {
    isRecursive = "false";
	createCookie("isRecursive", isRecursive, 365);
  }
}

// Function toggleShelf
// Input parameters: none
// Output parameters: none
// Side effects: open or close the shelf, setting DIV visibility to hidden or visible
//
function toggleShelf() {
  moveHandle = (isShelfOpen)?-1:1;
  visibilityShelf = (isShelfOpen)?"hidden":"visible";
  shelfHeight = parseInt(objShelf.style.height);
  objShelf.style.visibility = visibilityShelf;
  objHandle.style.top = (parseInt(objHandle.style.top)+(shelfHeight*moveHandle)) + "px";
  isShelfOpen = !isShelfOpen;
  createCookie("accessibleshelf", visibilityShelf, 365);
}

// Function toggleDescriptionBox
// Input parameters: none
// Output parameters: none
// Side effects: shows or hides a box describing the icons in the moving menu
//
function toggleDescriptionBox() {
  // Avoid multiple calls to the same routine
  if (!isFadingOut && !isFadingIn) {
    if (isDescriptionBoxOpen) {
      objDescriptionIco.src = "/wp-content/themes/duechiacchiere/img/icon_descriptionbox.gif";
      fadeOut(objDescriptionBox, "objDescriptionBox", 90, 25, 1);
    } else {
      objDescriptionIco.src = "/wp-content/themes/duechiacchiere/img/icon_descbox_close.gif";
	  objDescriptionBox.style.visibility = "visible";
	  obj.style.zIndex = 10;
	  fadeIn(objDescriptionBox, "objDescriptionBox", 0, 15, 90);
	}
	isDescriptionBoxOpen = !isDescriptionBoxOpen;
  } 
}


// Function setActiveStyleSheet (original by A List Apart)
// Input parameters: title of stylesheet to activate
// Output parameters: none
// Side effects: enables the given style
//
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  createCookie("activeStyle", title, 365);
}

// Function resetLayout
// Input parameters: none
// Output parameters: none
// Side effects: reset all defaults
//
function resetLayout() {
  stObj = (document.getElementById) ? document.getElementById(myContentID) : document.all(myContentID);

  // Set active style to default one (style)
  setActiveStyleSheet('style');
  
  // Close shelf
  if (isShelfOpen) {
    toggleShelf();
  }
  
  // Let the scrolling div move
  if (isRecursive == "false") {
    isRecursive = "true";
	createCookie("isRecursive", isRecursive, 365);
	checkLocation(obj);
  }
  
  // Align left
  isJustifiedCookie = readCookie("justifyText");
  isJustified = (isJustifiedCookie != null && isJustifiedCookie != undefined && isJustifiedCookie != "") ? isJustifiedCookie : "left";
  if (isJustified == "justify") {
    stObj.style.textAlign = "left";
    createCookie("justifyText", "left", 365);
  }

  stObj.style.fontSize =  '.96em';
  createCookie("fontSize", '.96', 365);
}

window.onload = function(e) {
  
  // Get the objects with given ID
  if (document.getElementById(myObjectToMove)) {
    obj = document.getElementById(myObjectToMove);
	objShelf = document.getElementById(myObjectShelf);
	objHandle = document.getElementById(myObjectHandle);
	objDescriptionBox = document.getElementById(myObjectDescriptionBox);
	objDescriptionIco = document.getElementById(myIconDescriptionBox);
  }
  else if (eval("document."+myObjectToMove)) {
    obj = eval("document."+myObjectToMove);
	objShelf = eval("document."+myObjectShelf);
	objHandle = eval("document."+myObjectHandle);
	objDescriptionBox = eval("document."+myObjectDescriptionBox);
	objDescriptionIco = eval("document."+myIconDescriptionBox);
  }
  else {
    obj = eval(myObjectToMove);
	objShelf = eval(myObjectShelf);
	objHandle = eval(myObjectHandle);
	objDescriptionBox = eval(myObjectDescriptionBox);
	objDescriptionIco = eval("document."+myIconDescriptionBox);
  }

  // Another cookie tells us if the menu is sticky or not
  isCookieRecursive = readCookie("isRecursive");
  isRecursive = (isCookieRecursive != null && isCookieRecursive != undefined && isCookieRecursive != "") ? isCookieRecursive : "true";
  
  // Netscape and compatible browsers use the WINDOW object for
  // storing window's properties
  //
  if (navigator.appName.indexOf("Netscape")!= -1) {
    myScrollTop = window.pageYOffset;
  }
  else {
    // Internet Explorer 6 changed implementation of its DOM, renaming
    // document.body into document.documentElement
    //
    if (document.documentElement && document.documentElement.scrollTop) {
      myScrollTop = document.documentElement.scrollTop;
    }
    else {
      // This applies to older IE browsers
      myScrollTop = document.body.scrollTop;
    }
  }
  
  // Let the menu fade in (only in front page)
  if (isFront) {
    setOpacity(obj, 0);
    obj.style.visibility = "visible";
    fadeIn(obj, "obj", 0, 25, 99);
  }
  else {
	obj.style.visibility = "visible";
  }
  
  // And now move it up and down, depending on cookie value
  if (isRecursive == "true") {
    checkLocation(obj);
  }
}

