﻿function showModalPopupViaClient(obj, str)
{
    $(obj).parent().find(".spanMoverFinal").insertAfter(".container");
    var modalPopupBehavior = $find(str);
    modalPopupBehavior.show();
}
function hideModalPopupViaClient(str)
{
    var modalPopupBehavior = $find(str);
    modalPopupBehavior.hide(); 
}

$(document).ready(
	function () {
        $('div.cores ul.opcoes li').bind('click', selecionaAtributoCor);
        $('div.descricao div.preco_comprar a').bind('click', validaCompra);
        $('div.detalhe_foto ul li img').bind('click', trocaImagem);
	}
);

	function fEstoque(estoque) {
	    if (estoque > 0) {
	        $("#divCompra").show();
	        $("#divDisp").hide();
	    }
	    else {
	        $("#divCompra").hide();
	        $("#divDisp").show();
	    }

	}

var selecionaAtributoCor = function(e)
{
	var listaCores = $('div.cores ul.opcoes li');	
	
	for(i=0; i<listaCores.length; i++)
	    listaCores[i].className = "";
    this.className = "act";

    var produtoFilho = ($(this).attr('produtofilho_id'));
    var sku = ($(this).attr('sku'));
    var descprod = ($(this).attr('descprod'));
    var estoque = ($(this).attr('temestoque'));

    //fEstoque(estoque);

    document.getElementById($('input.txtProdutoFilho', this.parentNode.parentNode.parentNode.parentNode).attr('id')).value = produtoFilho;
    document.getElementById($('small.smlCodProd', this.parentNode.parentNode.parentNode.parentNode).attr('id')).innerHTML = 'Cód ' + sku;
    document.getElementById($('p.pDesProd', this.parentNode.parentNode.parentNode.parentNode).attr('id')).innerHTML = descprod;

    var lista = $('div.detalhe_foto ul', this.parentNode.parentNode.parentNode.parentNode.parentNode);
	for(i=0; i<lista.length; i++)
	    lista[i].style.display = 'none';
    document.getElementById($('div.detalhe_foto ul.cor_'+produtoFilho, this.parentNode.parentNode.parentNode.parentNode.parentNode).attr('id')).style.display = '';
    
    var imgAmpliada = ($('div.detalhe_foto ul.cor_'+produtoFilho+ ' li img', this.parentNode.parentNode.parentNode.parentNode.parentNode))
    
    document.getElementById($('div.foto_g div.foto_ampl img', this.parentNode.parentNode.parentNode.parentNode.parentNode).attr('id')).src = ($(imgAmpliada[0]).attr('ampliada'));
       
	return false;
};

var validaCompra = function(e)
{
    var corSel = ($('input', this.parentNode).attr('value'));
    
    if(corSel == '')
    {
        alert('Você precisa selecionar uma cor.');
        return false;
    }
    return true;
};

var trocaImagem = function(e)
{
    var imgAmpliada = ($(this).attr('ampliada'));
    
    document.getElementById($('div.foto_g div.foto_ampl img', this.parentNode.parentNode.parentNode.parentNode).attr('id')).src = imgAmpliada;
    
    return false;
};
