/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var NUMBER_OF_IMGS = 0;
var TEXT_CONTAINER_WIDTH = 600;
var fadeValue = 0;
var fadeInElement = new Object();
var fadeOutElement = new Object();
var fadeLock = false;
fadeInElement.fadeValue = 0;
fadeInElement.timeoutID = 0;
fadeInElement.innerElements = null;
fadeOutElement.fadeValue = 100;
fadeOutElement.timeoutID = 0;
fadeOutElement.innerElements = null;
var text_to_display = new Object();
var textSlide;
var slide;
var currentImage = 0;
var moveGalleryIntervalId = 0;
var moveTextIntervalId = 0;
var galleryCurrentPosition = 0;
var textCurrentPosition = 0;
var galleryXTarget = 0;
var textXTarget;
var navigationBar = new Object();
var containerDimensions = new Object();
var timerID = 0;
var isPageAlreadyVisited = false;


/*
 * function for page preloading
 *
 **/


function preloadPage(pageId) {
    
    if(navigator.appName == "Microsoft Internet Explorer") {
         document.location = "explorer/explorer_warning.php";
    } else {
        if(false){
            var isImgOfGallery = false;
            var temp;
            var index = 0;
            var inner_index = 0;
            // Checking into cookies for browser cache
            if (document.cookie) {
                var cookies = document.cookie.split(';');
                for(index = 0; index < cookies.length; index ++) {
                    temp = cookies[index].split("=");
                    if (temp[0] == document.location && temp[1] == 1) {
                        isPageAlreadyVisited = true;
                    }
                }
            }
            // If this page has not been visited yet i preload its images
            if (!isPageAlreadyVisited) {
                var imgsToLoad = new Array(document.images.length);
                var srcs = document.getElementsByTagName("img");
                // Gets all the images of the tocuments and preloads them.
                // If an image is part of the gallery, it will be loaded by the gallery.
                for (index = 0; index < document.images.length; index++) {
                    imgsToLoad[index] = new Image();
                    temp = srcs[index].getAttribute("src").split("/");
                    for (inner_index = 0; inner_index < temp.length; inner_index++) {
                        isImgOfGallery = isImgOfGallery || (temp[inner_index] == "photogallery");
                    }
                    if (!isImgOfGallery) {
                        imgsToLoad[index].src = srcs[index].getAttribute("src");
                    }
                    isImgOfGallery = false;
                }
                document.cookie[0] = document.location+"="+1;
            }
        }
        isPageAlreadyVisited = false;
        snowcampFadeIn(pageId, 10);
    }
}

/*
 * function for image sliding
 *
 **/
function initializeGallery(gallery_width, gallery_height, button_prev_id, button_next_id, image_slide_id) {
    // setup width
    if(gallery_width <= 0) {
        containerDimensions.w = document.width;
        containerDimensions.resizable = true;
    } else {
        containerDimensions.w = gallery_width;
        containerDimensions.resizable = false;
    }
    // setup height
    if(gallery_height <= 0) {
        containerDimensions.h = document.height;
        containerDimensions.resizable = true;
    }else {
        containerDimensions.h = gallery_height;
        containerDimensions.resizable = false;
    }
    // associate image slide motion with buttons
    document.getElementById(button_prev_id).onclick = function() {
        moveImageSlideLeft(image_slide_id);
    }
    document.getElementById(button_next_id).onclick = function () {
        moveImageSlideRight(image_slide_id);
    }
    navigationBar.isLocked = false;
    setNumberOfImages();
}

function setNumberOfImages() {
    var index = 0;
    var temp;
    var isImgOfGallery = false;
    var srcs = document.getElementsByTagName("img");
    for (index = 0; index < document.images.length; index++) {
        temp = srcs[index].getAttribute("src").split("/");
        for (inner_index = 0; inner_index < temp.length; inner_index++) {
            isImgOfGallery = isImgOfGallery || (temp[inner_index] == "photogallery");
        }
        if (isImgOfGallery) {
            NUMBER_OF_IMGS++;
        }
        isImgOfGallery = false;
    }
}

/* called in html for each image of the photogallery */
function loadImage(src) {
    if (!isPageAlreadyVisited) {
        var img = new Image();
        img.src = src;
    }
}


/* left arrow function */
function moveImageSlideLeft(id) {
    if(!navigationBar.isLocked && currentImage > 0) {
        navigationBar.isLocked = true;
        galleryXTarget += containerDimensions.w;
        slide = document.getElementById(id);
        currentImage--;
        if(currentImage == NUMBER_OF_IMGS-4) {
            snowcampFadeIn("arrow_r", 10);
        } else if (currentImage == 0){
            snowcampFadeOut("arrow_l", -10, false);
        }
        moveGalleryIntervalId = setInterval(moveGallery, 10, slide, galleryXTarget);
    }
    
}

/* right arrow function */
function moveImageSlideRight(id) {
    if(!navigationBar.isLocked && currentImage < NUMBER_OF_IMGS-3) {
        navigationBar.isLocked = true;
        galleryXTarget -= containerDimensions.w;
        slide = document.getElementById(id);
        currentImage++;
        if(currentImage == 1) {
            snowcampFadeIn("arrow_l", 10);
        } else if(currentImage == NUMBER_OF_IMGS-3) {
            snowcampFadeOut("arrow_r", -10, false);
        }
        moveGalleryIntervalId = setInterval(moveGallery, 10, slide, galleryXTarget);
    }
}


function moveGallery(element, x) {
    if(galleryCurrentPosition >= (x - 1) && galleryCurrentPosition <= (x + 1)) {
        galleryCurrentPosition = x;
        element.style.left = galleryCurrentPosition + "px";
        navigationBar.isLocked = false;
        clearInterval(moveGalleryIntervalId);
    } else {
        galleryCurrentPosition += (x - galleryCurrentPosition) * 0.3;
        element.style.left = galleryCurrentPosition + "px";
    }
}

/* behavior of the main text content */
function snowcampSwitchText(id) {
    var menu_items = document.getElementsByName("text_menu");
    textSlide = document.getElementById("text_slide");
    var i = 0;
    while (i < menu_items.length) {
        if(menu_items[i].getAttribute("id") == id) {
            textXTarget = -i * TEXT_CONTAINER_WIDTH;
            textSlide.style.left = textXTarget + "px";
        }
        menu_items[i].setAttribute("class", "entry");
        menu_items[i].style.display = "block";
        menu_items[i].style.textDecoration = "none";
        i++;
    }
    var button_pressed = document.getElementById(id);
    button_pressed.setAttribute("class", "current");
    button_pressed.style.display = "block";
    button_pressed.style.textDecoration = "none";
}

function moveText(element, x) {
    if(textCurrentPosition <= (x - 1) && textCurrentPosition >= (x + 1)) {
        textCurrentPosition = x;
        clearInterval(moveTextIntervalId);
    }
    else if(textCurrentPosition >= (x - 1) && textCurrentPosition <= (x + 1)) {
        textCurrentPosition = x;
        navigationBar.isLocked = false;
        clearInterval(moveTextIntervalId);
    } else {
        textCurrentPosition += (x - textCurrentPosition) * 0.3;
        element.style.left = textCurrentPosition + "px";
    }
}

/* hide/show with fadeIn/fadeOut scripts */
/* fadeInSpeed between 1 and 100 */
function snowcampFadeIn(id, fadeInSpeed) {
    if (fadeInSpeed < 1 && fadeInSpeed > 100){
        fadeInSpeed = 10;
    }
    fadeInElement = document.getElementById(id);
    fadeInElement.innerElements = fadeInElement.childNodes;
    if(!fadeLock) {
        fadeLock = true;
        clearTimeout(fadeInElement.timeoutID);
        fadeInElement.timeoutID = 0;
        fadeInElement.style.opacity = "0";
        fadeInElement.style.visibility = "visible";
        fadeInElement.fadeValue = 0;
        fadeInIntervalId = setInterval(snowcampFade, 1, fadeInElement, fadeInSpeed);
    } else {
        fadeInElement.timeoutID = setTimeout(snowcampFadeIn, 10, id, fadeInSpeed);
    }
}

/* fadeOutSpeed between -100 and -1 */
function snowcampFadeOut(id, fadeOutSpeed, destroy) {
    if (fadeOutSpeed < -100 && fadeOutSpeed > -1){
        fadeOutSpeed = -10;
    }
    fadeOutElement = document.getElementById(id);
    if(!fadeLock) {
        fadeLock = true;
        clearTimeout(fadeOutElement.timeoutID);
        fadeOutElement.timeoutID = 0;
        fadeOutElement.fadeValue = 100;
        fadeOutIntervalId = setInterval(snowcampFade, 1, fadeOutElement, fadeOutSpeed, destroy);
    } else {
        fadeOutElement.timeoutID = setTimeout(snowcampFadeOut, 150, id, fadeOutSpeed, destroy);
    }
}

function snowcampFade(element, fadeSpeed, destroy) {
    element.fadeValue += fadeSpeed;
    element.style.opacity = (element.fadeValue)/100;
    if (element.fadeValue >= 100) {
        fadeLock = false;
        clearInterval(fadeInIntervalId);
    } else if (element.fadeValue <= 0) {
        element.style.opacity = "0";
        element.style.visibility = "hidden";
        fadeLock = false;
        clearInterval(fadeOutIntervalId);
    }
}
