/*Tecknosfera DBNET S.L.
	Element: LIB Navigator
	Programed by: francisco_javier_martinez AT hotmail DOT com
	To : Tecknosfera DBNET S.L.
*/


// "Clase LIB_navigator"
function LIB_navigator_object(theNavigator)
{
	var navigators = new Object();
	navigators["MSIE"] = "Microsoft Internet Explorer";
	navigators["Firefox"] = "Firefox";
	navigators["Opera"] = "Opera";
	navigators["Netscape"] = "Netscape";
	navigators["Safari"] = "Safari";


	// Variable pública que define el Nombre del Navegador
	var name = getNameWithUserAgent(theNavigator.userAgent);
			
	// Función Privada para cambiar el nombre del navegador
	function setName(theName)
	{
		name = theName;
	}
	
	// Función Pública para obtener el nombre del navegadorpara obtener el Nombre del Navegardor
	this.getName = function()
	{
		return(name)
	}
	
	// Función que recive el UserAgent del NAvegador y retorna el Noombre del Navegador
	function getNameWithUserAgent(theUserAgent)
	{
		for(elem in navigators)
		{
			if(theUserAgent.indexOf(elem) != -1)
			{
				return(navigators[elem]);
			}
		}
	}
	
	// Función pública para obtener la posición LEFT del objeto pasado como parámetro
	this.findLeftObj = function(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else
		{
			if(obj.x)
			{
				curleft += obj.x;
			}
		}
		return(curleft);
	}
	
	// Función pública para obtener la posición TOP del objeto pasado como parámetro
	this.findTopObj = function(obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else
		{
			if (obj.y)
			{
				curtop += obj.y;
			}
		}
		return(curtop);
	}

	
	var popup = null;
	// Función pública para abrir ventanas POPUP centradas en la pantalla
	this.openPopup = function(pageToOpen, popupName, popupWidth, popupHeight, bars)
	{
		// Si el popup ya está abierto, lo cerramos
		if(popup != null)
			popup.close();
					
		/*Cálculo del posicionamiento del Popup*/
		var screenWidth = parseInt(screen.availWidth);
		var screenHeight = parseInt(screen.availHeight);
		
		var screenCentralWidth = parseInt((screenWidth/2))
		var screenCentralHeight = parseInt((screenHeight/2))
	
		if((popupWidth == 0 || popupWidth == null) && (popupHeight == 0 || popupHeight == null))
		{
			popupWidth = screenWidth;
			popupHeight = screenHeight;
		}
		
		var leftPopup = screenCentralWidth - parseInt((popupWidth/2))
		topPopup = screenCentralHeight - parseInt((popupHeight/2))
	
		var argumentsBars = "statusbar=no,menubar=no,scrollbars=no";
		// Vemos que Barras stiene que cargar la Ventana
		if(bars)
		{
			if(bars.toLowerCase() == "nobars")
			{
				argumentsBars = "location=no,scrollbars=no,menubar=no,resizable=no,status=no,toolbar=no";
			}
			else
			{
				if (bars.toLowerCase() == "onlyscroll")
				{
					argumentsBars = "location=no,scrollbars=yes,menubar=no,resizable=no,status=no,toolbar=no";
				}
				else
				{
					if (bars.toLowerCase() == "onlyscroll")
					{
						argumentsBars = "location=no,scrollbars=yes,menubar=no,resizable=no,status=no,toolbar=no";
					}
					else
					{
						if(bars.toLowerCase() == "" || bars.toLowerCase() == "allbars")
						{
							argumentsBars = "location=yes,scrollbars=yes,menubar=yes,resizable=yes,status=no,toolbar=yes";
						}
					}
				}
			}
		}
		else
		{
			argumentsBars = "location=yes,scrollbars=yes,menubar=yes,resizable=yes,status=no,toolbar=yes";
		}
		if(this.getName() == "Firefox")
		{
			popupHeight= popupHeight + 20; // Incrementamos 20 en Firefox,  porque SIEMPRE saca barra de Estado
		}
		if (popupName == "")
		{
			popupName = "tecknosferaPopup_" + Math.random() * 9;
		}
		

		argumentos = "'fullscreen=no,height=" + popupHeight + ",left=" + leftPopup + "px," + argumentsBars + ",top=" + topPopup + "px,width=" + popupWidth + "'";
		var aleatorio = Math.round(Math.random()*10);
		
		popup = window.open(pageToOpen,popupName + aleatorio,argumentos)

		return(popup);
	}
	
	// Función para abrir una ventana a PANTALLA COMPLETA
	this.openPopupFullScreen = function(pageToOpen)
	{
		bars = "nobars";
		
		// Si el popup ya está abierto, lo cerramos
		if(popup != null)
			popup.close();
					
		/*Cálculo del posicionamiento del Popup*/
		var screenWidth = parseInt(screen.availWidth);
		var screenHeight = parseInt(screen.availHeight);
		
		var screenCentralWidth = parseInt((screenWidth/2))
		var screenCentralHeight = parseInt((screenHeight/2))
	
		popupWidth = screenWidth;
		popupHeight = screenHeight;
		
		var leftPopup = screenCentralWidth - parseInt((popupWidth/2))
		topPopup = screenCentralHeight - parseInt((popupHeight/2))

		var argumentsBars = "statusbar=no,menubar=no,scrollbars=no";
		// Vemos que Barras stiene que cargar la Ventana
		if(bars.toLowerCase() == "nobars")
		{
			argumentsBars = "statusbar=no,menubar=no,scrollbars=no";
		}
		if(this.getName() == "Firefox")
		{
			popupHeight= popupHeight + 20; // Incrementamos 20 en Firefox,  porque SIEMPRE saca barra de Estado
		}
		var varRandom = parseInt(Math.random() * 10);
		var windowName = 'popup_'+varRandom;
		popup = window.open(pageToOpen,windowName,"'" + argumentsBars + ",width=" + popupWidth + ",height=" + popupHeight + ",left = " + leftPopup + "px,top = " + topPopup + "px'")
	}
	
	// Función que recive como parámetro una ventana ya abierta y la centra ne la pantalla
	this.centerPopup = function(thePopupToCenter)
	{
		/*Cálculo del posicionamiento del Popup*/
		var screenWidth = parseInt(screen.availWidth);
		var screenHeight = parseInt(screen.availHeight);
		
		var screenCentralWidth = parseInt((screenWidth/2))
		var screenCentralHeight = parseInt((screenHeight/2))
	
		var leftPopup = screenCentralWidth - parseInt((thePopupToCenter.document.body.offsetWidth/2))
		var topPopup = screenCentralHeight - parseInt((thePopupToCenter.document.body.offsetHeight/2))
		
		thePopupToCenter.moveTo(leftPopup,topPopup)

	}
	
	// Función que nos da la tecla pulsada en el navegador, dependiendo del mismo
	this.getKeyCode = function(evt)
	{
		var e = (window.event) ? window.event : evt;
		
		var theKeyCode = null;
		switch(this.getName())
		{
			case "Microsoft Internet Explorer":
				theKeyCode = e.keyCode;
			break;
			
			case "Firefox":
				theKeyCode = e.which;
			break;
			
			default: 
				theKeyCode = e.keyCode;
		}
		
		return(theKeyCode);
	}
	
	// Function to add the actual page to Favorites
	this.addToFavorites = function()
	{
		var title = document.title;
		var url = document.location.href;
		if(document.all)
		{
			if(document.location.href.indexOf("http://") ==  -1)
			{
				url = "http://" + document.location.href;
			}
			// For MIE
			window.external.AddFavorite(url,title);
		}
		else
		{
			if(document.location.href.indexOf("http://") ==  -1)
			{
				url = "http://" + document.location.href;
			}
			// For Firefox
			window.sidebar.addPanel(title, url,"");
		}
		
	}
	
	// Function to GET the Screen Width
	this.getScreenWidth = function()
	{
		return(parseInt(screen.availWidth));
	}

	// Function to GET the Screen Width
	this.getScreenHeight = function()
	{
		return(parseInt(screen.availHeight));
	}
	
		
	// Function to GET the Usable Navigator Width
	this.getWidth = function()
	{
		return(parseInt(document.body.clientWidth));
	}

	// Function to GET the Usable Navigator Width
	this.getHeight = function()
	{
		return(parseInt(document.body.clientHeight));
	}
	
	/*
		Fonction to make the roll over efect to a image.
		Image SRC syntax: imageName_status.extension
						  imageName -> The name or URL of the image. (el nombre no puede tener guiones bajos, dado que es el separador del status)
						  Status = 0 o 1 (0 = Off		1 = On)
	*/
	this.rollOverImage = function(objReferer)
	{
		var theSRC = objReferer.src;
		var theExtension = theSRC.substr((theSRC.length) - 3, (theSRC.length));
		var actualStatus = theSRC.substr((theSRC.length) - 5, 1);
		if(actualStatus == 0)
		{
			newStatus = 1;
		}
		else
		{
			newStatus = 0;
		}
		objReferer.src = theSRC.substr(0,(theSRC.length) - 5) + newStatus + "." + theExtension;
	}

	this.changeImage = function(objReferer, status)
	{
		var theSRC = objReferer.src;
		var theExtension = theSRC.substr((theSRC.length) - 3, (theSRC.length));
		objReferer.src = theSRC.substr(0,(theSRC.length) - 5) + status + "." + theExtension;
	}
	
}

// Creamos un objeto de la clase LIB_navigator
var LIB_navigator = new LIB_navigator_object(navigator);

