function loadScript(url, callback) {

    var script = document.createElement('script')

    script.type = 'text/javascript';

    if (script.readyState){

        script.onreadystatechange = function(){

            if (script.readyState == "loaded" ||

                    script.readyState == "complete"){

                script.onreadystatechange = null;

                callback();

            }

        };

    } else {

        script.onload = function(){

            callback();

        };

    }

    script.src = url;

	document.getElementsByTagName("head")[0].appendChild(script);

}

function gowebShowCustomBox(link,title, filter) {

	var data = {

		'link' : link,

		'title' : title,

		'filter' : filter

	};

	$.ajax({

		url: link,

		cache: true,

		data: data,

		success: function(result){            

			$("#modal_box_generic .modal-title").html(title);

			$("#modal_box_generic .modal-body").html(result);



			$("#modal_box_generic").modal({

				keyboard: false,

				backdrop: 'static'

			});

		}

	});

}

function loadjscssfile(filename, filetype, async){

    if (filetype=="js"){

		var fileref=document.createElement('script');

		fileref.setAttribute("type","text/javascript");

		fileref.setAttribute("src", filename);

		if(async == 'true')

			fileref.setAttribute("async", "true");

    }

    else if (filetype=="css"){

		var fileref=document.createElement("link");

		fileref.setAttribute("rel", "stylesheet");

		fileref.setAttribute("type", "text/css");

		fileref.setAttribute("href", filename);

    }

	if (typeof fileref!="undefined")

		document.getElementsByTagName("head")[0].appendChild(fileref);

}

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

	if($("#content-slider").length) {

		$("#content-slider").lightSlider({

			item: 1,

			mode:'slide',

			auto:true,

			loop:true,

			pager:false,

			centerSlide:true,

			slideMargin:0,

			pause:6000,

			prevHtml:'<span class="glyphicon glyphicon-menu-left"></span>',

            nextHtml: '<span class="glyphicon glyphicon-menu-right"></span>'

		});

	}



	if($('.itens-out #itens').length) {

		var slider = $('.itens-out #itens').lightSlider({

			item:4,

			loop:true,

			auto:true,

			slideMove:2,

			pager:false,

			easing: 'cubic-bezier(0.25, 0, 0.25, 1)',

			speed:600,

			pause:3000,

			prevHtml:'<span class="glyphicon glyphicon-menu-left"></span>',

            nextHtml: '<span class="glyphicon glyphicon-menu-right"></span>'

		});

	}

	

	$('footer .menu-footer > .glyphicon').on('click', function() {

		if($(this).hasClass('glyphicon-triangle-top')) {

			$(this).removeClass('glyphicon-triangle-top');

			$(this).addClass('glyphicon-triangle-bottom');

		}

		else {

			$(this).removeClass('glyphicon-triangle-bottom');

			$(this).addClass('glyphicon-triangle-top');

		}

		$('.menu-footer .menu_catalogo').slideToggle();

	});



	if($('.form.view select').length)

		$('.form.view select').chosen({width: "100%"});

	

	$(window).scroll(function() {

		if($(this).scrollTop() > 300)

			$('.gotop').fadeIn();

		else

			$('.gotop').fadeOut();

	});

	

	$('.gotop .btn-go.up').click(function() {

		$('html, body').animate({scrollTop : 0},800);

		return false;

	});

	$('.productHover').css('height', $('.productTop').height());
	
	$('.js-NewsletterSubmit').click(function() {
    	
    	var politica = $('#politica:checked').val(),
    	    comercial = $('#comercial:checked').val(),
    	    promocional = $('#promocional:checked').val(),
    	    remover = $('#remover:checked').val();
    	    
        if (politica == 1 && comercial === undefined && promocional === undefined && remover === undefined) {
            $('.js-newsletterShowMessage').show();
            return false;
        }
    	
	});
	
	$('.js-madeirasMarker').click(function() {
    	
        var $map = $('.js-madeirasMap'),
            continent = $(this).data('continent');
            
        $('.js-madeirasContent').fadeOut();
            
    	$map.fadeOut(function() {
        	$map.attr('src', '/media/images/madeiras/mapa-' + continent + '.jpg').fadeIn()
    	});
    	
    	var data = {
        	continent: continent
    	};
        
        $.ajax({
            url: '/' + $('html').attr('lang') + '/catalogo/getMadeirasByContinent/',
            data: data,
            type: 'GET',
            dataType: 'JSON',
            error: function() {},
            success: function(result) {

                if (result.error === false) {
                    
                    $('.js-madeirasCategory').text(result.data.title);
                    $('.js-madeirasList').load('/' + $('html').attr('lang') + '/catalogo/madeirasList/categoria_id/' + result.data.id);
                    $('.js-madeirasContent').fadeIn();
                    
                }
                
            }
        });
    	
	});
	
	$('.js-madeirasBtnClose').click(function() {
    	
    	var $map = $('.js-madeirasMap');
    	
    	$('.js-madeirasContent').fadeOut();
    	$map.fadeOut(function() {
        	$map.attr('src', '/media/images/madeiras/mapa-original.jpg').fadeIn()
    	});
    	
    });

});