var closeButton ="http:/www.kemenkoz.com/images/close.gif";

//
// Get enter key
// in form
//

function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) 
	{
	    if (!document.getElementsByTagName){ return; }
    	var anchors = document.getElementsByTagName("a");

			// loop through all anchor tags
	    for (var i=0; i<anchors.length; i++)
	    {
	        var anchor = anchors[i];
	
	        if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox"))
	        {
	            anchor.onclick();
	            return false;
	        }
		    }
	} 
}  

//
// Microsoft search
// Encode text in utf8
//
function encode_utf8(texto) {
      texto = texto.replace(/\r\n/g,"\n");
      var utftext = "";
      for(var n=0; n<texto.length; n++)
          {
          var c=texto.charCodeAt(n);
          if (c<128)
              utftext += String.fromCharCode(c);
          else if((c>127) && (c<2048)) {
              utftext += String.fromCharCode((c>>6)|192);
              utftext += String.fromCharCode((c&63)|128);}
          else {
              utftext += String.fromCharCode((c>>12)|224);
              utftext += String.fromCharCode(((c>>6)&63)|128);
              utftext += String.fromCharCode((c&63)|128);}
          }
      	return utftext;
      }

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}


//
// listenKey()
//
function listenKey () 
{	
	document.onkeypress = getKey; 
}
	

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	//alert(document.forms[0].buscador.value);
	
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objIframe = document.getElementById('iframeGoogle');

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	var texto = encode_utf8(document.getElementById("buscador").value);
	if(texto=="") texto = "kirol"
	var sUrl = "http://www.google.com/custom?q=" + texto + "&sa=Búsqueda&client=pub-4977234912886524&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&cof=GALT:#008000;GL:1;DIV:#F2984C;VLC:663399;AH:center;BGC:B3B3B3;LBGC:FFFFFF;ALC:0000FF;LC:0000FF;T:FFFFFF;GFNT:0000FF;GIMP:0000FF;LH:50;LW:110;L:http://www.kemenkoz.com/fijos/imagen/kemen.jpg;S:http://;FORID:1&hl=es&"
	
	objIframe.setAttribute('src',sUrl);

	objLightbox.style.top = 80;
	objLightbox.style.left = 150;
	
	objLightbox.style.display = 'block';
}

//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
	
	objLightbox.setAttribute('src','cargando.htm');
}


//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}

	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onmouseover = function () {toolTip('Volver a kemenkoz',this); return false;}
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();


	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objLightbox.style.width = '75%';
	objLightbox.style.height = '70%';
	
	
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsg');
	//objKeyboardMsg.style.position = 'absolute';
	objKeyboardMsg.innerHTML = '<a href="#" onclick="hideLightbox(); return false;"><img src="/images/close.gif" alt="Volver a kemenkoz"></a>&nbsp;';
	//objLightbox.appendChild(objKeyboardMsg);

	var objIframe = document.createElement("iframe");
	objIframe.setAttribute('id','iframeGoogle');
	objIframe.setAttribute('src','cargando.htm');
	//objIframe.style.position = 'absolute';
	objIframe.setAttribute('frameborder','0');
	objIframe.setAttribute('border','0');
	objIframe.style.width = '98%';
	objIframe.style.height = '98%';
  //objLightbox.appendChild(objIframe);

	myTable = document.createElement("table");
	myTable.setAttribute('border','0');
	myTable.setAttribute('width','100%');
	myTable.setAttribute('height','100%');
	myTable.id = "TableOne";
	myTableBody = document.createElement("TBODY");
  
  row1 = document.createElement("tr");
  cell1 = document.createElement("td");
  cell1.setAttribute('align','right');
  cell1.setAttribute('height','10');
  cell1.appendChild(objKeyboardMsg);
  row1.appendChild(cell1);
  myTableBody.appendChild(row1);

  row2 = document.createElement("tr");
  cell2 = document.createElement("td");
  cell2.appendChild(objIframe);
  row2.appendChild(cell2);
  myTableBody.appendChild(row2);

	myTable.appendChild(myTableBody);
	objLightbox.appendChild(myTable);
	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);

}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

addLoadEvent(initLightbox);	// run initLightbox onLoad
//init();

