(function($){
    if(typeof $ == 'undefined'){
        alert('missing jquery');
        return;
    }

    $.scroller = function(html,options,callback){
        var target = $(html);
        var settings = $.extend($.scroller.defaults, options);
        var _top = Math.max(settings.topMin,0);
        if($.support.fixedPosition && settings.animate == false){
            target.css({
                position:'fixed',
                top:settings.top,
                right:settings.right
            });
        }else{
            $(window).scroll(function(){
                var scrolltop = $(window).scrollTop();
                if(scrolltop >= settings.top){
                    _top = $(window).scrollTop() + settings.top;
                }
                if(_top < settings.topMin){
                    _top = settings.topMin;
                }
                if(settings.animate){
                    target.stop().animate({top:_top},300);
                }else{
                    target.css({top:_top},300);
                }
            }).resize(function(){
                    $(this).scroll();
                });
        }

        $('body').append(target);
        setTimeout(function(){
            callback(target)
        },500);
    };

    $.scroller.defaults = {
        top:0,
        right:0,
        topMin:0,
        duration:300,
        animate:true
    };
})(jQuery);


function randomsort(a, b) {
return Math.random()>.5 ? -1 : 1;//用Math.random()函数生成0~1之间的随机数与0.5比较，返回-1或1
}
var arr = ['<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601788&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601788:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601787&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601787:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601789&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601789:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601786&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601786:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=1848200810&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:1848200810:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601784&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601784:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=712412894&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:712412894:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>',
'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601785&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601785:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>'];
var new_arr = arr.sort(randomsort);
var html = '<div class="right_scroll_contextxt">'
    +'<a title="快速帮助" class="fast-tag1" data-ga="客服.在线帮助">在线帮助</a>'
    +'<div class="help-box" style="display: none;">'
    +'<p>'
    +'<a title="售前咨询" target="chat" href="#" class="help_button" data-ga="客服.售前咨询">售前咨询</a>'
    +'</p>'
    +'<ul class="help-list0">'
    + new_arr.join(" ")
    +'</ul>'
    +'<p>'
    +'<a title="售后支持" target="chat" href="#" class="help_button" data-ga="客服.售后支持">售后支持</a>'
    +'</p>'

    +'<ul class="help-list0">'
    +'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601780&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601780:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>'
    +'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601781&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601781:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>'
    +'</ul>'
    +'<p>'
    +'<a title="网站备案" target="chat" href="#" class="help_button" data-ga="客服.网站备案">网站备案</a>'
    +'</p>'
	+'<ul class="help-list0">'
    +'<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=613601783&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:613601783:51" alt="点击这里给我发消息" title="点击这里给我发消息"/></a></li>'
    +'</ul>'
    +'</div>'
    +'</div>';

$(document).ready(function(){
    if(!$.scroller) return;
    $.scroller(html,{
        top:150,
        right:0,
        topMin:440,
        duration:300
    },function(target){
        var help_box = target.find(".help-box");
        target.find(".fast-tag1").click(function(e){
            e.preventDefault();
            e.stopPropagation();
            if(help_box.css('display') == "block"){
                $(this).removeClass("fast-tag2");
                help_box.toggle();
            }else{
                $(this).addClass("fast-tag2");
                help_box.toggle();
            }
        });
        target.find('.help_button').click(function(e){
            e.preventDefault();
            e.stopPropagation();
            var userID = 'null';
            if(typeof $.cookie == 'function' && $.cookie('login_aliyunid') != null){
                userID = $.cookie('login_aliyunid').replace(/\"/g,'');
            }
            var winWidth = 950;
            var winHeight = 500;
            var left = parseInt(($(window).width() - winWidth)/2);
            var top = parseInt(($(window).height() - winHeight)/2);
            var url = $(this).attr('href');
            window.open(url+"?userID="+userID,'','height=' + winHeight + ',width=' + winWidth + ',left=' + left + ',top=' + top + ',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
        });
        //        scroll.js的埋点处理
        var gaDom = target.find("a[data-ga]");
        if(gaDom.length > 0 ) {
            gaDom.click(function(e){
                var _target = $(this);
                var _ga = _target.attr("data-ga");
                if (typeof (_gaq) != "undefined" && _ga != '' ) {
                    var _gaArr = _ga.split('.');
                    if(_gaArr.length < 2) return;
                    var category ='Click';//必填项，参数为字符串
                    var action ='';//必填项，参数为字符串
                    var label = '';//可选，参数为字符串
                    if(_gaArr.length == 2){
                        category ='Click';
                        action  =_gaArr[0];//必填项，参数为字符串
                        label  = _gaArr[1];//可选，参数为字符串
                    }
                    else{
                        category =_gaArr.shift();//必填项，参数为字符串
                        action =_gaArr.shift();//必填项，参数为字符串
                        label = _gaArr.join('.');//可选，参数为字符串
                    }

                    _gaq.push(['_trackEvent', category, action, label]);

                }
            });
        }
    });
});