download = {
	categoria : '',
	modelo : '',
	dsBusca	 : null,
	numPGs : 0,
	itensPorPG : 10,
	pgAtual : 1,
	
	
	carregaCategorias : function(){

		response = downloads.carregaCategoria();
		if(response.error == null){
			var retorno = response.value;
			var ds = retorno;
			
			if(ds!=null && typeof(ds) == "object" && ds.Tables!=null){
				
				//define a quantidade de itens do dropdown(1 a mais da quantidade de itens do dataset)
				document.getElementById('cmbModelo').selectedIndex = 0;
				document.getElementById('cmbCategoria').selectedIndex = 0;
				document.getElementById('cmbModelo').length = 1;
				document.getElementById('cmbCategoria').length = ds.Tables[0].Rows.length+1;
				
				//adiciona um item no dropdown
				document.getElementById('cmbCategoria').options[0].text= 'Categoria';
				document.getElementById('cmbCategoria').options[0].value= '';
				
				//adiciona os itens do dataset no dropdown
				for(var i=0; i<ds.Tables[0].Rows.length; i++){
					var row = ds.Tables[0].Rows[i];
					document.getElementById('cmbCategoria').options[i+1].text= row.titulo;
					document.getElementById('cmbCategoria').options[i+1].value= row.idCategoria;
				}
			} 
		}
	},
	
	
	carregaModelo : function(categoria){
		if (categoria != ''){
			document.getElementById('cmbModelo').disabled = '';
			
			this.categoria = categoria;
			response = downloads.carregaModelo(categoria);
			if(response.error == null){
				var retorno = response.value;
				var ds = retorno;
				if(ds!=null && typeof(ds) == "object" && ds.Tables!=null){
					//define a quantidade de itens do dropdown(1 a mais da quantidade de itens do dataset)
					document.getElementById('cmbModelo').length = ds.Tables[0].Rows.length+1;

					//adiciona um item no dropdown
					document.getElementById('cmbModelo').options[0].text= 'Modelo';
					document.getElementById('cmbModelo').options[0].value= '';
					
					//adiciona os itens do dataset no dropdown
					for(var i=0; i<ds.Tables[0].Rows.length; i++){
						var row = ds.Tables[0].Rows[i];
						document.getElementById('cmbModelo').options[i+1].text= row.titulo;
						document.getElementById('cmbModelo').options[i+1].value= row.idProduto;
					}
				} 
			}
		}else{
			document.getElementById('cmbModelo').length = 1;
		}
	},
	
	
	
	busca : function(){
		idProduto = document.getElementById('cmbModelo').value;
		this.idProduto = idProduto;
		
		if (idProduto != ''){
			downloads.busca(idProduto, download.retornoResultados);
		}
	},
	
	
	
	retornoResultados : function(retorno){
		download.dsBusca = retorno.value;
		if(download.dsBusca!=null && typeof(download.dsBusca) == "object" && download.dsBusca.Tables!=null){
			if(download.dsBusca.Tables[0].Rows.length==0){
				document.getElementById('divResultado').innerHTML = 'Sua busca n&atilde;o retornou nenhum resultado';
			}else{
				download.carregaPG(1);			
			}
		}else{
			document.getElementById('divResultado').innerHTML = retorno.value;
		}
		
	},
	

	carregaPG : function(pg){
		document.getElementById('divResultado').innerHTML = '';
		
		var conteudo='';
		var template = document.getElementById('divTemplate').innerHTML;
		
		download.pgAtual = pg;
		
		i = parseInt((pg * download.itensPorPG) - download.itensPorPG);
        aux_i = i + parseInt(download.itensPorPG);

		for(; i<download.dsBusca.Tables[0].Rows.length && i<aux_i; i++){
			var row = download.dsBusca.Tables[0].Rows[i];
			
			conteudo += template;
			conteudo = conteudo.replace('_PRODUTO_', row.nomeProduto);
			conteudo = conteudo.replace('_TITULO_', row.titulo);
			conteudo = conteudo.replace('_ARQUIVO_', row.arquivo);
			conteudo = conteudo.replace('_ARQUIVOICO_', row.arquivo);
			conteudo = conteudo.replace('_TAMANHO_', downloads.retornaTamanho(row.arquivo).value);
			
		}
		document.getElementById('divResultado').innerHTML = conteudo;
	}
	
	
}