function image_animator(element, speed, direction, widthstart, widthmiddle, widthend) {

    var image_witdh = $('.' + element + ' a').attr('rel');

    if(direction) {
        // direction == right
        $('.' + element).css({right: '-' + image_witdh + 'px', width: widthmiddle -30}).animate({right: 650, width: widthmiddle}, speed);
    }
    else {
        // direction == left
        $('.' + element).css({left: '-' + image_witdh + 'px', width: widthmiddle - 30}).animate({left: 650, width: widthmiddle}, speed);
    }

}

function image_animator_end(random_end_number) {

    $('.bossendimages' + random_end_number).show().css({left: '-256px', width: '256px'}).animate({left: 0}, 1500);
    $('.bossendimages_person' + random_end_number).show().css({left: '-268px', width: '0px'}).animate({left: 256, width: '268px'}, 1500);
    $('.bossenddummy1').show().css({left: '-56px'}).animate({left: 556}, 1500);
    $('.bossenddummy2').show().css({left: '-23px'}).animate({left: 620}, 1500, function() {
        $('#startpage_imageanimation').addClass('animationend');
        
        var animationendlink = $('.animationendlink').attr('href');
        $('#startpage_imageanimation').click(function() {
            location.href=animationendlink;
        });
    });

}

var image_animator_loaded = 0;
var image_animator_preloader = new Array();
var image_animator_counter = 0;
function next_image_animator() {
    image_animator(settings[image_animator_counter][0][0], settings[image_animator_counter][0][1], settings[image_animator_counter][0][2], settings[image_animator_counter][0][3], settings[image_animator_counter][0][4], settings[image_animator_counter][0][5]);
    image_animator(settings[image_animator_counter][1][0], settings[image_animator_counter][1][1], settings[image_animator_counter][1][2], settings[image_animator_counter][1][3], settings[image_animator_counter][1][4], settings[image_animator_counter][1][5]);
    image_animator_counter++;
}

var neutralimages_list = new Array;
var contactperson_small_list = new Array;
var settings = new Array;

$(function() {

    if($('#startpage_imageanimation').length) {

        var neutralimages_length = $('.neutralimages').length - 1;
        var contactperson_small_length = $('.contactperson_small_item').length - 1;
        var bossendimages_length = $('.bossendimages').length - 1;

        for(i=0; i<=neutralimages_length; i++) {
            neutralimages_list[i] = 'neutralimages' + i;
        }
        for(i=0; i<=contactperson_small_length; i++) {
            contactperson_small_list[i] = 'contactperson_small' + i;
        }

        for(i=0; i<=2; i++) {
            var neutralimages_random = Math.floor(Math.random() * neutralimages_length);
            var contactperson_small_random = Math.floor(Math.random() * contactperson_small_length);
            var neutralimages_direction = Math.floor(Math.random() * 2);
            var contactperson_small_direction = Math.floor(Math.random() * 2);
            var neutralimages_speed = Math.floor(5900 + (Math.random()*500));
            var contactperson_small_speed = Math.floor(2500 + (Math.random()*1500));

            var neutralimages_random_select = neutralimages_list[neutralimages_random];
            var contactperson_small_random_select = contactperson_small_list.splice(contactperson_small_random, 1)[0];
            while(contactperson_small_random_select == undefined) {
                contactperson_small_random = Math.floor(Math.random() * contactperson_small_length);
                contactperson_small_random_select = contactperson_small_list.splice(contactperson_small_random, 1)[0];
            }

            settings[i] = new Array();
            settings[i][0] = new Array(neutralimages_random_select, neutralimages_speed, neutralimages_direction, 10, 300, 100);
            settings[i][1] = new Array(contactperson_small_random_select, contactperson_small_speed, contactperson_small_direction, 10, 300, 100);

            image_animator_preloader.push('.' + contactperson_small_random_select);
            image_animator_preloader.push('.' + neutralimages_random_select);
        }

        image_animator_preloader.push('.neutralimagesfix1');

        var bossendimages_random = Math.floor(Math.random() * bossendimages_length);
        image_animator_preloader.push('.bossendimages' + bossendimages_random);
        image_animator_preloader.push('.bossendimages_person' + bossendimages_random);

        for(i=0; i<image_animator_preloader.length; i++) {
            var img = document.createElement('img');
            $(img).load(function() {
                image_animator_loaded++;

                var progress_count = Math.round((image_animator_loaded / image_animator_preloader.length) * 100) + '%';

                $('.progress_count').html(progress_count);

                $('.preloader_progress').css({
                    width: progress_count
                });

                if(image_animator_loaded >= image_animator_preloader.length) {
                    start_animation(bossendimages_random);
                    $('.preloader_bar').remove();

                }
            });

            img.src = $(image_animator_preloader[i] + ' a').attr('href');
            img.style.width = $(image_animator_preloader[i] + ' a').attr('rel') + 'px';
            $(image_animator_preloader[i]).append(img);
        }
    }

});

function start_animation(bossendimages_random) {
    // dummy background animation
    image_animator('neutralimagesfix1', 6000, 1, 10, 268, 100);

    next_image_animator();
    setTimeout('next_image_animator()', 2300);
    setTimeout('image_animator_end(' + bossendimages_random + ')', 5250);
}
