(function($){
    
    $.fn.slider = function(o){
        return this.each(function(){
            if(!$s.lock){
               return new $s(this,o);
            }else{return $s;}
        });
    };
    
    var defaults = {
        effect: 'swing',
        items: '.items',
        next: '.next',
        prev: '.prev',
        play: '.play',
        pause: '.pause',
        preloader: '.preloader',
        navigator: '.navigator',
        loader: 'img/loader.gif',
        loaderBackground:'#fff',
        loadingFrame: false,
        width: 0,
        height: 0,
        step: 1,
        speed: 1000,
        interval: 3000,
        preload:true,
        autoscroll:true,
        autoplay: true,
        autopause: true,
        circular: true,
        vertical: false,
        fade: false,
        onCreate:function(){},
        onPlay:function(){},
        onPause:function(){},
        onChange:function(){},
        onMove:function(){}
    };
    
    $.slider = function(e,o){
        this.options = $.extend({}, defaults, o || {});
        this.headElement = e;
        this.lock = true;
        this.ready = false;
        this.setup();
        return this;
    };
    
    var $s = $.slider;
    
    $s.fn = $s.prototype = {
        slider: '2.0'
    };
    
    $s.fn.extend = $s.extend = $.extend;
    
    $s.fn.extend({
        setup: function(){
            var parent = this;
            this.index = 0;
            this.items = $(this.headElement).find(this.options.items);
            this.elements = $(this.items).children();
            this.tag = this.elements[0].tagName;
            this.options.onCreate(this);

            this.prevElement = $(this.headElement).find(this.options.prev);
            this.nextElement = $(this.headElement).find(this.options.next);
            this.playElement = $(this.headElement).find(this.options.play);
            this.pauseElement = $(this.headElement).find(this.options.pause);
            this.navigationElement = $(this.headElement).find(this.options.navigator);

            this.items.css({position:'absolute',width:'20000em',left:0,top:0});
            this.elements.css('float','left');
            $(this.elements[0]).addClass('active');
            
            if(this.elements.length<2){
                return false;
            }
            
            if(this.options.fade==true){
                $(this.elements[0]).css({zIndex:3})
            }

            if(this.options.preload==true){
                this.preloader();
            }else{
                this.ready = true;
            }

            $(this.prevElement).click(function(){
                parent.prev();
                return false;
            });

            $(this.nextElement).click(function(){
                parent.next();
                return false;
            });

            $(this.playElement).click(function(){
                parent.play();
                return false;
            });

            $(this.pauseElement).click(function(){
               parent.pause(true);
               return false;
            });

            this.navigation();
            this.circular();
            this.autoscroll(true);

            this.disable(0);
            
            return this;
        },
        preloader:function(){
            var parent = this;
            var preloader = $(this.headElement).children(this.options.preloader);
            if(!preloader.tag){
                preloader = $('<div>').addClass(this.options.preloader.substr(1,
                this.options.preloader.length-1)).prependTo($(this.headElement));
                if(parent.options.loadingFrame!=false){
                    preloader.css('background','url('+parent.options.loadingFrame+') top left no-repeat');
                }
                $('<div>').addClass('background').css({
                    width:$(parent.headElement).width(),
                    height:$(parent.headElement).height(),
                    background:parent.options.loaderBackground,
                    opacity:0.7
                }).prependTo(preloader);
                $('<div>').addClass('loader').css({
                    width:32,
                    height:32,
                    position:'absolute',
                    left:'50%',
                    top:'50%',
                    marginLeft:-16,
                    marginTop:-16,
                    background:'url('+parent.options.loader+') top left no-repeat'
                }).appendTo(preloader);
            }
            $(this.items).hide();
            $(window).bind('load', function() {
                var preload = new Array();
                var elements = parent.elements;
                if(parent.tag!='IMG'){
                    elements = parent.elements.find('img');
                }
                elements.each(function() {
                    //var s = $(this).attr("src").replace(/\.(.+)$/i, ".$1");
                    preload.push($(this).attr("src"));
                });
                $.each(preload,function(){
                    var img = document.createElement('img');
                    $(img).bind('load', function() {
                        if(preload[0]) {
                            this.src = preload.shift();
                        }
                    }).trigger('load');
                    if(preload.length==0){
                        preloader.hide();
                        $(parent.items).show();
                        var e = parent.elements;
                        parent.options.width = e.outerWidth(true);
                        parent.options.height = e.outerHeight(true);
                        var m = (parent.options.circular==true)?1:0;
                        if(parent.options.vertical==false && parent.options.fade!=true){
                            parent.items.css({
                                width:parent.options.width*(parent.elements.length+m),
                                height:parent.options.height
                            });
                        }else if(parent.options.fade!=true){
                            parent.items.css({
                                width:parent.options.width,
                                height:parent.options.height*(parent.elements.length+m)
                            });
                        }else{
                            parent.items.css({
                                width:parent.options.width,
                                height:parent.options.height
                            });
                            parent.items.children().css({
                                position:'absolute',
                                top:0,
                                left:0
                            });
                        }
                        parent.ready = true;
                    }
                });
            });
        },
        navigation:function(){
            if(this.options.navigator!=false){
                var parent = this;
                var e = this.navigationElement;
                e.text('');
                this.elements.each(function(i){
                    var rel = parent.headElement.id+'_seek';
                    e.append('<a href="#" class="'+rel+'" rel="'+i+'">'+(i+1)+'</a>');
                    e.children('a.'+rel).click(function(){
                        parent.goTo($(this).attr('rel'));
                        return false;
                    });
                });
            }
        },
        circular:function(){
            if(this.options.circular==true && this.options.fade!=true){
                var e = $(this.elements[0]);
                e.clone(true).addClass('clone').appendTo(this.items);
                this.elements = $(this.items).children();
            }
        },
        autoscroll:function(n){
            if(this.options.autoscroll==true && this.options.autoplay){
                var parent = this;
                if(this.timer){
                   this.timer =  window.clearInterval(this.timer);
                }
                this.timer = window.setInterval(function(){
                    parent.move();
                },this.options.interval);
                if(this.options.autopause==true && n==true){
                    $(this.items).bind('mouseenter',function(){
                        parent.pause();
                    }).bind('mouseleave',function(){
                        if(parent.stop!=true){
                            parent.play();
                        }
                    });
                }
            }
        },
        seek:function(i){
            var e;
            var parent = this;

            this.options.onChange(this);

            if(this.options.fade!=true){
                if(i<0){
                    i = this.index = this.elements.length-1;
                    this.items.css({
                        left:-$(this.elements[i]).position().left,
                        top:-$(this.elements[i]).position().top
                    });
                }
            }

            this.disable(i);
            
            if(this.ready==true){
                if(this.index>i){
                    this.revers = true;
                    this.index = i;
                }else{
                    this.revers = false;
                    this.index = this.index+1;
                }
                
                if(this.options.fade!=true){
                    e = $(this.elements[i]);
                }else{
                    if(i>=this.elements.length){
                        this.index = i = 0;
                    }
                    e = $(this.elements[i]);
                }
                
                if (this.options.vertical==false && this.options.fade!=true) {

                    this.items.animate({left:-e.position().left}, parent.options.speed, parent.options.effect, function(){
                        parent.callback();
                        parent.options.onMove(i,this,parent);
                    });

                } else if(this.options.fade!=true) {

                    this.items.animate({top:-e.position().top}, parent.options.speed, parent.options.effect, function(){
                        parent.callback();
                        parent.options.onMove(i,this,parent);
                    });
                    
                } else if(this.options.fade==true){
                    var p = $(this.elements[((i!=0)?(i-1):(this.elements.length-1))]);
                    p.addClass('last-active').css('zIndex',2);
                    e.addClass('active').css({opacity:0,zIndex:3})
                    .animate({opacity:1},parent.options.speed,parent.options.effect,function(){
                       p.removeClass('active last-active').css('zIndex',1);
                       parent.callback();
                       parent.options.onMove(i,this,parent);
                    });
                    
                }
                
            }
        },
        disable:function(i){
            if(i<1){
                this.prevElement.addClass('disable');
            }else if(this.prevElement.hasClass('disable')){
                this.prevElement.removeClass('disable');
            }
            
            if(i>=this.elements.length-1){
                this.nextElement.addClass('disable');
            }else if(this.nextElement.hasClass('disable')){
                this.nextElement.removeClass('disable');
            }
        },
        callback:function(){
            var parent = this;
                if(this.options.fade!=true && (this.index>=this.elements.length
                   || this.index<0 || this.index==this.elements.length-1)){
                    this.index = 0;
                    this.items.css({left:0,top:0});
                }
            if(this.options.navigator!=false){
                this.navigationElement.children('a').removeClass('active');
                this.navigationElement.children('a.'+this.headElement.id+'_seek[rel='+this.index+']').addClass('active');
                this.items.children().removeClass('active');
                var ef = this.items.children();
                $(ef[parent.index]).addClass('active');
            }
            this.disable(this.index);
        },
        move:function(){
            this.seek(this.index+this.options.step);
        },
        next:function(){
            var parent = this;
            if(this.options.autoscroll==true){
                this.pause();
                setTimeout(function(){
                    parent.play();
                },this.options.speed);
            }
            this.seek(this.index+1);
        },
        prev:function(){
            var parent = this;
            if(this.options.autoscroll==true){
                this.pause();
                setTimeout(function(){
                    parent.play();
                },this.options.speed);
            }
            this.seek(this.index-1);
        },
        goTo:function(i){
            this.index = i-1;
            var parent = this;
            if(this.options.autoscroll==true && this.stop!=true){
                this.pause();
                setTimeout(function(){
                    parent.play();
                },this.options.speed);
            }
            this.seek(i);
        },
        play:function(){
            this.options.autoplay = true;
            this.options.autoscroll = true;
            this.options.onPlay(this);
            this.stop = false;
            this.autoscroll();
        },
        pause:function(o){
            this.options.onPause(this);
            if(this.options.autoscroll==true){
                this.timer = window.clearInterval(this.timer);
            }if(o==true){
                this.stop = true;
            }
        }
    });
    
    $s.extend({
        defaults: function(d){
            return $.extend(defaults, d || {});
        },
        intval: function(v){
            v = parseInt(v);
            return isNaN(v) ? 0 : v;
        }
    });

})(jQuery);
