/*============================================================================================
 Fichier Javascript
 Fichier contenant divers fonction de recuperation d'information du navigateur comme les tailles et les positions de souris

 AUTEUR:					Alban BALLIEUX
 CREATION:					07/07/2008
 VARIABLE GLOBALES:			
 MODIFICATIONS:			
	Fabien THIERRY		- 03/04/2009 - Ajout de Size_ID et Centre_ID
===============================================================================================
	copyright 2009 Alban BALLIEUX - ballieuxa@laon.noirot
	http://www.phpmyportal.info

	This file is part of phpMyPortal.

    phpMyPortal is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    any later version.

    phpMyPortal is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with phpMyPortal; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===============================================================================================*/

/**
	Fonction générique permettant de recurpere la position de la scrollbar de la fentre du navigateur
	
	@return array	tableau contenant le déplacement en X(0),Y(1)
*/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

/**
 * Fonction générique permettant de recupere la taille de la fenetre du navigateur
 * 
 * @return {array}		Tableau contenant largeur(0) et la hauteur(1)
 */
function Size(){
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

/**
 * Recheche de la position verticale d'un element
 * Remonte recurssivement la page jusque HTML en ajoutant les hauteurs
 * 
 * @param {Object} obj		Element de recherche
 * 
 * @return {int}			Position verticale
 */
function GetTopPos(obj){
	var top=obj.offsetTop/1;
	while((obj=obj.offsetParent) != null){
		if(obj.tagName!='HTML')top += obj.offsetTop;
	}
	return top;
}

/**
 * Recheche de la position horizontal d'un element
 * Remonte recurssivement la page jusque HTML en ajoutant les largeurs
 * 
 * @param {Object} 	obj		Element de recherche
 * 
 * @return 	{int}			Position horizontal
 */
function GetLeftPos(obj){
	var left=obj.offsetLeft/1+1;
	while((obj=obj.offsetParent) != null){
		if(obj.tagName!='HTML')left += obj.offsetLeft;
	}
	return left;
}

/**
 * Recherche les marge inferieur de tout les elements parent
 *  
 * @param {Object} 	obj		Element de recherche
 * 
 * @return 	{int}			Marge total
 * @return 	{int}			Valeur par defaut
 */
function GetMarginBottom(Id, Defaut){
	return 0;
	var Bottom = 0;
	var PtId = GetPtId(Id);
	if (PtId==false) return '';
	while((PtId=PtId.parentNode) != document.documentElement){
		Bottom+=parseInt(GetCurrentStyle(PtId, 'margin-Bottom', 0));
		Bottom+=parseInt(GetCurrentStyle(PtId, 'padding-Bottom', 0));
		Bottom+=parseInt(GetCurrentStyle(PtId, 'border-Bottom', 0));
	}
	if (isset()){
		return Bottom;
	}else{
		return Defaut;
	}
}

/**
 * Calcul la taille d'une zone
 * 
 * @param		{string}	ID	L'ID de la zone DIV
 *
 * @return 		{array}		Tableau contenant largeur(0) et la hauteur(1)
 */
function Size_ID(ID){
	if (GetPtId(ID)){
		return [GetPtId(ID).clientWidth, GetPtId(ID).clientHeight];
	}else{
		return [0, 0];
	}
}

/**
 * Fonction qui redimmensionne une suite d'élément passé dans tid, avec val et coefficienté avec le tableau prop 
 * 
 * @param {Object} val
 * @param {Object} tid
 * @param {Object} prop
 */
function AjusteTailleMultiple(val,tid,prop){
	for (i in tid){
		if(tid[i]!=null && i!='containsValue'){
			taille=val*prop[i]/100;
			GetPtId(tid[i]).style.width=taille+'px';
		}
	}
}

/**
 * Permet de centrer une zone DIV (ou tout autre élément référencé par son ID) au milieu de la page
 * 
 * @param		{string}	ID	L'ID de la zone DIV
 *
 * @return 		{array}		Tableau contenant largeur(0) et la hauteur(1)
 */
function Centre_ID(ID){
	if (GetPtId(ID)){
		largeurpage=parseInt(Size()[0]);
		hauteurpage=parseInt(Size()[1]);

		Largeur=parseInt(Size_ID(ID)[0]);
		Hauteur=parseInt(Size_ID(ID)[1]);

		PosX = Math.round(largeurpage/2 - Largeur/2) + document.documentElement.scrollLeft;
		PosY = Math.round(hauteurpage/2 - Hauteur/2) + document.documentElement.scrollTop;
		
		Div=GetPtId(ID);
		Div.style.left=PosX+'px';
		Div.style.top=PosY+'px';
	}
}

/**
 * Positionne une fenetre en fonction du clic
 *
 * @param		{event}		e		Evenement relatif a la souris
 * @param		{string}	PtFen	Nom de la fenêtre a placer
 * 
 */
function PositionFenetre(e,PtFen){
	var DecalX=10;
	var DecalY=20;
	PtFen=GetPtId(PtFen);
	if (PtFen == false)		return '';
	PosX=e.clientX+DecalX;
	PosY=e.clientY+DecalY;

	if (Navigateur() == 'Netscape'){
		deltaX=window.innerWidth-PosX-PtFen.clientWidth;
		deltaY=window.innerHeight-PosY-PtFen.clientHeight;
	}else{
		deltaX=document.body.clientWidth-PosX-PtFen.clientWidth;
		deltaY=document.body.clientHeight-PosY-PtFen.clientHeight;
	}
	if (deltaX<0)PosX=PosX-PtFen.clientWidth - (DecalX*2);
	if (deltaY<0)PosY=PosY-PtFen.clientHeight - (DecalY*2);
	
	TScroll=getScrollXY();
	
	PtFen.style.left=PosX+TScroll[0]+'px';
	PtFen.style.top=PosY+TScroll[1]+'px';
	
	alert(Navigateur());
}
