/**
 * 右侧快速操作
 * kongge@office.weiphone.com
 * 2012.06.07
*/
jQuery(function ($) {
    //创建DOM
    var
	quickHTML = '<div class="quick_links_panel"><div id="quick_links" class="quick_links"><a href="#top" class="return_top"><i class="top"></i><span>返回顶部</span></a><a href="#" class="my_qlinks"><i class="setting"></i><span>个人中心</span></a><a href="#" class="message_list" ><i class="message"></i><span>站内消息</span><em class="num" style="display:none"></em></a><a href="#" class="leave_message"><i class="qa"></i><span>联系客服</span></a></div><div class="quick_toggle"><a href="javascript:;" class="toggle" title="展开/收起">&nbsp;</a></div></div><div id="quick_links_pop" class="quick_links_pop hide"></div>',
	quickShell = $(document.createElement('div')).html(quickHTML).addClass('quick_links_wrap'),
	quickLinks = quickShell.find('.quick_links');
    quickPanel = quickLinks.parent();
    quickShell.appendTo('body');

    //具体数据操作 
    var
	quickPopXHR,
	loadingTmpl = '<div class="loading" style="padding:30px 80px"><i></i><span>Loading...</span></div>',
	popTmpl = '<div class="title"><h3><i></i><%=title%></h3></div><div class="pop_panel"><%=content%></div><div class="arrow"><i></i></div><div class="fix_bg"></div>',
	quickPop = quickShell.find('#quick_links_pop'),
	quickDataFns = {
	    //个人中心
	    my_qlinks: {
	        title: '用户中心',
	        content: '<div class="links"><ul><li><a href="/user/">用户面板</a></li><li><a href="/user/order.html">订单管理</a></li><li><a href="/user/pay.html">在线充值</a></li><li><a href="/user/service.html">有问必答</a></li><li><a href="/user/pricelist.html">我的价格</a></li><li><a href="/user/moneylog.html">财务记录</a></li><li><a href="/user/editinfo.html">会员资料</a></li></ul></div>',
	        init: $.noop
	    },

	    //站内消息
	    message_list: {
	        title: '站内消息',
	        content: loadingTmpl,
	        init: function (ops) {
	            CommonEventAjax(5 * 1000, "/common/ashx/global.ashx", { "Action": "GetSiteMsg" }, function (data) {
	                var shell = $('#quick_links a.message_list');
	                if (0 == data - 0) {
	                    shell.find('em').remove();
	                } else {
	                    var num = data - 0;
	                    //消息总数最大只显示 99
	                    shell.append('<em class="num"><b>' + Math.min(99, num) + '</b></em>').show();
	                }
	                quickPop.html(ds.tmpl(popTmpl, {
	                    title: ops.title,
	                    content: '<div style="padding:50px;"><a href="/user/message.html">共有' + data + '条未读消息，点击查看</a></div>'
	                }));
	            });
	        }
	    },

	    //给客服留言
	    leave_message: {
	        title: '联系客服',
	        content: loadingTmpl,
	        init: function (ops) {
	            CommonEventAjax(5 * 1000, "/common/ashx/global.ashx", { "Action": "GetServiceData" }, function (data) {
	                var RelStr = '<div style=\"padding:10px;line-height:28px;\">';
	                var obj = eval(data);
	                var QQStr = '';
	                var TelStr = '';
	                var WWStr = '';
	                for (var i = 0; i < obj.length; i++) {
	                    if (obj[i].ServiceType == 1) {
	                        QQStr += '<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=' + obj[i].ServiceValue + '&site=qq&menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=2:' + obj[i].ServiceValue + ':52" alt="点击这里给我发消息" title="点击这里给我发消息"/>' + obj[i].ServiceName + '</a></li>';
	                    }
	                    if (obj[i].ServiceType == 2) {
	                        WWStr += '<li><a target="_blank" href="http://web.im.alisoft.com/msg.aw?v=2&uid=' + obj[i].ServiceValue + '&site=cnalichn&s=10"><img border="0" src="http://web.im.alisoft.com/online.aw?v=2&uid=' + obj[i].ServiceValue + '&site=cnalichn&s=10" alt="点击这里给我发消息" /></a></li>';
	                    }
	                    if (obj[i].ServiceType == 3) {
	                        TelStr += '<li>' + obj[i].ServiceName + ' ' + obj[i].ServiceValue + '</li>';
	                    }
	                }
	                RelStr += '<ul>';
	                if (QQStr != '') {
	                    RelStr += '<li>腾讯QQ</li>';
	                    RelStr += QQStr;
	                }
	                if (WWStr != '') {
	                    RelStr += '<li>淘宝旺旺</li>';
	                    RelStr += WWStr;
	                }
	                if (TelStr != '') {
	                    RelStr += '<li>客服电话</li>';
	                    RelStr += TelStr;
	                }
	                RelStr += '</ul>';
	                RelStr += '</div>';
	                console.log(RelStr);
	                quickPop.html(ds.tmpl(popTmpl, {
	                    title: ops.title,
	                    content: '<div>' + RelStr + '</div>'
	                }));
	            });
	            
	        }
	    }
	};

    //showQuickPop
    var
	prevPopType,
	prevTrigger,
	doc = $(document),
	popDisplayed = false,
	hideQuickPop = function () {
	    if (prevTrigger) {
	        prevTrigger.removeClass('current');
	    }
	    popDisplayed = false;
	    prevPopType = '';
	    quickPop.hide();
	},
	showQuickPop = function (type) {
	    if (quickPopXHR && quickPopXHR.abort) {
	        quickPopXHR.abort();
	    }
	    if (type !== prevPopType) {
	        var fn = quickDataFns[type];
	        quickPop.html(ds.tmpl(popTmpl, fn));
	        fn.init.call(this, fn);
	    }
	    doc.unbind('click.quick_links').one('click.quick_links', hideQuickPop);

	    quickPop[0].className = 'quick_links_pop quick_' + type;
	    popDisplayed = true;
	    prevPopType = type;
	    quickPop.show();
	};
    quickShell.bind('click.quick_links', function (e) {
        e.stopPropagation();
    });

    //通用事件处理
    var
	view = $(window),
	quickLinkCollapsed = !!ds.getCookie('ql_collapse'),
	getHandlerType = function (className) {
	    return className.replace(/current/g, '').replace(/\s+/, '');
	},
	showPopFn = function () {
	    var type = getHandlerType(this.className);
	    if (popDisplayed && type === prevPopType) {
	        return hideQuickPop();
	    }
	    showQuickPop(this.className);
	    if (prevTrigger) {
	        prevTrigger.removeClass('current');
	    }
	    prevTrigger = $(this).addClass('current');
	},
	quickHandlers = {
	    //购物车，最近浏览，商品咨询
	    my_qlinks: showPopFn,
	    message_list: showPopFn,
	    leave_message: showPopFn,
	    //返回顶部
	    return_top: function () {
	        ds.scrollTo(0, 0);
	        hideReturnTop();
	    },
	    toggle: function () {
	        quickLinkCollapsed = !quickLinkCollapsed;

	        quickShell[quickLinkCollapsed ? 'addClass' : 'removeClass']('quick_links_min');
	        ds.setCookie('ql_collapse', quickLinkCollapsed ? '1' : '', 30);
	    }
	};
    quickShell.delegate('a', 'click', function (e) {
        var type = getHandlerType(this.className);
        if (type && quickHandlers[type]) {
            quickHandlers[type].call(this);
            e.preventDefault();
        }
    });

    //Return top
    var scrollTimer, resizeTimer, minWidth = 1350;

    function resizeHandler() {
        clearTimeout(scrollTimer);
        scrollTimer = setTimeout(checkScroll, 160);
    }
    function checkResize() {
        quickShell[view.width() > 1340 ? 'removeClass' : 'addClass']('quick_links_dockright');
    }
    function scrollHandler() {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(checkResize, 160);
    }
    function checkScroll() {
        view.scrollTop() > 100 ? showReturnTop() : hideReturnTop();
    }
    function showReturnTop() {
        quickPanel.addClass('quick_links_allow_gotop');
    }
    function hideReturnTop() {
        quickPanel.removeClass('quick_links_allow_gotop');
    }

    view.bind('scroll.go_top', resizeHandler).bind('resize.quick_links', scrollHandler);
    quickLinkCollapsed && quickShell.addClass('quick_links_min');
    resizeHandler();
    scrollHandler();

});

//首次加载站内消息总数
jQuery(function ($) {
    var shell = $('#quick_links a.message_list');
    if (shell.find("em").length) {
        CommonEventAjax(5 * 1000, "/common/ashx/global.ashx", { "Action": "GetSiteMsg" }, function (data) {
            if (0 == data - 0) {
                shell.find('em').remove();
            } else {
                var num = data - 0;
                //消息总数最大只显示 99
                shell.append('<em class="num"><b>' + Math.min(99, num) + '</b></em>').show();
            }
        });

    }
});