// JavaScript Document
function rand (min, max) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman
    // %          note 1: See the commented out code below for a version which will work with our experimental (though probably unnecessary) srand() function)
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
    
    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;
    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
    
    /*
    // See note above for an explanation of the following alternative code
    
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: srand
    // %          note 1: This is a very possibly imperfect adaptation from the PHP source code
    var rand_seed, ctx, PHP_RAND_MAX=2147483647; // 0x7fffffff
 
    if (!this.php_js || this.php_js.rand_seed === undefined) {
        this.srand();
    }
    rand_seed = this.php_js.rand_seed;
 
    var argc = arguments.length;
    if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
 
    var do_rand = function (ctx) {
        return ((ctx * 1103515245 + 12345) % (PHP_RAND_MAX + 1));
    };
 
    var php_rand = function (ctxArg) { // php_rand_r
        this.php_js.rand_seed = do_rand(ctxArg);
        return parseInt(this.php_js.rand_seed, 10);
    };
 
    var number = php_rand(rand_seed);
 
    if (argc === 2) {
        number = min + parseInt(parseFloat(parseFloat(max) - min + 1.0) * (number/(PHP_RAND_MAX + 1.0)), 10);
    }
    return number;
    */
}
var randPublicidade = rand(1, 5);
var showPublicidade =true;


function loadThumb($tipo,$idFoto){
	var totalPrev =$('#produto-'+$tipo+'-'+$idFoto).prevAll().size();
	var totalNext = $('#produto-'+$tipo+'-'+$idFoto).nextAll().size();
	var posicao = $('ul#thumb-lista li').index($('#produto-'+$tipo+'-'+$idFoto));
	var leftItem = 0-(645*posicao);
	var proximoCodigo = $('#produto-'+$tipo+'-'+$idFoto).next().attr('codigo');
	var proximoTipo = $('#produto-'+$tipo+'-'+$idFoto).next().attr('tipo');
	var totalItens = $("ul#thumb-lista li").size();
	var imagemPreview= $('#produto-'+$tipo+'-'+$idFoto).attr('imagemPreview');
	var produtoPreview= $('#produto-'+$tipo+'-'+$idFoto).attr('produtoPreview');
	
	var proximoImagemPreview= $('#produto-'+proximoTipo+'-'+proximoCodigo).attr('imagemPreview');
	var proximoProdutoPreview= $('#produto-'+proximoTipo+'-'+proximoCodigo).attr('produtoPreview');
	
	$("ul#thumb-lista").animate({ left: leftItem+"px" },{ queue:false, duration:"slow" });
	$('#produto-'+$tipo+'-'+$idFoto).next().children('div').show();
	$('#produto-'+$tipo+'-'+$idFoto).next().nextAll().children('div').hide();
	$('#produto-'+$tipo+'-'+$idFoto).prev().prevAll().children('div').hide();
	$('#produto-'+$tipo+'-'+$idFoto).prev().children('div').show();
	$('#produto-'+$tipo+'-'+$idFoto).children('div').show();
	
	if ($('#produto-foto-'+$tipo+'-'+$idFoto).html() == '') {
		$('#produto-foto-'+$tipo+'-'+$idFoto).html('<img src="thumb/view.php?pasta=loja_produtos&subpasta='+produtoPreview+'&w=414&h=552&imagem='+imagemPreview+'" />');
	}
	if ($('#produto-foto-'+proximoTipo+'-'+proximoCodigo).html() == '') {
		$('#produto-foto-'+proximoTipo+'-'+proximoCodigo).html('<img src="thumb/view.php?pasta=loja_produtos&subpasta='+proximoProdutoPreview+'&w=414&h=552&imagem='+proximoImagemPreview+'" />');
	}
	/*if ($('#produto-'+$idFoto).prev().attr('alt'))
		$('#navPrev').html("<a onclick=\"loadThumb('"+$('#produto-'+$idFoto).prev().attr('alt')+"');\">anterior</a>");
		else $('#navPrev').html("");
	if ($('#produto-'+$idFoto).next().attr('alt'))
		$('#navNext').html("<a onclick=\"loadThumb('"+$('#produto-'+$idFoto).next().attr('alt')+"');\">proxima</a>");
	else $('#navNext').html("<a class=\"navFist\" onclick=\"loadThumb('"+$('ul#thumb-lista li:first').attr('alt')+"');\">proxima</a>");*/
	
	// Miniaturas
	//$("#produto-miniaturas-"+$idFoto+" div").removeClass("selecionado");
	//$("#miniatura-"+imagem.replace(".", "")).addClass("selecionado");
	
	// Thumbs
	$('.thumb-lista img').removeClass("selecionado");
	$('#thumb-foto-'+$tipo+'-'+$idFoto).addClass("selecionado");
	
	var totalThumbs = 16;
	var multi = 0;
	var pagina = 0;
	while ((posicao+1-multi) > totalThumbs) {
		multi+=totalThumbs;
		pagina++;
	}
	thumbNav (pagina);
};
function loadFoto($idProduto,$tipo,$codigo,$mask,$imagem){
	$('#produto-foto-'+$tipo+'-'+$codigo).html('<img src="thumb/view.php?pasta=loja_produtos&subpasta='+$idProduto+'&w=414&h=552&mask='+$mask+'&imagem='+$imagem+'" />');
	// Miniaturas
	$('#produto-miniaturas-'+$tipo+'-'+$codigo).children().removeClass("selecionado");
	$("#miniatura-"+$imagem.replace(".", "")).addClass("selecionado");
};
function thumbNav ($pag) {
	// Thumbs
	var totalItens = $("ul#thumb-lista li").size();
	var totalThumbs = 16;
	var totalPag = Math.ceil(totalItens/totalThumbs);
	var leftItem = -315*$pag;
	$("#thumbsProdutos").animate({ left: leftItem+"px" },{ queue:false, duration:"fast" });
	if ($pag > 0) $('#thumbNavPrev').html("<a onclick=\"thumbNav('"+(parseInt($pag)-1)+"');\">anterior</a>");
		else $('#thumbNavPrev').html("");
	if ($pag < totalPag-1) $('#thumbNavNext').html("<a onclick=\"thumbNav('"+(parseInt($pag)+1)+"');\">proxima</a>");
		else $('#thumbNavNext').html("");
}
jQuery(function($){
	$(".radioCores:radio").click(function () { 
		var cor = $(this).val(); 
		var codigo = $(this).attr("chave");
		var nomeCor = $(this).attr("nomeCor");
		var tipo = $(this).attr("tipo");
		$('#btnCores-'+codigo+' label').removeClass("ativo");
		$('#label-'+codigo+'-'+cor).addClass("ativo");
		//$("#titulo-tamanhos-"+codigo).html('Tamanhos para cor '+nomeCor);
		$("#titulo-tamanhos-"+codigo).html('carregando tamanhos...');
		$("#produto-foto-"+codigo+" img").fadeOut('fast');
		$("#tamanhos-"+codigo).html('');
		$("#btnComprar-"+codigo).html('');
		xajax_reloadProduto(tipo,codigo,cor);
	});
	/*$(".radioTamanhos:radio").click(function () { 
		var tamanho = $(this).val(); 
		var codigo = $(this).attr("chave");
		$("#tamanhos-"+codigo+" label").removeClass("ativo");
		$("#label-"+codigo+"-"+tamanho).addClass("ativo");
	});*/
});

