﻿var speed = 500;
var animateStartDelay = 5000;
var slideTime = 4000;
var current_person = null;
var t = null;
var t2 = null;
var animator = null;
var slideTimer = null;
var person_selected = false;
var ppl = new Array(16);
var current_index = 0;

$(document).ready(function() {

    ppl[0] = '06';
    ppl[1] = '10';
    ppl[2] = '00';
    ppl[3] = '03';
    ppl[4] = '02';
    ppl[5] = '00';
    ppl[6] = '07';
    ppl[7] = '01';
    ppl[8] = '00';
    ppl[9] = '04';
    ppl[10] = '11';
    ppl[11] = '00';
    ppl[12] = '05';
    ppl[13] = '09';
    ppl[14] = '08';
    ppl[15] = '00';

    preload();
    startAnimate();

    $('#p00').find('div').each(function() {
        $(this).fadeOut('fast');
    }
    );

    $('#homepage_map').find('area').each(function(i) {
        $(this).mouseover(function() {
            var id = $(this).attr('id').replace('a', '');
            stopAnimate();
            showPerson(id, false);
        });
    });
    $('#homepage_container').mouseout(function() {
        t2 = setTimeout(function() {
            resetAll();
        }, 500);
    });
});

function preload() {
    var i = 1;
    for (i = 1; i <= 11; i++) {
        var file = i.toString();
        if (file.length == 1) {
            file = '0' + file;
        }   
        var preload = document.createElement('img');
        preload.src = 'images/home/p' + file +'.jpg';
        delete preload;
    }

}

function showPerson(id, animate) {
    clearTimeout(t);
    clearTimeout(t2);
   
    
    if (current_person && current_person != id) {
        hidePerson(current_person, animate);
    }
    current_person = id;

    //$('#country').html(id);
   
    if (id.toString() == '00') {
        $('#banner_container').fadeTo(speed, 1);
        $('#p00').fadeTo(speed, 1);
    }
    else {
        
        person_selected = true;
        if (animate) {
            $('#p00').fadeTo(speed, 0.4);
            $('#b' + id).fadeIn(speed);
        }
        else {
            $('#p00').css({ opacity: '0.4' });
            $('#b' + id).css({ display: 'block' });
        }
        $('#b' + id).mouseout(function() {
            person_selected = false;
            t2 = setTimeout(function() {
                resetAll();
            }, 200);
        });
        t = setTimeout(function() {
            $('#banner_container').css({ opacity: '0.1' });
            $('#c' + id).fadeIn(speed);
        }, 200);
    }
}

function resetAll() {
    if (!person_selected) {
        clearTimeout(t);
        if (current_person) {
            hidePerson(current_person, true);
        }
        $('#banner_container').css({ opacity: '1.0' });
        $('#p00').css({ opacity: '1.0' });
        current_index = 0;
        startAnimate();
    }
}

function hidePerson(id, animate) {
    if (animate) {
        $('#b' + id).fadeOut(speed);
        $('#c' + id).fadeOut(speed);
    }
    else {
        $('#b' + id).css({ display: 'none' });
        $('#c' + id).css({ display: 'none' });
    }
}

function startAnimate() {
    stopAnimate();
    animator = window.setInterval(function() {
        changeSlide();
    }, animateStartDelay);
}

function stopAnimate() {
    if (animator) {
        clearInterval(animator);
        animator = null;
    }
    if (slideTimer) {
        clearInterval(slideTimer);
        slideTimer = null;
    }
}

function changeSlide() {
    stopAnimate();
    showPerson(ppl[current_index], true);
    current_index++;
    if (current_index > 15) {
        current_index = 0;
        //return;
    }
    slideTimer = window.setInterval(function() {
        changeSlide()
    }, slideTime);
}