/* ------------------------------------------------------------------ */
/* Copyright © Pierre TORRIS - www.ptorris.com - Tous droits réservés */	 
/* ------------------------------------------------------------------ */

/* ----------------- */
/* fonctions Cookies */	 
/* ----------------- */

function SetCookie (pname,pvalue,pexpires,ppath,pdomain,psecure) { 
	var argv = SetCookie.arguments; 
	var argc = SetCookie.arguments.length; 
	var expires = (argc > 2) ? argv[2] : null; 
	var path = (argc > 3) ? argv[3] : null; 
	var domain = (argc > 4) ? argv[4] : null; 
	var secure = (argc > 5) ? argv[5] : false; 
document.cookie = pname + "=" + escape (pvalue) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function GetCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}
    
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return GetCookieVal(j); }
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) { break; }
	}
	return null;
}

function DeleteCookie (name) { 
	var cval = GetCookie(name); 
	var expires = new Date(); 
	expires.setTime (expires.getTime() - 1); 
	document.cookie = name + "=" + cval + "; expires=" + expires.toGMTString(); 
} 

/* ------------------------------------------------------------------- */
/*  Fonctions développées par Pierre TORRIS -- © Tous droits réservés  */
/* ------------------------------------------------------------------- */

/* PTHP_PopupImage - © Pierre TORRIS - www.ptorris.com - All right reserved */
/*         Contact the author for use - Do not remove the copyright         */

function PTHP_PopupImage(sName, fName, sWidth, sHeight) {
	if (fName != null) {
		if (!PTHP || PTHP.closed) {
			PTHP = open("",'WPTHP','left=10,top=10,width='+sWidth+',height='+sHeight+',toolbar=no,scrollbars=no,location=no,directories=no,status=no,menubar=0,resizable=no');}
		else { 
			var obj	=	PTHP.document.body;
			var iWidth = parseInt(sWidth) - parseInt(obj.clientWidth);
			var iHeight = parseInt(sHeight) - parseInt(obj.clientHeight); 
			PTHP.resizeBy(iWidth,iHeight); 
			PTHP.document.clear();
		} 
		PTHP.document.write("<html><head>");
		PTHP.document.write("<title>Aper&#231;u de "+sName+"</title>");
		PTHP.document.write("<meta name='Copyright' content='COPYRIGHT (c) Pierre TORRIS' />");
		PTHP.document.write("</head>");
		PTHP.document.write("<script language=javascript>window.focus();</script>");
		PTHP.document.write("<body onclick='window.close()' leftMargin='0' rightMargin='0' topMargin='0' bottomMargin='0'>");
		PTHP.document.write("<div><img src='"+fName+"' alt='Cliquez pour refermer' /></div>");
		PTHP.document.write("</body></html>");
		PTHP.document.close();
	}
}

/* PTHP_SaveParams - © Pierre TORRIS - www.ptorris.com - All right reserved */
/*         Contact the author for use - Do not remove the copyright         */

function PTHP_SaveParams(id) {
	var list = new Array();
	var objs = document.getElementsByTagName("*"); 
	for (var i = 0; ( obj = objs[i] ); i++)	{
		if ((obj.id.indexOf(LAYER) != -1) && (obj.style.display == NONE)) {   
  		list.push(obj.id.substr(6,3)); } 
	}
	if (list.length != 0) {
		SetCookie(id,list,EXPIRES);
	} else { DeleteCookie(id); }
}

/* PTHP_Display - © Pierre TORRIS - www.ptorris.com - All right reserved */
/*       Contact the author for use - Do not remove the copyright        */

function PTHP_Display(id) {
	var value = NONE;
	var obj = document.getElementById(id);
	if (obj != null) {
		var temp = obj.style.display;
		if ((temp != null) && (temp == value)) { value = BLOCK; }
	}	
	obj.style.display = value;
}

/* PTHP_SetAutoLight - © Pierre TORRIS - www.ptorris.com - All right reserved */
/*          Contact the author for use - Do not remove the copyright          */

function PTHP_SetAutoLight() {
	var idx = 1 + iCOLOR;  
	if (idx > (aCOLOR.length -1)) { idx = 0; }
	if (idx >= 0) { iCOLOR = idx; }
	var objs = document.getElementsByTagName("*"); 
	for (var i = 0; ( obj = objs[i] ); i++)	{
		if (obj.className.indexOf(LIGHTING) != -1) {
			obj.style.backgroundColor = aCOLOR[iCOLOR]; }
	}
	if (iCOLOR != 0) { SetCookie('iCOLOR',iCOLOR,EXPIRES); } 
		else { DeleteCookie('iCOLOR'); }
}

/* PTHP_SetLayer - © Pierre TORRIS - www.ptorris.com - All right reserved */
/*        Contact the author for use - Do not remove the copyright        */

function PTHP_SetLayer(id) {	
	var subid = id.substr(2,3);
	var objlayer = document.getElementById(LAYER+subid);
	var objbutton = document.getElementById(BUTTON+subid);
	if ((objlayer != null) && (objbutton != null)) {
		var value = objlayer.style.display;
		if ((value != null) && (value == NONE)) {
			objlayer.style.display = "";  /* fix FF */
			objbutton.className = BLOCK;
		} else {
			objlayer.style.display = NONE;	
			objbutton.className = NONE;
		}
	}
	PTHP_SaveParams(id.substr(0,2));
}

/* functions - © Pierre TORRIS - www.ptorris.com - All right reserved */
/*      Contact the author for use - Do not remove the copyright      */

function PTHP_ChangeMenu() {
	var cminiweb = GetCookie(MINIWEB);
	var cmobile = GetCookie(MOBILE);
	if ( ((cminiweb != null) || (cmobile != null)) && ((screen.width < 1024) || (screen.height < 768)) ) {
		var i1 = "Ce bouton restaure le menu latéral, mais cela nécessite une résolution supérieure à 1024x768. Cette fonction est décrite dans l'aide du site.";
		alert(i1);
	} else {
		if (cmobile == null) {
			if (cminiweb == null) {
				SetCookie(MINIWEB,'0',EXPIRES); 
			} else { DeleteCookie(MINIWEB); }
		} else {
			if (cminiweb != null) {
				DeleteCookie(MINIWEB); }
			DeleteCookie(MOBILE);	
		}
		location.reload();
	}	
}

function PTHP_MiniWeb() {
	var cval = GetCookie(MINIWEB);
	if (cval != '0') {
		if ((screen.width < 1024) || (screen.height < 768)) {
			SetCookie(MINIWEB,'1',EXPIRES);	
		}	else { DeleteCookie(MINIWEB); }
	}	
}

function PTHP_SetMobile() {
	var cmobile = GetCookie(MOBILE);
	if (cmobile == null) {	
		SetCookie(MOBILE,'1',EXPIRES);
		location.reload();	
	} else { PTHP_ChangeMenu(); }	
}

function PTHP_SetFontSize(size) {
	var min = 9; var max = 15;
	var objbody = document.getElementsByTagName("body")[0];
	if (objbody != null) {
		if ((size < min) || (size > max)) {
		  size = iFONTSIZE;	}	
		objbody.style.fontSize = size+"px";
		if (size != iFONTSIZE) {
			SetCookie(FONTSIZE,size,EXPIRES);
		} else { 
			DeleteCookie(FONTSIZE);
		}	
	}
}

function PTHP_StepFontSize(step) {
	var property = 'font-size';
	var size = iFONTSIZE; var sz = iFONTSIZE;
	var objbody = document.getElementsByTagName("body")[0];
	if (objbody != null) {
		if (step != 0) {
			if (objbody.currentStyle) { 
				sz = parseInt(objbody.currentStyle[property], 10); 
			} else if (window.getComputedStyle) {            
				sz = parseInt(document.defaultView.getComputedStyle(objbody,null).getPropertyValue(property), 10);
			}	
			size = sz + parseInt(step, 10);
		}
		PTHP_SetFontSize(size);
	}		
} 

function PTHP_GetFontSize() {
	var cval = GetCookie(FONTSIZE);
	if (cval != null) {	
		PTHP_SetFontSize(parseInt(cval));
	}
}

function PTHP_ResetFontSize() {
	PTHP_SetFontSize(iFONTSIZE);
}

function SkipError() { return true; }

function AddFavoris() {
	var browser = navigator.appName;
	var version = parseInt(navigator.appVersion);
	if ( (browser == "Microsoft Internet Explorer") && (version >= 4) ) {
		window.external.AddFavorite("http://www.ptorris.com", "Pierre Torris Home Page");
	}	else {
		alert("Votre navigateur n\'est pas compatible avec cette option.\nPour ajouter le site dans vos favoris, appuyez sur CTRL D")  
	}
}

$(function() {
	if ($.browser.msie && $.browser.version < 7) return;
	$('li')
		.removeClass('highlight')
		.find('a')
		.append('<span class="hover" />').each(function() {
			var $span = $('> span.hover', this).css('opacity', 0);
			$(this).hover(function() {
				$span.stop().fadeTo(250, 1);
			}, function() {
				$span.stop().fadeTo(800, 0);
			});
		});
});

function goTop(a, t) {
	a = a || 0.1;
	t = t || 16;
	var x1 = 0;
	var y1 = 0;
	var x2 = 0;
	var y2 = 0;
	var x3 = 0;
	var y3 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	var x3 = window.scrollX || 0;
	var y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	var speed = 1 + a;
	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
	if(x > 0 || y > 0) {
		var f = "goTop(" + a + ", " + t + ")";
		window.setTimeout(f, t);
	}
}
  
function debug(obj,name) {
	var result = "";
	for (var i in obj)
		result += name + "." + i + " = " + obj[i] + "<br />";
//	return result;
	var w = open("",'POPUP');		
	w.document.write("<html><head><title>DEBUG</title>");
	w.document.write("<meta name='Copyright' content='COPYRIGHT (c) Pierre TORRIS' />");
	w.document.write("</head><body>"+result+"</body></html>");
	w.document.close();
}

/* -------------------------------------------- */
/* Initialisation - Simplification des routines */
/* -------------------------------------------- */

if (top != self) { top.location = self.location; }
window.onerror = SkipError;
var EXPIRES = new Date();
EXPIRES.setFullYear(EXPIRES.getFullYear()+2);
var MINIWEB = "MINIWEB"; PTHP_MiniWeb();
var MOBILE = "MOBILE";
var FONTSIZE = "FONTSIZE";
var iFONTSIZE = parseInt(12);
var NONE = "none"; var BLOCK = "block"; 
var LAYER = "layer_"; var BUTTON = "button_";
var LIGHTING = "lighting";
var PTHP = null; var iCOLOR = 0;
var idx = GetCookie('iCOLOR');
if ((idx != null) && (idx >= 0) && (idx < aCOLOR.length)) { iCOLOR = parseInt(idx); }
