// document.write('<script type="text/javascript" src="script/prototype.js"><\/script>');
// document.write('<script type="text/javascript" src="script/scriptaculous.js?load=effects"><\/script>');
    
function get(id) {
        if (document.getElementById(id)) {
            return document.getElementById(id);
        }
    return false;
}

function Slideshow_Engine(settings) {
    this.settings = settings;
    this.transitionEffect = '';
    this.slideArray = settings['slideArray'];
    this.maxWidth = settings['maxWidth'];
    this.maxHeight = settings['maxHeight'];
    this.slideDuration = (typeof settings['slideDuration'] == 'undefined') ? 5000 : parseInt(settings['slideDuration']) * 1000;
    this.transitionDuration = (typeof settings['transitionDuration'] == 'undefined') ? 1 : settings['transitionDuration'];
    this.setTransitionEffect((typeof settings['transitionEffect'] == 'undefined') ? 'Fade' : settings['transitionEffect']);
    this.slideIdx = 0;
    this.status = 'new';
    this.scale = (typeof settings['scale'] == 'undefined') ? 1 : settings['scale'];
}

Slideshow_Engine.prototype = {
    
    setTransitionEffect : function(effect) {
       //uses scriptaculous effects to transition slides
       this.transitionEffect = effect;
        if (   this.transitionEffect == 'BlindDown'
            || this.transitionEffect == 'SlideDown'
            || this.transitionEffect == 'Grow')
        {
            this.slideToTransition = 'new';
        }
        else this.slideToTransition = 'old';
    },
    
    construct : function(parentID) {
        this.parentID = parentID;
        //must have a parent div element to place the slideshow in
        var parent = document.getElementById(parentID);
        //make sure we have a parent container
        if (typeof parent == 'undefined') {
            alert('Please create a div element to place the slide show in and provide the parentID property in the initialize call.');
            return;
        }
        if (parent.tagName != 'DIV') {
            alert('The container element must be a DIV');
            return;
        }
        //remove any child controls from the parent
        parent.innerHTML = '';
        //setup the parent styles
        parent.style.position = 'relative';
        var divMain = document.createElement('DIV'); //the main container
        //first slide
        var divSlide = document.createElement('DIV');
        var divContainer = document.createElement('DIV');
        var divTitle = document.createElement('DIV');
        var divImage = document.createElement('DIV');
        var divCaption = document.createElement('DIV');
        var img1 = document.createElement('IMG');
        var img2 = document.createElement('IMG');
        //set the id's
        divSlide.id = parentID + '_slide_1';
        divTitle.id = parentID + '_title_1';
        divImage.id = parentID + '_imagecontainer_1';
        divCaption.id = parentID + '_caption_1';
        img1.id = parentID + '_image_1';
        //set the styles and classes
        divMain.style.position = 'relative';
        divSlide.style.position = 'absolute';
        divSlide.style.display = 'none';
        divImage.style.width = Math.ceil(this.maxWidth * this.scale) + 'px';
        divImage.style.height = Math.ceil(this.maxHeight * this.scale) + 'px';
        //divImage.style.verticalAlign = 'top';
        
        divSlide.className = parentID + '_slide';
        divTitle.className = parentID + '_title';
        divImage.className = parentID + '_imagecontainer';
        divCaption.className = parentID + '_caption';
        img1.className = parentID + '_image';
        
        divTitle.innerHTML = this.slideArray[0][3];
        divCaption.innerHTML = this.slideArray[0][1];
        
        // *** KRISZO **/
        var numer = 1;
        var ostatni = this.slideArray.length;
        get('numer_zdjecia').innerHTML = numer;
        
        img1['onload'] = new Function ("Slideshow.transitionSlide(this)");
        divImage.appendChild(img1);
        divContainer.appendChild(divTitle);
        divContainer.appendChild(divImage);
        divContainer.appendChild(divCaption);
        divSlide.appendChild(divContainer);
        divMain.appendChild(divSlide);
        //second slide
        divSlide = document.createElement('DIV');
        divContainer = document.createElement('DIV');
        divTitle = document.createElement('DIV');
        divImage = document.createElement('DIV');
        divCaption = document.createElement('DIV');
        img2['onload'] = new Function ("Slideshow.transitionSlide(this)");
        divSlide.id = parentID + '_slide_2';
        divTitle.id = parentID + '_title_2';
        divImage.id = parentID + '_imagecontainer_2';
        divCaption.id = parentID + '_caption_2';
        img2.id = parentID + '_image_2';
        divSlide.style.position = 'absolute';
        divSlide.style.display = 'none';
        divImage.style.width = Math.ceil(this.maxWidth * this.scale) + 'px';
        divImage.style.height = Math.ceil(this.maxHeight * this.scale) + 'px';
        divSlide.className = parentID + '_slide';
        divTitle.className = parentID + '_title';
        divImage.className = parentID + '_imagecontainer';
        divCaption.className = parentID + '_caption';
        img2.className = parentID + '_image';
        divImage.appendChild(img2);
        divContainer.appendChild(divTitle);
        divContainer.appendChild(divImage);
        divContainer.appendChild(divCaption);
        divSlide.appendChild(divContainer);
        divMain.appendChild(divSlide);
        parent.appendChild(divMain);
        this.status = 'running';
        //set the image height, width and margins
        img1.style.width = Math.floor(this.slideArray[0][4] * this.scale) + 'px';
        img1.style.height = Math.floor(this.slideArray[0][5] * this.scale) + 'px';
        // img1.style.marginLeft = Math.floor((this.maxWidth - this.slideArray[0][4]) / 2 * this.scale) + 'px';
        // img1.style.marginTop = Math.floor((this.maxHeight - this.slideArray[0][5]) / 2 * this.scale) + 'px';
        img1.src = this.slideArray[0][0];
    },
    
    transitionSlide : function(img) {
        var imgIDParts = img.id.split('_');
        this.slideCurrent = parseInt(imgIDParts[imgIDParts.length - 1]);
        this.slideNext = (this.slideCurrent == 1) ? 2 : 1;
        var slideOrder = this.setSlideOrder();
        if (this.slideToTransition == 'old') {
            //run the effect on the top slide
            eval("new Effect." + this.transitionEffect + "('" + slideOrder['top'] + "', {duration:" + this.transitionDuration + "})");
            //show the new slide
            new Effect.Appear(slideOrder['bottom']);
        }
        else {
            //run the effect on the top slide
            eval("new Effect." + this.transitionEffect + "('" + slideOrder['top'] + "', {duration:" + this.transitionDuration + "})");
            //transition the old slide out, que it at the end so the top transition occurs first
            new Effect.Fade(slideOrder['bottom'], {queue : 'end'});
        }
        //increment slideIdx
        this.slideIdx = (this.slideIdx == this.slideArray.length - 1) ? 0 : this.slideIdx + 1;
        //set the next slide image src
        this.slideTO = setTimeout("Slideshow.setNextImage(" + this.slideNext + ")", this.slideDuration);
    },
    
    setSlideOrder : function() {
        var slideTopID, slideBottomID = '';
        if (this.slideToTransition == 'old') {
            slideTopID = this.parentID + '_slide_' + this.slideNext;
            slideBottomID = this.parentID + '_slide_' + this.slideCurrent;
        }
        else {
            slideTopID = this.parentID + '_slide_' + this.slideCurrent;
            slideBottomID = this.parentID + '_slide_' + this.slideNext;
        }
        slideTop = document.getElementById(slideTopID);
        slideBottom = document.getElementById(slideBottomID);
        slideTop.style.zIndex = 50;
        slideBottom.style.zIndex = 0;
        return {top : slideTopID, bottom : slideBottomID};
    },
    
    setNextImage : function(slideNo) {
        var slideIdx = this.slideIdx;
        this.nextImage = document.getElementById(this.parentID + '_image_' + slideNo);
        this.nextSlide = document.getElementById(this.parentID + '_slide_' + slideNo);
        this.nextCaption = document.getElementById(this.parentID + '_caption_' + slideNo);
        this.nextTitle = document.getElementById(this.parentID + '_title_' + slideNo);
        this.nextTitle.innerHTML = this.slideArray[slideIdx][3];
        this.nextCaption.innerHTML = this.slideArray[slideIdx][1];
        
        // *** KRISZO **/
        var numer = this.slideIdx+1;
        var ostatni = this.slideArray.length;
        get('numer_zdjecia').innerHTML = numer;
        

        //set top margin based upon image height
        // this.nextImage.style.marginTop = Math.floor((this.maxHeight - this.slideArray[slideIdx][5]) * this.scale / 2) + 'px';
        // this.nextImage.style.marginLeft = Math.floor((this.maxWidth - this.slideArray[slideIdx][4]) * this.scale / 2) + 'px';
        //because of some weirdness with IE, leave nothing to chance, set the height and width
        this.nextImage.style.width = Math.floor(this.slideArray[slideIdx][4] * this.scale) + 'px';
        this.nextImage.style.height = Math.floor(this.slideArray[slideIdx][5] * this.scale) + 'px';
        this.nextImage.src = this.slideArray[slideIdx][0];
    },
    
    stop : function(){
        clearTimeout(this.slideTO);
        this.status = 'stopped';
    },
    
    start : function() {
        this.setNextImage(this.slideNext);
        this.status = 'running';
    },
    
    reset : function() {
        this.stop();
        this.slideIdx = 0;
        this.nextImage = null;
        this.nextSlide = null;
        this.nextCaption = null;
        this.nextTitle = null;
        this.slideCurrent = 1;
        this.slideNext = 2;
        this.setSlideOrder();
    }

}