﻿$(document).ready(function () {

    var needsZ = false;
    var seventhZ = new Array(0, 2, 2, 2, 2, 0, 2, 2);
    var fifthZ = new Array(2, 2, 2, 220, 2);

    var randomNumber = $("#randomSet").attr('data-randomset');
    if (randomNumber == 5) {
        $('#meetHero').css('z-index', 3);
        $('#sellHead iframe').css('z-index', 500);
        $('#sellHead p').css('z-index', 500);
    }

    switch (randomNumber) {
        case "1":
            callLoadImage(1, 4);
            firstSet(4);
            break;
        case "2":
            callLoadImage(2, 7);
            secondSet(7);
            break;
        case "3":
            callLoadImage(3, 7);
            thirdSet(7);
            break;
        case "4":
            callLoadImage(4, 5);
            fourthSet(5);
            break;
        case "5":
            callLoadImage(5, 4);
            fifthSet(4);
            needsZ = true;
            break;
        case "6":
            callLoadImage(6, 5);
            sixthSet(5);
            break;
        case "7":
            callLoadImage(7, 7);
            seventhSet(7);
            needsZ = true;
            break;
        case "8":
            callLoadImage(8, 7);
            eigthSet(7);
            break;
        case "9":
            callLoadImage(9, 6);
            ninthSet(6);
            break;
        case "10":
            callLoadImage(10, 6);
            tenthSet(6);
            break;  
        case "11":
            callLoadImage(11, 6);
            eleventhSet(6);
            break;
        case "12":
            callLoadImage(12, 6);
            twelfthSet(6);
            break;
    }

    function callLoadImage(set, max) {
        for (var i = 1; i < max + 1; i++) {
            loadImage(set, i);
        };
    }

    function loadImage(set, number) {

        var img = new Image();

        //wrap the image in jQuery

        $(img)
            .load(function () {

                $(this).hide();

                $('#meetHero').append(this);
            })

            .error(function () {
                console.log("Darn you error");
            })
            //.attr('src', '/images/meet-anim/' + set + '/' + number + '.png')
            .attr('src', 'http://onceuponachild.com.s3.amazonaws.com/meet-anim/' + set + '/' + number + '.png')
            .attr('id', 'heroImage' + number)
            .addClass("hero");
    }







    function firstSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(2000, 1500, 1000, 1000);
        showImage(1, delay, max, 1);
    }

    function secondSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(2000, 1500, 1000, 1000, 1000, 1000, 1000);
        showImage(1, delay, max, 5);
    }

    function thirdSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(2000, 1500, 1000, 1000, 1000, 1000, 1000);
        showImage(1, delay, max, 3);
    }

    function fourthSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(2000, 1500, 1000, 1000, 1000);
        showImage(1, delay, max, 2);
    }

    function fifthSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(2000, 1500, 1000, 1000, 1000);
        showImage(1, delay, max, 1);
    }

    function sixthSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(1000, 3000, 1000, 1000, 1000, 1000);
        showImage(1, delay, max, 2);
    }

    function seventhSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(1000, 1500, 1000, 1000, 1000, 1000, 1000, 1000);
        showImage(1, delay, max, 3);
    }

    function eigthSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(1000, 500, 500, 500, 500, 1000, 1000, 1000);
        showImage(1, delay, max, 5);
    }

    function ninthSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(1000, 1000, 1000, 500, 500, 500, 500);
        showImage(1, delay, max, 3);
    }

    function tenthSet(max) {
        //Currently this is stupid so numbers in array equal this (5th, 1st, 2nd, 3rd, 4th)   
        delay = new Array(1000, 1000, 1000, 1000, 1000, 1000, 1000);
        showImage(1, delay, max, 4);
    }

    function eleventhSet(max) {
        //Currently this is (1st, 2nd, 3rd, 4th, 5th, 6th)   
        delay = new Array(1000, 1500, 1500, 500, 500, 500, 500);
        showImage(1, delay, max, 3);
    }

    function twelfthSet(max) {
        //Currently this is (1st, 2nd, 3rd, 4th, 5th, 6th)   
        delay = new Array(1000, 1000, 1000, 500, 500, 500, 500);
        showImage(1, delay, max, 3);
    }



    function showImage(num, delay, max, goTo) {

        var timeoutId = setInterval(
            function () {
                var i = checkImage(num);

                if (i) {
                    //Hiding image that was showing and showing the one that is there
                    clearInterval(timeoutId);
                    magic(num, delay, max, goTo);
                }
            }, 500);

    }

    function magic(num, delay, max, goTo) {

        setTimeout(function () {
            $('.current').hide();
            $('.current').removeClass('current');

            $('#heroImage' + num).show().addClass('current');

            if (needsZ) {
                if (randomNumber == 7) {
                    $('#meetHero').css('z-index', seventhZ[num]);

                    if (num == 4) {
                        $('#sellHead iframe').css('z-index', 1);
                        $('#sellHead p').css('z-index', 1);
                    }
                    else {
                        $('#sellHead iframe').css('z-index', 500);
                        $('#sellHead p').css('z-index', 500);
                    }
                }
                else if (randomNumber == 5) {
                    $('#meetHero').css('z-index', fifthZ[num]);
                }

            }


            if (num === max)
                num = goTo;
            else
                num++;

            showImage(num, delay, max, goTo);

        }, delay[num - 1]);
    }


    function checkImage(num) {
        var img = $('#heroImage' + num);

        if (img.length === 0)
            return false;
        else
            return true;
    }

});



