/*Script para la barra de miarroba*/
function MiarrobaToolBar(nombre){
	// Definimos algunas variables ;)
	this.Clase			= nombre;															// Nombre con la que ha creado la clase
	this.ObjBody		= ((document.body)?document.body:document.documentElement);			// Body para el scrollTop y scrollLeft
	this.TodoOk			= true;																// Para saber si tenemos que ejecutar las cosas
	this.Compatible		= true;																// Saber si el navegador es compatible
	this.divMenu       = false;																//Menu desplegable
	this.botonAnterior	= false;															//Que boton se pulsó anteriormente

	// Miramos si es un navegador permitido...
	this.DetectaBrowsers();
	if( !this.IsGecko && !this.IsOpera && !this.IsIe ) {
		this.TodoOk = false;
		this.Compatible = false;
		return;
	} else {
		if( (this.IsIe && this.BrowserVer<5.5) || (this.IsGecko && this.BrowserVer<20050000) || (this.IsOpera && this.BrowserVer<9) ) {
			this.TodoOk = false;
			this.Compatible = false;
			return;
		}
	}

}


/****************************************************************************************
*	Detecta Gecko, Opera y Explorer
*	Detecta tambien Mac por posibles incompatibilidades...
*	Sacamos la version (o fecha para los Gecko) del navegador para filtrar versiones
*****************************************************************************************/
MiarrobaToolBar.prototype.DetectaBrowsers = function() {
	this.txtAgent	= navigator.userAgent.toLowerCase();
	this.IsGecko	= (navigator.product=="Gecko");														// Detectamos Gecko
	this.IsOpera	= (this.txtAgent.indexOf("opera")!==-1);											// Detectamos Opera
	this.IsIe		= ((this.txtAgent.indexOf("msie")!==-1) && (this.txtAgent.indexOf("opera")===-1));	// Detectamos Explorer
	this.IsMac		= (this.txtAgent.indexOf("mac")!==-1);												// Detectamos Mac
	
	if( this.IsIe ) {
		var Foo = navigator.appVersion.split("MSIE");
		this.BrowserVer = parseFloat(Foo[1]);
	} else if( this.IsOpera ) {
		var Foo = this.txtAgent.replace(/opera[\/ ]/, 'opera');
		Foo = Foo.split("opera");
		this.BrowserVer=parseFloat(Foo[1]);
	} else if( this.IsGecko ) {
		var Foo = this.txtAgent.replace(/gecko[\/ ]/, 'gecko');
		Foo = Foo.split("gecko");
		this.BrowserVer=parseFloat(Foo[1]);
	} else {
		this.BrowserVer = 0;
	}
}

/****************************************************************************************
*	Nos devuelve el objeto que le pedimos
*****************************************************************************************/
MiarrobaToolBar.prototype.$ = function(cual,obj) {
	if( typeof(obj)=='undefined' ) obj=document;
	return obj.getElementById(cual);
}


/*Funciones de Posicion*/

/****************************************************************************************
*	Nos devuelve la posicion de un objeto
*****************************************************************************************/
MiarrobaToolBar.prototype.findPos = function(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/****************************************************************************************
*	Nos devuelve scroll de la pantalla
*****************************************************************************************/
MiarrobaToolBar.prototype.findBodyScroll = function(){
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [x,y];
}

/******************************************************************************************
*	Nos devuelve el tamaño de un objeto
*******************************************************************************************/
MiarrobaToolBar.prototype.findObjSize = function(obj){
	var x,y;
	var test1 = obj.scrollHeight;
	var test2 = obj.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = obj.scrollWidth;
		y = obj.scrollHeight;
	}
	else // Explorer Mac;//would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = obj.offsetWidth;
		y = obj.offsetHeight;
	}
	return [parseInt(x),parseInt(y)];
}

/********************************************************************************************
*  Opciones de submenus	
********************************************************************************************/
MiarrobaToolBar.prototype.openSubmenu = function(boton,data){
	
	var open=true;
	if(this.divMenu!==false)
	{
		this.closeSubmenu(this.divMenu,this.botonAnterior);
		if(this.botonAnterior==boton)
		{
			this.botonAnterior=false;
			open=false;
		}
	}
	
	this.botonAnterior=boton;

	if(open===true)
	{
		this.divMenu=document.createElement('DIV');
		this.divMenu.className='matb_menu';
		//contenido del menu
		var HTML='';
		var z = data.length;
		for(x=0;x<z;x++)
		{
			if(data[x].link!="")
			{
				a=document.createElement('A');
				a.href=data[x].link;
				a.innerHTML=data[x].text;
				a.title=data[x].title;
			}
			else
			{
				a=document.createElement('DIV');
				a.className="sepSubMenu";
			}
			this.divMenu.appendChild(a);

		}
		//posicion del boton
		pos=this.findPos(boton);
		this.divMenu.style.top=(this.findObjSize(boton)[1])+'px';
		this.divMenu.style.left=pos[0]+'px';
		this.divMenu.style.width='150px';
		//this.$('ma_toolbar').zIndex=0;
		this.divMenu.style.zIndex=90000;
		boton.className=boton.className+' matb_menuSel matb_submenuSel';
		boton.style.zIndex=90001;
		//appendChild al body
		this.$('ma_toolbar').appendChild(this.divMenu);
	}
	boton.blur();
	return false;
}

MiarrobaToolBar.prototype.closeSubmenu = function(menu,boton){
	this.$('ma_toolbar').removeChild(menu);
	boton.className=boton.className.replace(/ ?matb_menuSel matb_submenuSel/,'');
	this.divMenu=false;
	return false;
}


//Formulario
MiarrobaToolBar.prototype.mtb_checkform = function(f)
{
	var errorMsg='';
	f.mtb_search_what.value=this.Trim(f.mtb_search_what.value);
	if( f.mtb_search_where.value=="") errorMsg=errorMsg+'\n+ '+this.mtb_search_error_2;
	if(f.mtb_search_what.value=="" ) errorMsg=errorMsg+'\n+ '+this.mtb_search_error_1;
	if(errorMsg!='')
	{
		alert('.: ERROR :.\n'+errorMsg);
		return false
	}
	return true;
}
/*Trim Functions*/
MiarrobaToolBar.prototype.LTrim = function(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
    var j=0, i = s.length;
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;
    s = s.substring(j, i);
  }
  return s;
}
MiarrobaToolBar.prototype.RTrim = function(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    var i = s.length - 1; 
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;
    s = s.substring(0, i+1);
  }
  return s;
}
MiarrobaToolBar.prototype.Trim = function(str)
{
  return this.RTrim(this.LTrim(str));
}

MiarrobaToolBar.prototype.loadscript = function(url){

	var ffss=document.getElementsByTagName('HEAD')[0];
	var loadScript=document.createElement('SCRIPT');
	loadScript.type='text/javascript';
	loadScript.src=url;
	ffss.appendChild(loadScript,ffss.lastChild);

}

MiarrobaToolBar.prototype.windowLayer=new InfoWindow('toolbarMiarroba.windowLayer');

//Blink Objects
function matb_blink(clase,obj,bt,cc,n)
{
	this.Clase=clase;
	this.blinkObj=obj;
	this.bt=bt;
	this.cc=cc;
	this.n=n;
	this.blinkOn();
}

matb_blink.prototype.blinkOn = function()
{
	this.blinkObj.className=this.blinkObj.className.replace(/ ?matb_Transparent/,'');
	if(this.cc>this.n)
	{
		this.n++;
		setTimeout(this.Clase+".blinkOff()",this.bt);
	}
}

matb_blink.prototype.blinkOff = function()
{
	this.blinkObj.className=this.blinkObj.className+' matb_Transparent';
	setTimeout(this.Clase+".blinkOn()",this.bt);
}
