﻿var pEvents;
var pWeddings;
function initProjectors() {
    // Static property used by all Projector objects to display images - ie, they all project onto the same big screen
    Projector.screen = new SwapImageFader("Feature");

    pEvents = new Projector();
    pEvents.load("Events", calcFilmLeftEdge(1), 100);
    //            pEvents.stepsize = 1;
    //            pEvents.glidesize = 40;
    //            pEvents.slidesize = 80;

    pWeddings = new Projector();
    pWeddings.load("Weddings", calcFilmLeftEdge(3), 100);
    //            pWeddings.glidesize = 70;
    //            pWeddings.glidefriction = 0.02;
    //            pWeddings.slidesize = 80;
    pWeddings.stepsize = 2;

    pBirthdays = new Projector();
    pBirthdays.load("Birthdays", calcFilmLeftEdge(4), 100);
    pBirthdays.stepsize = 3;

    pCelebrations = new Projector();
    pCelebrations.load("Celebrations", calcFilmLeftEdge(2), 100);

    if (document.body.addEventListener) {  // DOM Level 2 event model
        // Register capturing event handlers
        document.body.addEventListener("mousemove", mouseUnder.trackMouse, true);
    }
    else if (document.body.attachEvent) {  // IE 5+ Event Model
        // using Level 0 API rather than attachEvent() so that the handler knows the element to which it is registered
        // IE has no currentTarget equivalent, so we use the 'this' keyword instead.
        // 'this' will point to the element on which the handler is registered when using the Level 0 API
        document.body.onmousemove = mouseUnder.trackMouse;
    }

    document.getElementById("switch").onclick = switchClick;
    document.getElementById("switchHandle").onclick = switchClick;
}

var switch1;
function startPetals() {
    switch1 = new Switch(document.getElementById('fallImage'));
    var petalLoop1 = new Petals('fallImage', 'switch', switch1);
    switch1.petal = petalLoop1;
    petalLoop1.petalFallStart();
}

function pictureShow() {
    pEvents.startRollDown(0, 2);
    pWeddings.startRollDown(1, 4);
    pBirthdays.startRollDown(2, 3);
    pCelebrations.startRollDown(3);
    //pEvents.slideTo(5, 400);
    pEvents.glideAway(5);
    pWeddings.glideAway(8);
    pBirthdays.glideAway(6);
    pCelebrations.glideAway(7);

}

function glideAwayAll() {
    buttons["bEvents"].remoteOff();
    buttons["bWeddings"].remoteOff();
    buttons["bBirthdays"].remoteOff();
    buttons["bCelebrations"].remoteOff();
//    pEvents.glideAway();
//    pWeddings.glideAway();
//    pBirthdays.glideAway();
//    pCelebrations.glideAway();
}

function allProjectorsOff() {
    return pEvents.off() && pWeddings.off() && pBirthdays.off() && pCelebrations.off();
}

function switchClick() {
    if (allProjectorsOff()) {
        switch1.turnOn();
        pictureShow();
    }
    else {
        switch1.turnOff();
        glideAwayAll();
    }
}

function updateSwitch() {
    if (switch1 != undefined) {
        if (allProjectorsOff())
            switch1.turnOff();
        else
            switch1.turnOn();
    }
}

function preloadImages() {
    var preloader1 = new Preloader1();
    var preloader2 = new Preloader2();
    var preloader3 = new Preloader3();
    preloader1.oncompletion = preloader2.preload;
    preloader2.oncompletion = function() { preloader3.preload(); startPetals(); };
    preloader1.preload();
}

function Preloader1() {
    var that = this;
    // create an image element into which all images will be preloaded
    this.preloadElement = document.createElement('img');
    this.preloadElement.id = "componentPreload1";
    this.oncompletion = null;

    // create an array with all images paths to be preloaded so preload function can step through it one at a time.
    this.preloadList = [];
    this.preload = function() {
        var next = that.preloadList.pop();
        if (next) {
            that.preloadElement.src = that.preloadList.pop();
        }
        else // we've completed loading everything from the list
            that.oncompletion();
    };

    this.preloadElement.onload = that.preload;
    this.preloadElement.onerror = that.preload;
    this.preloadElement.onabort = that.preload;

    for (var i = 1; i < 31; i++) {
        var imageFile = "images/Switch Petal/cp" + i + ".png";
        this.preloadList.push(imageFile);
    }
}


function Preloader2() {
    var that = this;
    // create an image element into which all images will be preloaded
    this.preloadElement = document.createElement('img');
    this.preloadElement.id = "componentPreload2";
    this.oncompletion = null;

    // create an array with all images paths to be preloaded so preload function can step through it one at a time.
    this.preloadList = [];
    this.preload = function() {
        var next = that.preloadList.pop();
        if (next) {
            that.preloadElement.src = that.preloadList.pop();
        }
        else // we've completed loading everything from the list
            that.oncompletion();        
    };

    this.preloadElement.onload = that.preload;
    this.preloadElement.onerror = that.preload;
    this.preloadElement.onabort = that.preload;

    // preload switch images
    this.preloadList.push("images/Switch Petal/fallOff1.png");
    this.preloadList.push("images/Switch Petal/fallOff2.png");
    this.preloadList.push("images/Switch Petal/fallOff3.png");
    this.preloadList.push("images/Switch Petal/fallOff4.png");
    this.preloadList.push("images/Switch Petal/fallOff5.png");
    this.preloadList.push("images/Switch Petal/fallOff6.png");
    this.preloadList.push("images/Switch Petal/fallOff7.png");
    this.preloadList.push("images/Switch Petal/fallOff8.png");
    this.preloadList.push("images/Switch Petal/fallOn1.png");
    this.preloadList.push("images/Switch Petal/fallOn2.png");
    this.preloadList.push("images/Switch Petal/fallOn3.png");
    this.preloadList.push("images/Switch Petal/fallOn4.png");
    this.preloadList.push("images/Switch Petal/fallOn5.png");
    this.preloadList.push("images/Switch Petal/fallOn6.png");
    this.preloadList.push("images/Switch Petal/fallOn7.png");
    this.preloadList.push("images/Switch Petal/Switch - Off.jpg");
    this.preloadList.push("images/Switch Petal/Switch - On.jpg");
    this.preloadList.push("images/Switch Petal/SwitchbodyOnOverOff.png");
    this.preloadList.push("images/Switch Petal/SwitchHandleDown.png");
    this.preloadList.push("images/Switch Petal/SwitchHandleMidway.png");
    this.preloadList.push("images/Switch Petal/SwitchHandleUp.png");
    this.preloadList.push("images/Button Labels/Weddings-H.png");
    this.preloadList.push("images/Button Labels/Weddings-S.png");
    this.preloadList.push("images/Button Labels/Weddings-SH.png");
    this.preloadList.push("images/Button Labels/Weddings.png");
    this.preloadList.push("images/Button Labels/Events-H.png");
    this.preloadList.push("images/Button Labels/Events-S.png");
    this.preloadList.push("images/Button Labels/Events-SH.png");
    this.preloadList.push("images/Button Labels/Events.png");
    this.preloadList.push("images/Button Labels/Celebrations-H.png");
    this.preloadList.push("images/Button Labels/Celebrations-S.png");
    this.preloadList.push("images/Button Labels/Celebrations-SH.png");
    this.preloadList.push("images/Button Labels/Celebrations.png");
    this.preloadList.push("images/Button Labels/Birthdays-H.png");
    this.preloadList.push("images/Button Labels/Birthdays-S.png");
    this.preloadList.push("images/Button Labels/Birthdays-SH.png");
    this.preloadList.push("images/Button Labels/Birthdays.png");
}

function Preloader3() {
    var that = this;
    // create an image element into which all images will be preloaded
    this.preloadElement = Projector.screen.oldimageElement.cloneNode(true);
    this.preloadElement.id = "preload3";
    this.preloadElement.style.zIndex--;
    Projector.screen.oldimageElement.parentNode.appendChild(this.preloadElement);

    // create an array with all images paths to be preloaded so preload function can step through it one at a time.
    this.preloadList = [];
    this.preload = function() {
        var next = that.preloadList.pop();
        if (next) {
            that.preloadElement.src = that.preloadList.pop();
        }
    };

    this.preloadElement.onload = that.preload;
    this.preloadElement.onerror = that.preload;
    this.preloadElement.onabort = that.preload;

    for (var film in Filmstrip.collection) {
        var f = Filmstrip.collection[film];
        for (var i = 0; i < f.frames.length; i++) {
            this.preloadList.push(f.frames[i].trueMainImage);
        }
        for (var i = 0; i < f.frameQueue.length; i++) {
            this.preloadList.push(f.frameQueue[i].trueMainImage);
        }
    }

    this.preloadList.push("images/homepage image 400x540.jpg");
    this.preloadList.push("images/2255_girl_and_calf_1020.jpg");
    this.preloadList.push("images/flour-8d14161r.jpg");
    this.preloadList.push("images/1790_walker_garden_420.jpg");
    this.preloadList.push("images/milk-8a12137r.jpg");
    this.preloadList.push("images/cake-8b06233r.jpg");
    this.preloadList.push("images/13147_mcdonell_farm_1020.jpg");
    this.preloadList.push("images/2276_taking_a_break_1020.jpg");
    this.preloadList.push("images/16115_workers_w_crop_1020.jpg");
    this.preloadList.push("images/16115_workers_w_crop_1020.jpg");
    this.preloadList.push("images/treesnowview.jpg");
}
