﻿var YachutoEstilo = {};
YachutoEstilo.Padrao = {};
YachutoEstilo.Padrao.getElementChildren = function(element)
{
	var children = [];
	var child = element.firstChild;
	while (child)
	{
		if (child.nodeType == 1)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};

YachutoEstilo.Padrao.getElementChildren = function(element)
{
	var children = [];
	var child = element.firstChild;
	while (child)
	{
		if (child.nodeType == 1)
		{
			children.push(child);
		}
		child = child.nextSibling;
	}
	return children;
};
YachutoEstilo.Padrao.addClassName = function(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
		return;
	ele.className += (ele.className ? " " : "") + className;
};

YachutoEstilo.Padrao.contemClasse = function(ele, className)
{
    if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
		return true;
	return false;
}
YachutoEstilo.Padrao.removeClassName = function(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
		return;
	ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

YachutoEstilo.NovoEstiloYachuto = function(id, indiceInicial,menuContainer, menuNormal,menuSobre, menuSelecionado, conteudoNormal, conteudoSelecionado)
{
    var self = this;
    this.menuNormal = menuNormal;
    this.menuSobre = menuSobre;
    this.menuSelecionado = menuSelecionado;
    this.conteudoNormal = conteudoNormal;
    this.conteudoSelecionado = conteudoSelecionado;
    this.novoDiv = document.createElement("div");
    this.element = document.getElementById(id);
    this.subPrincipais = YachutoEstilo.Padrao.getElementChildren(this.element)
    this.subMenus = -1;
    this.onClickMenu = function()
    {
        var x2=0;
        for(x2=0;x2<self.subMenus.length;x2++)
        {
            
            YachutoEstilo.Padrao.removeClassName(self.subMenus[x2],self.menuSelecionado);
            YachutoEstilo.Padrao.addClassName(self.subMenus[x2],self.menuNormal);
        }
        
        YachutoEstilo.Padrao.removeClassName(this,self.menuNormal);
        YachutoEstilo.Padrao.addClassName(this,self.menuSelecionado);
        
        for(x2=0;x2<self.subPrincipais.length;x2++)
        {
            if(YachutoEstilo.Padrao.contemClasse(self.subPrincipais[x2],self.conteudoSelecionado))
            {
                YachutoEstilo.Padrao.removeClassName(self.subPrincipais[x2],self.conteudoSelecionado);
                YachutoEstilo.Padrao.addClassName(self.subPrincipais[x2],self.conteudoNormal);
            }
        }
        if(!YachutoEstilo.Padrao.contemClasse(self.subPrincipais[this.getAttribute("num")],self.conteudoSelecionado))
        {
            YachutoEstilo.Padrao.removeClassName(self.subPrincipais[this.getAttribute("num")],self.conteudoNormal);
            YachutoEstilo.Padrao.addClassName(self.subPrincipais[this.getAttribute("num")],self.conteudoSelecionado);
        }
        //alert((Math.round(this.getAttribute("num"))+1) + " - "+self.subPrincipais[(Math.round(this.getAttribute("num"))+1)].className);
        
    }
    
    this.onOverMenu = function()
    {
        YachutoEstilo.Padrao.removeClassName(this,self.menuNormal);
        YachutoEstilo.Padrao.addClassName(this,self.menuSobre);
    }
    this.onOutMenu = function()
    {
        YachutoEstilo.Padrao.removeClassName(this,self.menuSobre);
        YachutoEstilo.Padrao.addClassName(this,self.menuNormal);
    }
    
    var x=0;
    this.novoDiv.className = menuContainer;//"tabset_tabs";
    for(x=0;x<this.subPrincipais.length;x++)
    {
        var execute = YachutoEstilo.Padrao.getElementChildren(this.subPrincipais[x])[0];
        execute.setAttribute("num",x);
        execute.onclick = this.onClickMenu;
        execute.onmouseover = this.onOverMenu;
        execute.onmouseout = this.onOutMenu;
        if(indiceInicial == x)
            execute.onclick();
        this.novoDiv.appendChild(execute);
    }
    this.element.insertBefore(this.novoDiv,this.element.firstChild);
    this.subMenus = YachutoEstilo.Padrao.getElementChildren(this.novoDiv);
}
