var bAchou = false;
var ajaxBuscar = {
	recuperarLojas : function (result) {
		bAchou = false;

		for (var i in result) {
			if (result[i] == '______array' || result[i] == "" || typeof result[i] != 'object') continue;
			buscar.addNewLine(result[i]['identificacao'], result[i]['titulo']);

			bAchou = true;
		}

		if (!bAchou) {
			buscar.notFound(buscar.currentSearch);
		}
	}
}

ajaxBuscar = new objAjax.createAjax("Ajax/ajaxBuscar");
ajaxBuscar.setSimple(true);

var buscar = {
	tbl_ID		  : 'tblResults',
	selectedText  : '',
	selectHidden  : 'ds_categoria',
	currentSearch : '',
	showing		  : false,
	_timeout	  : null,

	recuperarLojas : function (id_shopping, ds_categoria, tags) {
		buscar.removeAllLines();

		if (String(ds_categoria) == "undefined")
			ds_categoria = "";

		if (String(tags) == "undefined")
			tags = "";

		buscar.currentSearch = tags;
		
		if (this._timeout != null) clearTimeout(this._timeout);

		this._timeout = setTimeout(function () {
										ajaxBuscar.recuperarLojas( Array(id_shopping, ds_categoria, tags) )
								   }, 500);
	},

	show : function (objLI) {
		this.selectedText = objLI.getAttribute('value');
		if (classObjetos.findObj(this.selectHidden)) {
			classObjetos.findObj(this.selectHidden).value = this.selectedText;
		}

		if (this._timeout != null)
			clearTimeout(this._timeout);
		
		if (!this.showing) {
			this.showing = true;

			this.addTimer(objLI);
			this.showHideAll(objLI);
		} else {
			this.showing = false;
			this.showHideAll(objLI);
		}
	},

	addNewLine : function (identificacao, ds_titulo) {
		var newLine  = this.getNewLine();
		var objLink1  = this.getCellAnchor( this.getLineCell( newLine, 0 ) );
		var objLink2  = this.getCellAnchor( this.getLineCell( newLine, 1 ) );

		if (this.validaConteudo(this.getTableLastTBody().getElementsByTagName('TR'), ds_titulo)) {
			newLine.style.display = 'block';
			objLink1.href		 += identificacao;
			objLink2.href		 += identificacao;
			newLine.onclick       = function () { location = objLink1.href; };
			newLine.style.cursor  = 'Pointer';
			newLine.removeAttribute('remove');
			
			objLink1.innerHTML = ds_titulo;		
			this.getTableLastTBody().appendChild(newLine);
		}
	},

	notFound : function (ds_busca) {
		var newLine  = this.getNewLine();
		var cellOne  = this.getLineCell( newLine, 0 );
		newLine.removeChild( this.getLineCell( newLine, 1 ) );
		newLine.style.display = 'block';
		
		cellOne.style.width = this.getTable().offsetWidth + 'px';
		cellOne.colSpan     = 2;
		cellOne.setAttribute('colSpan', 2);
		
		cellOne.innerHTML = "Nenhum registro encontrado.";
		
		this.getTableLastTBody().appendChild(newLine);
	},

	validaConteudo : function (objTRs, ds_conteudo) {
		var objCells;
		for (var i = 0; i < objTRs.length; i++) {
			objCells = objTRs[i].getElementsByTagName('TD');

			for (var j = 0; j < objCells.length; j++) {
				if (objCells[j].innerHTML == ds_conteudo) 
					return false;
			}
		}

		return true;
	},

	removeAllLines : function () {
		var objTBody = this.getTable().getElementsByTagName("TBODY");
		
		for (var i = 0; i < objTBody.length; i++) {
			if (objTBody[i].getAttribute('remove') == null)
				this.getTable().removeChild( objTBody[i] );
		}
		
		this.getTable().appendChild( document.createElement("TBODY") );
	},

	addTimer : function (objLI) {
		this._timeout = setTimeout( function () {
										buscar.showHideAll(objLI);
									}, 5000);
	},

	buscarDigito : function (valor) {
		if (valor.length > 0 && (valor.length % 3 == 0)) {
			//classObjetos.findObj('btnBusca').onclick();
		}
	},

	showHideAll : function (objLI) {
		var objUL  = objLI.parentNode;
		var objLIs = objUL.getElementsByTagName('LI');

		for (var i = 0; i < objLIs.length; i++) {
			if (objLIs[i].style.display == 'block' && objLIs[i].getAttribute('value') != objLI.getAttribute('value')) {
				objLIs[i].style.display = 'none';
			} else {
				objLIs[i].style.display = 'block';
			}
		}
	},

	getTable : function () { return classObjetos.findObj( this.tbl_ID ); },
	getTableTBody : function () { return this.getTable().getElementsByTagName('TBODY')[0]; },
	getTableLastTBody : function () { return this.getTable().getElementsByTagName('TBODY')[ 1 ]; },
	getNewLine : function () { return this.getTableTBody().getElementsByTagName('TR')[1].cloneNode(true); },
	getLineCell : function (objLine, index) { return objLine.getElementsByTagName('TD')[ index ]; },
	getCellAnchor : function (objCell) { return objCell.getElementsByTagName('A')[0]; }
}