//  Image types:
// 0 = person
// 1 = object
var currenthero = [{ "type": 0, "image": "heroC-02.jpg" }, { "type": 1, "image": "heroC-15.jpg" }, { "type": 0, "image": "heroC-14.jpg"}];
var herolist = [
{ "type": 0, "image": "heroC-01.jpg" }, { "type": 0, "image": "heroC-02.jpg" }, { "type": 0, "image": "heroC-03.jpg" },
{ "type": 0, "image": "heroC-04.jpg" }, { "type": 0, "image": "heroC-05.jpg" }, { "type": 0, "image": "heroC-06.jpg" },
{ "type": 1, "image": "heroC-07.jpg" }, { "type": 0, "image": "heroC-09.jpg" },
{ "type": 1, "image": "heroC-11.jpg" }, { "type": 1, "image": "heroC-12.jpg" },
{ "type": 0, "image": "heroC-13.jpg" }, { "type": 0, "image": "heroC-14.jpg" }, { "type": 1, "image": "heroC-15.jpg" },
{ "type": 0, "image": "heroC-16.jpg" }, { "type": 1, "image": "heroC-18.jpg" }, { "type": 0, "image": "heroC-20.jpg" },
{ "type": 0, "image": "heroC-21.jpg" }, { "type": 0, "image": "heroC-23b.jpg" }];
var sidehero = ["side-pictures1.jpg", "side-pictures2.jpg", "side-pictures3.jpg", "side-pictures6b.jpg"];
var currentside = "side-pictures1.jpg";
/* ScorePerfect */
var currentherosp = [{ "type": 0, "image": "heroC-02.jpg" }, { "type": 1, "image": "heroC-26c.jpg" }, { "type": 0, "image": "heroC-14.jpg"}];
var herolistsp = [
{ "type": 0, "image": "heroC-01.jpg" }, { "type": 0, "image": "heroC-02.jpg" }, { "type": 0, "image": "heroC-03.jpg" },
{ "type": 0, "image": "heroC-04.jpg" }, { "type": 0, "image": "heroC-05.jpg" }, { "type": 0, "image": "heroC-06.jpg" },
{ "type": 1, "image": "heroC-07.jpg" }, { "type": 0, "image": "heroC-09.jpg" },
{ "type": 1, "image": "heroC-11.jpg" }, { "type": 1, "image": "heroC-12.jpg" },
{ "type": 0, "image": "heroC-13.jpg" }, { "type": 0, "image": "heroC-14.jpg" }, 
{ "type": 0, "image": "heroC-16.jpg" },  { "type": 0, "image": "heroC-20.jpg" },
{ "type": 0, "image": "heroC-21.jpg" }, { "type": 0, "image": "heroC-23b.jpg" }, { "type": 1, "image": "heroC-26c.jpg" }];
var sideherosp = ["side-pictures2.jpg", "side-pictures6b.jpg"];
var currentsidesp = "side-pictures2.jpg";
var countperson = 0;
var lastpersonlist = new Array();
var lastswap = -1;
var $matrixlist;

function doHomeAnimationSP() {
    currenthero = currentherosp;
    sidehero = sideherosp;
    herolist = herolistsp;
    sidehero = sideherosp;
    currentside = currentsidesp;
    doHomeAnimation();
}

function doHomeAnimation() {
    $heroarea = $('#hero');
    $herolist = $('#hero .hero-block');

    for (idxhl = herolist.length - 1; idxhl >= 0; --idxhl) {
        if (herolist[idxhl].type == 0) {
            ++countperson;
        }
    }
    for (idxcur = currenthero.length - 1; idxcur >= 0; --idxcur) {
        if (currenthero[idxcur].type == 0) {
            lastpersonlist.push(currenthero[idxcur]);
        }
    }

    $hero1inside = $('#hero #block1 .inside');
    squares1 = getSquares($hero1inside.width(), $hero1inside.height(), 4, 5);
    addSquares($hero1inside, squares1, "fade");
    addSquaresBackground($hero1inside, squares1, "swap");

    $hero2inside = $('#hero #block2 .inside');
    squares2 = getSquares($hero2inside.width(), $hero2inside.height(), 4, 5);
    addSquares($hero2inside, squares2, "fade");
    addSquaresBackground($hero2inside, squares2, "swap");

    $hero3inside = $('#hero #block3 .inside');
    squares3 = getSquares($hero3inside.width(), $hero3inside.height(), 4, 5);
    addSquares($hero3inside, squares3, "fade");
    addSquaresBackground($hero3inside, squares3, "swap");

    $sideinside = $('#hero #side .inside');
    squaresi = getSquares($sideinside.width(), $sideinside.height(), 2, 3);
    addSquares($sideinside, squaresi, "fade");
    /*addSquares($sideinside, squaresi, "line");*/
    addSquaresBackground($sideinside, squaresi, "swap");

    $matrixlist = $('#hero .fade');
    $matrixlist.css('opacity', 0.01).show();
    $matrixlist.each(function () {
        //$(this).css('opacity', Math.random() * 0.25).show();
        $(this).delay(Math.floor(Math.random() * 12) * 50).fadeTo(400, Math.random() * 0.25);
    })
    setTimeout("doImageSwap()", 2000);
    setTimeout("fadeRandom()", 200);
}

function fadeRandom() {
    randidx = Math.floor(Math.random() * $matrixlist.length)
    $matrixlist.eq(randidx).fadeTo('slow', Math.random() * 0.25);
    setTimeout("fadeRandom()", 200);
}

function addSquares($target, squares, classname) {
    for (i = 0; i < squares.length; ++i) {
        $target.append("<div class='" + classname + "' style='top:" + squares[i].top + "px; left:" + squares[i].left + "px; width:" + squares[i].width + "px; height:" + squares[i].height + "px'></div>");
    }
}

function addSquaresBackground($target, squares, classname) {
    for (i = 0; i < squares.length; ++i) {
        $target.append("<div class='" + classname + "' style='top:" + squares[i].top + "px; left:" + squares[i].left + "px; width:" + squares[i].width + "px; height:" + squares[i].height + "px; background-position: -" + squares[i].left + "px -" + squares[i].top + "px'></div>");
    }
}

function getSquares(width, height, countx, county) {
    arrx = splitLine(width, countx);
    arry = splitLine(height, county);
    var items = new Array();
    for (y = 0; y < arry.length - 1; ++y) {
        for (x = 0; x < arrx.length - 1; ++x) {
            items.push({ "left": arrx[x], "top": arry[y], "width": arrx[x + 1] - arrx[x], "height": arry[y + 1] - arry[y] });
        }
    }
    return items;
}

function splitLine(length, count) {
    var list = new Array();
    var multiplier = (length * 1.0) / (count * 1.0);
    list[0] = 0;
    for (idx = 1; idx < count; ++idx) {
        list[idx] = Math.round(idx * multiplier);
    }
    list[count] = length;
    return list;
}

function doImageSwap() {
    if (Math.random() > 0.25)
        doImageSwapMain();
    else
        doImageSwapSide();
    setTimeout("doImageSwap()", 7000);
}

function doImageSwapMain() {
    // count how many people are displayed
    var peoplecount = 0;
    for (idx = currenthero.length-1; idx >= 0; --idx) {
        if (currenthero[idx].type == 0) {
            ++peoplecount;
        }
    }

    // pick an index based on what's displayed
    var swapindex;
    if(peoplecount<2)
    {
        swapindex = findSlotType(1);
    } else if(peoplecount>2)
    {
        swapindex = findSlotType(0);
    }
    else
    {
        swapindex = findSlotNormal();
    }
    lastswap = swapindex;

    var newhero;
    if(peoplecount<2) {
        newhero = findImagePerson();
    } else if(peoplecount>2) {
        newhero = findImageObject();
    } else {
        // if we're swapping an object, get another object
        if (currenthero[swapindex].type == 1) {
            newhero = findImageObject();
        }
        else {
            newhero = findImageAny();
        }
    }

    var newimage = "url('" + "http://cdn.www.testmasters.net/images/home/" + herolist[newhero].image + "')";
    currenthero[swapindex] = herolist[newhero];
    $swaphero = $herolist.eq(swapindex);

    doSwap($swaphero, newimage);
}

/* Find a normal slot */
function findSlotNormal() {
    var swapindex;
    do {
        swapindex = Math.floor(Math.random() * currenthero.length);
    } while (lastswap == swapindex);
    return swapindex;
}

/* find a slot of a type */
function findSlotType(slottype) {
    var swapindex;
    do {
        swapindex = Math.floor(Math.random() * currenthero.length);
    } while (lastswap == swapindex || currenthero[swapindex].type!=slottype);
    return swapindex;
}

/* finds any image */
function findImageAny() {
    if (Math.floor(Math.random() * herolist.length) < countperson) {
        return findImagePerson();
    }
    else {
        return findImageObject();
    }
}

function findImageObject() {
    return findImageType(1);
}

function findImagePerson() {
    var dupimage = false;
    do {
        var idxHero = findImageType(0);
        dupimage = isDupImage(idxHero);
    } while (dupimage);
    lastpersonlist.push(herolist[idxHero]);
    //showPersonList();
    return idxHero;
}

/* finds any image */
function findImageType(imagetype) {
    var newhero;
    var samehero = false;
    do {
        samehero = false;
        newhero = Math.floor(Math.random() * herolist.length);
        if (herolist[newhero].type != imagetype) {
            samehero = true;
            continue;
        }
        for (idx = currenthero.length - 1; idx >= 0; --idx) {
            if (currenthero[idx].image == herolist[newhero].image) {
                samehero = true;
            }
        }
    } while (samehero);
    return newhero;
}

function isDupImage(idxImage) {
    count = 8;
    var checkimage = herolist[idxImage].image;
    for (idxp = lastpersonlist.length-1; idxp >=0 && count>0; --idxp,--count) {
        if (lastpersonlist[idxp].image == checkimage) {
            return true;
        }
    }
    return false;
}

function doImageSwapSide() {
    var newhero;
    var samehero = false;
    do {
        samehero = false;
        newhero = Math.floor(Math.random() * sidehero.length);
       
        if (currentside == sidehero[newhero]) {
            samehero = true;
        }
        
    } while (samehero);

    var newimage = "url('" + "http://cdn.www.testmasters.net/images/home/" + sidehero[newhero] + "')";
    currentside = sidehero[newhero];
    $swaphero = $('#hero #side');

    doSwap($swaphero, newimage);
}

function doSwap($swaphero, newimage) {
    // get the squares
    $squares = $swaphero.find('.swap');
    $squares.css({ 'background-image': newimage, 'opacity': 0.01 }).show();
    var squarecount = $squares.length;
    fadeeach = 600 / squarecount;
    delaycount = 0;
    shufflelist = getRandomList(squarecount);
    for (isquare = squarecount; isquare >= 0; --isquare) {
        if (isquare == 0) {
            $squares.eq(shufflelist[isquare]).delay(delaycount).fadeTo(200, 1, function () {
                $swaphero.css('background-image', newimage);
                $squares.hide();
            });
        }
        else {
            $squares.eq(shufflelist[isquare]).delay(delaycount).fadeTo(200, 1);
        }
        delaycount += fadeeach;
    }
}

function positionBackground($boxlist) {
    $boxlist.each(function (i) {
        x = boxpositions[i].x;
        y = boxpositions[i].y
        $(this).css({ 'left': x + "px", 'top': y + "px" });
        mx = -x;
        my = -y;
        $(this).children().css({ 'backgroundPosition': " " + mx + "px " + my + "px" });
    });
}

function animateBox($box, delaytime, direction, oncomplete) {
    $boxinside = $box.find('.slidein');
    var effect;
    if (direction = 'fade') {
        $boxinside.css({ 'opacity': 0.01, 'marginTop': "0px", 'marginLeft': '0px' })
        $box.show();
        $boxinside.show().delay(delaytime).fadeTo(150, 1, oncomplete);
        return;
    }

    if (direction == 'down') {
        $boxinside.css({ 'marginTop': "-105px", 'marginLeft': '0px' });
        effect = { 'marginTop': "0px" };
    } else if (direction == 'left') {
        $boxinside.css({ 'marginLeft': "105px", 'marginTop': '0px' });
        effect = { 'marginLeft': "0px" };
    } else if (direction == 'up') {
        $boxinside.css({ 'marginTop': "105px", 'marginLeft': '0px' });
        effect = { 'marginTop': "0px" };
    } else if (direction == 'right') {
        $boxinside.css({ 'marginLeft': "-105px", 'marginTop': '0px' });
        effect = { 'marginLeft': "0px" };
    }
    $box.show();
    $boxinside.delay(delaytime).animate(effect, 150, oncomplete);
}

function showPersonList() {
    var text = "";
    for (idxp = 0; idxp < lastpersonlist.length; ++idxp) {
        text += lastpersonlist[idxp].image + " ";
    }
    $('#personlist').html(text);
}
