﻿$(function () {
    setCurrentTab();
    $('.dvContent img').each(function (i, n) {
        if ($(this).width() > 580) {
            $(this).width(580);
        }
    });

    $('#dvPlayList').player(0);
});

function setCurrentTab() {
    var local = window.location.pathname;
    var obj = $('#tabs6 a[href=' + local + ']')
     .css({ 'background-position': '0% -42px' })
     .children('span').css({ 'background-position': '100% -42px', 'color': '#222' });
}

function setDig() {
    if (dug) {
        $('#dvDig').css({ 'cursor': 'default' }).find('span:last-child').html('已顶成');
        return;
    }
    $('#dvDig').click(function (event) {
        var id = this.className;
        var info = $(this).find('span:last-child');
        info.html('提交中...');
        var _this = $(this);
        $.post('/Article/Dig/' + id, {}, function (data) {
            if (data.count > 0) {
                info.html('感谢参与');
                _this.find('span:first-child').html(data.count);
            }
            else {
                info.html('已顶成');
            }
            _this.css({ 'cursor': 'default' })
                .unbind(event);
        }, 'json');
    });
}

var ajaxTip = function (obj) {
    this.tipHtml = $('<div class="ajaxTip"><span></span></div>');
    this.warningImg = '/content/Images/AjaxTipWarning.gif';
    this.complatedImg = '/content/Images/AjaxTipComplete.gif';
    this.obj = obj;
};
ajaxTip.prototype = {
    show: function (txt, cssName) {
        this.obj.children('div.ajaxTip').remove();
        this.tipHtml.removeClass().addClass('ajaxTip ' + cssName).find('span').html(txt);
        this.tipHtml.appendTo(this.obj);
    },
    setClose: function (imgSrc) {
        if (!imgSrc) {
            return;
        }
        var _this = this;
        this.tipHtml.prepend($('<img src=' + imgSrc + ' alt="关闭"/>').click(function () {
            _this.tipHtml.remove();
        }));
    },
    warning: function (txt) {
        this.show(txt, 'redTip');
        this.setClose(this.warningImg);
    },
    complated: function (txt) {
        this.show(txt, 'greenTip');
        this.setClose(this.complatedImg);
    },
    loading: function () {
        this.show('正在提交数据...', 'redTip');
    },
    close: function () {
        this.tipHtml.remove();
    }
};

function submitComment(obj, objId, reId) {
    var container = $(obj).parents('div:first');
    var tip = new ajaxTip(container);
    var title = getVal('txtTitle');
    if (title == '') {
        tip.warning('请填写标题');
        return;
    }
    var name = getVal('txtName');
    var email = getVal('txtEmail');
    if (email != '') {
        var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if (!reg.test(email)) {
            tip.warning('请输入正确的Email地址');
            return;
        }
    }
    var homepage = getVal('txtHomepage');
    if (homepage != '' && !/^http:\/\/$/ig.test(homepage)) {
        var reg = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/ig;
        if (!reg.test(homepage)) {
            tip.warning('请输入正确的主页地址');
            return;
        }
    }
    var comment = getVal('txtComment','textarea');
    if (comment == '') {
       tip.warning('请填写您的评论内容');
       return;
    }
    if(comment.length > 300){
        tip.warning('评论内容不能超过三百字');
        return;
    }
    var reId = getVal('hidReId');
    var data = { 'objId': objId, 'reId': reId, 'title': escape(title), 'username': escape(name), 'homepage': homepage, 'email': email, 'comment': escape(comment),'reId': reId };
    tip.loading();

    disableObj = $(obj);
    disableButton();

    $.post('/Comment/Submit', data, function (data) {
        if (data.success == 'Ok') {
            tip.complated('感谢您的参与');
            loadComments(objId);
            setVal('txtComment', '', 'textarea');
            setVal('txtEmail', '');
            setVal('txtHomepage', 'http://');
            setVal('txtName', '');
        }
        else {
            tip.warning(data.success);
        }
    }, 'json');
    function getVal(name, type) {
        if (!type) {
            type = 'input';
        }
        var v = container.find(type + '[name=' + name + ']').val();
        return $.trim(v);
    }
    function setVal(name,value, type) {
        if (!type) {
            type = 'input';
        }
        container.find(type + '[name=' + name + ']').val(value);
    }
}

var disableObj;
var disableNumber = 0;
var disableTimer;

function disableButton() {
    disableObj.attr('disabled', 'disabled');
    disableTimer = setInterval('setDisabled()', 1000);
}
function setDisabled() {
    disableObj.val('等待提交(' + disableNumber + ')');
    disableNumber++;
    if (disableNumber > 10) {
        disableNumber = 0;
        disableObj.removeAttr('disabled');
        disableObj.val('提交');
        clearInterval(disableTimer);
    }
}

function loadComments(objId) {
    $.post('/Comment/List', { 'objId': objId }, function (data) {
        $('#dvArticleComments').html(data.html);
    }, 'json');
}

function optReplay(b, r, o) {
    var tip = new ajaxTip(o);
    $(o).text('加载中....').attr('disabled', 'disabled');
    $.post('/Ajax/LoadCommentControl', { objId: b, reId: r }, function (data) {
        if (!data.Success) {
            tip.warning(data.Html);
            return;
        }
        $(data.Html).appendTo($(o).parent('dd'));
        $(o).hide();
    }, 'json');
}

function closeCommentCtrl(r, o) {
    $('#' + o).remove();
    $('#rpl_' + r).removeAttr('disabled').text('回复').show();
}

function optComments(type, id,obj) {
    var container = $(obj).parent('dd');
    var tip = new ajaxTip(container);
    var data = { 'type': type, 'id': id };
    tip.loading();
    $.post('/Comment/Report', data, function (data) {
        var result = data.result;
        if (result == 0) {
            tip.warning("您已经参与过投票...");
            return;
        }
        else {
            $('#' + type + id).html('[' + result + ']');
            tip.complated('感谢您的参与...');
        }
    }, 'json');
}

$.fn.extend({
    player: function (index) {
        var items = $(this).find('li');
        items.eq(index).show();
        var _this = $(this);
        var pager = $(this).find('div.dvPalyPager');
        for (var i = items.length; i > 0; i--) {
            $('<span>' + (i) + '</span>').mouseover(function () {
                $(this).parent('div').find('span.current').removeClass('current');
                $(this).addClass('current');
                var index = $(this).text();
                _this.find('li').hide().eq(index - 1).show();
            }).toggleClass('current', (i - 1) == index)
									.appendTo(pager);
        }
        //$(numbers).appendTo();
    }
});