//backgroundPath = 'images/';
backgroundPath = 'wp-content/themes/nagashimatoft/style/images/';


fadeoutTimer = 1;
animationImgTimer = null;
currentPage = 1;
listFlipTimer = 80;
listFlipPause = 240;
randomBleed = 25;
listObj = null;

function init() {
    selectBg();

    if (pageID == '19') postIt();
    if (pageID == '15') scheduleIt();
}

function scheduleIt() {
}

function postIt() {
    cont = document.getElementById('container');
    if (cont != null) {
        cont.style.overflow = 'visible'; //add scrollbars to content
    }

    txtc = document.getElementById('text_content');
    if (txtc != null) {
        list = txtc.getElementsByTagName('ol');
        if (list.length == 0) {
            list = txtc.getElementsByTagName('ul');
            if (list.length == 0) {
                return false;
            }
        }

        listObj = list[0];
        listObj.style.visibility = 'hidden';
        bgNum = 1;

        newDiv = document.createElement('div');
        newDiv.id = 'postit_0';
        newDiv.className = 'postIt_picture';
        txtc.appendChild(newDiv);

        newDiv = document.createElement('div');
        newDiv.id = 'postit_buttonzzz';
        newDiv.className = 'postIt_buttons';
        txtc.appendChild(newDiv);


        liObjs = listObj.getElementsByTagName('li');

        for (x = 0; x < liObjs.length; x++) {
            newDiv = document.createElement('div');
            newDiv.id = 'postit_' + (x + 1);

                // add extra div so the post it can scroll (overflow auto)
                contentDiv = document.createElement('div');
                contentDiv.innerHTML = liObjs[x].innerHTML;
                        
            newDiv.appendChild(contentDiv);
            newDiv.className = 'postIt_' + bgNum;

            if (x > 2) newDiv.style.display = 'none'; // only show post-its on 1st page - paging will handle the rest

            rndPos = randomPosition();
            newDiv.style.marginLeft = rndPos[0] + 'px';
            newDiv.style.marginTop = rndPos[1] + 'px';
            txtc.appendChild(newDiv);
            
            bgNum += 1;
            if (bgNum > 4) bgNum = 1;
        }

        if (liObjs.length > 2) { // paging
            newLink = document.createElement('a');
            newLink.className = 'arrow_left';
            newLink.href = 'javascript:prevPostIts();';
            txtc.appendChild(newLink);

            newLink = document.createElement('a');
            newLink.className = 'arrow_right';
            newLink.href = 'javascript:nextPostIts();';
            txtc.appendChild(newLink);
        }

    }
}

function randomPosition() {
    x1 = Math.random() * randomBleed;
    y1 = Math.random() * randomBleed;

    x2 = Math.random() * (randomBleed * -1);
    y2 = Math.random() * (randomBleed * -1);
    
    return [Math.round(x1 + x2), Math.round(y2 + y2)];
}


function nextPostIts() {

    listMax = currentPage * 4;
    if (listMax <= listObj.getElementsByTagName('li').length) {
        currentPage += 1;

        rep = 1;
        for (x = (listMax - 4); x < listMax; x++) {
            obj = document.getElementById('postit_' + x);
            if (obj != null) {
                window.setTimeout('document.getElementById(\'postit_' + x + '\').style.display = \'none\';', rep * listFlipTimer);
                rep += 1;
            }
        }

        rep = 1;
        for (x = listMax; x < (listMax + 4); x++) {
            obj = document.getElementById('postit_' + x);
            if (obj != null) {
                window.setTimeout('document.getElementById(\'postit_' + x + '\').style.display = \'block\';', (rep * listFlipTimer) + listFlipPause);
                rep += 1;
            }
        }
    }
}


function prevPostIts() {

    listMax = currentPage * 4;
    if (listMax > 4) {
        currentPage -= 1;

        rep = 1;
        for (x = listMax; x >= (listMax - 4); x--) {
            obj = document.getElementById('postit_' + x);
            if (obj != null) {
                window.setTimeout('document.getElementById(\'postit_' + x + '\').style.display = \'none\';', rep * listFlipTimer);
                rep += 1;
            }
        }

        rep = 1;
        for (x = (listMax - 8); x < (listMax - 4); x++) {
            obj = document.getElementById('postit_' + x);
            if (obj != null) {
                window.setTimeout('document.getElementById(\'postit_' + x + '\').style.display = \'block\';', (rep * listFlipTimer) + listFlipPause);
                rep += 1;
            }
        }
    }
}

function selectBg() {
    containerDiv = document.getElementById('container');
    if (containerDiv != null) {
        if (bgName.length == 0) bgName = 'contact';

        containerDiv.style.backgroundImage = 'url(' + backgroundPath + 'bg-' + bgName + '.jpg)';
    }
}

function fadeOutSplash(doFade) {
    splashDiv = document.getElementById('splash');
    if (splashDiv != null) {
        b = 0;
        for (a = 10; a > -1; a--) {
            animationImgTimer = window.setTimeout('document.getElementById(\'splash\').style.filter = \'alpha(opacity=' + (a * 10) + ')\';', b * 70);
            animationImgTimer = window.setTimeout('document.getElementById(\'splash\').style.opacity = ' + a / 10 + ';', b * 70);
            b += 1;
        }
        animationImgTimer = window.setTimeout('document.getElementById(\'splash\').style.display = \'none\';', (b + 1) * 70);
    }
}




window.onload = function() {
    init();
}

