/*BrowserDetect****************************************************************/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
/******************************************************************************/

/*CANVAS***********************************************************************/
function CANVAS() {
	var anchoP=0;
	var altoP=0;
	function calculaCanvasSize() {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			ancho = window.innerWidth;
			alto = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			ancho = document.documentElement.clientWidth;
			alto = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			ancho = document.body.clientWidth;
			alto = document.body.clientHeight;
		}
		this.anchoP=ancho;
		this.altoP=alto;
	}
	
	function anchoCanvas() {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			ancho = window.innerWidth;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			ancho = document.documentElement.clientWidth;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			ancho = document.body.clientWidth;
		}
		return ancho;
	}
	
	function altoCanvas() {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			alto = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			alto = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			alto = document.body.clientHeight;
		}
		return alto;
	}

	function ancho () {
		return anchoCanvas();
	}
	
	function alto() {
		return altoCanvas();
	}
	
	this.ancho=ancho;
	this.alto=alto;
}
canvas=new CANVAS();
/******************************************************************************/

/*Mouse************************************************************************/
function MOUSE() {
	var X=0;
	var Y=0;
	function getMouseXY(e) {
		if (IE) { // para IE
			this.X = window.event.clientX;
			this.Y = window.event.clientY;
		}	
		else {  // para NS
			this.X = e.pageX;
			this.Y = e.pageY;
		}
		/*Por compatibilidad ponemos disponibles las variable globales mouseX y mouseY*/
		mouseX=this.X;
		mouseY=this.Y;
		//window.status=this.X + ":" + this.Y;
	}
	
	var IE = (BrowserDetect.browser=="Explorer")?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = getMouseXY;
}
mouse=new MOUSE();

/******************************************************************************/

/*********************************************************************/
/** Biblioteca *******************************************************/
/*********************************************************************/
function abreVentana (urlVentana,ancho,alto,posX,posY)
{
	especificaciones="top=" + posY + ",left=" + posX +
		",hotkeys=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+ ancho + ",height="+ alto;
	target="_blank";
	window.open(urlVentana,target,especificaciones);
}

function abreVentanaCentrada (urlVentana,ancho,alto)
{
	posX=(screen.width - ancho)/2;
	posY=(screen.height - alto)/2;
	especificaciones="top=" + posY + ",left=" + posX +
		",hotkeys=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+ ancho + ",height="+ alto;
	target="_blank";
	window.open(urlVentana,target,especificaciones);
}

function isnumber(str) { 
	if(str.length==0) {
		return false;
	} 
	numdecs = 0; 
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i); 
		if ((mychar >= "0" && mychar <= "9") || mychar == "." )	{ 
			if (mychar == ".") numdecs++; 
		} else {
			return false; 
		} 
		if (numdecs > 1) {
			return false;
		}
	} 
	return true; 
}

function isInteger(str,permitirLength0) { 

	if(str.length==0) {
		if (permitirLength0) {
			return true;
		} else {
			return false;
		}
	} 
	numdecs = 0; 
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i); 
		if (!(mychar >= "0" && mychar <= "9")) { 
			return false; 
		} 
		if (numdecs > 1) {
			return false;
		}
	} 
	return true; 
}

//Devuelve el tamao del area del documento (canvas)
function alertSize() {
  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;
  }
  window.alert( 'Width = ' + myWidth + '\n' + 'Height = ' + myHeight);
}

/*************************************************************************************************************/
function setZoom(img, dir, width, height, margin, zIndex, delay) {
  setTimeout(function() {
    if (img.dir==dir) {
      img.style.width=width;
      img.style.height=height;
      img.style.margin=margin;
      img.style.zIndex=zIndex;
    }
  }, delay);
}

function larger(img, width, height) {
  img.dir='rtl';
  //img.style.position='absolute';
  now=parseInt(img.style.zIndex);
  for (i=now+1; i<=10; i++) {
    w=(width*(10+i))/20+'px';
    h=(height*(10+i))/20+'px';
    m=(-i)+'px 0 0 '+(-width*i/40)+'px';
    setZoom(img, 'rtl', w, h, m, i, 20*(i-now));
  }
}

function smaller(img, width, height) {
  img.dir='ltr';
  now=parseInt(img.style.zIndex);
  for (i=now-1; i>=0; i--) {
    w=(width*(10+i))/20+'px';
    h=(height*(10+i))/20+'px';
    m=(-i)+'px 0 0 '+(-width*i/40)+'px';
    setZoom(img, 'ltr', w, h, m, i, 20*(now-i));
  }
  //setTimeout(img.style.position='static',400);
}
