jQuery(document).ready(function($) {

	if($( window ).width() < 1250)
	{
		/*
		var html = '<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-menu-down"></span></a>';
		$('.m-4').parent().append(html);
		$('.m-4').parent().addClass('dropdown-submenu');
		
		*/
		$('.menutop > div:first').css('display','none');
		$('.m-4').parent().parent().addClass('dl-menu');
		$(function() {
			$( '#dl-menu' ).dlmenu();
		});


	}
	else
	{
		


		$('#dl-menu').css('display','none');
	}

	$('ul.nav > li > a:not(.m-4)').hover(function () {
		deleteMainDiv();
	});
	

	$('.m-4').click(function (e) {
		if($( window ).width() > 1250)
		{
			createMainDiv(e);
			
			$( ".dropdown-submenu.open" ).removeClass( "open" );
			
		}
		else
		{
			createMobileMenu(e);
		}
		
		
		
		return false;
	});
	
	
	
	
});

function createMenuChild( categoryId ){

	if( $( ".cat-menu" ).length )
	{
		$( ".cat-menu" ).remove();
		$(" #featureProduct").remove();
	}

	if ( !$( ".cat-menu" ).length ) {
	 
		$.get( "/pt/catalogo/getChildMenus/parent_id/"+categoryId+"/", function( data ) {
		    
		    if( $( ".cat-menu" ).length )
        	{
        		$( ".cat-menu" ).remove();
        		$(" #featureProduct").remove();
        	}
		  
		  	if( data.length != 0 )
			{
				var menu = jQuery.parseJSON( data );

				$('#productMenuHolder > ul').removeClass('col-md-12');
				$('#productMenuHolder > ul').addClass('col-md-4');
				var html = '<ul class="col-md-4 cat-menu cat-'+categoryId+'">';
				$.each( menu, function( key, value ) {
					html += '<li class="menu-'+value.id+'" >'
					html += '<a href="/pt/catalogo/list/categoria_id/'+value.id+'"  > ' + value.titulo + '</a>';
					html += '</li>';
				});
				html += '</ul>';
				$('#productMenuHolder').append(html);
				getFeatureForProductMenu(categoryId);
			}
		});

	}

}

function deleteMainDiv(){
	$( "#productMenuHolder" ).remove();
}

function deleteMobileMenu(){
	$( "#productMobileMenuHolder" ).remove();
}



function createMainDiv(e){


	if ( !$( "#productMenuHolder" ).length ) {
	 
		$.get( "/pt/catalogo/getProductMenu", function( data ) {
		    
		    deleteMainDiv();
		  
			var menu = jQuery.parseJSON( data );
			var html = '<div id="productMenuHolder"><div class="produto-menu-titulo">Produtos</div><ul class="col-md-12" >';

			if( menu.length != 0 )
			{
				$.each( menu, function( key, value ) {
					html += '<li class="menu-'+value.id+'" >';
					if(value.id == 95)
					{
						html += '<a href="/pt/go/madeiras"  > Madeiras</a>';
					}
					else
					{
						html += '<a href="#" data-toggle="'+value.id+'" > ' + value.titulo + '</a>';
					}
					html += '</li>';
				});

				html += '</ul></div>';

				$(e.target).parent().append(html);

				$('#productMenuHolder > ul > li > a').click(function () {
					createMenuChild($( this ).attr('data-toggle'));
					$('#productMenuHolder > ul > li > a.active').removeClass('active');
					$( this ).addClass('active');
				});
				
				
				$("#productMenuHolder").on("mouseleave", function () {
            	    deleteMainDiv();
            	});	
            	
			}
		});
	}
	else
	{
	    console.log($( "#productMenuHolder" ).length);
	}
}

function createMobileMenu(e){
	//deleteMobileMenu();

	if ( !$( ".products" ).length ) {
	 
		$.get( "/pt/catalogo/getProductMenu", function( data ) {
		  
			var menu = jQuery.parseJSON( data );

			var html ='<ul class="dropdown-menu sub-menu products" >';

			if( menu.length != 0 )
			{
				$.each( menu, function( key, value ) {
					html += '<li>';
					if(value.id == 95)
					{
						html += '<a class="m-'+value.id+'" href="/pt/go/madeiras"  > Madeiras</a>';
					}
					else
					{
						html += '<a data-toggle="'+value.id+'" class="m-'+value.id+'" href="#"  > ' + value.titulo + '</a>';
						html += '<ul class="dropdown-menu sub-menu subCat subCat-'+value.id+'" ></ul>';
					}
					html += '</li>';
				});

				html += '</ul>';

				$(e.target).parent().addClass('dropdown-submenu');

				$(e.target).parent().append(html);

				
				$('.products > li > a').click(function () {
					createMobileMenuChild($( this ).attr('data-toggle'));
					/*
					$('#productMenuHolder > ul > li > a.active').removeClass('active');
					$( this ).addClass('active');
					*/
				});
				
				
			}
		});
	}
}

function createMobileMenuChild( categoryId ){

    $('.subCat').hide();
	$('.subCat-'+categoryId+' > li').remove();
	 
	$.get( "/pt/catalogo/getChildMenus/parent_id/"+categoryId+"/", function( data ) {
	  
	  	if( data.length != 0 )
		{
			var menu = jQuery.parseJSON( data );

			//var html = '<ul class="dropdown-menu sub-menu subCat-'+categoryId+'" >';

			var html = '';

			$.each( menu, function( key, value ) {
				html += '<li class="menu-'+value.id+'" >'
				html += '<a href="/pt/catalogo/list/categoria_id/'+value.id+'"  > ' + value.titulo + '</a>';
				html += '</li>';
			});

			//html += '</ul>';
			
			if(html == '') {
				window.location.href = '/pt/catalogo/listProdutos/categoria_id/'+categoryId;
			}
			else {
				$('.subCat-'+categoryId).append(html);	

				$('.subCat-'+categoryId).css('display','block');	
			}
		}
	});

}


function getFeatureForProductMenu(categoryId)
{
	if(!$( "#featureProduct" ).length){
		
		$.get( "/pt/catalogo/getFeatureProduct/category_id/"+categoryId+"/", function( data ) {

			var feature = jQuery.parseJSON( data );
			var descricao = feature.descricao;

			if (descricao !== null && descricao.length > 100) {
				descricao = (text.substr(0, text.lastIndexOf(' ', 97)) + '...');
			} else {
    			descricao = '';
			}

			var productLink = "/pt/catalogo/go/"+feature.url;
			
			var html='<div id="featureProduct" class="col-md-4">';
			html+='<div class="image-holder"> <img src="/uploads/catalogo/imagens/medium_'+feature.imagem+'" /> </div>';
			html+="<h2 style=' font-size: 20px; text-align: center;'> "+ feature.titulo +" </h2>";
			html+='<p style="font-size: 13px;" >' + descricao + '</p>';
			html+='<p style="text-align:center;" > <a href="'+ productLink +'" style="font-size:13px; text-align:center; color: #6d8632;"> VER TUDO </a> </p>';
			html+='</ div>';

			$('#productMenuHolder').append(html);
		});

	}


	
}