﻿
function showTopicError(detail) {
    $alert('错误', detail, '');
};

function confirmDelete(pid, showtopicid) {
    $confirm('确认删除', '<h3>您确认删除吗？</h3>', 'open_shanchu', function() { deletePost(showtopicid, pid); });
};
function deletePost(tid, pid) {
    onAction('ajax/DeleteMyPost.aspx?action=1&topicid=' + tid + '&postid=' + pid + '&seed=' + Math.random(), '删除', '帖子');
};

function showRatePanel(pid, showtopicid) {
    var xhr = new $Ajax.Request('ajax/RatePost.aspx?topicid=' + showtopicid + '&postid=' + pid + '&seed=' + Math.random(),
    {
        method: 'get',
        onComplete: function(transport) {
            if (transport.responseText.indexOf('<!--ERROR:-->') > 0) {
                $alert('评分失败', transport.responseText, 'open_tijiaobcg');
            } else {
                $modal('评分', transport.responseText, 'open_pingfen');
                Reposition();
            }
        }
    }
    );
};

function postRate(pid, showtopicid) {
    var xhr = new $Ajax.Request('ajax/RatePost.aspx?topicid=' + showtopicid + '&postid=' + pid + '&seed=' + Math.random(),
    {
        
        method: 'post',
        postBody: $('ratepostform').serialize(),
        onComplete: function(transport) {
            if (transport.responseText == 'ok') {
                new $Ajax.Updater('ratelog' + pid, 'ajax/getpostratelog.aspx?topicid=' + showtopicid + '&postid=' + pid + '&seed=' + Math.random());
                $modal.close();
                postHint(pid, '谢谢，您的评分已提交成功！', 'open_jubaocg');
            }
            else {

                $alert('评分失败', '<h3>' + transport.responseText + '</h3>', 'open_tijiaobcg', null, $T.UI.ModalPopup.DOM);
                //postHint(pid, transport.responseText,'open_jubaobcg');
            }
        }
    }
    );
};

function showPeportPost(pid, floorNum, topicTitle, showtopicid) {
    new $Ajax.Request('ajax/ReportPost.aspx?seed=' + Math.random(),
    {
        method: 'get',
        parameters: { 'topicid': showtopicid, 'postid': pid, 'url': encodeURI(location.href), 'floorNum': floorNum, 'topicTitle': encodeURI(topicTitle) },
        onComplete: function(transport) {
            if (transport.responseText == "noLogin") {
                postHint(pid, "未登录不能举报!", 'open_jubaobcg');
            }
            else if (transport.responseText == "reReport") {
                postHint(pid, "不要重复举报!", 'open_jubaobcg');
            }
            else {
                $modal('举报', transport.responseText, 'open_pingfen');
                Reposition();
                BindWaterMark("ta_moreReason", "输入更多举报理由，字数不超过50个汉字")
            }
        }
    });
};
function PeportPost(request, pid) {
    var reason = '';
    var moreReason = '';
    $$("input[name='cb_reason']").each(function(item) {
        if (item.checked) {
            reason += (item.value + ",");
        }
    });
    reason = reason.substring(0, reason.length - 1);
    moreReason = $F("ta_moreReason") == "输入更多举报理由，字数不超过50个汉字" ? "" : $F("ta_moreReason");
    new $Ajax.Request(request,
    {
        method: 'post',
        parameters: { 'reason': reason, 'moreReason': moreReason },
        onComplete: function(transport) {
            if (transport.responseText == 'ok') {
                $modal.close();
                postHint(pid, '谢谢，您的举报已提交成功！', 'open_jubaocg');
            }
            else {
                $("spanMsg").show();
                $("spanMsg").style.color = 'red';
                $("spanMsg").innerHTML = transport.responseText;
                setInterval(function() { $("spanMsg").hide(); }, 1500);
            }

        }
    });
}

function addFriend(uid) {
    var xhr = new $Ajax.Request('ajax/AddFriend.ashx?f=' + uid + '&seed=' + Math.random(),
    {
        method: 'get',
        onComplete: function(transport) {
            if (transport.responseText == 'ok') {
                $alert('请求发送成功', '<h3>您的好友请求已发送成功，请耐心等待！</h3>', 'open_tijiaocg');
            } else {
                $alert('请求发送失败', '<h3>' + transport.responseText + '</h3>', 'open_tijiaobcg');
            }
        }
    }
    );
}

function postHint(pid, contents, cssClass, onHide) {
    var hint = $('posthint_' + pid);
    if (hint) {
        hint.className = cssClass;
        hint.style.display = '';
        hint.innerHTML = contents;
        hint.timer = setTimeout(function() {
            hidePostHint(pid);
            if (onHide) {
                onHide();
            }
        }, 3000);
        hint.parentNode.style.position = 'relative';
    }
};
function hidePostHint(pid) {
    var hint = $('posthint_' + pid);
    if (hint) {
        hint.style.display = 'none';
        if (hint.timer) {
            clearTimeout(hint.timer);
        }
        hint.parentNode.style.position = 'static';
    }
};
$T.Util.ExecuteOnLoaded(function() {
    if (location.hash != '' && location.hash != null && location.hash != '#') {
        var anchors = $$('a[name=' + location.hash + ']');
        if (anchors && anchors.length > 0) {
            anchors[0].scrollTo();
        }
    }
});
function BindWaterMark(dom, watermark, initFocus) {
    dom = $(dom);
    if (dom) {
        dom.stopObserving('focus');
        dom.stopObserving('blur');
        dom.watermark = watermark;
        if (dom.value == '') {
            dom.value = dom.watermark;
            dom.style.color = '#999';
        }
        else {
            dom.style.color = '#333';
        }
        dom.observe('focus', function(event) {
            if (dom.value == dom.watermark) {
                dom.style.color = '#333';
                dom.value = '';
            }
        });

        dom.observe('blur', function(event) {
            if (dom.value == '') {
                dom.style.color = '#999';
                dom.value = dom.watermark;
            }
        });
        if (typeof initFocus != 'undefined' && initFocus) {
            dom.focus();
        }
    }
}
function Reposition() {
    var dom = $T.UI.ModalPopup.DOM != null ? $T.UI.ModalPopup.DOM : $T.UI.Confirm.DOM;
    if (dom) {
        if ($B.IE6 == true) {
            dom.style.position = 'absolute';
            dom.style.top = Math.floor(document.documentElement.scrollTop + document.documentElement.clientHeight / 2) + 'px';
            dom.style.left = Math.floor(document.documentElement.scrollLeft + document.documentElement.clientWidth / 2) + 'px';
        } else {
            dom.style.top = '50%';
            dom.style.left = '50%';
        }
        dom.style.marginLeft = (dom.offsetWidth / -2) + 'px';
        dom.style.marginTop = (dom.offsetHeight / -2) + 'px';
    }
}
function forumShareTopic(userid, tid) {
    var url = "ajax/WebShare.ashx";
    new $Ajax.Request(url, {
        parameters: { userid: userid, tid: tid },
        onComplete: function() {}
    });
}

//图片延时加载
Element.addMethods({
    lazyload: function(element, options) {
        function $restore() {
            if (true === $(element).hasAttribute('_src')) {
                $(element).writeAttribute({ src: $(element).readAttribute('_src') });
            }
        }
        function $scroll() {
            var scroll_y = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
            return parseInt(scroll_y);
        }
        function $height() {
            var window_height = window.innerHeight || document.documentElement.clientHeight;
            return parseInt(window_height);
        }
        var element = $(element);
        var options = Object.extend({
            threshold: 0,
            placeholder: 'http://pic.baa.bitautotech.com/webpic/images/loading.gif',
            event: 'scroll',
            frequency: 0.1
        }, options || {});

        var offset = $(element).cumulativeOffset()[1];
        var activate_on = (offset - options.threshold) - $height();

//        var old_source = $(element).readAttribute('_src');
//        var new_source = options.placeholder;

//        $(element)
//            .writeAttribute({ src: new_source })
//            .writeAttribute({ '_src': old_source });

        if ('scroll' === options.event) {
            new PeriodicalExecuter(function($executor) {
                if (activate_on <= $scroll()) {
                    $restore(); $executor.stop();
                }
            }, options.frequency);
        }
        else {
            $(element).observe(options.event, function(event) {
                $restore(); $(element).stopObserving();
            });
        }

        return $(element);
    }
});
function setcopy(text, alertmsg) {
    if (document.all) {
        clipboardData.setData('Text', text);
        alert(alertmsg);
    } else if (prompt('请按 Ctrl+C 将链接复制到剪贴板。', text)) {
        alert(alertmsg);
    }
}
function copyAdress(text, alertmsg, pid) 
{
    if (document.all) 
    {
        if (clipboardData.setData('Text', text)) {
            postHint(pid, alertmsg, 'open_jubaocg');
        }
    }
    else if (prompt('请按 Ctrl+C 将链接复制到剪贴板。', text)) 
    {
        postHint(pid, alertmsg, 'open_jubaocg'); 
    }
}
