//    Copyright (c) Franz J Mayrhofer, Gavilan College.  All rights reserved.

//-----------------------------------------------------------------------------
//    code adapted from http://www.cervantesvirtual.com/include/diccenlinea.js
//
//    provides dynamic access to the "Diccionario de la lengua española" DRAE
//    to use, select a word and press "d" or "D"
//
//    The BVC approach is preferrable to calling DRAE directly, since it will
//    suggest alternatives if the search fails
//
//    Added dynamic access to the "Diccionario Panhispánico de Dudas"
//    to use, select a word and press "u" or "U"
//
//-----------------------------------------------------------------------------


var nav4 = window.Event ? true : false;

if (document.layers)
{ 
  document.captureEvents(Event.KEYPRESS); 
}
document.onkeypress = pulsacion; 

function pulsacion(evnt) 
{
  	var cadena=extraePalabra(textoSeleccionado());
  	if (cadena.length==0) return;
  	if (nav4) 
	{
	  	keycd = evnt.which; 
	}
  	else
  	{  
	  	evnt = window.event; 
      	keycd = evnt.keyCode; 
  	}

  	if (keycd==100 || keycd==68)
  	{ 
		window.open("http://www.cervantesvirtual.com/include/diccenlinea.formato?palabra="+escape(cadena), "", "height=480,width=640,status,resizable");

//		window.open('http://buscon.rae.es/draeI/SrvltGUIBusUsual?TIPO_HTML=2&TIPO_BUS=3&LEMA='+escape(cadena)+' ','DRAE','width=640,height=480,status,resizable,scrollbars');
  	}

  	if (keycd==117 || keycd==85)
  	{ 
		window.open('http://buscon.rae.es/dpdI/SrvltGUIBusDPD?lema='+escape(cadena),'DPDudas','width=640,height=480,status,resizable,scrollbars');
  	}
} 

function extraePalabra(x) 
{
	x = x.toLowerCase();
  	x = x.replace(/[^a-záéíóúàèìòùäëïöüâêîôûçñ]*([a-záéíóúàèìòùäëïöüâêîôûçñ]+).*/, "$1");
  	return x;
}


function textoSeleccionado()
{
	if (nav4)	return document.getSelection();
	else		return document.selection.createRange().text;
}