/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function mran(ma,mi)
{
   return(Math.round(Math.random()*(ma-mi))+mi)
}

//
//  Configurationl
//
slideshowOptions = Object.extend({
    fileLoadingImage:        'images/slideshow/loading.gif',

    overlayOpacity: 0.8,   // controls transparency of shadow overlay

    animate: true,         // toggles resizing animations
    resizeSpeed: 7,        // controls the speed of the image resizing animations (1=slowest and 10=fastest)

    borderSize: 10,         //if you adjust the padding in the CSS, you will need to update this variable

    // When grouping images this is used to write: Image # of #.
    // Change it for non-english localization
    labelImage: "Image",
    labelOf: "of",
    width: 250,
    height: 100
}, window.LightboxOptions || {});

var Slideshow = Class.create({
    initialize: function(parent, el_name, delay) {
        this.delay = delay;
        this.element_name = el_name;
        this.element_count = $(parent).childElements().size();
        this.paused = 0;
        this.elements = [this.element_count];
        this.current = 1; //Math.round(Math.random()*(this.element_count-1)) + 1;
        $('slide_num_1').className = 'slide_num_active';
        this.effects = [];
		this.effects_slides = [];
        this.effect_navigator = null;
        var objBody = $(parent);
		
        objBody.observe('mouseover', function() {
            this.paused = 1;
            //try { this.effects_navigator.cancel(); } catch (e) {}
            //this.effects_navigator = new Effect.Appear('slideShow_navigator',{duration: 0.4});
        }.bind(this));  //pause on mouseover
        objBody.observe('mouseout', function() {
            this.paused = 0;
            //try { this.effects_navigator.cancel(); } catch (e) {}
            //this.effects_navigator = new Effect.Fade('slideShow_navigator',{duration: 0.4});
        }.bind(this));  //resume on mouseout
		$('slideShow_navigator').observe('mouseover', function() {
            this.paused = 1;
            //try { this.effects_navigator.cancel(); } catch (e) {}
            //this.effects_navigator = new Effect.Appear('slideShow_navigator',{duration: 0.4});
        }.bind(this));  //pause on mouseover
        $('slideShow_navigator').observe('mouseout', function() {
            this.paused = 0;
            //try { this.effects_navigator.cancel(); } catch (e) {}
            //this.effects_navigator = new Effect.Fade('slideShow_navigator',{duration: 0.4});
        }.bind(this));  //resume on mouseout
        /*
        $$('a.next_arrow').each(function(node) {
            node.observe('click', function(s){
                //this.elements[this.current].setStyle({ display: 'none' });
                this.update(1);
                //this.elements[this.current].setStyle({ display: 'block' });
                s.stop();
                return false;
            }.bind(this));
        }.bind(this));

        $$('a.prev_arrow').each(function(node) {
            node.observe('click', function(s){
                //this.elements[this.current].setStyle({ display: 'none' });
                this.update(-1);
                //this.elements[this.current].setStyle({ display: 'block' });
                s.stop();
                return false;
            }.bind(this));
        }.bind(this));
        */
		
        for (i = 1; i <= this.element_count; i++) {
			el = $(this.element_name + i);
			thumb = $('slide_num_' + i);
				thumb.value = i;
            this.elements[i] = el;
			//this.elements[i].onmouseover = this.onS.bind(this, i);
			//this.elements[i].value = i;
			thumb.onmouseover = this.onS.bind(this, thumb);
			//thumb.onmouseout = this.onH.bind(this, thumb);
			/*
			el.observe('mouseover', function() { 
				this.paused = 1;
				this.show(i);
			}.bind(this));  //pause on mouseover
			*/
			/*
            thumb.observe('mouseout', function() { 
				this.paused = 0;
			}.bind(this));  //resume on mouseout
			*/
            $$(this.elements[i].img).each(function(img) {
                img = new Image();
            });
        }

    },
	
	onS: function(link) {
		this.paused = 1;
		this.show(link.value);
        return false;
    },
	
    start: function() {
        //show first element without effect
        this.elements[this.current].setStyle({
            display: 'block'
        });
		
        this.executor = new PeriodicalExecuter(function() {
            this.next(); //start slidehow
        }.bind(this), this.delay);
		
    },

    // NEXT SLIDE
    next: function(){
        if (!this.paused) {
            this.update(1);
        }
    },

    // PREVIOUS SLIDE
    prev: function(){
        if (!this.paused) {
            this.update(-1);
        }
    },

    // MOVES SLIDES WITH STEP 1
    update: function(step) {
        if (!step) step = 1;
        this_slide = this.current;
        next_slide = this.checkSlide(step);  // MOVE SLIDESHOW

        this.animate(this_slide, next_slide);
    },

    // RETURN NEXT VALID SLIDE BASED ON STEP -1, 1
    checkSlide: function(step) {
        if (!step) step = 1;
        tmp = this.current + step;

        if (tmp > this.element_count) { tmp = 1; }
        if (tmp < 1) { tmp = this.element_count; }
        return tmp;
    },

    // ANIMATE SLIDE FROM TO
    animate: function(jump_from, jump_to) {
        $('slide_num_' + jump_from).className = 'slide_num_inactive';
        $('slide_num_' + jump_to).className = 'slide_num_active';
        if (jump_from == jump_to) {
            return;
        }

        try { this.effects[jump_from].cancel(); } catch (e) {}
            this.effects[jump_from] = new Effect.Fade(this.elements[jump_from], {duration: 1.5});

        try { this.effects[jump_to].cancel(); } catch (e) {}
            this.effects[jump_to] = new Effect.Appear(this.elements[jump_to], {duration: 1.5});
		
		try { this.effects_slides[jump_from].cancel(); } catch (e) {}
            this.effects_slides[jump_from] = new Effect.Fade($('slide_num_' + jump_from), {duration: 0.5, to: 0.6});
		
		try { this.effects_slides[jump_to].cancel(); } catch (e) {}
            this.effects_slides[jump_to] = new Effect.Fade($('slide_num_' + jump_to), {duration: 0.5, to: 1.0});
		
        this.current = jump_to;
    },

    // NAVIGATOR SHOW
    navigator_show: function() {
        try { this.effects_navigator.cancel(); } catch (e) {}
        /*
        if (this.paused == 1) this.effects_navigator = new Effect.Appear('slideShow_navigator',{duration: 0.4});
        else {
        */
       //if (this.paused == 0) {
            //this.paused = 1;
            try { this.effects_navigator.cancel(); } catch (e) {}
            this.effects_navigator = new Effect.Parallel(
                [
                    new Effect.Appear('slideShow_navigator',{duration: 0.4}),
                    new Effect.Move('slideShow_navigator',{duration: 0.4, x: 0, y: -40, mode: 'relative'})
                ],
                {
                    duration: 0.4
                }
            );
        //}
    },

    // NAVIGATOR HIDE
    navigator_hide: function() {
        try { this.effects_navigator.cancel(); } catch (e) {}
        /*
        if (this.paused == 0) this.effects_navigator = new Effect.Fade('slideShow_navigator',{duration: 0.4});
        else {
        */
        //if (this.paused == 1) {
            //this.paused = 0;
            try { this.effects_navigator.cancel(); } catch (e) {}
            this.effects_navigator = new Effect.Parallel(
                [
                    new Effect.Fade('slideShow_navigator',{duration: 0.4}),
                    new Effect.Move('slideShow_navigator',{duration: 0.4, x: 0, y: 40, mode: 'relative'})
                ],
                {
                    duration: 0.4
                }
            );
        //}
    },
    
    // SHOW SLIDE
    show: function(slide) {
        this_slide = this.current;
        next_slide = slide;
        if (next_slide > this.element_count) { next_slide = 1; }
        if (next_slide < 1) { next_slide = this.element_count; }
        this.animate(this_slide, next_slide);
    }
});

