(function($){
    $.fn.extend({
        myalbum:function(options){
            /*设定全局公共属性并写入json对象，外部均可修改*/
            var json={
                tab_tt_tag:".albumlist_tt ul",
                tab_tt_childtag:"li",
                tab_tt_container_tag:".items_list_container",
                tab_ct_tag:".albumlist_ct ul",
                tab_ct_childtag:"li",
                prev_tag:".prev",
                next_tag:".next",
                focusclassname:"focus",
                type:"mouseover",
                seconds_intval:5000,
                seconds_fade:600,
                seconds_animate:80,
                start:0,
                slideby:6,
                autoplay:true  //设置是否为自动播放
            };
            $.fn.extend(json,options);
            var a=json.start;
            var obj_tab_tt=$(this).find(json.tab_tt_tag);
            var obj_tab_tt_container=$(this).find(json.tab_tt_container_tag);
            var obj_tab_ct=$(this).find(json.tab_ct_tag);
            var obj_prev=$(this).find(json.prev_tag);
            var obj_next=$(this).find(json.next_tag);
            obj_tab_tt.each(function(i){
                var obj_tab_tt_child=obj_tab_tt.eq(i).find(json.tab_tt_childtag);
                var obj_tab_ct_child=obj_tab_ct.eq(i).find(json.tab_ct_childtag);
                var obj_tab_tt_container_width=obj_tab_tt_container.eq(i).width();
                var obj_tab_tt_width=parseInt(obj_tab_tt_child.eq(i).width()*obj_tab_tt_child.size());
                /*默认载入 实现定位功能*/
                obj_tab_tt_child.eq(json.start).addClass(json.focusclassname);
                obj_tab_ct_child.eq(json.start).fadeIn(json.seconds_fade);
                /*默认载入 实现自动播放*/
                intal_playtab();
                /*mouseover mouseout事件*/
                obj_tab_tt_child.mouseover(function(){
                    //alert("okok");
                    if(myTime){
                        clearInterval(myTime);
                    }
                    a=obj_tab_tt_child.index(this);
                    playtab(a);                                        
                }).mouseout(function(){
                    intal_playtab();    
                });
                obj_tab_ct_child.mouseover(function(){
                    if(myTime){
                        clearInterval(myTime);
                    }
                }).mouseout(function(){
                    intal_playtab();            
                });
                /*左右箭头点击前后翻转效果*/
                obj_next.eq(i).click(function(){
                    if(myTime){
                        clearInterval(myTime);
                    }
                    /*解决点击前后按钮后的focus样式定位*/
                    var left_data=parseInt(obj_tab_tt.eq(i).css("left"))-parseInt(json.slideby*obj_tab_tt_child.width());
                    obj_tab_tt.eq(i).animate({
                        left:left_data                         
                    },json.seconds_animate,function(){
                        //判断是否已到最后一张    
                        if(Math.abs(left_data)>=obj_tab_tt_width){
                            left_data=0;
                            obj_tab_tt.eq(i).css({left:left_data});
                        }
                        next_row_focus();
                        intal_playtab();
                    });
                });
                obj_prev.eq(i).click(function(){
                    if(myTime){
                        clearInterval(myTime);
                    }
                    var left_data=parseInt(json.slideby*obj_tab_tt_child.width())+parseInt(obj_tab_tt.eq(i).css("left"));
                    obj_tab_tt.eq(i).animate({
                        left:left_data                         
                    },json.seconds_animate,function(){
                        //判断是否已到第一张
                        if(left_data>=0){
                            left_data=0;    
                            obj_tab_tt.eq(i).css({left:left_data});
                        }
                        prev_row_focus();
                        intal_playtab();
                    });
                });
                /*全局选项卡函数*/
                function playtab(a){
                    obj_tab_tt_child.eq(a).addClass(json.focusclassname).siblings().removeClass(json.focusclassname);
                    obj_tab_ct_child.eq(a).fadeIn(json.seconds_fade).siblings().fadeOut(json.seconds_fade)
                }
                /*全局幻灯片播放函数*/
                function intal_playtab(){
                    if(json.autoplay==true){
                        myTime=setInterval(function(){
                            //alert(a);
                            if(a%json.slideby==0&&a!=0){
                                if(myTime){
                                    clearInterval(myTime);
                                }
                                var focus_row=parseInt(a/parseInt(json.slideby));
                                dataleft=-parseInt(focus_row*obj_tab_tt_container_width);
                                obj_tab_tt.eq(i).animate({
                                    left:dataleft               
                                },json.seconds_animate,function(){
                                    intal_playtab();
                                });
                            }
                            if(a==obj_tab_tt_child.size()){
                                a=0;
                                obj_tab_tt.eq(i).animate({
                                    left:0               
                                },json.seconds_animate,function(){
                                    return;
                                });
                            }
                            playtab(a);
                            a++;
                        },json.seconds_intval);    
                    }else{
                        myTime=function(){
                            return ;    
                        }
                    }
                }
                /*切换后一排的定位函数*/
                function next_row_focus(){
                    var focus_row=parseInt(a/parseInt(json.slideby))+1;
                    //alert(focus_row);
                    a=focus_row*json.slideby;
                    if(a>=obj_tab_tt_child.size()){
                        a=0;    
                    }
                    playtab(a);
                }
                /*切换前一排的定位函数*/
                function prev_row_focus(){
                    var focus_row=parseInt(a/parseInt(json.slideby))-1;
                    a=focus_row*json.slideby;
                    if(a<=0){
                        a=0;    
                    }
                    playtab(a);
                }
            });
        }
    });              
})(jQuery);
