$(document).ready(function() {
    var dots = $('.presentation:not(.presentation-services) .dot');
    var dots_services = $('.presentation-services .dot');

//    dots.each(function(k, dot) {
//        animateDot(dot);
//    });
    
    dots_services.bind('mouseover', function() {
        $(this).stop().animate({top: 50}, 250, 'swing');
    });
    
    dots_services.bind('mouseout', function() {
        $(this).stop().animate({top: 30}, 250, 'swing');
    });
    
    dots_services.click(function() {
        var a = $(this).find('.details');
        document.location = a.attr('href');
    });
    
//    dots_services.each(function(k, dot) {
//        animateDot(dot);
//    });
    
    dots.bind('mouseover', function() {
        if(this.timeout) {
            clearTimeout(this.timeout);
        }
        if($(this).css('z-index')!=10) {
            this.oldIndex = $(this).css('z-index');
        }
        $(this).css('z-index', 10);
        $(this).stop();
        var scale = 1.05;
        $(this).css('transform', 'scale(1.1)').css('-webkit-transform', 'scale('+scale+')').css('-moz-transform', 'scale('+scale+')').css('-ms-transform', 'scale('+scale+')');
        //$(this).animate({ top: this.originT, left: this.originL}, 500, 'ellipse');
    });
    
    dots.bind('mouseout', function() {
        var obj = this;
        this.timeout = setTimeout(function() {
            $(obj).stop();
            $(obj).css('z-index', obj.oldIndex);
            var scale = 1;
            $(obj).css('transform', 'scale('+scale+')').css('-webkit-transform', 'scale('+scale+')').css('-moz-transform', 'scale('+scale+')').css('-ms-transform', 'scale('+scale+')');
//            animateDot(obj);
        }, 40);
    });
    
//    dots.draggable({
//        stop: function(e, ui) {
//            var o = ui.helper[0];
//            clearTimeout(o.timeout);
//            ui.helper.animate({top: o.originT, left: o.originL}, 200, 'ellipse');
//        }
//    });
    
    
    $('#menu-item-199').hover(function() {
        $(this).children('.sub-menu').show();
    }, function() { 
        $(this).children('.sub-menu').hide();
    });
    
    $('#menu-item-199 a:first').click(function() { return false })
});

function animateDot(dot) {
    if(!dot.originT) {
        var t = parseFloat($(dot).css('top'));
        var l = parseFloat($(dot).css('left'));
        dot.originT = t;
        dot.originL = l;
    } else {
        var t = dot.originT;
        var l = dot.originL;
    }
    var r = Math.random();
    var d;
    if(r < 0.5) {
        d = 1;
    } else {
        d = -1;
    }
    if(!dot.d) {
        dot.d = d;
    }
    d = dot.d;
    
    var j = 2 + Math.ceil(Math.random()*4);
    
    var t1 = Math.floor(t-(d*j));
    var l1 = Math.floor(l-(d*j));
    var t2 = Math.floor(t-(d*(j*2)));
    var l3 = Math.floor(l+(d*j));
    
    var time1 = 700 + Math.ceil(Math.random() * 300);
    var time2 = 700 + Math.ceil(Math.random() * 300);
    var time3 = 700 + Math.ceil(Math.random() * 300);
    var time4 = 700 + Math.ceil(Math.random() * 300);
    
    var flip = true;
    
    $(dot).animate({ top: t1, left: l1, flip: flip }, time1, 'ellipse', function() {
        $(this).animate({ top: t2, left: l, flip: flip }, time2, 'ellipse', function() {
            $(this).animate({ top: t1, left: l3, flip: flip }, time3, 'ellipse', function() {
                $(this).animate({ top: t, left: l, flip: flip }, time4, 'ellipse', function() {
                    animateDot(this);
                });
            });
        });
    });
}


