// JavaScript Document

var ns4 = (document.layers)? true:false;   			//NS 4 
var ie4 = (document.all)? true:false;   				//IE 4 
var dom = (document.getElementById)? true:false;   //DOM

function SetClip(DivId,MyClip)
/*********************************************************
Fonction permettant de fixer la propriété clip d'un <DIV>.
Paramètres : 
			DivId 	->	propriété ID du <DIV>
			MyClip	->	tableau contenant les valeurs
							du clip (top,right,bottom,left)
**********************************************************/
	{
	if (dom)
		{
		document.getElementById(DivId).style.clip = "rect("+MyClip[0]+"px "+
			MyClip[1]+"px "+
			MyClip[2]+"px "+
			MyClip[3]+"px)";
		}
	else if (ie4) 
		{
		document.all[DivId].style.clip = "rect("+MyClip[0]+"px "+
			MyClip[1]+"px "+
			MyClip[2]+"px "+
			MyClip[3]+"px)";
		}
	else if (ns4)
		{
		document.layers[DivId].clip.top = MyClip[0];
		document.layers[DivId].clip.right = MyClip[1];
		document.layers[DivId].clip.bottom = MyClip[2];
		document.layers[DivId].clip.left = MyClip[3];
		}	
	}


function Show(DivId)
/*********************************************
Fonction permettant de rendre visible un DIV.
Paramètres : 
			DivId 		->	propriété ID du <DIV>
**********************************************/
	{
	if (dom)
		{
		document.getElementById(DivId).style.visibility = "visible";
		}
	else if (ie4) 
		{
		document.all[DivId].style.visibility = "visible";
		}
	else if (ns4)
		{
		document.layers[DivId].visibility = "show";
		}
	}


function Hide(DivId)
/*********************************************
Fonction permettant de rendre invisible un DIV.
Paramètres : 
			DivId 		->	propriété ID du <DIV>
**********************************************/
	{
	if (dom)
		{
		document.getElementById(DivId).style.visibility = "hidden";
		}
	else if (ie4) 
		{
		document.all[DivId].style.visibility = "hidden";
		}
	else if (ns4)
		{
		document.layers[DivId].visibility = "hide";
		}
	}


function PreloadImages()
/*********************************************
Préchargement des images passées en paramètre,
pour rendre fluide les rollover
**********************************************/

	{
	var MyArguments = PreloadImages.arguments;
	var MyImages = new Array();
	if (document.images)
		{
		for(var i=0; i<MyArguments.length; i++)
			{
			MyImages[i]= new Image();
			MyImages[i].src = MyArguments[i];
			}
		PreloadFlag = true;
		}
	}

function getHeight(DivId)
/******************************************************
Fonction permettant de connaître la hauteur d'un DIV.
*******************************************************/
	{
	if (dom)
		return (document.getElementById(DivId).offsetHeight);
	if (ie4) 
		return (document.all[DivId].clientHeight);
	if (ns4)
		return (document.layers[DivId].clip.height);
	}

function getWidth(DivId)
/******************************************************
Fonction permettant de connaître la largeur d'un DIV.
*******************************************************/
	{
	if (dom)
		return (document.getElementById(DivId).offsetWidth);
	if (ie4) 
		return (document.all[DivId].clientWidth);
	if (ns4)
		return (document.layers[DivId].clip.width);
	}
/* Get size of window into a 2 element array
It doesn't take account of the menu or the status bar
*/
function getWindowSize() {
	var MySize = new Array();
	if (window.innerWidth)
			MySize[0] = window.innerWidth;
	else
			MySize[0] = document.body.clientWidth;
	if (window.innerHeight)
			MySize[1] = window.innerHeight;
	else
			MySize[1] = document.body.clientHeight;
	return MySize;	
}

function clip50(id) {
    var rec = new Array(4);
	var wid = getWidth(id);
	var ht = getHeight(id);
	//top
	rec[0] = 0 + ht / 4;
	//right
	rec[1] = wid - wid/4;
	//bottom
	rec[2] = ht- ht/4;
	//left
	rec[3] = wid / 4;
	SetClip('rect2',rec);
}
function clip90(id) {
    var rec = new Array(4);
	var wid = getWidth(id);
	var ht = getHeight(id);
	//top
	rec[0] = 0 + ht / 10;
	//right
	rec[1] = wid - wid/10;
	//bottom
	rec[2] = ht- ht/10;
	//left
	rec[3] = wid / 10;
	SetClip('rect2',rec);
}
function clip100(id) {
    var rec = new Array(4);
	var wid = getWidth(id);
	var ht = getHeight(id);
	//top
	rec[0] = 0 ;
	//right
	rec[1] = wid ;
	//bottom
	rec[2] = ht;
	//left
	rec[3] = 0 ;
	SetClip('rect2',rec);
}
/* take the given element, hide it,
clip it to a nothing at the centre then
increase the size visible by n steps until
whole picture is visible 
param id of element as a string
param number of steps
 */	
var globalsteps;
var top;
var right;
var bottom;
var left;

function grow(id, steps, delay) {
	//alert("grow");
	Hide(id);
	//alert("grow");
	var wid = getWidth(id);
	var ht = getHeight(id);
	//alert("width="+wid+" height= "+ht);
	
	globalsteps = steps;
	top = new Array(steps);
	right = new Array(steps);
	bottom = new Array(steps);
	left = new Array(steps);
	
	for (i=0; i< steps; i++) {
		top[i] = (steps-1 - i) * ht /(steps * 2);
		bottom[i] = ht - top[i]
		left[i] = (steps-1 - i) * wid / (steps * 2);
		right[i] = wid - left[i];
	}
	clip(id, 0);
	Show(id);
	var timestring = "nextSize(0, '"+id+"',"+delay+")";
	//alert(timestring);
	setTimeout(timestring, delay);
}
function nextSize(step, id, delay) {
	var intstep = parseInt(step) + 1;
	if (intstep < parseInt(globalsteps)) {
    	clip(id, intstep);
		var timestring = "nextSize(" + intstep+", '"+id+"',"+delay+")";
		setTimeout(timestring, delay);
	} else {
		after_grow();
	} 
}
function clip(id, step) {
	var rec = new Array(4);
	rec[0] = top[step];
	rec[1] = right[step];
	rec[2] = bottom[step];
	rec[3] = left[step]; 
	//alert("Clip "+id+" to "+rec);
   SetClip(id, rec);
 }
 /* replace an image with the next in the sequence
 param id of the image object 
 param number of steps for contraction/expansion
 param delay between steps
 param prefix of URL to image names (=path to images below)
 param array of images (just the filename & extension)
 */
 function replace_image(id, steps, delay, prefix, images, divid) {
	 var index = -1;
	 var old_name = shorten(id.src);
	 //alert("Old image = "+old_name);
	 for (i=0; i<images.length; i++) {
		 //alert("images["+i+"]="+images[i]);
		 if(old_name == images[i]) {
			 //alert("matched");
			 index = i;
		 }
	 }
	 var new_name = old_name;
	 if (index >= 0) {
		 if (index >= images.length-1) {
			new_name = images[0];
		 } else {
			 new_name = images[index+1];
		 }
	 } else {
		 alert("error "+old_name+" not found in image list "+images);
	 }
	 //alert("new_name = "+new_name+" index="+index);
	 //alert("id="+id.id);
	 id.src =  prefix + new_name;
	 grow(divid, 5,100);
 }
/* extract filename from full URL */
function shorten(full) {
	var shortname = full;
	var pos = full.lastIndexOf("/");
	if (pos > 0) {
		shortname = full.substr(pos+1);
	}
	return shortname;
}

 
