function Produto()
{
	this.posicao_atual = 0;
	this.preloaded = 0;
	
	this.id_popup = null;
	this.popup_indique = null;
	this.popup_produto = null;

	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		this.flagOcultaSelectBusca = 1;
	}
	else
	{
		this.flagOcultaSelectBusca = 0;
	}
}
/**
 * imagemPreload
 * 
 * realiza o preload das imagens do produto
 * relacionado ao template (produto-miolo.tpl)
 */
Produto.prototype.imagemPreload = function()
{
	var img_loaded = new Array();
	for(var x=0;x<array_imagens_box.length;x++)
	{
		img_loaded[x] = new Image();
		img_loaded[x].src = array_imagens_box[x];
	}
}

Produto.prototype.adicionar_dois = function(idProdutoRecomendado)
{
	document.getElementById('produto_recomendado').value = idProdutoRecomendado;
	document.form_produto.submit();
}

/**
 * trocaImagem
 * 
 * realiza a troca das imagens do produto
 * relacionado ao template (produto-miolo.tpl)
 */
Produto.prototype.trocaImagem = function(direcao)
{
	if(!this.preloaded) this.imagemPreload();
	
	var seta_dir_on = urlimg + 'fotos-dir.gif';
	var seta_dir_off = urlimg + 'fotos-dir-off.gif';
	var seta_esq_on = urlimg + 'fotos-esq.gif';
	var seta_esq_off = urlimg + 'fotos-esq-off.gif';
	
	if(direcao == '+')
	{
		this.posicao_atual++;
		if(this.posicao_atual < array_imagens_box.length)
		{
			document.getElementById('seta_esq_produto_foto').src = seta_esq_on;
			document.getElementById('seta_dir_produto_foto').src = seta_dir_on;
			document.getElementById('produto_foto').src = array_imagens_box[this.posicao_atual];
		}
		else this.posicao_atual--;
	}
	else if(direcao == '-')
	{
		this.posicao_atual--;
		if(this.posicao_atual > -1)
		{
			document.getElementById('seta_esq_produto_foto').src = seta_esq_on;
			document.getElementById('seta_dir_produto_foto').src = seta_dir_on;
			document.getElementById('produto_foto').src = array_imagens_box[this.posicao_atual];
		}
		else this.posicao_atual++;
	}
	
	if(this.posicao_atual == array_imagens_box.length - 1)
		document.getElementById('seta_dir_produto_foto').src = seta_dir_off;
	else if(this.posicao_atual == 0)
		document.getElementById('seta_esq_produto_foto').src = seta_esq_off;
}

Produto.prototype.popup_cd = function(url_cd)
{
	sizey = '185';
	sizex = '260';
	dateobject = new Date();
	jan = window.open(url_cd,"jan"+dateobject.getTime(),'width='+sizex+',height='+sizey+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=no,top=150,left=150');
}

Produto.prototype.popup_impressao = function(url_impressao)
{
	sizey = '600';
	sizex = '800';
	jan = window.open(url_impressao,"janImpressao",'width='+sizex+',height='+sizey+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=yes,top=150,left=150');
}

Produto.prototype.imprime_popup = function()
{	
	window.print();
}

Produto.prototype.popup_agendamento = function(url_destino)
{
	sizey = '600';
	sizex = '700';
	jan = window.open(url_destino,"janAgendamento",'width='+sizex+',height='+sizey+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=yes,top=150,left=150');
}

Produto.prototype.fecha_popup = function()
{	
	window.close();
}

Produto.prototype.salva_popup = function()
{	
	alert('Falta propriedade');
}

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){
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }    
    
    
    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;
    }
    return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
}

/**
 * Layer flutuante geral
 */
Produto.prototype.appear = function()
{
	var objBuscaTopo = document.getElementById('field_busca_topo');
	if (this.flagOcultaSelectBusca && objBuscaTopo)
	{
		objBuscaTopo.style.display = 'none';
	}

	if(!this.popup_indique)
	{
		this.popup_indique = document.getElementById('layerGeral_popup').innerHTML;
	}	
	else
	{
		document.getElementById('layerGeral_popup').innerHTML = this.popup_indique;
	}	
	
	document.getElementById('layerGeral_popup').style.display = '';
	document.getElementById('layerGeral_popup').style.height = document.body.offsetHeight + 'px';
	document.getElementById('layerGeral_popup').style.width = document.body.offsetWidth + 'px';
	
	var pagina = getPageSize();
	
	document.getElementById('layerOpaco_popup').style.display = '';
	document.getElementById('layerOpaco_popup').style.height = pagina[1] + 'px';
	document.getElementById('layerOpaco_popup').style.width = pagina[0] + 'px';
}

/**
 * Layer flutuante opaco
 */
Produto.prototype.disappear = function()
{
	var objBuscaTopo = document.getElementById('field_busca_topo');
	if (this.flagOcultaSelectBusca && objBuscaTopo)
	{
		objBuscaTopo.style.display = '';
	}

	document.getElementById('layerOpaco_popup').style.display = 'none';
	document.getElementById('layerGeral_popup').style.display = 'none';
}


/**
 * Layer flutuante geral
 */
Produto.prototype.popup = function(id_popup)
{
	window.scrollTo(0,0);
	if(id_popup)
	{
		if(this.id_popup)
		{
			document.getElementById(this.id_popup).style.display = 'none';
		}
		document.getElementById(id_popup).style.display = '';
		this.id_popup = id_popup;
	}
	document.getElementById('layerGeral_alert').style.display = '';
	document.getElementById('layerGeral_alert').style.height = document.body.offsetHeight + 'px';
	document.getElementById('layerGeral_alert').style.width = document.body.offsetWidth + 'px';
	
	var pagina = getPageSize();
	
	document.getElementById('layerOpaco_alert').style.display = '';
	document.getElementById('layerOpaco_alert').style.height = pagina[1] + 'px';
	document.getElementById('layerOpaco_alert').style.width = pagina[0] + 'px';
	
}

/**
 * Layer flutuante geral (confirma a exclusao do produto da cesta)
 */
Produto.prototype.popup_excluirProdutoCesta = function(id_popup,urlExcluir)
{
	window.scrollTo(0,0);
	if(id_popup)
	{
		if(this.id_popup)
		{
			document.getElementById(this.id_popup).style.display = 'none';
		}
		document.getElementById(id_popup).style.display = '';
		this.id_popup = id_popup;
	}
	document.getElementById('layerGeral_alert').style.display = '';
	document.getElementById('layerGeral_alert').style.height = document.body.offsetHeight + 'px';
	document.getElementById('layerGeral_alert').style.width = document.body.offsetWidth + 'px';
	
	var pagina = getPageSize();
	
	document.getElementById('layerOpaco_alert').style.display = '';
	document.getElementById('layerOpaco_alert').style.height = pagina[1] + 'px';
	document.getElementById('layerOpaco_alert').style.width = pagina[0] + 'px';
	
	document.getElementById('idLinkDestino').onclick = function (){LinkAjax.send(urlExcluir, 'mioloCesta', 'carregando'); document.getElementById('layerOpaco_alert').style.display = 'none'; document.getElementById('layerGeral_alert').style.display = 'none'; } //""
}

/**
 * Layer flutuante opaco
 */
Produto.prototype.popup_close = function()
{
	document.getElementById('layerOpaco_alert').style.display = 'none';
	document.getElementById('layerGeral_alert').style.display = 'none';
}

/**
 * Layer flutuante geral
 */
Produto.prototype.popup_excluirprodutocesta = function(idProduto)
{
	window.scrollTo(0,0);
	if(idProduto)
	{
		if(this.idProduto)
		{
			document.getElementById(this.idProduto).style.display = 'none';
		}
		document.getElementById(idProduto).style.display = '';
		this.idProduto = idProduto;
	}
	document.getElementById('layerGeral_alert').style.display = '';
	document.getElementById('layerGeral_alert').style.height = document.body.offsetHeight + 'px';
	document.getElementById('layerGeral_alert').style.width = document.body.offsetWidth + 'px';
	
	document.getElementById('layerOpaco_alert').style.display = '';
	document.getElementById('layerOpaco_alert').style.height = document.body.offsetHeight + 'px';
	document.getElementById('layerOpaco_alert').style.width = document.body.offsetWidth + 'px';
}

/**
 * Cria os campos de forma dināmica
 */
Produto.prototype.adicionarCampoEmail = function(qtde)
{
	var varHtml = '';
	for(var x=0;x<qtde;x++)
	{
		varHtml+= '<label>E-mail '+(x+1)+' ';
		if(x == 0)
		{
			varHtml+= '*';
		}
		
		valor = '';
		if(document.getElementById('campoEmail'+(x+1)))
		valor = document.getElementById('campoEmail'+(x+1)).value;
		if(!valor)
		{
			valor = 'login@argo.com.br';
		}
		
		varHtml+= ':</label> <input id="campoEmail'+(x+1)+'" name="emailDestinatario[]" value="'+valor+'" type="text" class="inputtext" onfocus="Diversos.campoExemplo(this,\'login@argo.com.br\',event)" onblur="Diversos.campoExemplo(this,\'login@argo.com.br\',event)" />';
		
		if(x < (qtde-1))
		{
			varHtml+= '<br /><br />';
		}
	}
	document.getElementById('idCampoEmail').innerHTML = varHtml;
}

var Produto = new Produto();

