/**********************************************************************************/
/*												functions.js  -  description														*/
/*																------------																		*/
/*			Copyright					: (C) 2004 by Proxima Systems														*/
/*	  	Email							:	info@proximasystems.net																*/
/*	  	web								:	http://www.proximasystems.net													*/
/**********************************************************************************/


/*******************************************************************************/
/*	Función fReloadImage:	 encargada de recargar el objeto imágen pasado como	  */
/*												parámetro.																						*/
/*******************************************************************************/ 
function fReloadImage(myimage) 
   	{

   	var now = new Date(); //Pasámos un parámetro TEMP=fechaactual con fin de evitar caché.
   	if (document.images) 
   		{ 
   		if (myimage.src.indexOf('question.gif')==-1) //Si imágen es interrogación no hacemos nada.
   			{
   			if (myimage.src.indexOf('temp=')!=-1)
   				{
   				tempstring= myimage.src.substring(0,myimage.src.indexOf('temp='));
   				}
   			else//Si URL no contiene parámetro temp lo agregamos.
	   			{
  	 			tempstring=myimage.src;
   				}
   				myimage.src=  tempstring + "&temp=" + now.getTime(); //Cambiamos SRC del objeto.
   			}
	
    	} 
} 

/***************************************************************************************/
/*	Funcion myload: 	Se ejecuta cuando un objeto imágen se ejecuta con éxito (se carga)*/
/*																																											*/
/****************************************************************************************////Objeto this es el objeto image que llama a esta función.
function myload () 
{ 

	if (typeof (document.images[this.name])!='undefined')
		{
		/*Comprobamos si hay un objeto image con el mismo nombre que el que nos llama declarado*/
		document.images[this.name].src=this.src;
		document.images[this.name].id=this.id; 
	}
} 

/***************************************************************************************/
/*	Funcion myerror: 	Se ejecuta cuando un objeto imágen  NO se ejecuta con éxito 		 */
/*																																										 */
/****************************************************************************************/

function myerror() 
{ 
//alert ("Error loading " + this.src);
//Aqui código en caso de error al cargar imágenes... 
} 
 

/*******************************************************************************/
/*	Función fLoadImages()	--> 																								 */
/*			Encargada de intentar cargar tanto imágenes internas como externas 		 */
/* de TODAS las cámaras. Es ejecutada en el evento onload de cualquier página  */
/* que visualize vídeo.																													*/
/*******************************************************************************/ 
 
function fLoadImages(myarray) 
{ 

  /*******	Declaración objetos IMAGE, */
  /*******	2 por cámara, uno para IP interna y otro externa **/ 
     
  if (myarray[0])
		{

// 		Camera1Int = new Image(ShortWidth,ShortHeight); 
// 		//Camera1Int.src="http://" + Camera1IntIp + ":" + Camera1Port + ShortPath; 
// 		Camera1Int.src="http://" + Camera1IntIp + ":" + Camera1Port +  "/axis-cgi/jpg/image.cgi?resolution=" + 320 + "x" + 240 + "&text=" + ShortShowText + "&date="+ ShortShowDate + "&clock=" + ShortShowClock + "&compression=" + ShortCompression;
// 		Camera1Int.name="Camera1"; 
// 		Camera1Int.id=Camera1IntIp; 
// 		Camera1Int.onload=myload;	 
// 		Camera1Int.onerror=myerror; 
// 		
// 		Camera1Ext = new Image(ShortWidth,ShortHeight); 
// 		Camera1Ext.src="http://" + Camera1ExtIp + ":" + Camera1Port +  "/axis-cgi/jpg/image.cgi?resolution=" + 320 + "x" + 240 + "&text=" + ShortShowText + "&date="+ ShortShowDate + "&clock=" + ShortShowClock + "&compression=" + ShortCompression;
// 		Camera1Ext.name="Camera1"; 
// 		Camera1Ext.id=Camera1ExtIp; 
// 		Camera1Ext.onload=myload;	 
// 		Camera1Ext.onerror=myerror; 
		Camera1Int = new Image(ShortWidth,ShortHeight); 
		Camera1Int.src="http://" + Camera1IntIp + ":" + Camera1Port + ShortPath; 
		Camera1Int.name="Camera1"; 
		Camera1Int.id=Camera1IntIp; 
		Camera1Int.onload=myload;	 
		Camera1Int.onerror=myerror; 
		Camera1Ext = new Image(ShortWidth,ShortHeight); 
		Camera1Ext.src="http://" + Camera1ExtIp + ":" + Camera1Port + ShortPath; 
		Camera1Ext.name="Camera1"; 
		Camera1Ext.id=Camera1ExtIp; 
		Camera1Ext.onload=myload;
		Camera1Ext.onerror=myerror; 
		/****************************************/ 

		}
	/****************************************/ 	
if (myarray[1])
		{
		Camera2Int = new Image(ShortWidth,ShortHeight); 
		Camera2Int.src="http://" + Camera2IntIp + ":" + Camera2Port + ShortPath; 
		Camera2Int.name="Camera2"; 
		Camera2Int.id=Camera2IntIp; 
		Camera2Int.onload=myload;	 
		Camera2Int.onerror=myerror; 
		
		Camera2Ext = new Image(ShortWidth,ShortHeight); 
		Camera2Ext.src="http://" + Camera2ExtIp + ":" + Camera2Port + ShortPath; 
		Camera2Ext.name="Camera2"; 
		Camera2Ext.id=Camera2ExtIp; 
		Camera2Ext.onload=myload;	 
		Camera2Ext.onerror=myerror; 
		/****************************************/ 
		}
		
if (myarray[2])
		{		
		Camera3Int = new Image(ShortWidth,ShortHeight); 
		Camera3Int.src="http://" + Camera3IntIp + ":" + Camera3Port + ShortPath; 
		Camera3Int.name="Camera3"; 
		Camera3Int.id=Camera3IntIp; 
		Camera3Int.onload=myload;	 
		Camera3Int.onerror=myerror; 
		
		Camera3Ext = new Image(ShortWidth,ShortHeight); 
		Camera3Ext.src="http://" + Camera3ExtIp + ":" + Camera3Port + ShortPath; 
		Camera3Ext.name="Camera3"; 
		Camera3Ext.id=Camera3ExtIp; 
		Camera3Ext.onload=myload;	 
		Camera3Ext.onerror=myerror; 
		/****************************************/ 
		}
		
if (myarray[3])
		{		
		Camera4Int = new Image(ShortWidth,ShortHeight); 
		Camera4Int.src="http://" + Camera4IntIp + ":" + Camera4Port + ShortPath; 
		Camera4Int.name="Camera4"; 
		Camera4Int.id=Camera4IntIp; 
		Camera4Int.onload=myload;	 
		Camera4Int.onerror=myerror; 
		
		Camera4Ext = new Image(ShortWidth,ShortHeight); 
		Camera4Ext.src="http://" + Camera4ExtIp + ":" + Camera4Port + ShortPath; 
		Camera4Ext.name="Camera4"; 
		Camera4Ext.id=Camera4ExtIp; 
		Camera4Ext.onload=myload;	 
		Camera4Ext.onerror=myerror; 
		/****************************************/ 
		}
if (myarray[4])
		{		
		Camera5Int = new Image(ShortWidth,ShortHeight); 
		Camera5Int.src="http://" + Camera5IntIp + ":" + Camera5Port + ShortPath; 
		Camera5Int.name="Camera5"; 
		Camera5Int.id=Camera5IntIp; 
		Camera5Int.onload=myload;	 
		Camera5Int.onerror=myerror; 
		
		Camera5Ext = new Image(ShortWidth,ShortHeight); 
		Camera5Ext.src="http://" + Camera5ExtIp + ":" + Camera5Port + ShortPath; 
		Camera5Ext.name="Camera5"; 
		Camera5Ext.id=Camera5ExtIp; 
		Camera5Ext.onload=myload;	 
		Camera5Ext.onerror=myerror; 
		/****************************************/ 
		}
if (myarray[5])
		{		
		Camera6Int = new Image(ShortWidth,ShortHeight); 
		Camera6Int.src="http://" + Camera6IntIp + ":" + Camera6Port + ShortPath; 
		Camera6Int.name="Camera6"; 
		Camera6Int.id=Camera6IntIp; 
		Camera6Int.onload=myload;	 
		Camera6Int.onerror=myerror; 
		
		Camera6Ext = new Image(ShortWidth,ShortHeight); 
		Camera6Ext.src="http://" + Camera6ExtIp + ":" + Camera6Port + ShortPath; 
		Camera6Ext.name="Camera6"; 
		Camera6Ext.id=Camera6ExtIp; 
		Camera6Ext.onload=myload;	 
		Camera6Ext.onerror=myerror; 
		}
} 




 
/********************************************************************************/
/*	Función fVideo --> Basada en código de Axis. Muestra objeto de vídeo en     */
/*										el lugar en que sea llamada.															*/
/********************************************************************************/
/*	Parámetros:			width --> ancho del vídeo.																	*/
/*									height --> alto del vídeo.																	*/
/*									compression --> nivel de compresión (0-100).								*/
/*									clock --> ¿mostrar reloj? (0 ó 1).													*/
/*									date --> ¿mostrar fecha? (0 ó 1).														*/
/*									text --> ¿mostrar texto? (0 ó 1).														*/
/********************************************************************************/

function fVideo(IP,port,width,height,compression,clock,date,text)
{

    	// Set the BaseURL to the url of your camera
 		 // Example:  var BaseURL = 'http://172.21.1.122/';
  		// Since this file is located inside the unit itself, no base url is specified here
  		var BaseURL = "http://"+ IP + ":" + port +"/";
  		// DisplayWidth & DisplayHeight specifies the displayed width & Height of the image.
  		// You may change these numbers, the effect will be a stretch or a shrink of the image
  		var DisplayWidth = width;
  		var DisplayHeight = height;

  		// This is the path to the image generating file inside the camera itself
		var File = "axis-cgi/mjpg/video.cgi?resolution=" + width + "x" + height + "&compression=" + compression + "&clock=" + clock + "&date=" + date + "&text=" + text;
		// No changes required below this point

		var output = "";
		
		if ((navigator.appName == "Microsoft Internet Explorer") && 
   			(navigator.platform != "MacPPC") && (navigator.platform != "Mac68k"))
		{
  			// If Internet Explorer under Windows then use ActiveX 
  			output =  '<OBJECT ID="Player" width='	
  			output += DisplayWidth;
  			output += ' height=';
  			output += DisplayHeight;
  			output += ' CLASSID="CLSID:745395C8-D0E1-4227-8586-624CA9A10A8D" ';
  			output += 'CODEBASE="';
  			output += BaseURL;
  			output += '/activex/AMC.cab#version=3,11,0,1">';
  			output += '<PARAM NAME="MediaURL" VALUE="';
  			output += BaseURL;
  			output += File + '">';
  			output += '<param name="MediaType" value="mjpeg-unicast">';
  			output += '<param name="ShowStatusBar" value="0">';
  			output += '<param name="ShowToolbar" value="0">';
  			output += '<param name="AutoStart" value="1">';
  			output += '<param name="StretchToFit" value="1">';
  			output += '<BR><B>Axis Media Control</B><BR>';
  			output += 'The AXIS Media Control, which enables you ';
  			output += 'to view live image streams in Microsoft Internet';
  			output += ' Explorer, could not be registered on your computer.';
  			output += '<BR></OBJECT>';
			} 
		else {
  			// If not IE for Windows use the browser itself to display
  			theDate = new Date();
  			output = '<IMG SRC="';
  			output += BaseURL;
  			output += File;
  			output += '&dummy=' + theDate.getTime().toString(10);
  			output += '" HEIGHT="';
  			output += DisplayHeight;
  			output += '" WIDTH="';
  			output += DisplayWidth;
  			output += '" ALT="Camera Image">';
 		}
	document.write(output);
}


/********************************************************************************/
/*	Función fSendRequest --> Abre una nueva ventana que envía una solicitud 		*/
/*										de encendido/apagado para la salida digital de la cámara.	*/
/*										Dicha ventana será cerrada automáticamente.								*/
/********************************************************************************/
/*	Parámetros:			IP 		 --> 		Dirección IP de la cámara.										*/
/*									PORT	 --> 		Puerto HTTP.																	*/
/*									action -->    ON	u OFF																			*/
/********************************************************************************/

function fSendRequest(IP,PORT,action)
{
	

	var TARGET="";
	TARGET="http://" + IP + ":" + PORT + "/axis-cgi/io/output.cgi?action=1:";
	if(action=="ON"){
		TARGET=TARGET+"\\";
	}
	if(action=="OFF"){
		TARGET=TARGET+"/";
	}
	fPopWindow("about:blank",200,100);//Abrimos nueva ventana
	
	/*******Creamos página HTML a mostrar *********/
	
	newWindow.document.write("<?xml encoding=\"iso 8859-1\" version=\"1.0\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">");
	newWindow.document.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
	newWindow.document.write("<head>");
	newWindow.document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
	newWindow.document.write("<title>Processing request</title>");
	newWindow.document.write("</head>");
	newWindow.document.write("<frameset rows=\"100%,0%\" >");
	newWindow.document.write("<frame  name=\"main\" frameborder=\"0\" marginwidth=\"10\" marginheight=\"10\" noresize=\"noresize\"/>");
	newWindow.document.write("<frame name=\"hidden\" src=\"" + TARGET + "\" marginwidth=\"10\" marginheight=\"10\" noresize=\"noresize\" onload=\"self.close();\"/>");
	newWindow.document.write("</frameset>");
	newWindow.document.write("</html>");

	/***************************************************/
	//Importante! Cerrar documento
	newWindow.document.close();
	newWindow.frames.main.document.write("<html><body bgcolor=\"#869898\"><h3>Processing...<h3></body></html>");
	newWindow.frames.main.document.close();
}

/********************************************************************************/
/*	Función fPopWindow --> Abre una nueva ventana 													 		*/
/********************************************************************************/
/*	Parámetros:			theURL		--> 		Dirección IP de la cámara.								*/
/*									PORT		 	--> 		Puerto HTTP.															*/
/*									action 		-->    	ON	u OFF																	*/
/********************************************************************************/

function fPopWindow(theURL,width,height) 
	{
	newWindow = window.open(theURL,'newWindow','toolbar=no,menubar=no,resizable=no,scrollbars=yes,status=no,location=no,width='+width+',height='+height);
	}
	

/********************************************************************************/
/*	Función fOpenFtpWindow --> 																									*/
/********************************************************************************/

function fOpenFtpWindow(path)
{


if (Camera6Int.complete==true)
	{
	theURL="ftp://usuario:pass@IP_INTERNA:PORT" + path;
	fPopWindow(theURL,640,480);
	}
else if (Camera6Ext.complete==true)
	{
	theURL="ftp://usuario:pass@IP_EXTERNA:PORT" + path;
	fPopWindow(theURL,640,480);
	}
else
	{
		alert("Error, espere unos instantes y vuelva a intentarlo.\nSi el problema persiste consulte con Proxima Systems.")
	}
	

}
