/*SORT NUMBER*/
function sortNumberMin(a, b){
    return a - b;
}

function sortNumberMax(a, b){
    return b - a;
}

/*NUMBER RANDOM*/
function numberRandom(_n, _m){
    var _number = Math.floor(Math.random() * _n);

    if(_number < _m){
        _number = _m;
    }

    return _number;
}

/*PROXIMITY*/
function proximityIndex(_array, _number){
    _array = _array.sort(sortNumberMin);

    for(var i = 0; i < _array.length; i++){
        var _half = (_array[i + 1] - _array[i]) / 2;

        if(_array[i - 1] != undefined){
            _half = (_array[i] - _array[i - 1]) / 2;
        }

        if(_number <= _array[i + 1] / 2 || _number <= (_array[i] + (_half))){
            return i;
        }
    }
}

function proximity(_array, _number){
    return _array[proximityIndex(_array, _number)];
}

/*ONLOAD*/
$(function(){
	/*FOOTER LI FIRST*/
	$('.footer li:first').addClass('first');
	
	/*FORMA DE CONTATO LI FIRST*/
	$('.contato .formas_de_contato li:first').addClass('first');
	
	/*CONTATO*/
	$('.content.contato form button').click(function(){
		$('.content.contato form').validate({
			rules: {
				'txt_nome': {
					required: true
				},
				
				'txt_email': {
					required: true,
					email: true
				},
				
				'txt_comentario': {
					required: true
				}
			},
			
			messages: {
				'txt_nome': {
					required: ""
				},
				
				'txt_email': {
					required: "",
					email: ""
				},
				
				'txt_comentario': {
					required: ""
				}
			},
			
			submitHandler: function(){
    			$.ajax({
					type: 'POST',
					url: 'send.php',
					data: $('.content form').serializeArray(),
					success: function(){
						$('.content.contato form').html('<p>Mensagem enviada com sucesso</p>');
					}
				})
			}
		})
	})

	/*PROJETOS*/
	var _timeout = '';
	var _intScrollStop = '';
	var _arrayPos = [];
	var _scrollLeft = $('.projetos_design .projetos_design_content')[0].scrollLeft;
	var _w = $('.projetos_design .projetos_design_content ul li.item').outerWidth();

	for(var i = 0; i < $('.projetos_design .projetos_design_content ul li.item').length; i++){
	    var _entry = $('.projetos_design .projetos_design_content ul li.item').eq(i);
	    _entry.attr('data-index', parseInt(i + 1));
	    _arrayPos.push(_w * i);
	}

	$('.projetos_design ul:first').width(($('.projetos_design .projetos_design_content ul li.item').outerWidth() * $('.projetos_design .projetos_design_content ul li.item').length) - 5);

	$('.projetos_design .projetos_design_content li.item a').click(function(){
	    var _entry = $(this);
	    $('.projetos_design .projetos_design_content').animate({scrollLeft: _w * (_entry.parent().attr('data-index') - 1)}, 1000);

	    return false;
	});

    $('.projetos_design .projetos_design_content')[0].scrollLeft = (_scrollLeft + 1);
    $('.projetos_design .projetos_design_content')[0].scrollLeft = (_scrollLeft - 1);

    var _totalItensList = $('.projetos_design li .item_list li').length;
    var _itemList = 0;

    function fadeItensList(){
        if(_itemList > _totalItensList - 1){
            _itemList = 0;
        }

        $('.projetos_design li .item_list li').hide();

        $('.projetos_design li .item_list li').eq(_itemList).fadeIn(1000, function(){
            setTimeout(function(){
                fadeItensList();
            }, 4000);
        });

        _itemList++;
    }

    fadeItensList();

    $('.projetos_design .projetos_design_content').focus();
    $('body, html')[0].scrollTop = 0;

	$('.projetos_design .projetos_design_content').scroll(function(){
	    clearInterval(_timeout);
	    clearInterval(_intScrollStop);

	    _scrollLeft = $('.projetos_design .projetos_design_content')[0].scrollLeft;

        _timeout = setInterval(function(){
            if(_scrollLeft == $('.projetos_design .projetos_design_content')[0].scrollLeft){
                clearInterval(_timeout);
            	clearInterval(_intScrollStop);

                _intScrollStop = setTimeout(function(){
                    $('.projetos_design .projetos_design_content').animate({scrollLeft: proximity(_arrayPos, _scrollLeft)}, 200);
                }, 1000);
            }
        }, 100);
    })
})
