/*
 * Copyright 2007 ScanRate Financial Systems A/S
 * Henning C. Nielsen 20070606
 *
 * Logic for navigating slide show.
 */

var currentSlide = 0;
var slideCount = 0;
var slideShowMS = 5000;
var slideShowRunning = false;
var extension = '.png';

//Run init when the page loads.
addOnLoadEvent(init);

/* Initialize vars if that is possible.
 */
function init() {
    var slide_count = document.getElementById('slide_count');
    if (slide_count != null)
    {
        slideCount = slide_count.innerHTML;
    }
    
    var slide_num = document.getElementById('slide_num');
    if (slide_num != null)
    {
        currentSlide = slide_num.innerHTML - 1;
    }
}

function next_slide(pic_id) {
    var pic = document.getElementById(pic_id);
    var slide_num = document.getElementById('slide_num');
    
    if (pic == null)
        return;
    
    currentSlide = (currentSlide + 1) % slideCount;
    changeSlide(pic_id, currentSlide);
    
    if (slide_num != null)
        slide_num.innerHTML = currentSlide + 1;
}

function previous_slide(pic_id) {
    var slide_num = document.getElementById('slide_num');
    
    currentSlide = (currentSlide - 1);
    
    if (currentSlide <= -1)
        currentSlide = slideCount - 1;
        
    changeSlide(pic_id, currentSlide);
    
    if (slide_num != null)
        slide_num.innerHTML = currentSlide + 1;
}

function changeSlide(pic_id, number) {
    var pic = document.getElementById(pic_id);
    
    if (pic == null)
        return;
        
    pic.src = 'Presentation/Slide' + number + extension;
    
    if (number == slideCount - 1) {
        addImageMap(
            'Slide10',
            'mailto:fairvalue@telekurs.com',
            '285,530,680,575',
            'Send a mail to Telekurs Financial'
        );
    }
    else if (pic.attributes.getNamedItem('usemap') != null) {
        pic.attributes.removeNamedItem('usemap');
    }
}

function toggleSlideShow(pic_id) {
    var link = document.getElementById('toggle_slideshow');
    slideShowRunning = !slideShowRunning;
    
    if (slideShowRunning) {
        if (currentSlide >= slideCount - 1)
            currentSlide = -1;
        
        link.innerHTML = 'Stop slideshow';
        slideShow(pic_id);
    }
    else {
        link.innerHTML = 'Start slideshow';
    }
}

function slideShow(pic_id) {
    if (slideShowRunning) {
        if (currentSlide < slideCount)
            next_slide(pic_id);
        
        if (currentSlide < slideCount - 1)
            setTimeout("slideShow('" + pic_id + "')", slideShowMS);
    }
    
    if (currentSlide >= slideCount - 1)
        stopSlideShow();
}

function stopSlideShow() {
    var link = document.getElementById('toggle_slideshow');
    
    slideShowRunning = false;
    link.innerHTML = 'Start slideshow';
}

function addImageMap(id, href, coords, title) {
    //The image to add the image map to. This is hardcoded for starters.
    var img = document.getElementById('slideImage');
    
    if (img == null)
        return;
    
    //Create the elements and configure them.
    var map = document.createElement('map');
    map.id = id;
    map.name = id;
    
    var area = document.createElement('area');
    area.shape = 'rect';
    area.alt = title;
    area.title = title;
    area.coords = coords;
    area.href = href;
    
    var defaultArea = document.createElement('area');
    var nohref = document.createAttribute('nohref');
    nohref.value = 'nohref';
    defaultArea.shape = 'default';
    defaultArea.attributes.setNamedItem(nohref);
    
    //Add the areas to the map element.
    map.appendChild(area);
    map.appendChild(defaultArea);
    
    //Add the map to the block where the image is.
    img.parentNode.appendChild(map);
    //Tell the image to use an image map.
    var usemap = document.createAttribute('usemap');
    usemap.value = '#' + id;
    img.attributes.setNamedItem(usemap);
}