(function($){


    
    
    $.fn.arpGallery = function(userOptions) {
        
        
        return this.each(function() {
            var options = $.extend({},{
            
            },userOptions);
            
            var curIndex = null;
            var theList = $(this);
            var total = theList.children().length;
            
            var prevLink = $('<a href="#">&laquo; <span>prev</span></a>');
            var nextLink = $('<a href="#"><span>next</span> &raquo;</a>');
            var mainNav = $('<div class="mainNav"></div>').append(prevLink).append(nextLink).insertAfter(theList);
            
            prevLink.click(function(){
                var newIndex = curIndex - 1;
                if(newIndex<0){
                    newIndex = total-1;
                }
                switchSlide(newIndex);
                return false;
            });
            nextLink.click(function(){
                var newIndex = curIndex + 1;
                if(newIndex==total){
                    newIndex = 0;
                }
                switchSlide(newIndex);
                return false;
            });
            
            
            
            
            var switchSlide=function(newIndex){
                if(curIndex==null){
                    theList.children().eq(newIndex).fadeIn();
                    curIndex = newIndex;
                }
                else{
                    theList.children().eq(curIndex).fadeOut(function(){
                        theList.children().eq(newIndex).fadeIn();
                        curIndex = newIndex;
                    });
                }
                indexNav.find(".cur").removeClass("cur");
                indexNav.children().eq(newIndex).addClass("cur");
                
            };
            
            var indexNav = $('<ul class="indexNav"></ul>');
            theList.children().each(function(){
                var pli = $(this);
                if(!pli.hasClass("intro")){
                    
                    $(window).load(function(){
                    pli.setUpSlide=function(theLi){
                        
                        var theLi=theLi;
                            var theImg = theLi.find(".container img");
                            if(theImg.height()>0 && theImg.width()>0){
                                var newHeight = theImg.height()+8;
                                var newWidth = theImg.width()+8;
                                marginTop = (508-newHeight)/2;
                                marginLeft = (508-newWidth)/2;
                                theLi.find(".container").height(newHeight).width(newWidth).css({margin:marginTop+"px "+marginLeft+"px"}).hover(
                                    function(){ theLi.find(".desc").fadeIn("fast"); },
                                    function(){ theLi.find(".desc").fadeOut("fast"); }
                                
                                );
                                theLi.find(".desc").width(newWidth-30);
                                pli.css({display:"none",visibility:"visible"});
                            }
                            else{  
                                
                                setTimeout(function(){ theLi.setUpSlide(theLi); },20);
                                
                            }
                        }
                        pli.setUpSlide(pli);
                    });
                    
                    
                    
                    
                    
                    
                }
                else{
                    var marginTop = (508-pli.find(".container").height())/2;
                    pli.find(".container").css({margin:marginTop+"px 0px"});
                    pli.css({display:"none",visibility:"visible"});
                }
                var pliIndex = theList.children().index(pli);
                indexNav.append(
                    $("<li></li>").append(
                        $('<a href="#">'+(pliIndex+1)+'</a>').wrap("<li></li>").click(function(){
                            switchSlide(pliIndex);
                            return false;
                        })
                    )
                ).append(" ");
                
                
            });
            indexNav.insertAfter(mainNav);
            
            
            
            
            
            
            switchSlide(0);
            
          


        });
    };

})(jQuery);
