/**
 * Slider plugin
 */
(function($){
    var Slider=function(box,opts) {
        var container=box;
        var overflow;
        var slider;
        var count;
        var pages;
        var w_frame;
        var w_slider;
        var btn_next;
        var btn_prev;
        var first=1;
        var step_x;
        var correction = 1;
        var offset = opts.offset;
        
        overflow = container.find("." + container.attr("class") + "_hidden")           
        slider = overflow.find("." + container.attr("class") + "_slider").eq(0);
        count = slider.find("li").length;
        pages = Math.ceil(count/opts.step);
        w_frame = overflow.width() + offset;
        w_slider = w_frame * count/opts.inWindow;
        slider.css({
            'width' : w_slider 
        });
        step_x = w_frame/opts.inWindow*opts.step;
        btn_next = container.find('a.btn_next');
        btn_next.bind('click',function(){
            go_forward();
            return false;
        });
        if(pages == 1) {
            btn_next.css({
                'opacity' : opts.opacityBtn
            });
        }
        btn_prev = container.find('a.btn_prev');
        btn_prev.bind('click',function(){
            go_backward();
            return false;
        });
        btn_prev.css({
            'opacity' : opts.opacityBtn
        });
        
        if(!opts.slideEdge) {
            correction = 2;
        }
        
        var go_forward = function() {
            if(first < pages) {
                if ($.isFunction(opts.onStart)) {
                    opts.onStart.call(this);
                }                
                if((first < pages-1  && first > 1) || opts.slideEdge) {
                    var shift_x = -(first-correction)*step_x-step_x;
                    shift(shift_x);
                }
                var active = slider.find(".active");
                active.removeClass("active");
                active.next().addClass("active");
                if(first == 1) {
                    btn_prev.css({
                        'opacity' : 1.0
                    });
                }
                first++;
                if (first == pages) {
                    btn_next.css({
                        'opacity' : opts.opacityBtn
                    });
                }                
                if ($.isFunction(opts.onChange)) {
                    opts.onChange.call(this);
                }
            }
        }
        
        var go_backward = function() {
            if(first > 1) {
                if ($.isFunction(opts.onStart)) {
                    opts.onStart.call(this);
                }
                if((first < pages && first > 2) || opts.slideEdge){
                    var shift_x = -(first-correction)*step_x+step_x;
                    shift(shift_x);
                }
                var active = slider.find(".active");
                active.removeClass("active");
                active.prev().addClass("active");
                if(first == pages) {
                    btn_next.css({
                        'opacity' : 1.0
                    });
                }
                first--;
                if (first == 1) {
                    btn_prev.css({
                        'opacity' : opts.opacityBtn
                    });
                }              
                if ($.isFunction(opts.onChange)) {
                    opts.onChange.call(this);
                }
            }
        }
        
        var shift = function(x) {
            slider.animate({
                left: x
            }, opts.duration);
        }
        
    };
    
    $.fn.slider = function(options) {           
        var defaults = {  
            step: 1,  
            inWindow: 3,
            duration: 'fast',
            slideEdge: true,
            offset: 0,
            opacityBtn: 1.0,
            onStart: null,
            onChange: null
        };  
    
        var opts = $.extend(defaults, options);  
      
        return this.each(function() {      
            return new Slider($(this),opts);
        });  
    };  
})(jQuery);

/**
 * Image switch plugin
 */
(function($){
    var imageSwitcher=function(box, opts) {
        var source = box;
        var imgContainer = $(opts.mainImage);
        var count = source.find("li").length;
        var active = 0;
        var loading = 0;
        var imgArray = $.makeArray($(source.find("a")));
        
        source.find("li").bind('click', function(){
            set_active($(this).find("a"));
            change_image($(this).find("a"));
            return false;
        });
        
        $("#nav_prev").bind('mouseenter', function(){
            if(active > 0 && loading == 0) {
                $(this).css({
                    'cursor': 'pointer'
                });
                $("#arrow_left").show();
            }
        });
        
        $("#nav_prev").bind('mouseleave', function(){
            $(this).css({
                'cursor': 'auto'
            });
            $("#arrow_left").hide();
        });
        $("#nav_prev").bind('click', function(){
            if(active > 0) {
                active--;
                change_by_index(active);
            }
            return false;
        });
        
        $("#nav_next").bind('mouseenter', function(){
            if(active < count-1 && loading == 0) {
                $(this).css({
                    'cursor': 'pointer'
                });
                $("#arrow_right").show();
            }
        });
        
        $("#nav_next").bind('mouseleave', function(){
            $(this).css({
                'cursor': 'auto'
            });
            $("#arrow_right").hide();
        });
        $("#nav_next").bind('click', function(){
            if(active < count-1) {
                active++;
                change_by_index(active);        
            }
            return false;
        });
        
        $(document).keydown(function(objEvent) {
            var keycode = event.keyCode;
            if (keycode == 37 && active > 0) {
                active--;
                change_by_index(active);
            }
            if (keycode == 39 && active < count-1) {
                active++;
                change_by_index(active);  
            }
        });
        
        var set_active = function(clicked) {
            active = 0;
            
            for(i=0; i<count; i++) {
                if(clicked.attr("href") == $(imgArray[i]).attr('href')) {
                    break;
                }
                active++;
            }
        }
        
        var change_by_index = function(index) {
            if(index >= 0 && index < count) {
                change_image($(imgArray[index]))
            }
        }
        
        var change_image = function(clickedImg) {
            var newImg = new Image();
            var alt = clickedImg.find("img").attr("alt");
            var title = clickedImg.find("img").attr("title");
            var href = clickedImg.attr("href");
       
            // skrytí a odstranění původního obrázku
            imgContainer.find("#image").hide();
            imgContainer.find("p").remove(); 
            imgContainer.find("#loading").show();   
            loading = 1;
            imgContainer.css({
                width : '640px'
            });
            newImg.src = href;
            
            newImg.onload = function() {
                $(this).hide();
                imgContainer.find("#image").attr('src', href);
                imgContainer.find("#image").remove();
                imgContainer.append(this);
                imgContainer.append("<p>" + alt + "</p>");
                $(this).fadeIn();
                imgContainer.find("#loading").hide();
                $(this).attr("alt", alt);
                $(this).attr("title", title);
                $(this).attr("id", "image");
                $(this).attr("style", "max-width: 100%;");  
                imgContainer.css({
                    width : this.width
                });
                loading = 0;
                
                newImg.onload=function(){};
            };
        }
        
      
    }
    
    $.fn.imageSwitch = function(options) {           
        var defaults = {  
            mainImage: "#main_image",  
            duration: "fast",
            loaded: "",      // cesta k nacitanemu souboru
            alt: "",
            title: "",
            complete: null
        };  
    
        var opts = $.extend(defaults, options);  
      
        return this.each(function() {      
            return new imageSwitcher($(this), opts);
        });  
    };
})(jQuery);

$(document).ready(function() {    
    $(".gallery").slider({
        opacityBtn: 0.5,
        step: 4,
        inWindow: 4,
        offset: 10
    });
    $(".gallery").imageSwitch({
        mainImage: ".main_image"
    });
});
