var cookies = document.cookie;
var userName = getCookie("mmmUserName");
var welcomeMsg = "Nenhum usu&aacute;rio logado.";
if(userName != null && userName != 'null' && userName != ''){
	welcomeMsg = "Ol&aacute;, "+userName+"!";
	if($('botaoAcesso')) {
		$('botaoAcesso').innerHTML = '<a class="sair" href="'+ctx+'/sair.html?logout=true">(Sair)</a>';
	}
}
if($('username')){
	$('username').innerHTML = welcomeMsg;
}
Event.observe(window, 'load', verificarCarrinho);
Event.observe(window, 'load', abrirRecomendacaoAmigo);

if(getCookie("mmmCanal") == 30) {
	Event.observe($('menu'), 'click', mostrarThrobber);
	
	var produtosBox = new Array();
	produtosBox = getElementsByClass('produto-box');
	
	for(i = 0; i < produtosBox.length; i++)
		Event.observe(produtosBox[i], 'click', mostrarThrobber);
	
	Event.observe(window,'load',desativarAtendimento);
}

function mostrarThrobber() {
	displayPopIn('carregando', 'block');
	window.setTimeout('sumirThrobber()', 15000);
}
function sumirThrobber() {
	displayPopIn('carregando', 'none');
}

function verificarCarrinho() {
	if($('carrinho-container') != null) {
		var carrinho = getCookie("mmmCarrinho");
		var pedido = getCookie("mmmPedido");
	
		if(carrinho != null && carrinho == "true") { 
			carregaCarrinho();
		}
		
		if(carrinho == null){
			if(pedido == null) {
				carregaCarrinho();
			} else { 
				recuperaCarrinho();
			}
		}
	}
}

function getCookie(name) {
	var start = cookies.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != cookies.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = cookies.indexOf(";",len);
	if (end == -1) end = cookies.length;
	
	return cookies.substring(len,end);	
}
function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	return true;
}
function eraseCookie(name) {
	return setCookie(name,"",-1);
}

function abrirRecomendacaoAmigo(){
	var rec = getCookie("mmmRecomendacao");
	var recSuc = getCookie("mmmRecomendacaoSucesso");
	if($('recAmigo')){
		if(recSuc != null){
			if(removeAllRecomendacaoCookies())
				new Ajax.Updater('recAmigo', 'recomendacaoAmigo_Loja.html', {evalScripts: true, parameters: { acao: 'sucesso', numEnvios:getCookie("mmmRecomendacaoNumEnvios")} });
		}
		else if(rec != null){
			if(removeAllRecomendacaoCookies())
				new Ajax.Updater('recAmigo', 'recomendacaoAmigo_Loja.html', {evalScripts : true});
		}
	}
}
function removeAllRecomendacaoCookies(){
	if(eraseCookie("mmmRecomendacao"))
		return true;
}

function getElementsByClass(searchClass) {
	var classElements = new Array();
	
	var els = document.getElementsByTagName('*');
	
	for (i = 0, j = 0; i < els.length; i++) {
		if (searchClass == els[i].className) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


