var scrolltotop={
    /* setting参数:
    *       startline: 出现回到顶部按钮的滚动条scrollTop距离
    *       scrollto: 点击后设置scrollTop的值.
    *       scrollduration: 回到顶部的持续时间
    *       fadeduration: 淡入淡出的持续时间
    * */
	setting: {
        startline:200,
        scrollto: 0,
        scrollduration:1000,
        fadeduration:[500, 100],
        controlHTML: '',
        controlOffsetX:5,
        controlOffsetY:5,
        pageWidth:950,
        anchorkeyword:'#top'
    },
	/*controlHTML:'<img src="go-top.png" style="width:24px; height:24px" />', //回到顶部的html代码 ID="topcontrol"
	controlattrs: {offsetx:5, offsety:5}, //回到顶部 right/ bottom 位置
	anchorkeyword: '#top', //猫点链接*/

	state: {isvisible:false, shouldvisible:false},

	scrollup:function(){
        //如果绝对定位
		if (!this.cssfixedsupport){
			this.$control.css({opacity:0});
		}
        //检测setting.scrollto是否为数字
		var dest=isNaN(this.options.scrollto)? this.options.scrollto : parseInt(this.options.scrollto);
	    //如果setting.scrollto为字符串，并且#dest元素存在，当猫点用。
        if (typeof dest=="string" && jQuery('#'+dest).length==1){
			dest=jQuery('#'+dest).offset().top;//#dest元素的绝对位置
            alert(dest)
        }else{
			dest=0;
        }
        //回到顶部
		this.$body.animate({scrollTop: dest}, this.options.scrollduration);
	},
    controlXY:function(){
        var $window=jQuery(window);
        var $windowWidth=$window.width();
        var controlx;
        if(this.options.pageWidth==null){
            controlx=0;
        }else{

            if ($windowWidth > this.options.controlOffsetX * 2 + this.options.pageWidth) {
                controlx = ($windowWidth - this.options.pageWidth) / 2 + this.options.pageWidth + this.options.controlOffsetX;
            }else{
                controlx = $windowWidth - this.$control.width()-this.options.controlOffsetX;
            }
        }

        var controly=$window.height() - this.$control.height()-this.options.controlOffsetY;
		controly=!this.cssfixedsupport ? $window.scrollTop() + controly : controly;
        return [controlx,controly];
    },

	togglecontrol:function(){
        clearTimeout(scrolltotop.timer);
		var scrolltop=jQuery(window).scrollTop();

        this.$control.css({
            position : scrolltotop.cssfixedsupport ? 'fixed' : 'absolute',
            top : scrolltotop.controlXY()[1]
            //left : scrolltotop.controlXY()[0]
        });
        if(this.options.pageWidth==null){
            this.$control.css({
                right : scrolltotop.controlXY()[0]
            });
        }else{
            this.$control.css({
                left : scrolltotop.controlXY()[0]
            });
        }
        //scrollTop是否大于setting.startline
        //alert(this.options.startline)
		this.state.shouldvisible = ( scrolltop >= this.options.startline ) ? true : false;
		if (this.state.shouldvisible && !this.state.isvisible){
			this.$control.stop().animate({opacity:1}, this.options.fadeduration[0]);
			this.state.isvisible=true;
		}else if (this.state.shouldvisible == false && this.state.isvisible){
			this.$control.stop().animate({opacity:0}, this.options.fadeduration[1]);
			this.state.isvisible=false;
		}
	},
	
	init:function(options){
        this.options = jQuery.extend({},scrolltotop.setting, options);
        var that=this;
        //var mainobj=scrolltotop;
        var iebrws=document.all;//判断IE
        //不是IE或者IE7+浏览器
        scrolltotop.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest;
        //opera的$('body')
        scrolltotop.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body');
        //回到顶部的属性

        scrolltotop.$control=$('<div id="topcontrol" class="to_top">'+this.options.controlHTML+'</div>')
            .css({
                opacity : 0,
                cursor : 'pointer'
            })
            .attr({title:'回到顶部'})
            .click(function(event){
                scrolltotop.scrollup();
                event.preventDefault();
            })
            .appendTo('body');
        //检查ie6及一下浏览器
        if (document.all && !window.XMLHttpRequest && scrolltotop.$control.text()!=''){
            scrolltotop.$control.css({width:scrolltotop.$control.width()});
        }
        //显示或隐藏"回到顶部"
        scrolltotop.togglecontrol();
        //猫点链接回到顶部快速链接
        $('a[href="' + that.options.anchorkeyword +'"]').click(function(event){
            scrolltotop.scrollup();
            event.preventDefault();
        });
        //窗口onscroll及onresize事件绑定
        $(window).bind('scroll resize', function(e){
            scrolltotop.timer=setTimeout(function(){
                scrolltotop.togglecontrol()
            },500);
            //scrolltotop.togglecontrol();
        });
        return this;
	}
};


