// Script for sliding images to the bottom of the page //
// Version V1.0.0, 06/10/2011 WBM.
// Copyright 2011. Property of Teamwork 
//

// Declare globals
var sttimer = 0
var origopacity = 100;
var revopacity = 0;
var newopacity = 0;
var numpositioned = 0;
var menuopacity = 0;

// Images
var objImage = new Array(8);
var objImageCopy = new Array(8);
var objImageName = new Array(8);
objImageName[0] = 'blackboards';
objImageName[1] = 'teamawayday';
objImageName[2] = 'sadteenager';
objImageName[3] = 'facilitation';
objImageName[4] = 'twentyfourhour';
objImageName[5] = 'challengedays';
objImageName[6] = 'testimonials';
objImageName[7] = 'theteam';
var objImageScalar = new Array(8);

// Image start positions
var startx = new Array(8);
var starty = new Array(8);
var imagex = new Array(8);
var imagey = new Array(8);
var posx = new Array(8);
var posy = new Array(8);
var screenHeight = 0;

// Offset timers - this defines the order and delay that the images fall
var arrStart = new Array();
arrStart[0] = 600;
arrStart[1] = 1800;
arrStart[2] = 4200;
arrStart[3] = 3000;
arrStart[4] = 1200;
arrStart[5] = 100;
arrStart[6] = 2400;
arrStart[7] = 3600;

// Set the horizontal position of the final image
var objImageStop = new Array(8);
objImageStop[0] = 300;
objImageStop[1] = 450;
objImageStop[2] = 640;
objImageStop[3] = 830;
objImageStop[4] = 360;
objImageStop[5] = 530;
objImageStop[6] = 720;
objImageStop[7] = 910;

// Timers for running the image slides
var arrTimer = new Array(8);

// Target height for the image (resizing)
var targheight = 70;

// Distance moved and frequency timer
var movey = 2;
var freq = 2;

self.setTimeout('fctRollover()',40000);

// Slide image to bottom of page 
function fctRollover() {
    // Used if sliding to bottom of document
    //screenHeight = fctDocHeight();
    
    // Used if sliding to the bottom of the screen
    screenHeight = fctScreenHeight();
    
    // Position the final menubar image
    document.getElementById("footerimageindex").style.top = (parseInt(screenHeight) - targheight - 30) + "px";
    
    // Factor in any scrolling already done on this page
    screenHeight += fctScrolledY();
    
    // Start the image slider
    fctSlider();
    
    // Fade out the original text
    self.setTimeout('fctFadeOutText()', 1000);
    
    // Fade in the new text
    self.setTimeout('fctFadeInText()', 8000);
}

// Slide image to bottom of page 
function fctSlider() {
	// Loop through each image and check if to start routine
    for (index = 0; index < 8; index++) {
        // Check if this image is due to start - time must be exact otherwise images in progress will be restarted
        if (arrStart[index] == sttimer) {
            //alert(index)
            // Create objects for the original and duplicate images
            objImage[index] = document.getElementById(objImageName[index]);
            objImageCopy[index] = document.getElementById(objImageName[index] + 'copy');
            
            // Find the current position of the image
            startx[index] = posx[index] = fctGetXoffset(objImage[index]);
            starty[index] = posy[index] = fctGetYoffset(objImage[index]);
            //alert(starty[index])
            
            // Set the start position of the slider image
            objImageCopy[index].style.left = startx[index] + 'px';
            objImageCopy[index].style.top = starty[index] + 'px';
        	
            // Show the slider image and hide the original
            objImageCopy[index].style.visibility = 'visible';
            objImage[index].style.visibility = 'hidden';
            
            // Get the dimensions of the image
            imagey[index] = objImage[index].height;
            //alert(imagey[index])
            
            // Calculate the scalar for this image - this is the relation between vertical and horizontal travel
            objImageScalar[index] = (objImageStop[index] - startx[index]) / (screenHeight - targheight - starty[index]);
            //alert(objImageScalar[index])
            
            // Call the respective function for this image
            switch (index) {
                case 1:
                    fctSlideImage1();
                    break;
                case 2:
                    fctSlideImage2();
                    break;
                case 3:
                    fctSlideImage3();
                    break;
                case 4:
                    fctSlideImage4();
                    break;
                case 5:
                    fctSlideImage5();
                    break;
                case 6:
                    fctSlideImage6();
                    break;
                case 7:
                    fctSlideImage7();
                    break;
                default:
                    fctSlideImage0();
            }    
        }
    }
    
    // Check again after a short delay
    sttimer += 10;
    self.setTimeout("fctSlider()", 10);
}

// Find the vertical position (offset) for the specified object
function fctGetYoffset(objThis) {
	// Set default as top of the page
	var intPosY = 0;
    
	// Check that this browser supports object offsets
	if (objThis.offsetParent) {
		// Loop whie the current object has a parent
		do {
			// Add the offset of the current object to the running totals
			intPosY += objThis.offsetTop;
		} while (objThis = objThis.offsetParent);
	}
	
	// Return the vertical offset for this object
	return intPosY;
}

// Find the horizontal position (offset) for the specified image
function fctGetXoffset(objThis) {
	// Set default as top of the page
	var intPosX = 0;
    
	// Check that this browser supports object offsets
	if (objThis.offsetParent) {
		// Loop whie the current object has a parent
		do {
			// Add the offset of the current object to the running totals
			intPosX += objThis.offsetLeft;
		} while (objThis = objThis.offsetParent);
	}
	
	// Return the vertical offset for this object
	return intPosX;
}

// Get the document height - this is used to determine the final position of the image
function fctDocHeight() {
    // Create document objects
    var db = document.body;
    var dde = document.documentElement;
    
    // Get the total height of the document
    var docHeight = Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight)
    return docHeight;
}

// Slide image 0
function fctSlideImage0() {
    // Check if this image has reached the target
    if (posy[0] < (screenHeight - targheight)) {
        // Move the image
        posy[0] += movey;
        objImageCopy[0].style.top = posy[0] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[0] - starty[0];
        objImageCopy[0].style.left = (startx[0] + (movedy * objImageScalar[0])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[0] = self.setTimeout("fctSlideImage0()", freq);
    }
    else {
        fctResizeImage0();
    }
}

// Slide image 1
function fctSlideImage1() {
    // Check if this image has reached the target
    if (posy[1] < (screenHeight - targheight)) {
        // Move the image
        posy[1] += movey;
        objImageCopy[1].style.top = posy[1] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[1] - starty[1];
        objImageCopy[1].style.left = (startx[1] + (movedy * objImageScalar[1])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[1] = self.setTimeout("fctSlideImage1()", freq);
    }
    else {
        fctResizeImage1();
    }
}

// Slide image 2
function fctSlideImage2() {
    // Check if this image has reached the target
    if (posy[2] < (screenHeight - targheight)) {
        // Move the image
        posy[2] += movey;
        objImageCopy[2].style.top = posy[2] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[2] - starty[2];
        objImageCopy[2].style.left = (startx[2] + (movedy * objImageScalar[2])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[2] = self.setTimeout("fctSlideImage2()", freq);
    }
    else {
        fctResizeImage2();
    }
}

// Slide image 3
function fctSlideImage3() {
    // Check if this image has reached the target
    if (posy[3] < (screenHeight - targheight)) {
        // Move the image
        posy[3] += movey;
        objImageCopy[3].style.top = posy[3] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[3] - starty[3];
        objImageCopy[3].style.left = (startx[3] + (movedy * objImageScalar[3])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[3] = self.setTimeout("fctSlideImage3()", freq);
    }
    else {
        fctResizeImage3();
    }
}

// Slide image 4
function fctSlideImage4() {
    // Check if this image has reached the target
    if (posy[4] < (screenHeight - targheight)) {
        // Move the image
        posy[4] += movey;
        objImageCopy[4].style.top = posy[4] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[4] - starty[4];
        objImageCopy[4].style.left = (startx[4] + (movedy * objImageScalar[4])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[4] = self.setTimeout("fctSlideImage4()", freq);
    }
    else {
        fctResizeImage4();
    }
}

// Slide image 5
function fctSlideImage5() {
    // Check if this image has reached the target
    if (posy[5] < (screenHeight - targheight)) {
        // Move the image
        posy[5] += movey;
        objImageCopy[5].style.top = posy[5] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[5] - starty[5];
        objImageCopy[5].style.left = (startx[5] + (movedy * objImageScalar[5])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[5] = self.setTimeout("fctSlideImage5()", freq);
    }
    else {
        fctResizeImage5();
    }
}

// Slide image 6
function fctSlideImage6() {
    // Check if this image has reached the target
    if (posy[6] < (screenHeight - targheight)) {
        // Move the image
        posy[6] += movey;
        objImageCopy[6].style.top = posy[6] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[6] - starty[6];
        objImageCopy[6].style.left = (startx[6] + (movedy * objImageScalar[6])) + 'px';
        
        // Recall this function after a short delay
        arrTimer[6] = self.setTimeout("fctSlideImage6()", freq);
    }
    else {
        fctResizeImage6();
    }
}

// Slide image 7
function fctSlideImage7() {
    // Check if this image has reached the target
    if (posy[7] < (screenHeight - targheight)) {
        // Move the image vertically
        posy[7] += movey;
        objImageCopy[7].style.top = posy[7] + 'px';
        
        // Set the horizontal position of the image
        movedy = posy[7] - starty[7];
        objImageCopy[7].style.left = (startx[7] + (movedy * objImageScalar[7])) + 'px';
        //alert(objImageCopy[7].style.left)
        
        // Recall this function after a short delay
        arrTimer[7] = self.setTimeout("fctSlideImage7()", freq);
    }
    else {
        fctResizeImage7();
    }
}


// Resize image 0
function fctResizeImage0() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[0].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[0].height - 2) / objImageCopy[0].height;
       
        // Shrink the image
        objImageCopy[0].height = (objImageCopy[0].height * imagescalar);
        objImageCopy[0].width = (objImageCopy[0].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[0] = self.setTimeout("fctResizeImage0()", 10);
    }
    else {
        //objImageCopy[0].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 1
function fctResizeImage1() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[1].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[1].height - 2) / objImageCopy[1].height;
        
        // Shrink the image
        objImageCopy[1].height = (objImageCopy[1].height * imagescalar);
        objImageCopy[1].width = (objImageCopy[1].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[1] = self.setTimeout("fctResizeImage1()", 10);
    }
    else {
        //objImageCopy[1].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 2
function fctResizeImage2() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[2].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[2].height - 2) / objImageCopy[2].height;
        
        // Shrink the image
        objImageCopy[2].height = (objImageCopy[2].height * imagescalar);
        objImageCopy[2].width = (objImageCopy[2].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[2] = self.setTimeout("fctResizeImage2()", 10);
    }
    else {
        //objImageCopy[2].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 3
function fctResizeImage3() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[3].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[3].height - 2) / objImageCopy[3].height;
        
        // Shrink the image
        objImageCopy[3].height = (objImageCopy[3].height * imagescalar);
        objImageCopy[3].width = (objImageCopy[3].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[3] = self.setTimeout("fctResizeImage3()", 10);
    }
    else {
        //objImageCopy[3].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 4
function fctResizeImage4() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[4].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[4].height - 2) / objImageCopy[4].height;
        
        // Shrink the image
        objImageCopy[4].height = (objImageCopy[4].height * imagescalar);
        objImageCopy[4].width = (objImageCopy[4].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[4] = self.setTimeout("fctResizeImage4()", 10);
    }
    else {
        //objImageCopy[4].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 5
function fctResizeImage5() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[5].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[5].height - 2) / objImageCopy[5].height;
        
        // Shrink the image
        objImageCopy[5].height = (objImageCopy[5].height * imagescalar);
        objImageCopy[5].width = (objImageCopy[5].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[5] = self.setTimeout("fctResizeImage5()", 10);
    }
    else {
        //objImageCopy[5].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 6
function fctResizeImage6() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[6].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[6].height - 2) / objImageCopy[6].height;
        
        // Shrink the image
        objImageCopy[6].height = (objImageCopy[6].height * imagescalar);
        objImageCopy[6].width = (objImageCopy[6].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[6] = self.setTimeout("fctResizeImage6()", 10);
    }
    else {
        //objImageCopy[6].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// Resize image 7
function fctResizeImage7() {
    // Check if this image has reached the target height
    if (targheight < objImageCopy[7].height) {
        // Calculate scalar for resizing the image
        imagescalar = (objImageCopy[7].height - 2) / objImageCopy[7].height;
        
        // Shrink the image
        objImageCopy[7].height = (objImageCopy[7].height * imagescalar);
        objImageCopy[7].width = (objImageCopy[7].width * imagescalar);
        
        // Recall this function after a short delay
        arrTimer[7] = self.setTimeout("fctResizeImage7()", 10);
    }
    else {
        //objImageCopy[7].style.visibility = 'hidden';
        numpositioned++;
        if (numpositioned >= objImageName.length) fctShowMenubar();
    }
}

// All the images are at their final position and need replacing with the menubar
function fctShowMenubar() {
    menuopacity += 5;
    
    // Fade out the images and fade in the menubar
    objImageCopy[0].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[0].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[1].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[1].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[2].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[2].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[3].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[3].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[4].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[4].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[5].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[5].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[6].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[6].style.opacity = (100 - menuopacity) / 100;
    objImageCopy[7].style.filter = 'alpha(opacity=' + (100 - menuopacity) + ')';
    objImageCopy[7].style.opacity = (100 - menuopacity) / 100;
    
    // Fade in the menubar
    document.getElementById("footerimage").style.filter = 'alpha(opacity=' + menuopacity + ')';
    document.getElementById("footerimage").style.opacity = (menuopacity / 100);
    document.getElementById("footerimageindex").style.visibility = 'visible';
    
    // Recall the function after a short time
    if (menuopacity < 100) self.setTimeout('fctShowMenubar()', 50);
}

// Fade out original text
function fctFadeOutText() {
    // Check the current opacity of the original text
    if (origopacity > 0) {
        // Fade the text out
        origopacity -= 2;
        
        // Loop through all page links and fade them out
        for (var index = 0; index < objImageName.length; index++) {
            //document.getElementById(objImageName[index] + "-link").style.opacity = parseInt(origopacity / 100);
            //document.getElementById(objImageName[index] + "-link").style.filter = 'alpha(opacity=' + origopacity + ')';
            //alert(objImageName[index])
            document.getElementById(objImageName[index] + "-link").style.filter = 'alpha(opacity=' + origopacity + ')';
            document.getElementById(objImageName[index] + "-link").style.opacity = origopacity / 100;
        }

        self.setTimeout("fctFadeOutText()", 100);
    }
    else {
        // Loop through all page links and fade them out
        for (var index = 0; index < objImageName.length; index++) {
            // Hide the text completely
            document.getElementById(objImageName[index] + '-link').style.visibility = 'hidden';
        }
    }
}

// Fade in new text
function fctFadeInText() {
    // Check the current opacity of the original text
    if (revopacity < 100) {
        // Fade the text out
        revopacity += 2;
        
        // Fade in replacement
        document.getElementById("replacetext").style.filter = 'alpha(opacity=' + revopacity + ')';
        document.getElementById("replacetext").style.opacity = revopacity / 100;
    }
    document.getElementById("replacetext").style.visibility = 'visible';

    self.setTimeout("fctFadeInText()", 100);
}

function fctFader(thisObject, thisOpacity) {
 if (navigator.appName.indexOf("Netscape")!=-1 &&parseInt(navigator.appVersion)>=5) {
    thisObject.style.MozOpacity=opacity/100
 }
 else if (navigator.appName.indexOf("Microsoft")!= -1 && parseInt(navigator.appVersion)>=4) {
    thisObject.filters.alpha.opacity=50
 }
}

// Get the height of the visible screen
function fctScreenHeight() {
    var screenHeight = 0;
    
    // Test browser type
    if (typeof(window.innerHeight) == 'number') {
        // Non-IE browser
        screenHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        // IE browser in standards compliant mode
        screenHeight = document.documentElement.clientHeight;
    }
    else if (document.body && document.body.clientHeight) {
        // IE browser in non-compliant mode
        screenHeight = document.body.clientHeight;
    }
    
    // Return the screen height
    return screenHeight;
}

// Get the height of the visible screen
function fctScrolledY() {
    var scrolledY = 0;
    
    // Test browser type
    if (typeof(window.innerHeight) == 'number') {
        // Non-IE browser
        scrolledY = window.pageYOffset;
    }
    else if (document.body && document.body.scrollTop) {
        // DOM compliant mode
        scrolledY = document.body.scrollTop;
    }
    else if (document.body && document.body.clientHeight) {
        // IE6 standards compliant mode
        scrolledY = document.documentElement.scrollTop;
    }
    
    // Return the vertical scroll amount
    return scrolledY;
}
