/**
 * @fileoverview  JavaScript Slideshow
 *
 * Simple slideshow using prototype and scriptaculous.
 * 
 * Usage:
 * ------
 * <script src="prototype.js"></script>
 * <script src="effects.js"></script>
 * <script src="slideshow.js"></script>
 * <style type="text/css">
 *     #slideshow { position: relative; width: 100px; height: 100px; }
 *     #slideshow div { position: absolute; left: 0; top: 0; }
 * </style>
 * <div class="slideshow" id="slideshow">
 *     <div class="slide"><img src="slide1.jpg">   </div>
 *     <div class="slide"><img src="slide2.jpg"></div>
 *     <div class="slide"><img src="slide3.jpg"></div>
 * </div>
 * <script type="text/javascript">new Slideshow('slideshow', 3000);</script>
 *
 * See also: http://blog.remvee.net/post/17
 *
 * @package    Slideshow
 * @author     R.W. van 't Veer
 * @author     Murat Purc (murat@purc.de)
 * @copyright  (c) 2006 - R.W. van 't Veer
 *
 * Changes:
 * 2006-06-20 by Murat Purc (murat@purc.de), Added handling of Elements having display none styles.
 * 2008-09-28 by Murat Purc (murat@purc.de), Refactoring of code
 */


/**
 * Class Slideshow
 */
Slideshow = Class.create({

    /**
     * Array of slide elements
     * @type  {array}
     */
    aSlides: [],

    /**
     * Actual position
     * @type  {int}
     */
    current: 0,

    /**
     * Time to wait between fades
     * @type  {int}
     */
    timeout: 4000,
    
    
    /**
     * gibt den Pfad des aktuellen Bildes im Hauptfenster aus
     */
    _getactMainElem: function () {
	var temp = $('main_image').src;
	var actMainElem = temp;
//	var actMainElem = temp.substring(27);
	return actMainElem;
},	
    
    /**
     * Constructor
     * 
     * @param  {string}  Id of element which contains the images
     * @param  {int}     Time between two slides
     */
    initialize: function (slideshow, timeout) {

        var nl = $(slideshow).getElementsByTagName('div');
        for (var i = 0; i < nl.length; i++) {
            if (Element.hasClassName(nl[i], 'slide')) {
                nl[i].style.zIndex = nl.length - i;
                Element.hide(nl[i]);
                this.aSlides.push(nl[i]);
            }
        }
        this.timeout = timeout;
        
        if (nl.length > 1) {
	        //TODO waht if only 1 element??
	        Element.show(slideshow);
	
	        //check if slide is mainelem
	        var currentelem = 'id' + this.current;
	        var actSlideElem = $(currentelem).title;
	      
	        var temp = this._getactMainElem(); 
	        if (temp == actSlideElem) {
	        	// if equales start with next
		      	this.current = (this.current + 1) % this.aSlides.length;
	        } 
	        Element.setStyle(this.aSlides[this.current], {display: 'inline'});        
	        setTimeout((function(){this._next();}).bind(this), this.timeout);
        }
        
    },


    /**
     * Runs fade effect and prepares next fade
     */
    _next: function() {
            	
    	if ( this.aSlides.length > 2 ) {
    	
	    	for (var i = 0; i < this.aSlides.length; i++) {
	            var slide = this.aSlides[(this.current + i) % this.aSlides.length];
	            slide.style.zIndex = this.aSlides.length - i;
	        }
	    	
	
	        Effect.Fade(this.aSlides[this.current], {
	            afterFinish: function(effect) {
	        		effect.element.style.zIndex = 0;
	                Element.show(effect.element);
	                Element.setOpacity(effect.element, 1);
	            }
	        });
	        
	        //set next element to show and check if next is mainelem
	        this.current = (this.current + 1) % this.aSlides.length;
	        var currentelem = 'id' + this.current;
	    	var actSlideElem = $(currentelem).title;
	    	var temp = this._getactMainElem(); 
	        
	        if (temp == actSlideElem) {
	        	//if equals then increas
	        	this.current = (this.current + 1) % this.aSlides.length;
	        }
	        
	        //show next element
	        var currentelemIncres = 'id' + this.current;
	        $(currentelemIncres).style.display='inline';
//	        Element.setStyle(this.aSlides[this.current], {display: 'inline'});
	        setTimeout((function(){this._next();}).bind(this), this.timeout);
	        
    	
    	}else{
    		
    		//check if main is same as slide
    		var currentelem = 'id' + this.current;
	    	var actSlideElem = $(currentelem).title;
	    	var temp = this._getactMainElem(); 
	        
	        if (temp == actSlideElem) {
	        	//if equaels  then fade curent slide
	        	 Effect.Fade(this.aSlides[this.current], {
	 	            afterFinish: function(effect) {
	 	        	//alert ('stop dupa pretenzii 1');
	 	        		effect.element.style.zIndex = 0;
	 	                Element.show(effect.element);
	 	                Element.setOpacity(effect.element, 1);
	 	            }
	 	        });
	        	//increase curent and show the new current
		        this.current = (this.current + 1) % this.aSlides.length;
		        var currentelemIncres = 'id' + this.current;
		        $(currentelemIncres).style.display='inline';

	        }
	        setTimeout((function(){this._next();}).bind(this), this.timeout);
//    	
    	}
    	
    	
    }


});
