﻿//调用方法在底部

//飘浮广告
function Floating() {
    // JavaScript Document
    //设置广告块
    this.divLeft = 0;
    //设置广告块初始左边位置
    this.divTop = 0;
    //设置广告块初始顶部位置
    this.divWidth = 225;
    //设置广告块宽度
    this.divHeight = 80;
    //设置广告块高度
    this.divImg = "/yj/pdzzh05.jpg";
    //设置广告图片的URL地址
    this.divUrl = "/url.aspx";
    //设置广告链接
    this.divTitle = "title";
    //设置div标题
    this._stepx = 1;
    this._stepy = 1;
    //初始化每次偏离的象素
    this.moveSpeed = 40;
    //速度
    this.varId;
    //获取setInterval的ID
    this.close=true;
    this.BeforeClose;
    this.InnerHtml;
    this.Floating_Div_Id = "Floating_Div_Id";

    this.Write = function() {
        var strHtml = "";
        strHtml += "<DIV id=\"" + this.Floating_Div_Id + "\" style=\"position:absolute; left:" + this.divLeft +
        "px; top:" + this.divTop + "px; width:" + this.divWidth + "px; height:" + this.divHeight + "px; z-index:1000;\" >";
        if (this.InnerHtml != null)
            strHtml += this.InnerHtml();
        if (this.divImg != null)
            strHtml += "<a href=\"###\" onclick=\"loadpage('yjxx.aspx')\" ><img src=\"" + this.divImg + "\" border=\"0\" alt=\"" + this.divTitle + "\"></a>";
        if (this.close)
            strHtml += "<div style=\"float:right;\"><a href=\"javascript:\" id='FloaingClose'><font style='font-size:5px;'>关闭</font></a></div>";
        strHtml += "</DIV>";
        document.body.innerHTML += strHtml;
        if (this.close)
            AttachEvent('click', document.getElementById('FloaingClose'), this.Close, this);
        AttachEvent('mouseover', document.getElementById(this.Floating_Div_Id), this.Clear, this);
        AttachEvent('mouseout', document.getElementById(this.Floating_Div_Id), this.Interval, this);
    }
    this.Close = function(my, e) {
        if (my.BeforeClose != null) {
            if (my.BeforeClose()) {
                my.Clear(my, my.Clear);
                document.getElementById(my.Floating_Div_Id).style.display = 'none';
            }
        }
        else {
            my.Clear(my, my.Clear);
            document.getElementById(my.Floating_Div_Id).style.display = 'none';
        }
    }
    this.Clear = function(my, e) {
        if (my.varId) {
            clearTimeout(my.varId);
            my.varId = 0;
        }
    }

    //移动
    this.moveAd = function(my, e) {
    if (document.getElementById(my.Floating_Div_Id).style.display == 'none')
            my.Clear(my, my.Clear);
        var adLeft = parseInt(document.getElementById(my.Floating_Div_Id).style.left);
        var adTop = parseInt(document.getElementById(my.Floating_Div_Id).style.top);
        var adWidth = parseInt(document.getElementById(my.Floating_Div_Id).style.width);
        var adHeight = parseInt(document.getElementById(my.Floating_Div_Id).style.height);
        var _bodyLeft = document.body.scrollLeft;
        var _bodyTop = document.body.scrollTop;
        var _bodyHeight = document.body.clientHeight + _bodyTop;
        var _bodyWidth = document.body.clientWidth + _bodyLeft;
        if (adLeft <= _bodyLeft && this._stepx < 0) {
            this._stepx = -1 * this._stepx;
        }
        if (adTop <= _bodyTop && this._stepy < 0) {
            this._stepy = -1 * this._stepy;
        }
        if ((adLeft + adWidth) >= _bodyWidth && this._stepx > 0) {
            this._stepx = -1 * this._stepx;
        }
        if ((adTop + adHeight) >= _bodyHeight && this._stepy > 0) {
            this._stepy = -1 * this._stepy;
        }
        document.getElementById(my.Floating_Div_Id).style.left = (adLeft + this._stepx) + "px";
        document.getElementById(my.Floating_Div_Id).style.top = (adTop + this._stepy) + "px";
    }
    this.Interval = function(my, e) {
        my.Clear(my, my.Clear);
        my.varId = window.setInterval("moveAds()", my.moveSpeed);
    }
}
///事件，需添加事件的对象，事件名，参数集合对象
function AttachEvent(type, target, handler, owner) {
    var eventHandler = handler;
    if (owner) {
        // 在这里重新定义一个处理函数
        eventHander = function(e) {
            handler(owner, e);
        }
    }
    if (window.document.all)
        target.attachEvent("on" + type, eventHander);
    else
        target.addEventListener(type, eventHander, false);
}

var move1 = new Floating();
function moveAds() {
    move1.moveAd(move1, move1.moveAd);
}

/*
//加载完后调用方法
        function MoveInit(val) {
            move1.divLeft = 0;
            move1.divTop = 0;
            move1.divWidth = 108;
            move1.divHeight = 81;
            //            move1.divImg = "/yj/pdzzh0" + val + ".jpg";
            move1.divImg = "/yj/p0101001.jpg";
            move1.divUrl = "/default.aspx";
            move1.divTitle = "地质灾害";

            move1._stepx = 1; 
            move1._stepy = 1;
            move1.moveSpeed = 40;
//            move1.BeforeClose = beforeClose;
//            move1.close = true;
//            move1.InnerHtml;
//            move1.Floating_Div_Id = "Floating_Div_Id";
            
            //以上都有默认值，下面是必需的
            move1.Write();
            move1.Interval(move1, move1.Interval);
        }
*/







var xmlHttp;

function init() {
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}


function callServer() {
    init();
    var url = document.location.href;
    if (url.indexOf("/") != -1) {
        var l = url.lastIndexOf("/");
        url = url.substr(0, l + 1);
    }
    xmlHttp.open("get", url+"getyjxh.aspx", true);
    xmlHttp.onreadystatechange = updatePage;
    xmlHttp.send(null);
}
function updatePage() {
    if (xmlHttp.readyState == 4) {
        var response = xmlHttp.responseText;  //这个就是请求后得到的Html源码，再根据源码处理自己要做的事
        if (response != "###" && response!="")
            MoveInit(response);
    }
}

function MoveInit(val) {
    move1.divLeft = 0;
    move1.divTop = 0;
    move1.divWidth = 100;
    move1.divHeight = 80;
    move1.divImg = val;
    move1.divUrl = "###";
    move1.divTitle = "预警信号";
    move1._stepx = 1;
    move1._stepy = 1;
    move1.moveSpeed = 40;
    move1.Write();
    move1.Interval(move1, move1.Interval);
}

window.onload = callServer;




