function gE(tI, tDoc) { var d=tDoc || document, r=[], n, i;
  if (tI instanceof Array) { for (n=tI.length, i=0; i<n; i++) { r[r.length]=gE(tI[i]); } return r; }
  else { return (d.getElementById && d.getElementById(tI)) || (d.all && d.all[tI]); }
}
function makeArray(collection, forceNumeric) { var i=0, v, r=[];
  for (v in collection) { if (forceNumeric && (v=='length' || v=='item' || v=='namedItem')) { continue; }
    r[((forceNumeric)?i++:v)]=collection[v];
  } return r;
}
var createNamedElement=function (){}; //Cria elemento com "name" -- cross-browser
(function () {
  try {
    var nE=document.createElement('<div name="foo">'); //Jeito esquisitão do IE criar objetos com "name"
    if (nE.tagName != 'DIV' || nE.name != 'foo') { /* força o "catch" */ throw 'create element error'; }
    //Se passou do passo acima, estamos em um navegador que usa o "jeito IE", então criamos a função de acordo
    createNamedElement = function (tT, tN) {
      return document.createElement('<'+ tT +' name="'+ tN +'"></'+ tT +'>');
    };
  } catch (e) { //Forma bonita e funcional, como deveria sempre ser
    createNamedElement = function (tT, tN) { var newEl;
      return ((newEl=document.createElement(tT)) && (newEl.name=tN) && newEl);
    };
  }
})();
function cTN(tV) { return (tV && document.createTextNode && document.createTextNode(tV)); }
function cE(tN, tPs, tC) { // newCreateElement -- <str tagName> [, obj tProperties] [, str/arr conteúdo]
  if (!tN || !document.createElement) { return false; } var nE, tER, i, n, tP;
  if (arguments.length == 1) { return document.createElement(tN); } //criação simples
  if (arguments.length == 2) { tC=tPs; tPs={}; } //Parãmetros 1 e 3.
  nE=(tPs.name) ? createNamedElement(tN, tPs.name) : document.createElement(tN);
  for (i in tPs) { //Há um tratamento especial para o "class" e "className".
    if (i == 'class' || i == 'className') { nE.className=tPs[i]; }
    else { nE.setAttribute(i, tPs[i]); } //Os outros usam o setAttribute padrão
  }
  if (tC) { if (!(tC instanceof Array)) { /* DRY! */ tC=[tC]; }
    for (i=0, n=tC.length; i<n; i++) {
      nE.appendChild( (typeof(tC[i])=='string') ? cTN(tC[i]) : tC[i] );
    }
  }
  return nE;
}
function rEs(tO) { var i, n; if (!tO) { return false; } //RemoveElements
  if (!tO.nodeName) { if (!tO.length) { return false; }
    for (n=tO.length; n--;) { if (!rEs(tO[n])) { return false; } } return true;
  } else { tO.parentNode.removeChild(tO); return true; }
}
function getEv(e) { return e || (window.event ? window.event : false); }
function getSrc(e) { var ev=getEv(e); if (!ev) { return false; }
  return ev.target || ev.srcElement;
}
function poin(e) { var ev=getEv(e); if (!ev) { return false; }
  if (ev.preventDefault) { ev.preventDefault(); } ev.returnValue=false; return false;
}
function isDescendantOf(tO, tT) { while ((tO=tO.parentNode)) { if (tO == tT) { return true; } } return false; }


BelezaNatural.mini_nome=function (i, minSize) { i=i.toString();
  if (minSize === undefined) { minSize=2; } while (i.length < minSize) { i='0'+ i; }
  return BelezaNatural.mini_prefix + i + BelezaNatural.mini_suffix;
};
BelezaNatural.mini_path=function (obj) { var path=[BelezaNatural.pasta, obj.pasta];
  if (BelezaNatural.mini_pasta) { path[path.length]=BelezaNatural.mini_pasta; }
  return path.join('/') + '/';
};
BelezaNatural.big_nome=function (i, minSize) { i=i.toString();
  if (minSize === undefined) { minSize=2; } while (i.length < minSize) { i='0'+ i; }
  return BelezaNatural.big_prefix + i + BelezaNatural.big_suffix;
};
BelezaNatural.big_path=function (obj) { var path=[BelezaNatural.pasta, obj.pasta];
  if (BelezaNatural.big_pasta) { path[path.length]=BelezaNatural.big_pasta; }
  return path.join('/') +'/';
};
BelezaNatural.ddclick=function (e, onlyClose) {
  var opdiv=gE('cabelosopcoes'); if (!opdiv) { return false; }
  if (!BelezaNatural.ddaberto && BelezaNatural.onlyClose) { return poin(e); }
  $(opdiv)[((BelezaNatural.ddaberto) ? 'fadeOut' : 'slideDown')]();
  BelezaNatural.ddaberto=!(!!BelezaNatural.ddaberto);
  if (BelezaNatural.ddaberto) { $(document).click(BelezaNatural.checkClickOutsite); }
  else { $(document).unbind('click'); }
  return poin(e);
};
BelezaNatural.cortesLI=[];
BelezaNatural.corteAtual=null;
BelezaNatural.selecionaCorte=function (e) { if (!BelezaNatural.ddaberto) { return false; }
  if (BelezaNatural.animando) { return false; } //Evitar a troca de penteado na animação.
  var tE=getSrc(e), tA=(tE.nodeName.toLowerCase() != 'a') ? tE.getElementsByTagName('a')[0] : tE;
  if (!tE || !tA) { return false; } var i=tA.href.split('?').pop().split('=').pop();
  if (BelezaNatural.corteAtual !== (parseInt(i) -1)) {
    $(BelezaNatural.cortesLI[BelezaNatural.corteAtual]).removeClass('selecionado');
    $(tA.parentNode).addClass('selecionado');
    BelezaNatural.popula(BelezaNatural.cortes[i -1], i);
    BelezaNatural.mudaAngulo(BelezaNatural.cortes[i -1], 1);
  } return BelezaNatural.ddclick(e, 1);
};
BelezaNatural.popula=function (tO, corteI) { corteI=parseInt(corteI); BelezaNatural.corteAtual=corteI -1;
  var tD=gE('cabelosescolhido'); if (!tD) { return false; } rEs(tD.childNodes);
  var nome_arquivo=BelezaNatural.mini_nome('1');
  tD.appendChild(cE('img', {'src': BelezaNatural.mini_path(tO) + nome_arquivo}, ''));
  tD.appendChild(cE('span', tO.nome));
  var i=0, n=tO.angulos, nome_arquivo; tD=gE('cabeloslista'); rEs(tD.childNodes);
  for (; i<n; i++) { nome_arquivo=BelezaNatural.big_nome(i +1);
    tD.appendChild(cE('a', {'href': BelezaNatural.big_path(tO) + nome_arquivo, 'class': (i +1)},
      cE('img', {'src': BelezaNatural.mini_path(tO) + BelezaNatural.mini_nome(i +1)}, '')
    ));
  }
  return true;
};
BelezaNatural.animando=0; BelezaNatural.showText=null;
BelezaNatural.mudaAngulo=function (tO, i) { if (BelezaNatural.animando) { return false; }
  var tI=gE('destaqueimg'), tD=gE('destaquedesc'); if (!tI || !tD) { return false; }
  BelezaNatural.animando=1; var nome_arquivo=BelezaNatural.big_nome(i);
  var nE=cE('img', {'src': BelezaNatural.big_path(tO) + nome_arquivo}, '');
  nE.style.zIndex=2; $(nE).hide(); tI.appendChild(nE); $(nE).fadeIn(1000, BelezaNatural.finishedChange);
  $(tD).animate({'top': 373}, 373, 'linear', BelezaNatural.finishedHiding);
  BelezaNatural.showText=tO.descricao; return true;
};
BelezaNatural.finishedHiding=function () { var tO=gE('destaquedesc');
  if (!tO) { return false; } rEs(tO.childNodes); tO.appendChild(cE('span', {}, BelezaNatural.showText));
  $(tO).animate({'top': 373}, 373, 'linear');
  return true;
};
BelezaNatural.finishedChange=function () { var tI=gE('destaqueimg'); if (!tI) { return false; }
  var tIs=makeArray(tI.childNodes); tIs.pop(); rEs(tIs); tI.childNodes[0].style.zIndex=1;
  return !(BelezaNatural.animando=0);
};

BelezaNatural.clickAngulo=function (e) { var tE=getSrc(e), i=0, n=3, found=0;
  for (; i<n; i++) { if (tE.nodeName.toLowerCase() == 'a') { found=1; break } else { tE=tE.parentNode; } }
  if (!found) { return true; } var atual=BelezaNatural.corteAtual;
  BelezaNatural.mudaAngulo(BelezaNatural.cortes[atual], tE.className);
  return poin(e);
};
BelezaNatural.checkClickOutsite=function (e) { var tE=getSrc(e), tO=gE('cabelosdropdown');
  if (!isDescendantOf(tE, tO)) { return BelezaNatural.ddclick(e, 1); }
  return true;
};

$(function() { var tO;
  //Populating the dropdown list
  tO=gE('cabelosopcoes'); if (!tO) { return false; }
  var tUL=cE('ul'), i=0, n=BelezaNatural.cortes.length, corte, nome_arquivo, nE;
  for (; i<n; i++) { corte=BelezaNatural.cortes[i];
    nome_arquivo=BelezaNatural.mini_nome('1');
    nE=cE('li', {}, cE('a', {'href': '?corte='+ (i +1)},
      [cE('img', {'src': BelezaNatural.mini_path(corte) + nome_arquivo}, ''), ' '+ corte.nome]
    )); BelezaNatural.cortesLI[BelezaNatural.cortesLI.length]=nE;
    tUL.appendChild(nE);
  } $(tUL).click(BelezaNatural.selecionaCorte);
  $(tO).hide(); tO.appendChild(tUL);

  //Creating a dropdown that works :)
  tO=gE('cabelosdropdown');
  nE=cE('a', {'class': 'seta', 'href': '#'}, cE('img', {'src': 'img/cabelos/seta_dropdown.png'}, ''));
  tO.appendChild(nE); $(nE).click(BelezaNatural.ddclick);

  //Hiding default text space
  tO=gE('destaquedesc'); tO.style.top='448px';

  //Adding onclick action
  $('#cabeloslista').click(BelezaNatural.clickAngulo);

  //Vem, morena, vem... Vem, morena, vem sambar.
  $(BelezaNatural.cortesLI[0]).addClass('selecionado');
  BelezaNatural.popula(BelezaNatural.cortes[0], 1);
  BelezaNatural.mudaAngulo(BelezaNatural.cortes[0], 1);

  $('#boxcontato form').submit(function() {
    var tV=$('.nome')[0];
    if (tV.value.replace(/ /g, '') == '' || tV.value == 'CAMPO OBRIGATÓRIO!') { return notifyError(tV); }
    var tV=$('.email')[0];
    if (tV.value.replace(/ /g, '') == '' || tV.value == 'CAMPO OBRIGATÓRIO!') { return notifyError(tV); }
    if (!tV.value.match(/[a-z0-9_-][a-z0-9_\-\.\+]+@[a-z0-9_\-\.]{3,}(\.[a-z]{2})+/g)) {
      return notifyError(tV, 1);
    }
    var tV=$('.mensagem')[0];
    if (tV.value.replace(/ /g, '') == '' || tV.value == 'CAMPO OBRIGATÓRIO!') { return notifyError(tV); }
    return true; //ê!
  });
});
function notifyError(campo, deixarvalor) {
  $(campo).addClass('erro'); if (!deixarvalor) { $(campo)[0].value='CAMPO OBRIGATÓRIO!'; }
  $(campo).focus(function() {
    if (this.value == 'CAMPO OBRIGATÓRIO!') { this.value = ''; }
    $(this).removeClass('erro').unbind('focus'); }
  ); return false;
}