/**
* @version 1.0 stable
* @package DJ Image Slider
* @subpackage DJ Image Slider Module
* @copyright Copyright (C) 2010 Blue Constant Media LTD, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://design-joomla.eu
* @author email contact@design-joomla.eu
* @developer Szymon Woronowski - szymon.woronowski@design-joomla.eu
*
*
* DJ Image Slider is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DJ Image Slider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DJ Image Slider. If not, see <http://www.gnu.org/licenses/>.
*
*/

var DocumentLoaded = false;

window.addEvent('load',function(){DocumentLoaded = true});

var DJImageSlider = new Class({

    initialize: function(id, slider_type, slide_size, visible_slides, options){

        var slider_size = 0;
        var loaded_images = 0;
        var max_slides = 0;
        var current_slide = 0;
        var slider = 'slider' + id;
        var autoplay = options.auto;
        var stop = 0;
        var show_nav = 0;
		var is_fading = false;
        
        var slides = $$('#slider'+ id +' li');
        slides.each(function(){
            slider_size += slide_size;
            loaded_images++;
        })
        
        max_slides = loaded_images - visible_slides;
		
        $(slider).setStyle('position', 'relative');
		
        var slideImages;
		if (slider_type == 2) { // fade
			slides.setStyle('position', 'absolute');
			slides.setStyle('top', 0);
			slides.setStyle('left', 0);
			$(slider).setStyle('width', slide_size);
			slides.setOpacity(0);
			slides[0].setOpacity(1);
			slides[0].setStyle('z-index','10');
			$('navigation' + id).setStyle('z-index',20);
						
		} else if (slider_type == 1) { // vertical
            $(slider).setStyle('top', 0);
            $(slider).setStyle('height', slider_size);
            slideImages = new Fx.Style(slider, 'top', {
                duration: options.duration,
                transition: options.transition,
                wait: false
            });
        }
        else { // horizontal
            $(slider).setStyle('left', 0);
            $(slider).setStyle('width', slider_size);
            slideImages = new Fx.Style(slider, 'left', {
                duration: options.duration,
                transition: options.transition,
                wait: false
            });
        }
        
        var play = new Fx.Style('play' + id, 'opacity', {duration: 200, wait: false}).set(0);
        var pause = new Fx.Style('pause' + id, 'opacity', {duration: 200, wait: false}).set(0);
        
        $('next' + id).addEvent('click', function(){
            hideNavigation();
            nextSlide();
        });
        
        $('prev' + id).addEvent('click', function(){
            hideNavigation();
            prevSlide();
        });
        
        $('play' + id).addEvent('click', function(){
            changeNavigation();
            autoplay = 1;
        });
        
        $('pause' + id).addEvent('click', function(){
            changeNavigation();
            autoplay = 0;
        });
        
        $('djslider' + id).addEvents({
            'mouseenter': function(){
                showNavigation();
                stop = 1;
            },
            'mouseleave': function(){
                hideNavigation();
                stop = 0;
            }
        });
		
        function nextSlide(){
			if (slider_type == 2)
				nextFade();
			else {
				if (current_slide < max_slides) 
					current_slide++;
				else 
					current_slide = 0;
				slideImages.start(-slide_size * current_slide);
			}
        }
        
        function prevSlide(){
			if (slider_type == 2) {
				prevFade();
			}
			else {
				if (current_slide > 0) {
					current_slide--;
				}
				else {
					current_slide = max_slides;
				}
				slideImages.start(-slide_size * current_slide);
			}
        }
        
		function nextFade(){
			if(is_fading) return;
			is_fading = true;
			prev_slide = current_slide;
			if (current_slide < max_slides) {
				current_slide++;
			}
			else {
				current_slide = 0;
			}
				
			makeFade(prev_slide);
		}
		
		function prevFade(){
			if(is_fading) return;
			is_fading = true;
			prev_slide = current_slide;
			if (current_slide > 0) {
				current_slide--;
			}
			else {
				current_slide = max_slides;
			}
			
			makeFade(prev_slide);			
		}
		
		function makeFade(prev_slide){
			slides[current_slide].setOpacity(1);
			slides[prev_slide].effect('opacity',{duration: options.duration, wait: true}).start(0).chain(function(){
				slides[prev_slide].setStyle('z-index',0);
				slides[current_slide].setStyle('z-index',10);
				is_fading = false;
			});
		}
		
        function hideNavigation(){
            if (!autoplay) {
                play.start(stop, 0).chain(function(){
                    if (!show_nav) 
                        $('play' + id).setStyle('display', 'none');
                });
            }
            else {
                pause.start(stop, 0).chain(function(){
                    if (!show_nav) 
                        $('pause' + id).setStyle('display', 'none');
                });
            }
            show_nav = 0;
        }
        
        function showNavigation(){
            if (!autoplay) {
                $('play' + id).setStyle('display', 'inline');
                play.start(stop, 1);
            }
            else {
                $('pause' + id).setStyle('display', 'inline');
                pause.start(stop, 1);
            }
            show_nav = 1;
        }
        function changeNavigation(){
            if (autoplay) {
                $('pause' + id).setStyle('display', 'none');
                pause.set(0);
                $('play' + id).setStyle('display', 'inline');
                play.set(1);
            }
            else {
                $('play' + id).setStyle('display', 'none');
                play.set(0);
                $('pause' + id).setStyle('display', 'inline');
                pause.set(1);
            }
        }
        
        function slidePlay(){
            setTimeout(function(){
                if (autoplay && !stop) 
                    nextSlide();
                slidePlay();
            }, options.delay);
        }
		
		function sliderLoaded(){
			// hide loader and show slider
			if (/\bMSIE 8.0\b/.test(navigator.appVersion)) { // only for IE8
				var visibles = new Array();
				for (var i = 0; i < visible_slides; i++) {
					visibles[i] = slides[i];
				}
				visibles.each(function(el){
					el.setStyle('opacity', 0);
				});
			}
			
			$('djslider' + id).setOpacity(0);
			$('djslider' + id).setStyle('display','block');
			$('djslider-loader' + id).setStyle('background','none');
			
			$('djslider' + id).effect('opacity',{wait: true, duration: 500}).start(0,1);
			
			if (/\bMSIE 8.0\b/.test(navigator.appVersion)) { // only for IE8
				visibles.each(function(el){
					el.effect('opacity', {
						wait: true,
						duration: 500
					}).start(0, 1);
				});
			}
			// count and change djslider dimensions
			buttons_height = $('next' + id).getStyle('height').toInt();
			buttons_height = Math.max(buttons_height,$('prev' + id).getStyle('height').toInt());
			$('navigation' + id).setStyle('height',buttons_height);
        	button_pos = $('navigation' + id).getStyle('top').toInt();
        	djslider_height = $('djslider' + id).getStyle('height').toInt();
			if(button_pos > 0) {
				new_height = buttons_height + button_pos;
			} else {
				new_height = djslider_height - button_pos;
			}        	
        	if (new_height > djslider_height) {
            	$('djslider' + id).setStyle('height', new_height);
				$('djslider-loader' + id).setStyle('height', new_height);
        		if (button_pos < 0) {
					$('navigation' + id).setStyle('top', 0);
					$('slider-container' + id).setStyle('top', -button_pos);
				}
        	}
			
			// start autoplay
			slidePlay();
		}
		
		if (DocumentLoaded) {
			sliderLoaded();
		}
		else {
			window.addEvent('load', sliderLoaded);
		}
        
    }
    
});
