$(function(){
	
	

	// Lightbox
	$("a[rel='lightbox']").lightBox({
		imageLoading: 'images/lightbox-ico-loading.gif',
		imageBtnClose: 'images/lightbox-btn-close.gif',
		imageBtnPrev: 'images/lightbox-btn-prev.gif',
		imageBtnNext: 'images/lightbox-btn-next.gif',
		txtImage: 'Imagem',
		txtOf: 'de'
	});
	
	
	// Máscaras
	$("input[name='cpf']").mask("999.999.999-99");
	$("input[name='cnpj']").mask("99.999.999/9999-99");
	$("input[name='cep']").mask("99999-999");
	$("input[name='telefone']").mask("(99) 9999-9999");
	
	
	// Desativa clique direito
	$("a[rel='produto']").noContext();
	
	
	
	// Álbum
	$(".album.carousel > ul").jcarousel({
		scroll: 4
    });
	$(".album2.carousel > ul").jcarousel({
		scroll: 3
    });
	
	
	

	
	// Chat
	$(".open_window_chat").click(function(){
		window.open('http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=5c4acdf3d831e987@apps.messenger.live.com&mkt=pt-BR','chat','width=530,height=400,left=0,top=0,scrollbars=no');
	});
	



	// Cadastro
	$(".cadastro input[name='pessoa']").change(function(){
		if ($(this).val() == 'PF') {
			$(".cadastro input[name='cpf']").closest("tr").show();
			$(".cadastro input[name='cnpj'], .cadastro input[name='empresa']").closest("tr").hide();
		} else {
			$(".cadastro input[name='cpf']").closest("tr").hide();
			$(".cadastro input[name='cnpj'], .cadastro input[name='empresa']").closest("tr").show();
		}
	});

	
	
	
	// Produto
	$("a[rel='produto']").click(function() {
		$.fancybox({
			'padding'		: 0,
			'href'			: this.href,
			'type'			: 'ajax',
			'onComplete'    : function() {
				$("a[rel='lightbox']").lightBox({
					imageLoading: 'images/lightbox-ico-loading.gif',
					imageBtnClose: 'images/lightbox-btn-close.gif',
					imageBtnPrev: 'images/lightbox-btn-prev.gif',
					imageBtnNext: 'images/lightbox-btn-next.gif',
					txtImage: 'Imagem',
					txtOf: 'de'
				});
				$(".album2.carousel > ul").jcarousel({
					scroll: 3
			    });
			}
		});
	
		return false;
	});
	
	
	
	// Adiciona ao carrinho
	$("form[name='addcarrinho']").live('submit', function(){
		var $id = $(this).find("input[name='id']").val();
		var $qtd = $(this).find("input[name='qtd']").val();

		$.ajax({
			url: '?secao=addcarrinho',
			dataType: 'text',
			type: 'POST',
			cache: false,
			data: {'id': $id, 'qtd': $qtd},
			beforeSend: function() {
				$(".produtoview_adicionando").show();
			},
			success: function($text) {
				$(".produtoview_adicionando").hide();
				if ($text == 'ok') {
					window.location.href = '?secao=carrinho';
				} else {
					alert($text);
				}
			}
		});
		return false;
	});
	
	
	
	// Youtube
	$("a[rel='youtube']").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'	    	: 640,
				'height'		: 390,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
	
	
	
	// Fade in images so there isn't a color "pop" document load and then on window load
	/*if ($.browser.mozilla && !!document.createElement('canvas').getContext) {
		$(".ultimas-fotos li img").animate({opacity:1},500);
		
		// clone image
		$('.ultimas-fotos li img').each(function(){
			var el = $(this);
			el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
				var el = $(this);
				el.parent().css({"width":this.width,"height":this.height});
				el.dequeue();
			});
			this.src = grayscale(this.src);
		});
		
		// Fade image 
		$('.ultimas-fotos li img').mouseover(function(){
			$(this).parent().find('img:first').stop().animate({opacity:1}, 200);
		})
		$('.img_grayscale').mouseout(function(){
			$(this).stop().animate({opacity:0}, 1000);
		});
	}*/
	
	
	
	var $destaque_current;
	destaque(0);
	setTimeout(destaque_next, 10000);
	
	
	var $promocao_current;
	promocao(0);
	setTimeout(promocao_next, 6000);

	
	
});




function destaque($k) {
	$(".destaque_thumb a:eq("+$k+")").addClass("selected").parent().siblings().find("a").removeClass("selected");
	$(".destaque_foto").hide();
	$(".destaque_foto:eq("+$k+")").fadeIn('slow');
	$destaque_current = $k;
}

function destaque_next() {
	var $destaque_count = $(".destaque_thumbs a").length;
	if (($destaque_current+1) == $destaque_count) {
		destaque(0);
	} else {
		destaque($destaque_current+1);
	}
	setTimeout(destaque_next, 10000);
}



function promocao($k) {
	$(".promocao_banner").hide();
	$(".promocao_banner:eq("+$k+")").show();
	$promocao_current = $k;
}

function promocao_next() {
	var $promocao_count = $(".promocao_banner").length;
	if (($promocao_current+1) == $promocao_count) {
		promocao(0);
	} else {
		promocao($promocao_current+1);
	}
	setTimeout(promocao_next, 6000);
}



// Grayscale w canvas method
function grayscale(src){
	var canvas = document.createElement('canvas');
	var ctx = canvas.getContext('2d');
	var imgObj = new Image();
	imgObj.src = src;
	canvas.width = imgObj.width;
	canvas.height = imgObj.height; 
	ctx.drawImage(imgObj, 0, 0); 
	var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
	for(var y = 0; y < imgPixels.height; y++){
		for(var x = 0; x < imgPixels.width; x++){
			var i = (y * 4) * imgPixels.width + x * 4;
			var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
			imgPixels.data[i] = avg; 
			imgPixels.data[i + 1] = avg; 
			imgPixels.data[i + 2] = avg;
		}
	}
	ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}


