﻿/** Exempelkod för hur ett slideshowobjekt ska se ut

articleSlideshowArray = []; // Array med objekt

articleSlideshowArray[0] = new Object();
articleSlideshowArray[0].id = "SlideshowArticle0"; // artikelobjektets id
articleSlideshowArray[0].content = "ingresstext";
articleSlideshowArray[0].image = new Image();
articleSlideshowArray[0].image.alt = "bild-alt-text"
articleSlideshowArray[0].image.title = "bild-title-text"
articleSlideshowArray[0].image.src = "bild-url"

**/

// Skapar ett slideshow-objekt (kräver jQuery)
// bigImageId: id-värdet för den stora bilden som visas (img-element)
// puffingId: id-värdet för ingress-elementet (div-element, null om det inte används)
// articleArray: referens till den array som innehåller artikelobjekten
// playButtonSrc: sökväg till bilden för play-knappen
// pauseButtonSrc: sökväg till bilden för paus-knappen
// counterId: id till element som tar emot det aktuella bildnumret (index + 1)
function Slideshow(bigImageId, puffingId, articleArray, playButtonSrc, pauseButtonSrc, counterId,w,h,thickboxLinkID) {
    this.currentSlideshowArticleIndex = 0;
    this.bigImageId = bigImageId;
    this.puffingId = puffingId;            
    this.articleArray = articleArray;
    this.timer = null;
    this.thumbRangeStart = null;
    this.thumbRangeLength = null;            
    this.counterId = counterId;
    
    if (playButtonSrc !== null) {
        this.playButton = new Image();
        this.playButton.src = playButtonSrc;
        this.playButton.alt = "Play-knapp";
        this.playButton.title = "Starta bildspel";
    }
    
    if (pauseButtonSrc !== null) {
        this.pauseButton = new Image();
        this.pauseButton.src = pauseButtonSrc;
        this.pauseButton.alt = "Pause-knapp";
        this.pauseButton.title = "Stoppa bildspel";    
    }
    
    var inst = this;
    
    // Visar en viss artikel (om dir inte är en int) ELLER går till nästa om dir > 0 eller föregående om dir <= 0
    this.show = function(dir) {
        var previousArticle = inst.articleArray[inst.currentSlideshowArticleIndex];
        var slideshowArticle = null;
        if (previousArticle !== null) {
            $("#" + previousArticle.id + "_thumb").removeClass("Current");                            
        }            

        if (isNaN(dir)) {
            // Anta att dir motsvarar idt för artikeln som ska visas,
            for (var i = 0; i < inst.articleArray.length; i++) {
                if (inst.articleArray[i].id == dir) {
                    slideshowArticle = inst.articleArray[i];
                    inst.currentSlideshowArticleIndex = i;
                }
            }
        } else {
            if (dir > 0) {
                inst.currentSlideshowArticleIndex = inst.currentSlideshowArticleIndex + 1 < inst.articleArray.length ? inst.currentSlideshowArticleIndex + 1 : 0;
            } else {
                inst.currentSlideshowArticleIndex = inst.currentSlideshowArticleIndex - 1 >= 0 ? inst.currentSlideshowArticleIndex - 1 : inst.articleArray.length - 1;
            }
            slideshowArticle = inst.articleArray[inst.currentSlideshowArticleIndex];

//console.log(slideshowArticle.image);
//console.log(inst);

        }            
                    
        if (slideshowArticle !== null) {  
            if (inst.puffingId) {
                $("#" + String(inst.puffingId)).html(slideshowArticle.content);
            }
            if (inst.bigImageId) {                                                
                if (slideshowArticle.imagePreloadSrc) {
                    slideshowArticle.image.src = slideshowArticle.imagePreloadSrc;
                    slideshowArticle.imagePreloadSrc = null;
                }    
                
                $("#" + String(inst.bigImageId)).attr({
                    alt: slideshowArticle.image.alt, 
                    title: slideshowArticle.image.title, 
                    width: slideshowArticle.image.width,
                    height: slideshowArticle.image.height,
                    src: slideshowArticle.image.src});     
                    
                    //alert(slideshowArticle.image.width);
                   document.getElementById(thickboxLinkID).href=slideshowArticle.image.src;
                   //alert(document.getElementById(thickboxLinkID).href);
	                //document.getElementById(inst.bigImageId).height=slideshowArticle.image.height;
	                

    var newwidth,newheight;
       

                      
    widthRatio = slideshowArticle.image.width / w; 
    heightRatio = slideshowArticle.image.height / h;


    if (widthRatio > heightRatio)
    {
        //Anpassa efter bredd
        if (slideshowArticle.image.width >= w)
        {
            newwidth = parseInt(slideshowArticle.image.width / widthRatio+0.5);
            newheight = parseInt( slideshowArticle.image.height / widthRatio+0.5);
        }
        else
        {
            newwidth = slideshowArticle.image.width;
            newheight =  slideshowArticle.image.height;    
        }        
    }
    else
    {
        //Anpassa efter höjd
        if (slideshowArticle.image.height >= h)
        {
            newwidth = parseInt(slideshowArticle.image.width / heightRatio+0.5);
            newheight = parseInt(slideshowArticle.image.height / heightRatio+0.5);
        }
        else
        {
            newwidth = slideshowArticle.image.width;
            newheight =  slideshowArticle.image.height;    
        }        
}
	                
	                        document.getElementById(inst.bigImageId).width=newwidth;
	                document.getElementById(inst.bigImageId).height=newheight;          
	                
                    
                    
                                                
            }
            $("#" + slideshowArticle.id + "_thumb").addClass("Current");     
        }
        
        if (inst.counterId !== null) {
            $("#" + inst.counterId).html(inst.currentSlideshowArticleIndex + 1);            
        }
    };
    
    // Byter innehållet i puff-elementet mot den aktiva artikelns puff
    this.swapPuffContent = function(elementId, positionId) {
        var articleObject = inst.getArticleObject(elementId);
        var currentObject = inst.currentArticle();
        if (articleObject !== null && currentObject !== null) {
            inst.setPuffContent(positionId, currentObject.id);
            inst.show(articleObject.id);
        }
    };
    
    this.setPuffContent = function(targetId, elementId) {
        var articleObject = inst.getArticleObject(elementId);
        if (articleObject !== null) {
            $("#" + targetId).html(articleObject.puffHtml);            
        }
    };
    
    // Hämtar ett object utifrån ett visst id
    this.getArticleObject = function(elementId) {
        for (i = 0; i < inst.articleArray.length; i++) {                
            if (inst.articleArray[i].id == elementId) {
                return inst.articleArray[i];
            }
        }        
        return null;
    };
    
    // Startar eller stoppar det automatiska bildspelet
    // delay: hur länge varje artikel ska visas (minst 1000 ms)
    // valfri parameter - button: referens till play-knappen så att ikonen kan bytas för att reflektera statusen på bildspelet
    this.toggleAuto = function(delay) {
        if (inst.timer === null) {            
            if (arguments.length > 1) {
                inst.start(delay, arguments[1]);
            } else {
                inst.start(delay);
            }
        } else {            
            if (arguments.length > 1) {
                inst.stop(arguments[1]);
            } else {
                inst.stop();
            }
        }
    };
    
    // Startar bildspelet
    this.start = function(delay) {
        inst.delay = Math.max(1000, delay);
        inst.timer = setInterval(inst.auto, inst.delay);
        if (arguments.length > 1) {
            var buttonElement = arguments[1];
            if (buttonElement !== null) {
                buttonElement.src = this.pauseButton.src;
                buttonElement.alt = this.pauseButton.alt;
                buttonElement.title = this.pauseButton.title;
            }
        }
    };
    
    // Stoppar bildspelet
    this.stop = function() {
        clearInterval(inst.timer);
        inst.timer = null;
        if (arguments.length > 0) {
            var buttonElement = arguments[0];
            if (buttonElement !== null) {
                buttonElement.src = this.playButton.src;
                buttonElement.alt = this.playButton.alt;
                buttonElement.title = this.playButton.title;
            }
        }
    };
    
    // Anger hur många tumnaglar som ska visas
    // index: startindex (0 är första tumnageln)
    // length: anger hur många tumnaglar som ska visas
    this.setThumbRange = function(index, length) {
        index = parseInt(index, 10);
        length = parseInt(length, 10);
        inst.thumbRangeStart = isNaN(index) ? null : Math.max(0, index);
        inst.thumbRangeLength = isNaN(length) ? null : Math.max(0, length);                             
    };
    
    this.setThumbnailVisibility = function(start, length) {        
        if (start !== null && length !== null) {
            start = parseInt(start, 10);
            length = parseInt(length, 10);
            if (isNaN(start) || isNaN(length)) {
                return null;
            }
            
            var array = inst.articleArray;            
            for (var i = 0; i < inst.articleArray.length; i++) {
                var thumbnailId = String(inst.articleArray[i].id) + "_thumb";
                var element = document.getElementById(thumbnailId);
                if (element !== null) {
                    if (i >= start && i < (start + length)) {                        
                        element.style.position = "static";
                        element.style.visibility = "visible";
                    } else {                        
                        element.style.position = "absolute";
                        element.style.visibility = "hidden";
                    }
                }                
            }
        }
    };
    
    // Callback-metod för det automatiska bildspelet
    this.auto = function() {
        inst.show(1);              
    };
    
    // Uppdaterar bildspels-blocket med den aktuella bilden
    this.update = function() {
        var slideshowObj = inst.articleArray[inst.currentSlideshowArticleIndex];
        if (slideshowObj !== null) {
            inst.show(slideshowObj.id);
        }
        
        inst.setThumbnailVisibility(inst.thumbRangeStart, inst.thumbRangeLength, true);
    };
    
    // Returnerar indexet för den aktuella bilden (0-baserat)
    this.currentImage = function() {
        return inst.currentSlideshowArticleIndex;
    };
    
    this.currentArticle = function() {
        return inst.articleArray[inst.currentSlideshowArticleIndex];
    };
}

function SetPagingStyle(active,id)
{

    for(i=0;i<shImageCount;i++)
    {
        document.getElementById('shPaging'+i).style.fontWeight="normal";
    }
    if(active==1)
    {
        document.getElementById(id).style.fontWeight="bold";
    }
    else
    {
        document.getElementById(id).style.fontWeight="normal";
    }
}