﻿$(document).ready(function () {
    i = 1;
    myText = "Inspiring the whole within you.";
    distance = 40;

    $("#showMe" + i).fadeIn("slow").animate({ "left": "+=" + distance + "px", opacity: "1" }, 3000);
    i++;
});

function nextPoemLine() {
    if (i > 1 && i < 9) {
        x = (i - 1);
        whichShowMe = "#showMe" + x;
        $(whichShowMe).fadeTo(1000, 0.1).fadeOut(0);
    }
    if (i < 9) {
        speed = 4000;

        switch (i) {
            case 2:
                myText = "See whole.";
                distance = 270;
                speed = 6000;
                break;
            case 3:
                myText = "Be whole.";
                distance = 285;
                speed = 6000;
                break;
            case 4:
                myText = "Live whole.";
                distance = 265;
                speed = 6000;
                break;
            case 5:
                myText = "Making all of you matter.";
                distance = 100;
                break;
            case 6:
                myText = "Allowing whole moves.";
                distance = 130;
                break;
            case 7:
                myText = "Integrating the whole.";
                distance = 140;
                break;
            case 8:
                myText = "The center of whole. You.";
                distance = 110;
                break;
        }

        $("#showMe" + i).delay(1000).fadeIn("slow").animate({ "left": "+=" + distance + "px", opacity: "1" }, speed);
        i++;
    }
    else {
        clearInterval(myInterval);
    }
}

var myInterval = setInterval("nextPoemLine()", 10000);



/*** start slide functionality ***/
var s1 = false;
var s2 = false;
var increment = 3;
var startPos = 1;
var endPos = 1;
var direction = 'up';
var clickWait = false;
var ccw = null;

$(document).ready(function () {
    endPos = $('.sidebar DIV.active').length;
    updatePosition();

    //setInterval('changePlaces()', 8000);

});

function registerClick() {
    clickWait = true;
    if (ccw != null) {
        clearTimeout(ccw);
    }
    ccw = setTimeout('clearClickWait()', 12000);    
}

function clearClickWait() {
    // for browser compatibility
    clickWait = false;
}

function changePlaces() {
    if (clickWait == false) {       
        if (startPos == 1) {
            direction = "up";
        }
        else if (endPos == (parseInt($('.sidebar DIV').length))-1) {        
            direction = "down";
        }

        if(direction == "up") { 
            moveUp();    
        }    
        else if(direction=="down") { 
            moveDown();
        }
    }
}

function moveUp() {
    var $active = $('.sidebar DIV.active:last');

    if ($active.next().length > 0 && !s1 && !s2) {
        document.getElementById("sidebardown").src = "/images/homepage/arrowdown.png";
        s1 = true;
        s2 = true;  
        $next = $active.next()

        $('.sidebar DIV.active:first').css({ opacity: 1.0 }).animate({ opacity: 0.0 }, 90, null);
        $('.sidebar DIV.active:first').slideUp(500, function () { $('.sidebar DIV.active:first').removeClass('active'); s1 = false; });
        $next.slideDown(500, function () { s2 = false; }).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 350, null).addClass('active');

        if ($('.sidebar DIV.active:last').next().length == 0) {
            document.getElementById("sidebarup").src = "/images/homepage/arrowup_deactivated.png";
        }

        resetPositionUp(); 
    } 
}

function resetPositionUp() {

    if (!s1 && !s2) {
        if (endPos + 1 <= (parseInt($('.sidebar DIV').length) - parseInt(1))) {
            startPos++;
            endPos++;
            updatePosition();
        }
    }
    else {
        setTimeout('resetPositionUp()', 50);
    }
}

function moveDown() {

    var $active = $('.sidebar DIV.active:first');

    if ($active.prev().length > 0 && !s1 && !s2) {
        document.getElementById("sidebarup").src = "/images/homepage/arrowup.png";
        s1 = true;
        s2 = true;
        $next = $active.prev()

        $('.sidebar DIV.active:last').css({ opacity: 1.0 }).animate({ opacity: 0.0 }, 150, null);
        $('.sidebar DIV.active:last').slideUp(500, function () { $('.sidebar DIV.active:last').removeClass('active'); s1 = false; });
        $next.slideDown(500, function () { s2 = false; }).css({ opacity: 0.0 }).animate({ opacity: 1.0 }, 350, null).addClass('active');
        if ($('.sidebar DIV.active:first').prev().length == 0) {
            document.getElementById("sidebardown").src = "/images/homepage/arrowdown_deactivated.png";
        }

        resetPositionDown();
    }
}

function resetPositionDown() {

    if(!s1 && !s2) { 

        if (startPos - 1 >= 1) {
            startPos--;
            endPos--;
            updatePosition();
        }
    }
    else {
        setTimeout('resetPositionDown()', 50);
    }
}

function updatePosition() {
    var total = (parseInt($('.sidebar DIV').length) - parseInt(1));
    if (endPos + 1 > total) { endPos = total; }

    document.getElementById("sidebarpos").textContent = startPos + ' to ' + endPos + ' of ' + total;
}

/*** end slide functionality ***/

