// copyright - begin
//
//   Copyright (C) 2005 Espace Courbe inc.
//   You are free to modify and redistribute this code as long
//   as the following message is included:
//
//   Last modified:
//   $Date: 2006/06/13 15:08:28 $
//
//   Portions Copyright (C) 2006 Espace Courbe inc.
//   Home Page: http://www.espacecourbe.com/
//
//   Author: Christian Fecteau -- christian.fecteau@espacecourbe.com
//   Creation:  2005/12/06 17:05:02
//
// copyright - end

/*

$Id: swapstyle.js,v 1.3 2006/06/13 15:08:28 christian Exp $
$Revision: 1.3 $

*/


/*

This script is used to change text size. It's either
the default size or an increased one (only one).

The change is made with 2 alternate stylesheets.

The script also manages the appearance of a button
to swap between the 2 stylesheets. The button is a
hyperlink on an image

Variables required:

var 
cookie_style = 'styleStorefr', // name of the cookie
a_img_nom = 'a_img_nom_', // name of the button image
a_lien_nom = 'a_lien_nom_', // name of the hyperlink
a_img_src = 'taille-texte.gif', // src of the image when text is set to default
aa_img_src = 'taille-texte-aa.gif', // src of the image when text is increased
a_title = 'Diminuer le texte', // value of the cookie when text is set to default
aa_title = 'Grossir le texte', // value of the cookie when text is increased
ss_html = ''; // html for the button

ss_html += '<a name="' + a_lien_nom + '" href="javascript:swapStyle()" title="' + aa_title + '">';
ss_html += '<img name="' + a_img_nom + '" src="' + a_img_src + '" alt="' + aa_title + '" />';
ss_html += '</a>';

*/


// this variable checks whether or not the browser
// supports this script to change style sheets
var allow_change_stylesheets = (
	(
		document.styleSheets ||
		(window.opera && document.childNodes) ||
		(window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 && document.createElement)
	) &&
	!navigator.__ice_version
)

// if so, we register an onload and onunload event handler
if (allow_change_stylesheets)
{
	// this var will hold the status of the text size
	// and will be used if the cookie fails to be read
	var taille_du_texte = 0;
	// stack eventual alien handlers already registered
	var swapstyle_old_onload = null;
	if (typeof window.onload == "function")
	{
		swapstyle_old_onload = window.onload;
	}
	window.onload = function()
	{
		if (swapstyle_old_onload)
		{
			swapstyle_old_onload();
			swapstyle_old_onload = null;
		}
		// this array was originally defined in the useStyleAgain function
		// but we need it to be defined onload...
		window.MWJss = [];
		// the "true" arg tells that the function is called 
		// onload and not by the user with the button
		swapStyle(true);
	}
	// stack eventual alien handlers already registered
	var swap_old_onunload = null;
	if (typeof window.onunload == "function")
	{
		swap_old_onunload = window.onunload;
	}
	window.onunload = function()
	{
		if (swap_old_onunload)
		{
			swap_old_onunload();
			swap_old_onunload = null;
		}
		// store a cookie with value = title(s) of the activated alternate stylesheet(s)
		rememberStyle(cookie_style,10);
	}
}

// this function writes the html for the button
function ecritBoutonTailleTexte()
{
	if (
		typeof allow_change_stylesheets == 'boolean' &&
		allow_change_stylesheets == true &&
		typeof ss_html == 'string'
	)
	{
		document.writeln(ss_html);
	}
}

// the main function called onload (check=true) and by the user (check=undefined)
function swapStyle(check)
{
	// to manage button properties based on taille_du_texte
	function setNewProp(s,t)
	{
		if (typeof a_img_nom == 'undefined' || !document.images[a_img_nom]) { return; }
		document.images[a_img_nom].src = eval(s + 'a_img_src');
		document.images[a_img_nom].alt = eval(t + 'a_title');
		var lien;
		if (document.all) { lien = document.all[a_lien_nom]; }
		else if (document.links) { lien = document.links[a_lien_nom]; }
		lien.title = eval(t + 'a_title');
		if (lien.blur) lien.blur();
	}
	// get cookie for taille_du_texte
	var c = unescape(retrieveCookie(cookie_style));
	// we load the page and a cookie is set or not for the default style, we abort.
	if (c == a_title && check)
	{
		return;
	}
	// the user pressed the button to increase text size
	else if ((c == a_title || taille_du_texte == 0) && !check)
	{
		changeStyle(aa_title);
		setNewProp('a','');
		taille_du_texte = 1;
	}
	// the cookie (or the current text size) is set to the non-default value:
	// either we load the page or the user pressed the button... "check" will tell us
	else if (c == aa_title || taille_du_texte == 1)
	{
		check == true ? useStyleAgain(cookie_style) : changeStyle(a_title);
		check == true ? setNewProp('a','') : setNewProp('','a');
		taille_du_texte = (check == true) ? 1 : 0;
	}
}

/***********************************************************************************************
                             Script to swap between stylesheets
  Written by Mark Wilton-Jones, 05/12/2002. v2.2.1 updated 14/03/2006 for dynamic stylesheets
************************************************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

To set up the page in the first place:

	Inbetween the <head> tags, put:

		<script src="PATH TO SCRIPT/swapstyle.js" type="text/javascript" language="javascript1.2"></script>

	Also between the head tags, put your stylesheets, best done as external links, but you can use
	<style ...> tags as well.

		Stylesheets cannot be switched if they have no title attribute and will be used at all times:

			<link rel="stylesheet" type="text/css" href="all.css">

		Stylesheets will be on by default if they have a title attribute and their rel attribute is set to 'stylesheet'.
		Most browsers will only allow one of these to be defined (or several sharing the same title):

			<link rel="stylesheet" type="text/css" href="default.css" title="Default">

		Stylesheets will be off by default if they have a title attribute and their rel attribute is set to 'alternate stylesheet':

			<link rel="alternate stylesheet" type="text/css" href="contrast.css" title="High Contrast">
			<link rel="alternate stylesheet" type="text/css" href="bigFont.css" title="Big Font">

To swap between stylesheets:

	changeStyle();                           //switches off all stylesheets that have title attributes
	changeStyle('Default');                  //switches off all stylesheets that have title attributes that do not match 'Default'
	changeStyle('High Contrast');            //switches off all stylesheets that have title attributes that do not match 'High Contrast'
	changeStyle('Big Font');                 //switches off all stylesheets that have title attributes that do not match 'Big Font'
	changeStyle('High Contrast','Big Font'); //switches off all stylesheets that have title attributes that do not match 'High Contrast' or 'Big Font'

	Opera 7+ and Mozilla also allow users to switch between stylesheets using the view menu (only one at a time though ...)

To make the script remember the user's choice of stylesheets, for example to use on more than one page or if they reload
- includes stylesheets chosen using the view menu in Gecko - it will only attempt to store a cookie if they actually
changed something:

	In these examples, I call the cookie used to store the choice 'styleTestStore'. You could use any name you like.

	To remember only until the browser window is closed:

		<body onload="useStyleAgain('styleTestStore');" onunload="rememberStyle('styleTestStore');">

	To remember for 10 days (for example):

		<body onload="useStyleAgain('styleTestStore');" onunload="rememberStyle('styleTestStore',10);">

If you are going to provide users with a mechanism to change stylesheets, you may want to check
if the browser will allow you to change stylsheets first. Use:

	if( document.styleSheets || ( window.opera && document.childNodes ) || ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 && document.createElement ) ) {
		document.write('Something that allows them to choose stylesheets');
	}

It's not perfect, because it will also appear in ICEbrowser, which makes a mess when it tries to
change to an alternate stylesheet. If you want, you can use
	if( ( document.styleSheets || ( window.opera && document.childNodes ) || ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 && document.createElement ) ) && !navigator.__ice_version ) {
but you should then update that if ICE is updated to make it work properly.
________________________________________________________________________________________________*/


function getAllSheets()
{
	if (!window.ScriptEngine && navigator.__ice_version)
	{
		return document.styleSheets;
	}
	if (document.getElementsByTagName)
	{
		var
		Lt = document.getElementsByTagName('link'),
		St = document.getElementsByTagName('style');
	}
	else if (document.styleSheets && document.all)
	{
		var
		Lt = document.all.tags('LINK'),
		St = document.all.tags('STYLE');
	}
	else
	{
		return [];
	}
	for (var x = 0, os = []; Lt[x]; x++)
	{
		var rel = Lt[x].rel ? Lt[x].rel : Lt[x].getAttribute ? Lt[x].getAttribute('rel') : '';
		if (typeof(rel) == 'string' && rel.toLowerCase().indexOf('style') + 1)
		{
			os[os.length] = Lt[x];
		}
	}
	for (var x = 0; St[x]; x++)
	{
		os[os.length] = St[x];
	}
	return os;
}

function changeStyle()
{
	window.userHasChosen = window.MWJss;
	for (var x = 0, ss = getAllSheets(); ss[x]; x++)
	{
		if (ss[x].title)
		{
			ss[x].disabled = true;
		}
		for (var y = 0; y < arguments.length; y++)
		{
			if (ss[x].title == arguments[y])
			{
				ss[x].disabled = false;
			}
		}
	}
	rememberStyle(cookie_style,10);
}

// TODO: make the script ignore any non-relevant 
// alternate stylesheet (not just the "printable version" one)
function rememberStyle(cookieName, cookieLife)
{
	// the line below makes sure that the alernate stylesheet for printable version
	// does not interfere. The printable version has a variable called "vi_title"...
	if (typeof vi_title != 'string') vi_title = 'vi_title';
	for (var viewUsed = false, ss = getAllSheets(), x = 0; window.MWJss && MWJss[x] && ss[x]; x++)
	{
		if (ss[x].disabled != MWJss[x])
		{
			viewUsed = true;
			break;
		}
	}
	if (!window.userHasChosen && !viewUsed)
	{
		return;
	}
	for (var x = 0, outLine = '', doneYet = []; ss[x]; x++)
	{
		if (
			ss[x].title && ss[x].disabled == false &&
			!doneYet[ss[x].title] &&
			// we don't want the "printable version" alternate stylesheet
			// to be seen in this loop...
			unescape(ss[x].title).indexOf(vi_title) == -1
		)
		{
			doneYet[ss[x].title] = true;
			outLine += (outLine ? ' MWJ ' : '') + escape(ss[x].title);
		}
	}
	if (ss.length)
	{
		document.cookie = escape(cookieName) + '=' + escape(outLine) + (cookieLife ? ';expires=' + new Date((new Date()).getTime() + (cookieLife * 86400000)).toGMTString() : '') + ';path=/';
	}
}

function useStyleAgain(cookieName)
{
	for (var x = 0; x < document.cookie.split("; ").length; x++)
	{
		var oneCookie = document.cookie.split("; ")[x].split("=");
		if (oneCookie[0] == escape(cookieName))
		{
			var styleStrings = unescape(oneCookie[1]).split(" MWJ ");
			for (var y = 0, funcStr = ''; styleStrings[y]; y++)
			{
				funcStr += ( y ? ',' : '' ) + 'unescape( styleStrings[' + y + '] )';
			}
			eval('changeStyle(' + funcStr + ');');
			break;
		}
	}
	// ADDED: "if (typeof window.MWJss == 'undefined')" because
	// we don't want to define it again since it is defined onload
	if (typeof window.MWJss == 'undefined') { window.MWJss = []; }
	for (var ss = getAllSheets(), x = 0; ss[x]; x++)
	{
		MWJss[x] = ss[x].disabled;
	}
}
