﻿/* Gift.js 礼物窗体的脚本*/
function PostGift(ObjectId, ObjectDetailId, GiftGiveUserId)
{
    var oGift = new Gift(ObjectId, ObjectDetailId, GiftGiveUserId);
    oGift.show();
    //oGift.setBG();
    oGift.title("送礼物");
    oGift.giftList();
}


Gift = function(ObjectId, ObjectDetailId, GiftGiveUserId)
{
    this._saveTabIndexes = new Array();
    this._saveDesableSelect = new Array();
    this._tagWithTabIndex = new Array('A', 'AREA', 'BUTTON', 'INPUT', 'OBJECT', 'SELECT', 'TEXTAREA', 'IFRAME');
    this.backgroundElement = null;
    this.foregroundElement = null;
    this.scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    this.scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    this.giftDialog = null;
    this.giftDialogTitle = null;
    this.giftDialogContent = null;
    this.clientWidth = null;
    this.clientHeight = null;
    this.hoverClass = "onHover";
    this.CheckedClass = "GiftCheckedCss";
    this.currentChecked = null;
    this.priorChecked = null;
    this.dataGift = null;
    this.objectId = ObjectId;
    this.giftgiveuserid = GiftGiveUserId;
    this.objectdetailId = ObjectDetailId;
    this.pageIndex = 1;
    this.pageSize = 6;
    this.currentPageIndex = 1;
    this.pageCount = null;
    this.LoadGiftImg = new Array();
    this.init();
};


Gift.prototype.init = function()
{
    var sBox = '\
            <table class="pop_dialog_table" style="display: none" id="D_Gift_Dialog">\
                <tr>\
                    <td class="pop_topleft">\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_topright">\
                    </td>\
                </tr>\
                <tr>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_content">\
                        <div class="divMsgFrameCss">\
                            <div class="divMsgTitleCss" id="D_Gift_Title">\
                                </div>\
                            <div class="divMsgMainCss" id="D_Gift_Content">\
                            </div>\
                        </div>\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                </tr>\
                <tr>\
                    <td class="pop_bottomleft">\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_bottomright">\
                    </td>\
                </tr>\
            </table>\  '

    var oDiv = document.createElement('span');
    oDiv.innerHTML = sBox;
    document.body.appendChild(oDiv);

    this.giftDialogTitle = $get("D_Gift_Title");
    this.giftDialogContent = $get("D_Gift_Content");
    this.giftDialog = $get("D_Gift_Dialog");
};

//显示GIFT窗体
Gift.prototype.show = function()
{
    this.giftDialog.style.display = '';
    this.giftDialog.style.position = 'absolute';
    this.giftDialog.style.zIndex = 10001;
    this.middle(this.giftDialog);
    this.setBG();
};


//获取当前样式
Gift.prototype.getCurrentStyle = function(element, attribute, defaultValue)
{
    var currentValue = null;
    if (element)
    {
        if (element.currentStyle)
        {
            currentValue = element.currentStyle[attribute];
        } else if (document.defaultView && document.defaultView.getComputedStyle)
        {
            var style = document.defaultView.getComputedStyle(element, null);
            if (style)
            {
                currentValue = style[attribute];
            }
        }

        if (!currentValue && element.style.getPropertyValue)
        {
            currentValue = element.style.getPropertyValue(attribute);
        }
        else if (!currentValue && element.style.getAttribute)
        {
            currentValue = element.style.getAttribute(attribute);
        }
    }

    if ((!currentValue || currentValue == "" || typeof (currentValue) === 'undefined'))
    {
        if (typeof (defaultValue) != 'undefined')
        {
            currentValue = defaultValue;
        }
        else
        {
            currentValue = null;
        }
    }
    return currentValue;
};

//设置窗体居中
Gift.prototype.middle = function(element)
{
    switch (Sys.Browser.agent)
    {
        case Sys.Browser.InternetExplorer:
            this.clientWidth = document.documentElement.clientWidth;
            this.clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            this.clientWidth = window.innerWidth;
            this.clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            this.clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            this.clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            this.clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            this.clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    var foregroundelementwidth = element.offsetWidth ? element.offsetWidth : element.scrollWidth;

    xCoord = ((this.clientWidth - foregroundelementwidth) / 2);

    var foregroundelementheight = element.offsetHeight ? element.offsetHeight : element.scrollHeight;

    yCoord = ((this.clientHeight - foregroundelementheight) / 2);

    if (element.style.position == 'absolute')
    {
        xCoord += this.scrollLeft;
    }
    element.style.left = (xCoord) + 'px';


    if (element.style.position == 'absolute')
    {
        yCoord += this.scrollTop;
    }
    element.style.top = yCoord + 'px';
};

//设置透明背景
Gift.prototype.setBG = function()
{
    if (document.getElementById("TB_HideSelect") == null)
    {
        $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
    }
    if (document.getElementById("TBtooltip") == null)
    {
        $('<div id="TBtooltip" class="tooltip"></div>').appendTo(document.body).hide();
    }
    this.overlaySize();
};

Gift.prototype.overlaySize = function()
{
    if (window.innerHeight && window.scrollMaxY)
    {
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
        yScroll = document.body.scrollHeight;
    } else
    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight;
    }
    $("#TB_overlay").css("height", yScroll + "px");
};

//关闭
Gift.prototype.close = function()
{
    //    document.body.removeChild(this.backgroundElement);
    //    document.body.removeChild(this.giftDialog);
    //    this.backgroundElement.style.display = 'none';
    //    this.giftDialog.style.display = 'none';
    //$('#D_Gift_Dialog').remove();
    $('#D_Gift_Dialog,#TB_overlay,#TB_HideSelect').remove();
    //this.restoreTab();
};


Gift.prototype.close_reload = function()
{
    //    document.body.removeChild(this.backgroundElement);
    //    document.body.removeChild(this.giftDialog);
    //    this.backgroundElement.style.display = 'none';
    //    this.giftDialog.style.display = 'none';
    //$('#D_Gift_Dialog').remove();
    $('#D_Gift_Dialog,#TB_overlay,#TB_HideSelect').remove();
    window.location.href = location.href;
    //this.restoreTab();
};

//窗体标题
Gift.prototype.title = function(msgstr)
{

    this.giftDialogTitle.innerHTML = msgstr;
};

//窗体载入动画
Gift.prototype.loading = function()
{
    this.giftDialogContent.innerHTML = "<span class='msgBigError'>正在加载数据，请等待...</span>";
};

//窗体内容添加
Gift.prototype.innerContent = function(txt)
{
    this.giftDialogContent.innerHTML = txt;
};

//礼物列表显示
Gift.prototype.giftList = function()
{
    var self = this;
    if (parseInt(this.giftgiveuserid) > 0)
    {
        this.loading();
        Fuwoo.BopWeb.WS.gift.CheckStatus(this.giftgiveuserid,
        this.objectId,
        this.objectdetailId,
        function(result)
        {
            if (result > 0)
            {
                Fuwoo.BopWeb.WS.gift.List(function(r) { self.buildList(r); }
            );
            }
            else
            { self.showMSG(result); }
        });

    }
    else
        this.showMSG('-3');
};

//礼物列表构建HTML
Gift.prototype.buildList = function(result)
{

    this.dataGift = result;
    var sBox = null;
    if (result.length == 0)
        sBox = '<span class="msgBigError">目前没有礼物！</span>';
    else
    {
        this.preLoadIMG(); //预先加载图片修正ie6图片可能不出来的问题
        sBox = '\
                        <div class="giftContainer" id="giftContainer">\
                        <ul>\
                        ';
        for (var i = (this.currentPageIndex - 1) * this.pageSize; i < (this.currentPageIndex * this.pageSize); i++)
        {
            if (i < this.dataGift.length)
            {
                sBox += '\
                                <li id="' + i + '"><div  style="visibility:hidden;" id="div_' + i + '" class="GiftCheckedClass"></div><a href="javascript:void(0)" id="a_' + i + '" title="' + this.dataGift[i].GiftContent + '">\
                            <img id="img_' + this.dataGift[i].GiftId + '" src="' + this.LoadGiftImg[i].src + '" width="100" height="100" />\
                            </a>\
                            ' + this.dataGift[i].GiftName + '<em>' + this.dataGift[i].GiftLeaveCount + '个 ' + this.convertPrice(this.dataGift[i].GiftPrice) + '</em>\
                            ' + this.convertExperience(this.dataGift[i].GiftExperience) + this.convertCharm(this.dataGift[i].GiftCharm) + this.convertMoney(this.dataGift[i].GiftAmount) + '</li>';
            }
        }
        sBox += '</ul></div>';
    }
    var mBox = '<div style="clear:both;"></div><div class=\"divMsgBottomCss\">\
                    <input type="button" name="Submit" value="确定" class="inputsubmit" id="btnNext"/>\
                    <input type="button" name="Submit" value="取消" class="inputaux" id="btnCancel" />\
                    </div>';
    this.innerContent(sBox + this.page() + mBox);
    this.addEvent();
    this.middle(this.giftDialog);
};

Gift.prototype.preLoadIMG = function()
{
    for (var i = 0; i < this.dataGift.length; i++)
    {
        this.LoadGiftImg[i] = new Image();
        this.LoadGiftImg[i].src = this.dataGift[i].GiftPicture;
    }

};
//增加事件
Gift.prototype.addEvent = function()
{
    var self = this;
    //礼物内容加事件
    var oLi = $get("giftContainer").getElementsByTagName("LI");
    for (var i = 0; i < oLi.length; i++)
    {
        var va = oLi[i].id;
        var aObject = this.gettypeA(oLi[i]);
        this.addEventM(aObject, va);
    }
    //页脚加事件
    var pLi = $get("D_Gift_page").getElementsByTagName("LI");
    for (var i = 0; i < pLi.length; i++)
    {
        var ah = i + 1;
        var as = this.gettypeB(pLi[i]);

        this.addEventP(as, ah);
    }

    //按钮加事件
    var btnNext = $get("btnNext");
    this.addEventListener(btnNext, "click", function(e) { return self.btnNext(); }, false);
    var btnCancel = $get("btnCancel");
    this.addEventListener(btnCancel, "click", function(e) { return self.close(); }, false);
};


Gift.prototype.btnNext = function()
{

    if (this.currentChecked != null)
    {
        this.bulidNext();
    }
    else
    { alert("请先选择一个礼物！"); }

};

Gift.prototype.bulidNext = function()
{
    var self = this;
    var xBox = '\
            <div style="padding:10px;">\
            <div style="float:left; margin-right:20px; "><img  src="' + this.LoadGiftImg[this.currentChecked].src + '"  style="border:solid 1px #d7d7d7 "/></div>\
            <div style="font-size:10.5pt; float:left;">\
            <p style="font-size:10.5pt">' + this.dataGift[this.currentChecked].GiftName + ' </p>\
            <dl style="font-size:9pt;">\
            <dt style="float:left; color:#808080">效果:</dt><dd>' + this.convertExperience(this.dataGift[this.currentChecked].GiftExperience) + this.convertCharm(this.dataGift[this.currentChecked].GiftCharm) + this.convertMoney(this.dataGift[this.currentChecked].GiftAmount) + ' </dd>\
            <dt style="float:left; color:#808080">价格:</dt><dd>' + this.convertPrice(this.dataGift[this.currentChecked].GiftPrice) + ' </dd>\
            </dl>\
            <p>\
            <select name="select" id="D_Gift_select" style="width:180px;">\
            <option value="0">所有人都能看到</option>\
            <option value="1">只有我和对方能看到</option>\
            <option value="2">匿名</option>\
            </select></p></div></div><div style="clear:both;height:50px;"></div>\
            ';

    var mBox = '<div class=\"divMsgBottomCss\">\
                        <input type="button" name="Submit" value="确定" class="inputsubmit" id="btnNext"/>\
                        <input type="button" name="Submit" value="取消" class="inputaux" id="btnCancel" />\
                        </div>';
    this.innerContent(xBox + mBox);
    this.title("您确定要赠送吗？");

    var btnNext = $get("btnNext");
    this.addEventListener(btnNext, "click", function(e) { return self.giveGift(); }, false);
    var btnCancel = $get("btnCancel");
    this.addEventListener(btnCancel, "click", function(e) { return self.close(); }, false);
    this.middle(this.giftDialog);
};

//送出礼物事件
Gift.prototype.giveGift = function()
{
    this.btndisable();
    var o = new Object();
    o.GiftId = parseInt(this.dataGift[this.currentChecked].GiftId);
    o.ObjectId = parseInt(this.objectId);
    o.ObjectDetailId = parseInt(this.objectdetailId);
    o.GiftGiveUserId = parseInt(this.giftgiveuserid);
    o.GiftGivePrivacyId = parseInt($get("D_Gift_select").value);
    //alert(o.ObjectId+","+o.GiftGiveUserId+","+o.ObjectId+","+o.ObjectDetailId);
    var self = this;
    Fuwoo.BopWeb.WS.gift.GiveGift(o, function(result) { self.showMSG(result); });

};


Gift.prototype.page = function()
{
    this.pageCount = parseInt(Math.ceil(parseFloat(this.dataGift.length) / parseFloat(this.pageSize)));
    var pBox = '\
    <UL class="D_Gift_page" id="D_Gift_page">';
    for (var i = 1; i <= this.pageCount; i++)
    {
        if (i == this.currentPageIndex)
            pBox += '<LI><A href="javascript:void(0)" class="checked">' + i + '</A></LI>';
        else
            pBox += '<LI><A href="javascript:void(0)" >' + i + '</A></LI>';
    }
    pBox += '</UL><div class="clearfix1"></div>';

    return pBox;
    //PageCount = (int)Math.Ceiling(((double)RecordCount) / ((double)PageSize));
};


Gift.prototype.changePage = function(page_i)
{
    if (this.currentPageIndex != page_i)
    {
        this.currentPageIndex = page_i;
        this.priorChecked = null;
        this.currentChecked = null;
        this.buildList(this.dataGift);
    }

}
//设置按钮不可用
Gift.prototype.btndisable = function()
{
    $get("btnNext").disabled = "disabled";
};

//显示最后结果信息
Gift.prototype.showMSG = function(result)
{
    var titlemsg = null;
    var xBox = null;
    var self = this;
    switch (parseInt(result))
    {
        case 0:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">礼物赠送失败，礼物未能送到对方那里，请稍候再试！</span>';
        case -1:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">礼物赠送失败，您的F币不够！</span>';
            break;
        case -2:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">礼物赠送失败，礼物已卖完！</span>';
            break;
        case -3:
            titlemsg = "未登录";
            xBox = '<span class="msgBigError">你不能送礼物，请先登录！</span>';
            break;
        case -4:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">礼物赠送失败，礼物不能送给自己！</span>';
            break;
        case -5:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">礼物赠送失败，这里你已经送过1次了！</span>';
            break;
        case -6:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">礼物赠送失败，今天你已经送过了，明天再来吧！</span>';
            break;
        default:
            titlemsg = "完成";
            xBox = '<span class="msgBigError">礼物赠送成功，等待对方上线就能看到了！</span>';
    }

    var mBox = '';
    //if (result <= 0)
        mBox = '<div class=\"divMsgBottomCss\">\
                        <input type="button" name="Submit" id="btngiftClose" value="关闭" class="inputaux"/>\
                        </div>';
    this.innerContent(xBox + mBox);
    this.title(titlemsg);
    this.middle(this.giftDialog);

    if (result > 0)
    {
        var btnClose = $get("btngiftClose");
        this.addEventListener(btnClose, "click", function(e) { return self.close(); }, false);
    }
    else
    {
        var btnClose = $get("btngiftClose");
        this.addEventListener(btnClose, "click", function(e) { return self.close(); }, false);
    }
};

//经验转换
Gift.prototype.convertExperience = function(intx)
{
    if (parseInt(intx) > 0)
        return '&nbsp;经验<font style="color:green">+' + intx + '</font>';
    else if (parseInt(intx) < 0)
        return '&nbsp;经验<font style="color:red">' + intx + '</font>';
    else
        return '';
};

//魅力转换
Gift.prototype.convertCharm = function(intx)
{

    if (parseInt(intx) > 0)
        return '&nbsp;魅力<font style="color:green">+' + intx + '</font>';
    else if (parseInt(intx) < 0)
        return '&nbsp;魅力<font style="color:red">' + intx + '</font>';
    else
        return '';

};

//F币转换
Gift.prototype.convertMoney = function(intx)
{

    if (parseInt(intx) > 0)
        return '&nbsp;F币<font style="color:green">+' + intx + '</font>';
    else if (parseInt(intx) < 0)
        return '&nbsp;F币<font style="color:red">' + intx + '</font>';
    else
        return '';

};

Gift.prototype.addEventM = function(ahref, id)
{
    var self = this;
    this.addEventListener(ahref, "click", function(e) { return self.onClick(id); }, false);
};


Gift.prototype.addEventP = function(ahref, id)
{
    var self = this;
    this.addEventListener(ahref, "click", function(e) { return self.changePage(id); }, false);
};

Gift.prototype.gettypeA = function(element)
{
    return element.childNodes[1];
};

Gift.prototype.gettypeB = function(element)
{
    //alert(element.childNodes[0].innerHTML);
    return element.childNodes[0];
};

Gift.prototype.onClick = function(elementId)
{
    this.priorChecked = this.currentChecked;

    this.currentChecked = elementId;
    //$get("a_"+elementId);
    this.addClassName($get("a_" + this.currentChecked), this.CheckedClass);
    $get("div_" + this.currentChecked).style.visibility = "visible";

    if (this.currentChecked != this.priorChecked)
        if (this.priorChecked != null)
    {
        $get("div_" + this.priorChecked).style.visibility = "hidden";
        this.removeClassName($get("a_" + this.priorChecked), this.CheckedClass);
    }
};

Gift.prototype.onMouseOver = function(element)
{

    this.addClassName(element, this.hoverClass);
};


Gift.prototype.onMouseOut = function(element)
{

    this.removeClassName(element, this.hoverClass);
};

Gift.prototype.convertPrice = function(ms)
{
    if (parseInt(ms) > 0)
        return ms + " F币";
    else
        return "免费";
};


Gift.prototype.addEventListener = function(element, eventType, handler, capture)
{
    try
    {
        if (element.addEventListener)
            element.addEventListener(eventType, handler, capture);
        else if (element.attachEvent)
            element.attachEvent("on" + eventType, handler);
    }
    catch (e) { }
};


Gift.prototype.addClassName = function(ele, className)
{
    if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
        return;
    ele.className += (ele.className ? " " : "") + className;
};

Gift.prototype.removeClassName = function(ele, className)
{
    if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
        return;
    ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

Gift.prototype.hasClassName = function(ele, className)
{
    if (!ele || !className || !ele.className || ele.className.search(new RegExp("\\b" + className + "\\b")) == -1)
        return false;
    return true;
};


function Qb(r, s)
{
    return Ya(r, s);
}

function Ya(r, ia)
{
    var kb = 0;
    while (r)
    {
        kb += r[ia];
        r = r.offsetParent
    }
    return kb
}

function Qb1(e)//IE和firfox兼容
{
    if (isie)
        return e;
    else
        return e + "px";
}
    
