
cachedImages = new Array();
black = new Array();

filenames = new Array();
filenames["img_matwork"] = "./graphics/shapes/pilates_white.png";
filenames["text_matwork"] = "./graphics/texts/matwork_white.png";
filenames["div_matwork"] = "./graphics/menu_photos/matwork.jpg";
filenames["img_allegro"] = "./graphics/shapes/pilates_white.png";
filenames["text_allegro"] = "./graphics/texts/allegro_white.png";
filenames["div_allegro"] = "./graphics/menu_photos/allegro.jpg";
filenames["img_rehabilitation"] = "./graphics/shapes/pilates_white.png";
filenames["text_rehabilitation"] = "./graphics/texts/rehabilitation_white.png";
filenames["div_rehabilitation"] = "./graphics/menu_photos/rehabilitation.jpg";
filenames["img_praevention"] = "./graphics/shapes/pilates_white.png";
filenames["text_praevention"] = "./graphics/texts/praevention_white.png";
filenames["div_praevention"] = "./graphics/menu_photos/praevention.jpg";



function loadCached(id) {
    if (!cachedImages[id]) {
        cachedImages[id] = new Image();
        cachedImages[id].src = filenames[id];
    }
}

function preLoad() {
    for (var id in filenames) {
        loadCached(id);
    }
}

function mouseOver(id) {
    imgId = "img_" + id;
    img = document.getElementById(imgId);
	
	
    if (img) {
		loadCached(imgId);
        black[imgId] = img.src;
        img.src = cachedImages[imgId].src;
    }

    textId = "text_" + id;
    text = document.getElementById(textId);

    if (text) {
        loadCached(textId);
        black[textId] = text.src;
        text.src = cachedImages[textId].src;
    }

    divId = "div_" + id;
    div = document.getElementById(divId);
	
    if (div) {
        loadCached(divId);
        div.style.backgroundImage = "url(" + cachedImages[divId].src + ")"; 
		}    
}

function mouseClick(id, link) {
    mouseOut(id);
    if (link != null)
        window.location.href = link;
}

function mouseOut(id) {
    imgId = "img_" + id;
    img = document.getElementById(imgId);
    if (img) {
        img.src = black[imgId];
    }

    textId = "text_" + id;
    text = document.getElementById(textId);
    if (text) {
        text.src = black[textId];
    }  
    
    divId = "div_" + id;
    div = document.getElementById(divId);

    if (div) {
        div.style.backgroundImage = "";
    } 
}


function setOpacity(id, opacity) {
    var el = document.getElementById(id);
    if (el) {
        var style = el.style;
        var op = opacity * 0.01;
        style.opacity = op;
        style.MozOpacity = op;
        style.KhtmlOpacity = op;
        style.filter = "alpha(opacity=" + opacity + ")";       
    }
}

function animateOpacity(id, opacityStart, opacityEnd, duration) {
    if (opacityStart == opacityEnd)
        return;

    var frameCount = 40;
    var frameDuration = Math.round(duration / frameCount);
    var frameIdx = 0;

    var change = (opacityEnd - opacityStart) / frameCount;
    var i = opacityStart;
    do {
        setTimeout("setOpacity('" + id + "', "+i+")", (frameIdx++) * frameDuration);
        i += change;       
    }while (i != opacityEnd+change)
}

function switchOpacity(id, duration) {
    var el = document.getElementById(id);
    if (el) {
        if (el.style.opacity == 0)
            animateOpacity(id, 0, 100, duration);
        else
            animateOpacity(id, 100, 0, duration);
    }
}

function blendIn(imageId, duration) {
    var theDiv = document.getElementById("changerDiv");
    var theImg = document.getElementById("changerImg");

    if (theDiv && theImg) {
        theDiv.style.backgroundImage = "url(" + theImg.src + ")";
        setOpacity("changerImg", 0);

        loadCached(imageId);
        theImg.src = filenames[imageId];

        animateOpacity("changerImg", 0, 100, duration);
        var otherImageId = "";
        if (imageId == "changer1")
            otherImageId = "changer2";
        else if (imageId == "changer2")
            otherImageId = filenames["changer3"] != null ? "changer3" : "changer1";
        else if (imageId == "changer3")
            otherImageId = filenames["changer4"] != null ? "changer4" : "changer1";
        else if (imageId == "changer4")
            otherImageId = filenames["changer5"] != null ? "changer5" : "changer1";
        else if (imageId == "changer5")
            otherImageId = "changer1";        
            
       // var otherImageId = imageId == "changer1" ? "changer2" : "changer1";

        setTimeout("blendIn('"+otherImageId+"', " + duration + ");", 2*duration);
    }
}


var scrollTimer;
function divScroll(id, direction) {
    clearTimeout(scrollTimer);
    d = document.getElementById(id)

    if (direction == -1) {
        y = d.scrollTop;
        if (y < d.scrollHeight - d.offsetHeight) {
            y += 3;
            d.scrollTop = y;
            scrollTimer = setTimeout("divScroll('" + id + "',-1)", 50);
        }
        else {
            clearTimeout(scrollTimer);
        }        
    }
    if (direction == 1) {
        y = d.scrollTop;
        if (y > 0) {
            y -= 3;
            d.scrollTop = y;
            scrollTimer = setTimeout("divScroll('" + id + "',1)", 50);
        }

        else {
            clearTimeout(scrollTimer);
        }        
    }     

}

function endScroll() {
    clearTimeout(scrollTimer);
}
