﻿var hammerCanMove = true;
var currPid = 0;
var currLeaveAmount = 0;
var luckyListTimeObj = null;
jQuery(document).ready(function () {
    jQuery('#btnViewExample').unbind().click(function () {
        jQuery('#imgPhotoSrc').attr('src', '/ShowSkins/18show/images/wealthshop/example.jpg');
        jQuery('#linkPrizeName').attr('href', 'http://www.18show.cn/wealthshop/prize_63_0.html').html('夏氏不锈钢保温杯');
        jQuery('#linkUserName').attr('href', 'javascript:;').html('xxxx');
        jQuery('#divSendWords').html('这个茶杯质量不错哦，多谢易展提供精美的礼品。美女加茶杯感觉“爽”！');
        jQuery('#fontWorth').html('1000财富值');
        jQuery('#divUploadTime').html('上传时间：2010-07-10 10:15');
        jQuery('#iframeTransparent,#divTransparentBg,#divViewExample').show();
        ResizeImage_Special(jQuery('#imgPhotoSrc')[0], 360, 270);
    });

    jQuery('#divViewExample').find('.close').unbind().click(function () {
        jQuery('#iframeTransparent,#divTransparentBg,#divViewExample').hide();
    });

    jQuery('#divGoToLottery').find('.close').unbind().click(function () {
        jQuery('#iframeTransparent,#divTransparentBg,#divGoToLottery').hide();
    });

    jQuery('#btnGoToWish').unbind().click(function () {
        if (jQuery('#hidUserId').val() <= 0) {
            alert('请先登录后再许愿！');
            NoLogin();
            return false;
        }
        jQuery('#divGoToWish').find('iframe').attr('src', '/GoToWish.aspx?requestUrl=/');
        jQuery('#iframeTransparent,#divTransparentBg,#divGoToWish').show();
    });


    jQuery('.photoinfo-border,.wishinfo-border').each(function () {
        var $photoInfoObj = jQuery(this);
        var $tempObj = $photoInfoObj.find('.photoimg,.sendwords,.prizename');
        if (jQuery('.photoinfo-border').length > 0)
            $tempObj = $photoInfoObj.find('.photoimg,.sendwords');
        $tempObj.unbind().click(function () {
            if (jQuery('#imgPhotoSrc').length > 0)
                jQuery('#imgPhotoSrc').attr('src', $photoInfoObj.find('.photosrc').attr('src'));
            if (jQuery('#linkPrizeName').length > 0)
                jQuery('#linkPrizeName').attr('href', $photoInfoObj.find('.prizename').attr('href')).html($photoInfoObj.find('.prizename').html());
            if (jQuery('#linkUserName').length > 0)
                jQuery('#linkUserName').attr('href', $photoInfoObj.find('.username').attr('href')).html($photoInfoObj.find('.username').html());
            if (jQuery('#divSendWords').length > 0) {
                if ($photoInfoObj.find('.sendwords').find('span').length > 0)
                    jQuery('#divSendWords').html($photoInfoObj.find('.sendwords').find('span').html());
                else
                    jQuery('#divSendWords').html($photoInfoObj.find('.sendwords').html());
            }
            if (jQuery('#fontWorth').length > 0)
                jQuery('#fontWorth').html($photoInfoObj.find('.worth').html());
            if (jQuery('#divUploadTime').length > 0)
                jQuery('#divUploadTime').html($photoInfoObj.find('.uploadtime').html());
            jQuery('#iframeTransparent,#divTransparentBg,#divViewExample').show();
            ResizeImage_Special(jQuery('#imgPhotoSrc')[0], 360, 270);
        });
    });

    jQuery('.lotterymain').unbind().mousemove(function (event) {
        if (!hammerCanMove) return;
        var x = event.clientX - getABSLeft(this) - 30;
        var y = event.clientY - getABSTop(this) - 80;
        jQuery('#spanHammer').css('top', y).css('left', x).show();
    }).mouseout(function () {
        if (!hammerCanMove) return;
        jQuery('#spanHammer').hide();
    }).find('.lotterytips').unbind().mousemove(function (event) {
        if (!hammerCanMove) return;
        event.stopPropagation();
        jQuery('#spanHammer').hide();
    }).end().find('a.egg').unbind().hover(function () {
        if (!hammerCanMove) return;
        jQuery(this).addClass('egg-over');
    }, function () {
        jQuery(this).removeClass('egg-over');
    }).click(function () {
        if (!hammerCanMove) return;
        jQuery('#spanHammer').removeClass().addClass('hammer-do');
        jQuery(this).addClass('egg1');
        hammerCanMove = false;
        jQuery('.lotterymain').css('cursor', 'default');
        jQuery('a.egg').css('cursor', 'default');
        jQuery('.lotterytips').html('幸运蛋正在裂开，激动人心的时刻...'); //等待开奖
        var currIndex = jQuery(this).attr('index');
        jQuery.ajax({
            type: "POST",
            url: '/GoToWishAction.aspx',
            data: { 'action': 'lottery', 'currIndex': currIndex, 'pId': currPid, 'userId': jQuery('#hidUserId').val() },
            success: function (data) {
                if (data == 'nomoney') { //财富值不够
                    jQuery('#fontLotteryTips').html('&nbsp;哎呀，财富值不够，%&gt;_&lt;%');
                    jQuery('#divNoMoneyMain').show();
                    jQuery('#divLotteryMain').hide();
                    jQuery('#divGoToLottery').css('width', '450px').css('margin', '-105px 0 0 -225px');
                    var $actionLink = jQuery('#divGoToLottery').find('.actions').find('a');
                    $actionLink.attr('href', $actionLink.attr('url')).attr('target', '_blank').html('立即赚取');
                    return;
                }

                if (data == 'noprize') { //礼品兑完了
                    jQuery('#fontLotteryTips').html('&nbsp;很抱歉！您要的礼品已被抢完了！');
                    jQuery('#divNoMoneyMain').show();
                    jQuery('#divLotteryMain').hide();
                    jQuery('#divGoToLottery').css('width', '450px').css('margin', '-105px 0 0 -225px');
                    return;
                }

                if (data.length == 0) {
                    alert('抱歉，抽奖发生错误，请稍后重试！');
                    return;
                }

                var leaveMoney = jQuery('#divGoToLottery').find('.lotteryMyMoney').html() - jQuery('#divGoToLottery').find('.lotteryCost').html();
                jQuery('#divGoToLottery').find('.lotteryMyMoney').html(leaveMoney);

                if (!isNaN(data)) {//未抽中 
                    jQuery('a.egg[index=' + data + ']').addClass('egg4');
                    jQuery('a.egg1').removeClass('egg1').addClass('egg2');

                    jQuery('.lotterytips').addClass('lotterytips1').html('哎呀！抽不中！<br />\
                        <font style="display:inline-block;width:305px;text-align:left;font-size:12px;font-weight:normal"><a href="'
                        + jQuery('#divGoToLottery').find('.lotterySponsor').attr('href') + '" target="_blank" style="color:#FAFF66">'
                        + jQuery('#divGoToLottery').find('.lotterySponsor').attr('title') + '</a>温馨提示：</font><br/>\
                        <a href="javascript:;" class="lotterybutton">再来一次</a>')
                    .find('.lotterybutton').unbind().click(function () {
                        InitLottery();
                    });
                }
                else { //抽中
                    jQuery('a.egg1').removeClass('egg1').addClass('egg3');
                    jQuery('.lotterytips').addClass('lotterytips1').html('恭喜您！金花四溅！<br />\
                        <font style="display:inline-block;width:305px;text-align:left;font-size:12px;font-weight:normal"><a href="'
                        + jQuery('#divGoToLottery').find('.lotterySponsor').attr('href') + '" target="_blank" style="color:#FAFF66">'
                        + jQuery('#divGoToLottery').find('.lotterySponsor').attr('title') + '</a>温馨提示：</font><br/>\
                        <a href="' + data + '" target="_blank" class="lotterybutton">马上去领奖</a>')
                    .find('.lotterybutton').unbind().click(function () {
                        jQuery('#iframeTransparent,#divTransparentBg,#divGoToLottery').hide();
                    });
                }
            },
            error: function (xmlHttpRequest, error) {
            }
        });
    });

    var ulHtml = jQuery('#ulLuckyList').html();
    jQuery('#ulLuckyList').html(ulHtml + ulHtml).hover(function () {
        if (luckyListTimeObj != null)
            clearTimeout(luckyListTimeObj);
    }, function () {
        luckyListTimeObj = setTimeout('ScrollLuckyList()', 2000);
    });
    if (jQuery('#ulLuckyList').find('li').length > 2)
        luckyListTimeObj = setTimeout('ScrollLuckyList()', 2000);
});

function ScrollLuckyList() {
    var top = parseInt(jQuery('#ulLuckyList').css('top'));
    jQuery('#ulLuckyList').animate({ top: top - 27 }, 200, null, function () {
        var height = jQuery('#ulLuckyList').height() / 2;
        var top = parseInt(jQuery('#ulLuckyList').css('top'));
        if (top == -height) {
            jQuery('#ulLuckyList').css('top', 0);
        }
    });
    if (luckyListTimeObj != null)
        clearTimeout(luckyListTimeObj);
    luckyListTimeObj = setTimeout('ScrollLuckyList()', 2000);
}

//初始化抽奖
function InitLottery() {
    var leaveMoney = jQuery('#divGoToLottery').find('.lotteryMyMoney').html() - jQuery('#divGoToLottery').find('.lotteryCost').html();
    if (leaveMoney < 0) {
        jQuery('#fontLotteryTips').html('&nbsp;哎呀，财富值不够，%&gt;_&lt;%');
        jQuery('#divNoMoneyMain').show();
        jQuery('#divLotteryMain').hide();
        jQuery('#divGoToLottery').css('width', '450px').css('margin', '-105px 0 0 -225px');
        var $actionLink = jQuery('#divGoToLottery').find('.actions').find('a');
        $actionLink.attr('href', $actionLink.attr('url')).attr('target', '_blank').html('立即赚取');
        return;
    }
    else {
        jQuery('#divNoMoneyMain').hide();
        jQuery('#divLotteryMain').show();
        jQuery('#divGoToLottery').css('width', '610px').css('margin', '-265px 0 0 -305px');
        var $actionLink = jQuery('#divGoToLottery').find('.actions').find('a');
        $actionLink.attr('href', 'javascript:;').attr('target', '_self').html('关 闭');
    }
    //    else {
    //        jQuery('#fontLotteryTips').html('&nbsp;很抱歉！您要的礼品已被抢完了！');
    //        jQuery('#divNoMoneyMain').show();
    //        jQuery('#divLotteryMain').hide();
    //        jQuery('#divGoToLottery').css('width', '450px').css('margin', '-105px 0 0 -225px');
    //        return;
    //    }
    hammerCanMove = true;
    jQuery('.lotterytips').removeClass('lotterytips1').html('请选择一个金蛋用力砸下去...');
    jQuery('.egg').removeClass('egg1')
                                .removeClass('egg2')
                                .removeClass('egg3')
                                .removeClass('egg4');
    jQuery('#spanHammer').removeClass().addClass('hammer');
    jQuery('.lotterymain').css('cursor', 'url("/ShowSkins/18show/images/wealthshop/a.ico"),auto');
    jQuery('a.egg').css('cursor', 'url("/ShowSkins/18show/images/wealthshop/a.ico"),auto');
}

//去抽奖
function GoToLottery(pId, leaveAmount, obj) {
    if (jQuery('#hidUserId').val() <= 0) {
        alert('请先登录后再抽奖！');
        NoLogin();
        return false;
    }
    if (leaveAmount <= 0) {
        return;
    }
    currPid = pId;
    currLeaveAmount = leaveAmount;
    var $currObj = jQuery(obj).parent().parent().parent();
    jQuery('#divGoToLottery').find('.lotteryPrizeName').html($currObj.find('.lotteryPrizeName').html()).attr('href', $currObj.find('.lotteryPrizeName').prev().attr('href'));
    jQuery('#divGoToLottery').find('.lotteryCost').html($currObj.find('.lotteryCost').html());
    jQuery('#divGoToLottery').find('.lotterySponsor').attr('title', $currObj.find('.lotterySponsor').attr('title')).attr('href', $currObj.find('.lotterySponsor').attr('href')).find('img').attr('src', $currObj.find('.lotterySponsor').html());
    jQuery('#iframeTransparent,#divTransparentBg,#divGoToLottery').show();
    InitLottery();
}

function HideGoToWish() {
    jQuery('#iframeTransparent,#divTransparentBg,#divGoToWish').hide();
}

function ReloadWindow() {
    location.reload();
}

function NoLogin() {
    window.location = '/main/login.aspx?returnUrl=' + document.location.href;
}
//获取触发事件控件left绝对位置
function getABSLeft(obj) {
    var x = obj.offsetLeft;
    while (obj = obj.offsetParent) {
        x += obj.offsetLeft;
    }
    return x;
}

//获取触发事件控件top绝对位置
function getABSTop(obj) {
    var y = obj.offsetTop;
    while (obj = obj.offsetParent) {
        y += obj.offsetTop;
    }
    return y;
}

function RedirectPointChange(pid, leaveamount) {
    if (leaveamount == 0) { alert("对不起，礼品已经全部兑换完！"); return false; }
    else {
        jQuery.ajax({
            type: "POST",
            url: "/Main/AjaxDataPage.aspx",
            data: { 'DataType': 'ExchangePrize', 'pId': pid },
            success: function (data) {
                if (data == 'redirect') {
                    if (loginUserId > 0) { window.open(ComAdminUrl.hostUrl + "/Show/PointPrizeInfo.aspx?Pid=" + pid); }
                    else { alert('请先登录后再兑换！'); window.location = "/main/login.aspx?returnUrl=/wealthshop/prize_" + pid + ".html"; }
                }
                else { alert(data); return false; }
            }
        });
    }
}
