﻿// JScript

var hidePopor, showPopor, joinAct ;

window.onload=function() {

    var FloatDiv, PoporBox
		, g_actUrl = "http://ent.9sky.com/promo/kimi/"
        , FloatDivID="UP_PoporBox_FloatDiv" , PoporBoxID= "UP_PoporBox_Result"  // 浮层ID
        , FloatDivSize = "width: 190; height: 300px;", PoporBoxSize = "width:260px; height:80px;"
        , FloatDivHideTime = 10000, PoporBoxHideTime = 10000
        , passDate = new Date(2008,9,28); //活动截止日期，月份范围0~11
        
    var loadByIframe = function(theDiv,src,loadedFunc){
        if($("iframe[id^='PostPanel']").length>0){  // 有FCK编辑器的地方不弹窗
            return false;
        }
        document.domain='9sky.com'; 
        var iframeW = "<iframe frameborder='0' scrolling='no' allowTransparency='true' src='"+src+"' style='width:100%;height:100%'></iframe>";
        theDiv.html(theDiv.html() + iframeW);
	    if(loadedFunc!=null)loadedFunc(theDiv);	
    }
   
    if(passDate.getTime() < new Date().getTime())return; // 过期了
        
    var rnd = parseInt(Math.random() * 8);
    
    if (rnd > 0 && rnd < 5) {
        // 添加隐藏的 div 浮层 用于加载收集弹窗
        FloatDiv = $('<div>').attr({id:FloatDivID,style:FloatDivSize}).hide();
        $('body').append(FloatDiv);// +'FILTER: alpha(opacity=20);'
        
        // 绝对定位在右下角(兼容IE及火狐) 
        FloatDiv.html("<style>div#"+FloatDivID+"\n{position: fixed!important; position: absolute; z-index: 10;\nleft: expression(( - "+FloatDivID+".offsetWidth +(document.documentElement.clientWidth ?document.documentElement.clientWidth : document.body.clientWidth)+ (noValue2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft)) + 'px' );\ntop: expression(( - "+FloatDivID+".offsetHeight +(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) +(noValue = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ))+ 'px' );\nright: 0px;\nbottom: 0px;\n}\n</style>");
        
        // 在DIV中增加IFRAM用于加载页面
        var src = g_actUrl + 'Proxy/FloatDiv.aspx';
        loadByIframe(FloatDiv ,src, function(theDiv){
            theDiv.show();
            window.setTimeout("$('#"+FloatDivID+"').hide().html('');$('#"+FloatDivID+"').remove();",FloatDivHideTime);
        });
    }

    joinAct = function(id) {
        if (id > 4 || id < 1) return;
        if (!isLogin()) {
            alert("亲爱的九天网友，请登录以后再参与活动！");
            location.href = ' http://home.9sky.com/passport/login.aspx?return=' + encodeURIComponent(g_actUrl) + 'index.aspx';
            return false;
        }

        var sid = "IfukAjax";
        var s1 = document.getElementById(sid);
        if (s1) { document.getElementsByTagName("head")[0].removeChild(s1); }
        s1 = document.createElement("script");
        s1.setAttribute("id", sid);
        s1.type = "text/javascript";
        s1.src = g_actUrl + "Proxy/Ajax.aspx?date=" + Math.random() + "&act=join&fID=" + id;
        if ($.browser.msie) {
            s1.onreadystatechange = function() {
                var state = this.readyState;
                if (state == "loaded" || state == "interactive" || state == "complete") {
                    this.onreadystatechange = null;
                    if (result) {
                        FloatDiv.hide();
                        showPopor(id);
                    } else if (!result && status == 1) {
                        alert("您今天已经找到50个，请明天再来找吧!");
                    } else if (!result && status == 2) {
                        alert("亲爱的九天网友，请登录以后再参与活动!");
                        location.href = ' http://home.9sky.com/passport/login.aspx?return=' + encodeURIComponent(g_actUrl) + 'index.aspx';
                    } else {
                        alert("系统繁忙,请稍后再试! 错误代码:" + state);
                    }
                }
            };
        } else {
            s1.onload = function() {
                s1.onload = null;
                if (result) {
                    FloatDiv.hide();
                    showPopor(id);
                } else if (!result && status == 1) {
                    alert("您今天已经找到50个，请明天再来找吧!");
                } else if (!result && status == 2) {
                    alert("亲爱的九天网友，请登录以后再参与活动!");
                    location.href = ' http://home.9sky.com/passport/login.aspx?return=' + encodeURIComponent(g_actUrl) + 'index.aspx';
                } else {
                    alert("系统繁忙,请稍后再试! 错误代码:" + state);
                }
            };
        }
        document.getElementsByTagName("head")[0].appendChild(s1);
    };

    showPopor = function(num) {
        // 发送请求
        var srcTry = g_actUrl + 'Proxy/PickResult.aspx?num=' + num;

        // 使用弹窗代替  blockUI
        FloatDivSize = PoporBoxSize;        
        FloatDiv = $('<div>').attr({id:FloatDivID,style:FloatDivSize}).hide();
        $('body').append(FloatDiv);        
        // 绝对定位在右下角(兼容IE及火狐) 
        FloatDiv.html("<style>div#"+FloatDivID+"\n{position: fixed!important; position: absolute; z-index: 10;\nleft: expression(( - "+FloatDivID+".offsetWidth +(document.documentElement.clientWidth ?document.documentElement.clientWidth : document.body.clientWidth)+ (noValue2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft)) + 'px' );\ntop: expression(( - "+FloatDivID+".offsetHeight +(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) +(noValue = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ))+ 'px' );\nright: 0px;\nbottom: 0px;\n}\n</style>");
        // 在DIV中增加IFRAME用于加载页面
        loadByIframe(FloatDiv ,srcTry, function(theDiv){
            theDiv.show();
            window.setTimeout("$('#"+FloatDivID+"').hide().html('');$('#"+FloatDivID+"').remove();",FloatDivHideTime);
        });
        
//        PoporBox = $('<div>').attr({id:PoporBoxID, style:PoporBoxSize}).hide();
//        $('body').append(PoporBox);
//        
//        loadByIframe(PoporBox,srcTry, function(blockDiv) {
//            $.blockUI({
//                    message: blockDiv
//                        //,css:{
//                        //   top:($(window).height()-PoporBox.height())/2+'px', 
//                        //   left:($(window).width()-PoporBox.width())/2+'px'
//                        //}
//            });
//            //if(!$.browser.msie){ blockDiv.attr('style','position:fixed; left:250px; top:60px');}
//            $('a.close_x').click(function() {
//                PoporBox.hide();
//                $('.blockUI').remove();
//            });
//      });

    }
     
    // 隐藏浮层 
    hideFloat = function() {
        FloatDiv.hide();
    }
	
    // 获取cookie
    function getCookie(name, key) {
        var arg = name + "=";
        var i = document.cookie.indexOf(arg);
        if (i == -1) return '';
        var str = document.cookie.substring(i + arg.length);
        var j = str.indexOf(";");
        if (j > -1) str = str.substring(0, j);
        if (key && key.length > 0) {
            arg = key + "=";
            i = str.indexOf(arg);
            if (i == -1) return '';
            str = str.substring(i + arg.length);
            j = str.indexOf("&");
            if (j > -1) str = str.substring(0, j);
        }
        return decodeURI(str);
    }

    //获取登录状态
    function getLoginState() {
        var v = getCookie("9Sky_Passporta");
        if (v == "" || v == null) { v = getCookie("9Sky_Passportb"); }
        if (v == "" || v == null) { return false; } else { return true; }
    }

    //是否已登录
    function isLogin() {
        return getLoginState();
    }
}
//
