﻿$(function() {
    InitPropertyValue();
    GetAndHideEmptyRow(); //获取空行并隐藏
    InitShowOrHide(); //初始化显示/隐藏属性值信息
    if (typeof (currPageName) != 'undefined' && currPageName == 'EditProperty') {
        $('.publictable > tbody').find('tr').each(function() {
            var pid = $(this).attr('propertyid');
            var vid = $(this).attr('valueid');
            if (pid != undefined && $.trim(pid).length > 0 && vid != undefined && $.trim(vid).length > 0 && $.trim($(this).text()).length == 0) {
                $.ajax({
                    type: "POST",
                    url: topicControllerUrl + '/ClearEmptyPropertyValueAjax/', //清除混合空值
                    data: { 'topicID': topicId, 'propertyID': pid, 'multiValueID': vid },
                    success: function(data) {
                    }
                });
            }
        });
    }
});

//初始化显示/隐藏属性值信息
function InitShowOrHide() {
    //显示/隐藏属性值详细信息
    $('.ShowPropertyInfo').unbind().click(function() {
        var $currTable = $(this).parent().parent().next();
        if ($currTable.css('display') == 'none') {
            $(this).removeClass('maxicon').addClass('minicon');
            $(this).parent().parent().css('border-bottom', '0px');
            $(this).attr('title', '隐藏详细信息');
            $currTable.show();
        }
        else {
            $currTable.hide();
            $(this).removeClass('minicon').addClass('maxicon');
            if ($(this).parent().attr('class').indexOf('titleborder') == -1) {
                $(this).parent().parent().css('border', '1px solid #CFCFCF');
            }
            else {
                $(this).parent().parent().css('border-bottom', '1px solid #CFCFCF');
            }
            $(this).attr('title', '展开详细信息');
        }
    });
}

//初始化图片属性列表
function InitImgPropertyList() {
    $('.download-icon').unbind().mouseover(function() {
        ShowHelp(this, -20);
    });

    $('.download-icon').next().unbind().mouseover(function() {
        ShowHelp(this, -20);
    });

    $('.topicBrowserImgDiv').unbind().hover(function() {
        if (typeof (currUserID) == "undefined")
            $(this).find('.settopiccover-icon').show();
    }, function() {
        $(this).find('.settopiccover-icon').hide();
    });

    //设为封面
    $('.settopiccover-icon').unbind().click(function() {
        //判断是否登录
        if (userID == 0) //未登录
        {
            ShowLogin();
            return;
        }
        var imgUrl = $(this).attr('imgfilename');
        ConfirmTips("系统提示：", "确定设置该图片为当前主题封面吗？", "ConfirmReSetTopicConver('" + imgUrl + "')");
    });

    //图片属性点击浏览大图
    $('.topicBrowserImg').unbind().click(function() {
        var $currTd = $(this).parent().parent().parent();
        var currIndex = $currTd.find('.pager').length > 0 ? $currTd.find('.pager').attr('currindex') : 1;
        var currImgObj = $(this).find('img');
        var hideObj = $currTd.find('.hidcontent[pageindex=' + currIndex + ']');
        var imageCount = $currTd.find('.propertyValueRowCount').html();
        ShowViewImage(currImgObj, hideObj, currIndex, imageCount);
    });
}

//ViewImage.ascx调用获取下一页图片
function GetAllImageAjax(currTd, currIndex, imageCount) {
    var propertyId = currTd.find('.browserImgDiv').attr('propertyid');
    var tempUserID = typeof (missionUserID) == 'undefined' ? 0 : missionUserID;
    if (propertyId != undefined) {
        $.ajax({
            type: "POST",
            url: topicControllerUrl + '/GetAllImageAjax/',
            data: { 'topicID': topicId, 'propertyID': propertyId, 'missionUserID': tempUserID, 'pageIndex': currIndex },
            success: function(data) {
                currTd.append(data);
                viCurrHideObj = currTd.find('.hidcontent[pageindex=' + currIndex + ']');
                VI_ReLoadCurrImg(viCurrHideObj);
            },
            error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
        });
    }
}

//初始化视频属性列表
function InitVideoPropertyList() {
    //播放视频
    $('.topicViewVideoDiv').unbind('click').click(function() {
        var url = $(this).find('a').attr('href');
        if (url.indexOf('.swf') == -1 || url.indexOf('http://player.youku.com') != -1)
            window.open(url, "videowindow", "height=570, width=960, toolbar= no, menubar=no, scrollbars=no, resizable=no, location=no, status=no,top=100,left=300");
        else
            window.open(url, "videowindow", "height=570, width=960, toolbar= no, menubar=no, scrollbars=no, resizable=yes, location=no, status=no,top=100,left=300");
    });

    //播放视频
    $('.browserVideoDiv').find('.playicon').unbind('click').click(function() {
        var url = $(this).find('a').attr('href');
        if (url.indexOf('.swf') == -1 || url.indexOf('http://player.youku.com') != -1)
            window.open(url, "videowindow", "height=570, width=960, toolbar= no, menubar=no, scrollbars=no, resizable=no, location=no, status=no,top=100,left=300");
        else
            window.open(url, "videowindow", "height=570, width=960, toolbar= no, menubar=no, scrollbars=no, resizable=yes, location=no, status=no,top=100,left=300");
    });
}
    
function InitPropertyValue(){
    if ($.browser.mozilla && $('.publictable').length > 0) {
//        $('.publictable').css('table-layout', 'fixed');
    }

    InitImgPropertyList();

    InitVideoPropertyList();

    $('.isdelete').attr('title', '删除待审批');


    $('.moreinfo').unbind().each(function() {
        var $currPager = $(this).find('.pager');
        var $currTd = $currPager.parent().parent();
        var pageCount = $(this).attr('pagecount');
        var pID = $(this).attr('propertyid');
        $currPager.find('a').unbind().click(function() {
            if ($(this).attr('class') == undefined || $(this).attr('class').indexOf('button') == -1) {
                var currIndex = $(this).attr('currIndex');
                ReGetPropertyValue($currTd, pID, currIndex);
            }
        });
        $currPager.find('a.first').click(function() {
            var currIndex = 1;
            ReGetPropertyValue($currTd, pID, currIndex);
        });
        $currPager.find('a.prev').click(function() {
            var currIndex = parseInt($currPager.attr('currIndex'));
            if (currIndex > 1)
                currIndex--;
            else
                return;
            ReGetPropertyValue($currTd, pID, currIndex);
        });
        $currPager.find('a.next').click(function() {
            var currIndex = parseInt($currPager.attr('currIndex'));
            if (currIndex < pageCount)
                currIndex++;
            else
                return;
            ReGetPropertyValue($currTd, pID, currIndex);
        });
        $currPager.find('a.last').click(function() {
            var currIndex = pageCount;
            ReGetPropertyValue($currTd, pID, currIndex);
        });
    });


    //查看更多属性值信息
    $('.more').unbind().click(function() {
        var propertyId = $(this).attr('propertyId');
        var $currTd = $(this).parent().parent();
        ShowLoading();
        var tempUserID = typeof (missionUserID) == 'undefined' ? 0 : missionUserID;
        $.ajax({
            type: "POST",
            url: topicControllerUrl + '/GetAllPropertyValueAjax/',
            data: { 'topicID': topicId, 'propertyID': propertyId, 'missionUserID': tempUserID },
            success: function(data) {
                HideLoading();
                $currTd.html(data);
                try {
                    InitShareTopic();
                }
                catch (err) { }
            },
            error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
        });
    });

    if ($('.imgCheckBox').length==0)
        GetEmptyRow(); //重新获取空行
    
    try {
        InitShareTopic();
    }
    catch (err) { }
}

function ReGetPropertyValue(currTd, pID, pIndex) {
    ShowLoading();
    var tempUserID = typeof (missionUserID) == 'undefined' ? 0 : missionUserID;
    $.ajax({
        type: "POST",
        url: topicControllerUrl + '/GetPropertyValueAjax/',
        data: { 'topicID': topicId, 'propertyID': pID, 'missionUserID': tempUserID, 'pageIndex': pIndex },
        success: function(data) {
            HideLoading();
            currTd.html(data);
            InitPropertyValue();
        },
        error: function(xmlHttpRequest, error) { HideLoading(); AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2); }
    });
}

//重设封面图片
function ConfirmReSetTopicConver(imgUrl) {
    ShowLoading();
    $.ajax({
        type: "POST",
        url: topicControllerUrl + '/ReSetTopicImageAjax/',
        data: { 'topicID': topicId, 'imgUrl': imgUrl },
        success: function(data) {
            HideLoading();
            if (data.length > 0) {
                var dataArr = data.split('|');

                $('.propertyvalue-table:eq(0)').find('.td3:eq(0)').html('<a style="float: left; margin: 0pt 5px 5px 0pt;" href="javascript:void(0)" class="topicBrowserImg" title="点击看原图"><img width="118" height="78" border="0" src="' + dataArr[0] + '" source="' + dataArr[1] + '" onload="ResizeImage(this,118,90,3)"></a>');

                ShowTips("成功设置图片为封面！");
            }
            else
                AlertTips("系统提示：", "设置图片为封面失败！", 2);
        },
        error: function(xmlHttpRequest, error) {
            HideLoading();
            AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
        }
    });
}

//混合属性值分页输入页码回车
function CheckPageNum(e, obj) {
    var event = e || window.event;
    if (event.keyCode == 13) $(obj).next().click();
}

//混合属性值分页转到功能
function ReSkipPage(obj) {
    var currIndex = $.trim($(obj).prev().val());
    var $currTd = $(obj).parent().parent().parent();
    var $moreObj = $currTd.find('.moreinfo');
    var pID = $moreObj.attr('propertyid');
    var pageCount = $moreObj.attr('pagecount');
    if (currIndex.length > 0) {
        if (parseInt(currIndex) > parseInt(pageCount))
            currIndex = pageCount;
        else if (parseInt(currIndex) < 1)
            currIndex = 1;

        ReGetPropertyValue($currTd, pID, currIndex);
    }
}

//隐藏空行
function GetAndHideEmptyRow() {
    var hideRowCount;
    $('.propertyvalue-table > tbody').not('.viewpropertyvalue-table > tbody,.otherproeprtyvalue-table > tbody').each(function(i) {
        var $emptyValueCount = $(this).parent().prev().find('.emptyvaluecount');
        if ($emptyValueCount.length == 0) {
            $emptyValueCount = $(this).parent().parent().prev().find('.emptyvaluecount');
        }
        if (typeof (currPageName) != 'undefined') {
            if ($(this).find('.tr1').length == $(this).find('tr').length) {
                if (currPageName != 'EditProperty') {
                    $emptyValueCount.parent().hide();
                }
                return;
            }
        }
        hideRowCount = 0;
        $(this).children().each(function(index) {
            if ($.trim($(this).find('td:eq(2)').text()).length == 0) {
                if (typeof (currPageName) != 'undefined') {
                    if (currPageName != 'EditProperty') {
                        if ($(this).find('.necessaryproperty').length == 0) {
                            $(this).hide();
                            $(this).find('td').hide();
                        }
                    }
                }
                hideRowCount++;
            }
        });
        if (typeof (currPageName) != 'undefined') {
            if (currPageName != 'EditProperty') {
                if (hideRowCount == 0)
                    $emptyValueCount.parent().hide();
                else
                    $emptyValueCount.parent().show();
            } else {
                $emptyValueCount.text(hideRowCount);
            }
        }
    });

    if (typeof (currPageName) != 'undefined') {
        if (currPageName != 'EditProperty') {
            HideEmptyCols(); //隐藏空值列
        }
    }

    $('td').each(function() {
        if ($.trim($(this).html()).length == 0)
            $(this).html('&nbsp;');
    });
}

//不隐藏空行
function GetEmptyRow() {
    var hideRowCount;
    $('.propertyvalue-table > tbody').not('.viewpropertyvalue-table > tbody,.otherproeprtyvalue-table > tbody').each(function(i) {
        hideRowCount = 0;
        $(this).children().each(function(index) {
            if ($.trim($(this).children('td:eq(2)').text()).length == 0 && $(this).children('td:eq(2)').find('img').length == 0) {
                $(this).addClass('tr1');
                hideRowCount++;
            }
            else {
                $(this).removeClass('tr1');
            }
        });
        var $emptyValueCount = $(this).parent().prev().find('.emptyvaluecount');
        if ($emptyValueCount.length == 0) {
            $emptyValueCount = $(this).parent().parent().prev().find('.emptyvaluecount');
        }
        if (typeof (currPageName) != 'undefined') {
            if ($(this).find('.tr1').length == $(this).find('tr').length) {
                if (currPageName != 'EditProperty') {
                    $(this).find('tr').show();
                    $(this).find('tr').find('td').show();
                    $emptyValueCount.parent().hide();
                    return;
                }
            }
            if (currPageName != 'EditProperty') {
                if (hideRowCount == 0)
                    $emptyValueCount.parent().hide();
                else
                    $emptyValueCount.parent().show();
            } else {
                $emptyValueCount.text(hideRowCount);
            }
        }
    });

    if (typeof (currPageName) != 'undefined') {
        if (currPageName != 'EditProperty')
            HideEmptyCols(); //隐藏空值列
    }

    $('td').each(function() {
        if ($.trim($(this).html()).length == 0)
            $(this).html('&nbsp;');
    });
}

//隐藏空值列
function HideEmptyCols() {
    $('.propertyvalue-table > tbody,.propertytable-view > tbody').find('.publictable > tbody').each(function(index) {
        var $tbody = $(this);
        var colsCount = $tbody.find('tr:eq(0) td').length;
        for (var i = 0; i < colsCount; i++) {
            var tempHtml = '';
            $tbody.find('tr').each(function(index) {
                var $tr = $(this);
                $tr.find('td:eq(' + i + ')').each(function(index) {
                    tempHtml += $.trim($(this).text());
                });
            });
            if (tempHtml.length == 0) {
                $tbody.find('tr').find('td:eq(' + i + ')').hide();
                $tbody.parent().find('th:eq(' + i + ')').hide();
            }
        }
    });

    $('.publictable').css('width', '100%');
}
