(function($){
    $.fn.salah_slider = function(options){
        var defaults = {
            width: 400,
            height: 400,
            speed: 3000,
            easing: "",
            next:'',
            prev:'',
            curr_slide:1,
            dir:'left',
            slides_no:1,
            interval:0,
            active : 'active',
            slides_cont:'.slides',
            children:'li',
            parent_slide:'',
            parent_slide_extra_hw:0,
            parent_extra_hw:0,
            generate_slider:false,
            slider:'.slider',
            slider_sub:'a',
            flexible:false
        };        
        var options = $.extend(defaults, options);
        return this.each(function(){
            $(this).wrap('<div class="warp" style="position:relative"></div>');
            var slide = $(this).parent();
            if(options.generate_slider==true){
                var slider='<div class="'+(options.slider).substr(1)+'">';
                for(i=0;i<$(options.slides_cont+">"+options.children,slide).length;i++){
                    slider+='<a href="#">'+i+'</a>';
                }
                slider+='</div>';
                $(slide).append(slider);
            }
            else{
                var slider_obj= $(slide).prev(options.slider);
                $(slide).append(slider_obj);
            }

            switch(options.dir){
                case "up":
                    $(options.slides_cont,slide).css({
                        "height":($(options.slides_cont+">"+options.children,slide).length*options.height)+"px",
                        'top':'0px',
                        'position':'absolute',
                        'bottom':'auto'
                    });
                    $(options.slides_cont+'>'+options.children,slide).css({
                        "height":options.height
                    });
                    break;
                case "down":
                    $(options.slides_cont,slide).css({
                        "height":($(options.slides_cont+">"+options.children,slide).length*options.height)+"px",
                        'bottom':'0px',
                        'position':'absolute',
                        'top':'auto'
                    });
                    $(options.slides_cont+'>'+options.children,slide).css({
                        "height":options.height
                    });
                    break;
                case "right":
                    if(options.flexible==true){
                        newheight=($(options.slides_cont+'>'+options.children+':eq('+(0)+')',slide).outerHeight());
                        $(options.slides_cont,slide).stop().animate({
                            'height':(newheight+'px')
                        },options.speed/2,function(){
                            newheight=($(options.slides_cont+'>'+options.children+':eq('+(0)+')',slide).outerHeight());
                            $(options.slides_cont,slide).animate({
                                'height':(newheight+'px')
                            },100);
                        });
                        $(options.slides_cont,slide).parent().stop().animate({
                            'height':((newheight+options.parent_extra_hw)+'px')
                        },0,options.easing).addClass('slides-box');

                    }
                    $(options.slides_cont,slide).css({
                        "width":($(options.slides_cont+">"+options.children,slide).length*options.width)+"px",
                        'right':'0px',
                        'position':'absolute',
                        'left':'auto'
                    });
                    $(options.slides_cont+'>'+options.children,slide).css({
                        "width":options.width,
                        'float':'right',
                        'overflow':'hidden'
                    });
                    break;
                case "left":
                default :
                    if(options.flexible==true){
//                        setTimeout();
                        newheight=($(options.slides_cont+'>'+options.children+':eq('+(0)+')',slide).outerHeight());                        
                        $(options.slides_cont,slide).stop().animate({
                            'height':(newheight+'px')
                        },options.speed/2,function(){
                            newheight=($(options.slides_cont+'>'+options.children+':eq('+(0)+')',slide).outerHeight());
                            $(options.slides_cont,slide).animate({
                                'height':(newheight+'px')
                            },100);
                        });
                        newheight=($(options.slides_cont+'>'+options.children+':eq('+(0)+')',slide).outerHeight());
                        
                        $(options.slides_cont,slide).parent().stop().animate({
                            'height':((newheight+options.parent_extra_hw)+'px')
                        },0,options.easing).addClass('slides-box');
                            
                            
                    }
                    if(options.width!='auto'){
                        $(options.slides_cont,slide).css({
                            "width":($(options.slides_cont+">"+options.children,slide).length*options.width)+"px",
                            'left':'0px',
                            'position':'absolute',
                            'right':'auto'
                        });
                        $(options.slides_cont+'>'+options.children,slide).css({
                            "width":options.width,
                            'float':'left',
                            'overflow':'hidden'
                        });
                    }else{
                        var total_width=0;
                        $(options.slides_cont+">"+options.children,slide).each(function(){
                            total_width+=$(this).outerWidth();
                        });
                        //                        alert(total_width)
                        $(options.slides_cont,slide).css({
                            "width":(total_width)+"px",
                            'left':'0px',
                            'position':'absolute',
                            'right':'auto'
                        });
                    }
                    break;
            }

            
                
            $(options.slider+" "+options.slider_sub).removeClass(options.active);
            $(options.slider+" "+options.slider_sub+":first").addClass(options.active);
            $(options.slider+" "+options.slider_sub).click(function(){			
                options.curr_slide=$(options.slider+" "+options.slider_sub).index(this)+1;
                move_slides(options.curr_slide);
                return false
            });
            
            var currentlly_moving=false;
			
            var next=function next(){
                if(currentlly_moving==false){
                    var temp_interval=options.interval;
                    options.curr_slide+=options.slides_no;
                    if(options.curr_slide>$(options.slides_cont+">"+options.children,slide).length){
                        options.curr_slide=options.slides_no;
                        temp_interval=options.interval+10000
                    }
                    move_slides(options.curr_slide);
                    if(options.interval>0){
                        setTimeout(next,temp_interval);
                    }
                }
                return false;
            }
            $(options.next,slide).click(next);

            $(options.prev,slide).click(function(){
                options.curr_slide-=options.slides_no;
                if(options.curr_slide<=0)
                    options.curr_slide=$(options.slides_cont+">"+options.children,slide).length;
                move_slides(options.curr_slide);
                return false;
            })

            if(options.interval>0){
                setTimeout(next,options.interval);
            }
			
            function move_slides(slide_num){
                currentlly_moving=true;
                num= slide_num;                
                $(options.slider+" "+options.slider_sub).removeClass(options.active);
                $(options.slider+" "+options.slider_sub+":nth-child("+(options.curr_slide)+")").addClass(options.active);
                switch(options.dir){
                    case "up":
                        newpos=-(num-options.slides_no)*options.height;
                        $(options.slides_cont,slide).stop().animate({
                            "top":newpos+"px"
                        },options.speed,options.easing,function(){currentlly_moving=false;});
                        break;
                    case "down":
                        newpos=-(num-options.slides_no)*options.height;
                        $(options.slides_cont,slide).stop().animate({
                            "bottom":newpos+"px"
                        },options.speed,options.easing,function(){currentlly_moving=false;});
                        break;
                    case "right":
                        newpos=-(num-options.slides_no)*options.width;
                        if(options.flexible==true){
                            newheight=($(options.slides_cont+'>'+options.children+':eq('+(num-1)+')',slide).outerHeight());                            
                            $(options.slides_cont,slide).stop().animate({
                                'height':(newheight+'px')
                            },options.speed/2,function(){currentlly_moving=false;});
                            $(options.slides_cont,slide).stop().animate({
                                "right":newpos+"px",
                                'height':(newheight+'px')
                            },options.speed,options.easing,function(){currentlly_moving=false;});
                            $(options.slides_cont,slide).parent().stop().animate({
                                'height':((newheight+options.parent_extra_hw)+'px')
                            },options.speed,options.easing,function(){currentlly_moving=false;});
                            if(options.parent_slide!=''){
                                $(slide).parent().parent(options.parent_slide).stop().animate({
                                    'height':((newheight+options.parent_extra_hw+options.parent_slide_extra_hw)+'px')
                                },options.speed,options.easing,function(){currentlly_moving=false;});
                                if($(slide).parent().parent(options.parent_slide).length>0)
                                    $(slide).parent().stop().animate({
                                        'height':((newheight+options.parent_extra_hw)+'px')
                                    },options.speed,options.easing,function(){currentlly_moving=false;});

                            }
                        }else{
                            $(options.slides_cont,slide).stop().animate({
                                "right":newpos+"px"
                            },options.speed,options.easing,function(){currentlly_moving=false;});
                        }
                        break;
                    case "left":
                    default :
                        if(options.width!='auto'){
                            newpos=-(num-options.slides_no)*options.width;
                        }else{
                            var newpos=0;
                            var slides_counter=0;
                            $(options.slides_cont+">"+options.children,slide).each(function(){
                                slides_counter++;
                                if(num<slides_counter)
                                    newpos-=$(this).outerWidth();
                                else
                                    return;
                            //                                alert($(options.slides_cont+">"+options.children,slide+"["+slides_counter+"]").html())
                            });
                        //newpos=-(num-options.slides_no)*options.width;                            
                        }
                        //alert(newpos);
                        if(options.flexible==true){
                            newheight=($(options.slides_cont+'>'+options.children+':eq('+(num-1)+')',slide).outerHeight());
                            $(options.slides_cont,slide).stop().animate({
                                'height':(newheight+'px')
                            },options.speed/2,function(){currentlly_moving=false;});
                            $(options.slides_cont,slide).stop().animate({
                                "left":newpos+"px",
                                'height':(newheight+'px')
                            },options.speed,options.easing,function(){currentlly_moving=false;});
                            $(options.slides_cont,slide).parent().stop().animate({
                                'height':((newheight+options.parent_extra_hw)+'px')
                            },options.speed,options.easing,function(){currentlly_moving=false;});
                            if(options.parent_slide!=''){
                                $(slide).parent().parent(options.parent_slide).stop().animate({
                                    'height':((newheight+options.parent_extra_hw+options.parent_slide_extra_hw)+'px')
                                },options.speed,options.easing,function(){currentlly_moving=false;});
                                if($(slide).parent().parent(options.parent_slide).length>0)
                                    $(slide).parent().stop().animate({
                                        'height':((newheight+options.parent_extra_hw)+'px')
                                    },options.speed,options.easing,function(){currentlly_moving=false;});
                                
                            }
                        }else{
                            $(options.slides_cont,slide).stop().animate({
                                "left":newpos+"px"
                            },options.speed,options.easing,function(){currentlly_moving=false;});
                        }
                        
                        break;
                }
            }
            if (window.location.hash) {
                options.curr_slide = window.location.hash.substr(2);
                options.curr_slide++;
                move_slides(options.curr_slide);
            }
            else{
                options.curr_slide = 1;                
                move_slides(options.curr_slide);                
            }
        });
    };
})(jQuery);
