$(function() {
    /* Ação de clique no "próximo" do carrossel */
    
    var numItems = $('#companySponsorSlider li').length;
    if (numItems > 2) {
        if (numItems < 5) {
            $('#companySponsorSlider ul').append($('#companySponsorSlider ul li').slice(0,(5-numItems)));
        }
        $('#companySponsorList p.previous,#companySponsorList p.previous a,#companySponsorList p.next,#companySponsorList p.next a').css('display','block');
        $('#companySponsorList p.next a').click(function(e) {
            e.stopPropagation();
            e.preventDefault();
            if ($('#companySponsorList ul:animated').length == 0) {
                var diff = 121;
                var currDesloc = $('#companySponsorList ul').css('marginLeft').replace(/px/,'');
                var totalWidth = $('#companySponsorList ul li').length * diff;
                var newMargin = (currDesloc*-1)+(diff*2);
                if ( (newMargin+(diff*2)) > totalWidth ) {
                    $('#companySponsorList ul')
                        .append($('#companySponsorList ul li').slice(0,2))
                        .css('marginLeft', ((newMargin-(diff*4))*-1) +'px');
                    newMargin = parseFloat(currDesloc)*-1;
                }
                $('#companySponsorList ul').animate( {"marginLeft":(newMargin*-1)+"px"});
            }
        });
    
        /* Ação de clique no "anterior" do carrossel */
        $('#companySponsorList p.previous a').click(function(e) {
            e.stopPropagation();
            e.preventDefault();
            if ($('#companySponsorList ul:animated').length == 0) {
                var diff = 121;
                var currDesloc = $('#companySponsorList ul').css('marginLeft').replace(/px/,'');
                var totalWidth = $('#companySponsorList ul li').length * diff;
                var newMargin = ((currDesloc*-1)+(diff*2)*-1);
                if ( currDesloc > ((diff*2)*-1) ) {
                    var totalItems = $('#companySponsorList ul li').length;
                    $('#companySponsorList ul')
                        .prepend($('#companySponsorList ul li').slice( (totalItems-2),(totalItems) ))
                        .css('marginLeft', (newMargin+'px'));
                    newMargin = (newMargin)+(diff*2);
                }
                $('#companySponsorList ul').animate( {"marginLeft":(newMargin*-1)+"px"});
            }
        });
        
    }
    
	/*Envia Busca da home*/
	$('#formQuickSearch a').click(function(e){
		e.stopPropagation();
		e.preventDefault();
		$('#formQuickSearch').submit();
	});
    
    // Quebra de lista de três colunas
    var twoColumnsList = $('.workingAreasWrapper .listBreak.twoColumns');
    ListBreak.make(2, twoColumnsList);
    
    // Aleatorização das imagens na home
    (function(){
        var imagesLength = HOME_IMAGES_URLS.length;
        var randomIndex = Math.floor(Math.random() * imagesLength);
        $('#jobAreasWrapper').css('background-image', 'url('+HOME_IMAGES_URLS[randomIndex]+')');
    })();
});
