// JavaScript Document
document.onkeydown= function(e){
	var txt=document.getElementById('txtfind');
	if(txt){		
		if($.browser.msie){		
			if(window.event.keyCode == 13){
				txtfind(txt);
			}			
		}else{
			if(e.which == 13){
				txtfind(txt);	
			}
		}
	}		
}
function wind(id){
	//atributos	
	this.wind=document.createElement('div');
	this.wind.id=id;
	this.wind.className='wind';
	
	var wind_header=document.createElement('div');
	var wind_content=document.createElement('div');
	var wind_footer=document.createElement('div');	
	
	wind_header.id='wind_header';
	wind_content.id='wind_content';
	wind_footer.id='wind_footer';
	
	var wind_header_right=document.createElement('div');
	var wind_header_left=document.createElement('div');
	
	//header =======================================================
	wind_header_right.id='wind_header_right';
	wind_header_left.id='wind_header_left';
	wind_header_right.className='head_right'
	wind_header_left.className='head_left';
	
	wind_header_right.appendChild(document.createElement('span'))
	wind_header_right.childNodes[0].onclick=removeWindow;
	wind_header_right.childNodes[0].appendChild(document.createTextNode('Cerrar'));
	wind_header_right.childNodes[0].className='btncerrar';
	
	wind_header_left.appendChild(document.createElement('span'))
	
	
	wind_header.appendChild(wind_header_right);
	wind_header.appendChild(wind_header_left);
	//header =======================================================	
	
	this.wind.appendChild(wind_header);
	this.wind.appendChild(wind_content);
	this.wind.appendChild(wind_footer);
	
	//metodos
	this.show=showWindow;
	this.remove=removeWindow;
	this.add=addItem;
	this.setInfo=setInfo;
	
}
function showWindow(idobj){
	if(idobj){
		document.getElementById(idobj).appendChild(this.wind);
		document.getElementById('search1').style.left=getDimensions(document.getElementById('txtfind')).x - getDimensions(document.getElementById('search1')).w + getDimensions(document.getElementById('txtfind')).w + 'px';
	}else{
		document.body.appendChild(this.wind);
	}
}
function removeWindow(){
	var t=this.parentNode.parentNode.parentNode;
	t.parentNode.removeChild(t);
//	document.body.removeChild(t);
}
function addItem(title, page, path, content){
	var reg=document.createElement('div');
	var br=document.createElement('br');	
	var pointer=document.createElement('a');
	pointer.href='http://www.plasticacolombia.com/' + path + page;
	pointer.className='linkitem';
	pointer.appendChild(document.createTextNode(title));
	reg.style.paddingTop = '5px';
	reg.paddingBottom = '5px';
	reg.appendChild(pointer);
	reg.appendChild(br);
	reg.appendChild(document.createTextNode(content));	
	//agrega linea espaciadora
	var spacer = document.createElement('div');
	var imgspace = document.createElement('img');
	spacer.style.width = '95%';
	spacer.style.height = '5px';
	spacer.style.marginTop = '3px';
	spacer.style.backgroundImage = 'url(http://www.plasticacolombia.com/img/generales/linespace.gif)';
	spacer.style.backgroundRepeat = 'repeat-x';
	imgspace = document.createElement('img');
	imgspace.src = 'http://www.plasticacolombia.com/img/generales/spacer.gif';
	imgspace.style.height = '5px';
	spacer.appendChild(imgspace);
	reg.appendChild(spacer);
	//this.wind.childNodes[1].appendChild(spacer);
	//agrega el registro a la ventana de resultados
	this.wind.childNodes[1].appendChild(reg);		
}
function setInfo(busqueda, count){
	this.wind.childNodes[0].appendChild(document.createTextNode(count + ' Results for: "' + busqueda + '"'));
}
function txtfind(txtfind){
	if(document.getElementById('search1')){document.getElementById('search1').parentNode.removeChild(document.getElementById('search1'))};
	if(txtfind.value.length < 4 || txtfind.value=='Search'){return false};
	$.post('http://www.plasticacolombia.com/servercode/search_ing.php', {txtfind:jQuery.trim(txtfind.value)}, function(resp){
		if(resp!=''){
			if(resp != 'noresult'){
				var regs=resp.split('|');
				showResults(txtfind.value, regs);
			}else{
				var vent = new wind('search1');
				vent.setInfo(txtfind.value, 0)
				vent.show('dv_search');
			}
		}
	});
}
function showResults(busqueda, regs){
	var vent=new wind('search1');
	var titulo;
	for(var i=0;i<regs.length;i++){
		titulo=(regs[i].split('^^')[0].indexOf('.')==-1)?regs[i].split('^^')[0]:regs[i].split('^^')[0].split('.')[0];
		vent.add(titulo, regs[i].split('^^')[1], regs[i].split('^^')[2], encode_utf8(regs[i].split('^^')[3]));
	}
	vent.setInfo(busqueda, regs.length);
	vent.show('dv_search');
}
function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1)
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}
getDimensions = function(oElement) {
    var x, y, w, h;
    x = y = w = h = 0;
    if (document.getBoxObjectFor) { // Mozilla
      var oBox = document.getBoxObjectFor(oElement);
      x = oBox.x-1;
      w = oBox.width;
      y = oBox.y-1;
      h = oBox.height;
    }
    else if (oElement.getBoundingClientRect) { // IE
      var oRect = oElement.getBoundingClientRect();
      x = oRect.left-2;
      w = oElement.clientWidth;
      y = oRect.top-2;
      h = oElement.clientHeight;
    }
    return {x: x, y: y, w: w, h: h};
  }
  function txtSearch(obj, a){
	  if(a==1){
		  if(obj.value=='Buscar'){
			  obj.value='';
		  }
	  }else if(a==0){
		  if(obj.value==''){
			  obj.value='Buscar';
		  }
	  }
  }
