﻿$(function() {
    $("#subMenuProdutos>li>a").each(function() {
        $(this).mouseover(function() {
            var href = $(this).attr("href");
            var c = getParameterOfStringByName(href, "c");

            var colecao = $("div [id*='divProMenu']>ul");
            var novoItem = $("#menuCategoria" + c);
            colecao.removeClass("ativo");
            colecao.addClass("inativo");
            novoItem.removeClass("inativo");
            novoItem.addClass("ativo");
        });
    });

	$("#menuTopoAnterior").click(function() {
		var listaAtiva = $(this).parent().parent();
		var nomeNovaLista = "#" + listaAtiva.attr("id").replace("_2", "");
        listaAtiva.removeClass("ativo");
		listaAtiva.addClass("inativo");
		$(nomeNovaLista).removeClass("inativo");
		$(nomeNovaLista).addClass("ativo");
        return false;
    });

	$("#menuTopoProximo").click(function() {
		var listaAtiva = $(this).parent().parent();
		var nomeNovaLista = "#" + listaAtiva.attr("id") + "_2";
        listaAtiva.removeClass("ativo");
		listaAtiva.addClass("inativo");
		$(nomeNovaLista).removeClass("inativo");
		$(nomeNovaLista).addClass("ativo");
        return false;
    });

    $("#menuProdutosAnterior").click(function() {
        navegarAnterior($("#menuProdutos>li"), "#produto");
        return false;
    });
    $("#menuProdutosProximo").click(function() {
        navegarProximo($("#menuProdutos>li"), "#produto");
        return false;
    });

    $("#imagensProdutoAnterior").click(function() {
        navegarAnterior($("#imagensProduto>img"), "#imagem");
        return false;
    });
    $("#imagensProdutoProximo").click(function() {
        navegarProximo($("#imagensProduto>img"), "#imagem");
        return false;
    });

    var navegarAnterior = function(colecao, aliasItem) {
        /*var indexItemAtivo = colecao.index($(".ativo"));*/
        var indexItemAtivo = getItemAtivo(colecao);
        var novoIndexItem = indexItemAtivo - 1;
        if (indexItemAtivo <= 0) {
            novoIndexItem = colecao.length - 1;
        }
        var novoItem = $(aliasItem + novoIndexItem);
        colecao.removeClass("ativo");
        colecao.addClass("inativo");
        novoItem.addClass("ativo");
    }

    var navegarProximo = function(colecao, aliasItem) {
        /*var indexItemAtivo = colecao.index($(".ativo"));*/
        var indexItemAtivo = getItemAtivo(colecao);
        var novoIndexItem = indexItemAtivo + 1;
        if ((novoIndexItem + 1) > colecao.length) {
            novoIndexItem = 0;
        }
        var novoItem = $(aliasItem + novoIndexItem);
        colecao.removeClass("ativo");
        colecao.addClass("inativo");
        novoItem.addClass("ativo");
    }

    var getItemAtivo = function(colecao) {
        var i = 0;
        var itemAtivo = 0;
        colecao.each(function() {
            if ($(this).hasClass("ativo")) {
                itemAtivo = i;
            }
            i++;
        });
        return itemAtivo;
    }
});

function getParameterOfStringByName(orig, name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(orig);
  if( results == null )
    return "";
  else
    return results[1];
}