﻿//<![CDATA[
var intervalId;
var timeoutId;
var crowId;
$(function(){
    loadCrow(100);
    intervalId = setInterval("loadCrow(3000)", 30000);
    $("#btnFlashCrow").click(function(){
        detail();
        return false;
    });
    
});
function loadCrow(timeSpan){
    if($("#crowFlyDetail").html().length < 10){
        if(checkOptionTimeSpan()){
            getYaYaFlash();
            $("#crowFlying").fadeOut();
            setTimeout("$('#crowFlying').fadeIn()", timeSpan);
        }
    }
}
function detail(){
    if($("#flyCrowId")[0] == null){
        $.ajax({
            type: "POST",
            url: "../Crows/DetailFlying.aspx",
            data: "",
            timeout: 60000,
            success: function(msg){
                $("#crowFlyDetail").html(msg).slideDown();
                clearInterval(intervalId);
                if($("#isLogin").val() == "1"){
                    getYaYaFlash($("#crowStatusSource").val());
                    getScoreMsg();
                    setTimeSpanCookie();
                }
            },
            error: function(xmlHttp){
            }
        }) ;
    }
    return false;
}
function checkOptionTimeSpan()
{
    var maxTimeSpan = 30;
    var myDate = new Date();
    var currentTotalSecond = (myDate.getHours() * 60 + myDate.getMinutes()) * 60 + myDate.getSeconds();
    if($.cookie("tj") == null){
        return true;
    }
    else{
        return ((currentTotalSecond - parseInt($.cookie("tj"))) > maxTimeSpan) ? true : false;
    }
}
function getYaYaFlash(swf){
    var yy = (swf == null) ? "/swf/yaya0.swf" : swf;
    var so = new SWFObject(""+yy+"", "yyswf", "125", "200", "8", "transparent");
    so.addParam("wmode", "transparent");
    so.write("yayaSwf");
}
function goonFlying(){
    if($("#isLogin").val() == "1"){
        updatePressIndex();
    }
    else{
        hideCrow();
    }
    setTimeSpanCookie();
    intervalId = setInterval("loadCrow(3000)", 30000);
    return false;
}
function setTimeSpanCookie(){
    var myDate = new Date();
    var currentTotalSecond = (myDate.getHours() * 60 + myDate.getMinutes()) * 60 + myDate.getSeconds();
    $.cookie("tj",currentTotalSecond,{path:"/"}); 
}
function hideCrow(){
    $("#crowFlyDetail").html("");
    $("#crowFlyDetail").slideUp("slow");
    $("#crowFlying").fadeOut("slow");
    clearTimeout(timeoutId);
    timeoutId = setTimeout("$('#crowOptMsg').fadeOut()", 3000);
}
function reply(){
    checkLogin();
    crowId = $("#flyCrowId").val();
    var done = checkDone("yyr1"+crowId);
    if(!done){
        var content = $.trim($("#flyCrowContent").val());
        if(content.length < 6) { showError("回复必须6个字以上"); return false;}
        var yyPostValidation = encodeURIComponent($("#yyPostValidation").val());    
        $("#weaponBoxFlying").fadeOut();
        $("#crowOptMsg").fadeOut();
        $.ajax({
            type: "POST",
            url: "../Crows/ReplyInsert.aspx",
            data: "crowId=" + crowId + "&giftType=1&yyPostValidation="+ yyPostValidation +"&content="+content,
            timeout: 60000,
            success: function(msg){
                if(msg == "操作成功") { 
                    loadCrowReplyList(crowId);
                    displayMessage(1, msg, 1);
                }
                else{
                    displayMessage(2, msg, 1);
                }
            },
            error: function(xmlHttp){
                displayMessage(2, xmlHttp.responseText, 1);
            }
        }) ;
    }
    else{
        clearTimeout(timeoutId);
        $("#crowOptMsg").html("<br/><span class='error2'>你已经回复过了</span>").fadeIn();
        timeoutId = setTimeout("$('#crowOptMsg').fadeOut()", 6000);
    }
    return false;
}
function loadCrowReplyList(crowId){
    $.ajax({
        type: "GET",
        url: "../Crows/ReplyList.aspx",
        data: "crowId=" + crowId,
        timeout: 60000,
        success: function(msg){
            $("#crowFlyReplyList").html(msg);
        },
        error: function(xmlHttp){
        }
    }) ;
}
function send(giftType, weaponId){
    checkLogin();
    crowId = $("#flyCrowId").val();
    var done = checkDone("yyr"+giftType+crowId);
    if(!done){
        $("#crowOptMsg").fadeOut();
        var url = document.URL;
        var yyPostValidation = encodeURIComponent($("#yyPostValidation").val());
        $("#weaponBoxFlying").fadeOut();
        $.ajax({
            type: "POST",
            url: "../Crows/ReplyInsert.aspx",
            data: "weaponId=" + weaponId +"&crowId=" + crowId + "&giftType="+ giftType + "&yyPostValidation=" + yyPostValidation,
            timeout: 60000,
            success: function(msg){
                if(msg == "操作成功") { 
                    displayMessage(1, msg, giftType);
                }
                else{
                    displayMessage(2, msg, giftType);
                }
            },
            error: function(xmlHttp){
                displayMessage(2, "", giftType);
            }
        }) ;
    }
    else{
        clearTimeout(timeoutId);
        $("#crowOptMsg").html("<br/><span class='error2'>你已经操作过了</span>").fadeIn();
        timeoutId = setTimeout("$('#crowOptMsg').fadeOut()", 6000);
    }
    return false;
}
function displayMessage(msgType, msg, giftType){
    if(msgType == 2 || msg != "操作成功"){ showError(msg); return false;}

    switch(giftType){
        case 1:
          setPressIndex(5);
          $("#slipQty").text(parseInt($("#slipQty").text()) + 1);
          $("#flyCrowContent").val("");
          showScoreMsg("+5");
          $.cookie('yyr1'+ crowId, '1');
          break;
        case 2:
          setPressIndex(1);
          changeQty("gumQty");
          showScoreMsg("+1");
          $.cookie('yyr2'+ crowId, '1');
          break;
        case 3:
          setPressIndex(2);
          changeQty("weaponQty");
          showScoreMsg("+2");
          $.cookie('yyr3'+ crowId, '1');
          break;
    }
}

function getScoreMsg(){
    var crowId = $("#flyCrowId").val();
    var crowStatus = parseInt($("#crowStatus").val());
    var limit = parseInt($("#limit").val());
    $.ajax({
        type: "POST",
        url: "../Crows/GetScoreMsg.aspx",
        data: "crowId="+ crowId +"&limit="+ limit +"&crowStatus=" + crowStatus,
        timeout: 60000,
        success: function(msg){
            setPressIndex(1);
            show(msg);
            if(msg.indexOf("三个月QQ红钻会员") > -1){
                setTimeout("alert('流星划过，丫丫的愿望成真了！丫丫送你“三个月QQ红钻会员”，告诉丫丫你的qq号码吧！');location.href='/Members/ChangeInfo.aspx';", 2000);
            }
        },
        error: function(xmlHttp){
        }
    }) ;
}

function updatePressIndex(){
    var crowId = $("#flyCrowId").val();
    var pindex = parseInt($("#pindex").val());
    var yyPostValidation = encodeURIComponent($("#yyPostValidation").val());
    if(pindex > 0){
        $.ajax({
            type: "POST",
            url: "../Crows/UpdatePressIndex.aspx",
            data: "crowId="+ crowId +"&pindex=" + pindex + "&yyPostValidation=" + yyPostValidation,
            timeout: 60000,
            success: function(msg){
                show(msg);
                hideCrow();
            }
        }) ;
    }
}

function setPressIndex(index){
    var p = parseInt($("#pindex").val()) + index;
    $("#pindex").val(p);
    $.cookie("p",p,{path:"/"}); 
}

function changeQty(id){
    $("#"+id).text("(" + parseInt(parseInt($("#"+id).text().replace("(","").replace(")","")) + 1) + ")");
}
function show(msg){
    if($.trim(msg).length > 0){
        clearTimeout(timeoutId);
        $("#crowOptMsg").html(msg).fadeIn();
        timeoutId = setTimeout("$('#crowOptMsg').fadeOut()", 6000);
    }
}
function showError(msg){
    if(msg.length > 0)
        show("<br/><span class='error2'>"+ msg +"</span>");
    else
        show("<br/><span class='error2'>操作失败，请稍候再试。</span>");
}
function showScoreMsg(score){
    show("<br/>操作成功!<br/>会员体力<span class='no'>"+score+"</span>");
}
function checkDone(cookieName){
    return ($.cookie(cookieName) == "1") ? true : false;
}

function getWeaponList()
{
    checkLogin();

    $("#crowOptMsg").fadeOut();
    $("#weaponBoxFlying").css({ position:"absolute", right: "53px", top: "160px" });
    $("#weaponBoxFlying").fadeIn();
}
function hideWeaponList(){
    $("#weaponBoxFlying").fadeOut();
}
function checkLogin(){
    if($("#isLogin").val() != "1"){
        window.location.href = "/Login.aspx?ReturnUrl=" + document.URL;
        return false;
    }
}
function clearCookie(){
    //$.cookie('yy', null);
    //$.cookie('p', null);
}

function showDownListBox(){
	$("#weaponBox").fadeIn();
}
function hideDownListBox(){
	$("#weaponBox").slideUp();
}
(function (bool) {
    var html;
    if (bool) {
        html = window.HTMLElement.prototype;
        window.__defineGetter__("event", function () {
            var o = arguments.callee;
            do {
                if (o.arguments[0] instanceof Event) return o.arguments[0];
            } while (o = o.caller);
            return null;
        });
        Event.prototype.__defineGetter__("fromElement", function () {
            return this.relatedTarget;
        });
        html.contains = function (o) {
            do {
                if (o == this) return true;
            } while (o = o.parentNode);
            return false;
        };
    }
    })(/Firefox/.test(window.navigator.userAgent));

    function hit(){
        var dvBox = document.getElementById("weaponBox");
        showDownListBox();
        dvBox.onmouseout = function(){
        var dvBox = this, e = window.event;
        if(!dvBox.contains(e.toElement || e.fromElement))
            hideDownListBox();
    }
}
//]]>