var imain = 1;

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function getel(doc, id)
{
	if (doc.all)
		return doc.all[id];
	else
		return doc.getElementById(id);
}
function matriz(n)  {
   this.length=n
   for (var i=0;i<n;i=i+1)  {
     this[i] = ''
   }
}

var stl = "_";
var com = "_";
var imagens = new matriz(0);
var hbase = "";
var mostraLogin = 1;
var reloadLogin = 0;
var language = 'pt';

function adjust(novacom, base, valMostraLogin, lang)
{
	// guarda variaveis
	hbase = base;
	mostraLogin = valMostraLogin;
	language = lang;
	
	// oculta imediatamente o login caso solicitado
	if (getel(document, 'iflogin'))
		getel(document, 'iflogin').style.display = (mostraLogin ? 'block' : 'none');
	
	// determina o estilo
	if ((novacom == 'revendedores') || (novacom == 'imprensa') || (novacom == 'saude') || (novacom == 'produtos') || (novacom == ''))
		newstl = 'institucional';
	else
		newstl = novacom;
	
	// se mudou a comunidade
	if (com != novacom)
	{
		com = novacom;
		
		// menu
		m = getel(document, 'ifmenu');
		m.style.height = '0px';
		//window.frames['ifmenu'].window.location.replace('about:blank');
		window.frames['ifmenu'].window.location.replace(hbase + 'controls/menu.aspx?id=' + com);
		
		// imagens
		if ((com == 'produtos') && (language == 'pt'))
			ajdustImg('bg_top_default.jpg', 'tdtop', 1, newstl, document);
		else if (com != '')
			ajdustImg((language == 'en' ? 'en/' : '') + 'bg_top.jpg', 'tdtop', 1, com, document);
		else
			ajdustImg((language == 'en' ? 'en/' : '') + 'bg_top_default.jpg', 'tdtop', 1, newstl, document);
		
		// links
		getel(document, 'lnkContato').href = 'formulario.aspx?idComunidade=' + com;
		getel(document, 'lnkIndique').href = 'indique.aspx?id=' + com;
		getel(document, 'lnkIndique').style.display = (com != '' ? 'inline' : 'none');
		if (language == 'pt')
			document.fbusca.contexto[1].value = com;
	}

	// se mudou o estilo
	if (stl != newstl)
	{
		stl = newstl;
	
		// imagens diversas
		if (language == 'pt')
		{
			ajdustImg('busca.gif', 'busca', 0, stl, document);
			ajdustImg('but_ok.gif', 'but_ok', 0, stl, document);
			ajdustImg('no_site_todo.gif', 'no_site_todo', 0, stl, document);
			ajdustImg('nesta_comunidade.gif', 'nesta_comunidade', 0, stl, document);
			ajdustImg('box_comunidades.gif', 'box_comunidades', 1, stl, document);
		}
		ajdustImg('bg_left.gif', 'bg_left', 1, stl, document);
		ajdustImg((language == 'en' ? 'en/' : '') + 'but_indique.gif', 'but_indique', 0, stl, document);
		ajdustImg('bg_footer.gif', 'bg_footer', 1, stl, document);

		// menu de comunidades
		if (language == 'pt')
		{
		    ids = new Array('menopausa', 'emagrecimento', 'maturidade', 'energia', 'infantil', 'educacao', 'bioativos');
			for (i=0; i<ids.length; i++)
			{
				if (stl != '')
					ajdustImg('but_' + ids[i] + '.gif', 'but_' + ids[i], 0, stl, document);
				getel(document, 'lnk_' + ids[i]).style.display = (stl == ids[i] ? 'none' : 'inline');
			}
		}

		// estilos diversos
		rule('.form', 0).border = '1px solid ' + cor();
		rule('a:hover', 0).color = cor();
		rule('a:active', 0).color = cor();
		getel(document, 'tdtop').style.backgroundRepeat = "no-repeat";
		getel(document, 'tdtop').style.backgroundColor = cor();
	}
}

function adjustLogin(valReloadLogin)
{
	reloadLogin = valReloadLogin;
		
	if (!reloadLogin)
		doAdjustLogin();
	else
	{
		if (!getel(document, 'iflogin'))
			return;
		getel(document, 'iflogin').style.display = 'none';	
		//window.frames['iflogin'].window.location.replace('about:blank');
		window.frames['iflogin'].window.location.replace(hbase + 'controls/login.aspx?rnd=' + (Math.random()));
	}
}

function doAdjustLogin()
{
	if (!getel(document, 'iflogin'))
		return;
	getel(document, 'iflogin').style.display = (mostraLogin ? 'block' : 'none');
 	ajdustImg('but_ok.gif', 'btnOk', 0, stl, window.frames['iflogin'].document);
	rule('.form', 1).border = '1px solid ' + cor();
	rule('a.login:hover', 1).color = cor2();
	if (getel(window.frames['iflogin'].document, 'lnkEsqueci') != null)
		getel(window.frames['iflogin'].document, 'lnkEsqueci').href = '../esqueciSenha.aspx?idComunidade=' + com;
	if (getel(window.frames['iflogin'].document, 'lnkCadastro') != null)
		getel(window.frames['iflogin'].document, 'lnkCadastro').href = '../cadastro.aspx?idComunidade=' + com;
	if (getel(window.frames['iflogin'].document, 'lnkAlterarCadastro') != null)
		getel(window.frames['iflogin'].document, 'lnkAlterarCadastro').href = '../cadastro.aspx?idComunidade=' + com;
	if (getel(window.frames['iflogin'].document, 'lnkAlterarSenha') != null)
		getel(window.frames['iflogin'].document, 'lnkAlterarSenha').href = '../alteraSenha.aspx?idComunidade=' + com;
	if (getel(window.frames['iflogin'].document, 'lnkPedidos') != null)
	{
		getel(window.frames['iflogin'].document, 'lnkPedidos').href = (window.location.protocol == 'http:' ? '../loja/login.aspx' : '../sslout.aspx?loja/login.aspx');
		getel(window.frames['iflogin'].document, 'lnkPedidos').target = (window.location.protocol == 'http:' ? 'ifcontent' : '_top');
	}
}

function ajdustImg(src, eid, bg, sl, doc)
{
	if (getel(doc, eid) == null)
		return;
	
	if (imagens[sl] == null)
		imagens[sl] = new matriz(0);
	
	if (imagens[sl][src] == null)
		imagens[sl][src] = newImage(hbase + sl + '/images/' + src);
	
	if (!bg)
	{
		getel(doc, eid).src = imagens[sl][src].src;
		getel(doc, eid).style.display = 'inline';
	}
	else
		getel(doc, eid).style.background = "url('" + imagens[sl][src].src + "')";
}

function adjustMenuHeight()
{
	getel(document, 'ifmenu').style.height = (window.frames['ifmenu'].document.body.scrollHeight) + 'px';
}

function cor()
{
	if ((stl == "") || (stl == "institucional"))
		return "#26946B";
	else if (stl == "energia")
		return "#B32E31";
	else if (stl == "emagrecimento")
		return "#F8A219";
	else if (stl == "infantil")
		return "#F77676";
	else if (stl == "loja")
		return "#106CC4";
	else if (stl == "maturidade")
		return "#008C84";
	else if (stl == "nutricao")
		return "#FF7200";
    else if (stl == "menopausa")
        return "#727F8C";
    else if (stl == "bioativos")
        return "#163216";
    else
		return "";
}

function cor2()
{
	if ((stl == "") || (stl == "institucional"))
		return "#01562D";
	else if (stl == "energia")
		return "#560002";
	else if (stl == "emagrecimento")
		return "#BE6B00";
	else if (stl == "infantil")
		return "#003174";
	else if (stl == "loja")
		return "#FDA769";
	else if (stl == "maturidade")
		return "#03625D";
	else if (stl == "nutricao")
		return "#B25000";
	else if (stl == "menopausa")
		return "#003174";
    else if (stl == "bioativos")
        return "#161b16";
    else
		return "";
}

function rule(id, login)
{
	if (!login)
		sl = document.styleSheets[0];
	else
		sl = window.frames['iflogin'].document.styleSheets[0];

	r = sl.rules ? sl.rules : sl.cssRules;		
	for (i=0; i<r.length; i++)
		if (r[i].selectorText.toLowerCase() == id.toLowerCase())
			return r[i].style;
	return null;
}

function escreve(s) {
    document.write(s);
}

/* 18/11/2008 */
function escreveFlash(id, src, width, height, align, salign, transparent, mozila, versao, onmouseover, onmouseout, style, scale) {
    s = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + (versao) + ',0,0,0" width="' +
        (width) + '" height="' + (height) + '" id="' +
        id + '" ' + (align != '' ? 'align="' + align + '"' : '') +
        (onmouseover != '' ? ' onmouseover="' + onmouseover + '"' : '') +
        (onmouseout != '' ? ' onmouseout="' + onmouseout + '"' : '') +
        (typeof (style) != "undefined" ? ' style="' + style + '"' : '') +
        '><param name="quality" value="high" />' +
        (src != '' ? '<param name="movie" value="' + src + '" />' : '') +
        (salign != '' ? '<param name="salign" value="' + salign + '" />' : '') +
        (transparent ? '<param name="wmode" value="transparent" />' : '') +
        '<param name="menu" value="false" />' +
        '<param name="scale" value="' + (typeof(scale) != "undefined" ? scale : 'noscale') + '">';
    if (mozila)
        s += '<embed ' +
            (src != '' ? 'src="' + src + '" ' : '') +
            'loop="false" menu="false" quality="high" width="' +
            (width) + '" height="' + (height) + '" name="' + id + '" ' +
            (align != '' ? 'align="' + align + '" ' : '') +
            (salign != '' ? 'salign="' + salign + '" ' : '') +
            (onmouseover != '' ? 'onmouseover="' + onmouseover + '" ' : '') +
            (onmouseout != '' ? 'onmouseout="' + onmouseout + '" ' : '') +
            (transparent ? 'wmode="transparent" ' : '') +
            (typeof (style) != "undefined" ? 'style="' + style + '" ' : '') +
            'scale="' + (typeof (scale) != "undefined" ? scale : 'noscale') + '" ' +
            'allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" swliveconnect="true" />';
    s += '</object>';
    escreve(s);
}
