﻿var $FILE_HOST = 'http://file.tatbjs.org.cn';
function LoadAd(size, dictid, sender) {
    // /<summary>加载广告</summary>
    $.ajax({
        type: 'GET',
        dataType: 'json',
        data: { dictid: dictid ,cache:true},
        url: '/Guest/Data/GetArticleByDictid.ajax',
        success: function (data) {
            var items = [];
            for (var i = 0; i < data.length; i++) {
                items.push(new Image());
                items[items.length - 1].src = $FILE_HOST + '/images/'
				+ (size ? size + "/" : "") + data[i].articleid + '.jpg'; ;
                items[items.length - 1].title = data[i].title;
                items[items.length - 1].link = data[i].description == 'http://'
				? 'javascript:'
				: data[i].description;
                items[items.length - 1].notes = data[i].content;
            }
            sender.init(items);
        },
        error: function (data) {
        }
    })
    return true;
}

function TAdSlide(id, img) {
    // /<summary>幻灯片广告。[传入的img DOM对象，需要有style的Filter属性]</summary>
    this.items = [];
    this.speed = 3100;
    this.isIE = document.all;
    this.link = 0;
    this.image = 0;
    this.delay = null;
    this.name = id;
    this.img = img;

    this.play = function () {
        // /<summary>播放幻灯片</summary>
        if (!document.images)
            return
        if (this.isIE)
            this.img.filters[0].apply();
        this.img.src = this.items[this.image].src;
        if (this.isIE)
            this.img.filters[0].play();
        this.img.link = this.items[this.image].link;
	this.img.style.cursor='pointer';

        this.image = (this.image < this.items.length - 1) ? this.image + 1 : 0;
	this.img.abbr=this.image.link;
	this.img.onclick=function(){window.location.href=this.link;}
        setTimeout(this.name + '.play()', this.speed + this.delay);
    };

    this.init = function (data) {
        // /<summary>初始化</summary>
        // /<param name="oContainer" type="DOM Object">div容器</param>
        // /<param name="sid" type="Integer">广告分类id</param>
        // /<param name="fn" type="String">实例名</param>
        if (!this.img)
            return;
        this.items = data;
        if (this.items.length == 0 || !this.items[0])
            return;

        this.img.src = this.items[0].src;
        this.img.style.display = '';
        this.delay = (this.isIE) ? this.img.filters[0].duration * 1000 : 0;
        this.play();
    };
}

function TAdCouplet(id) {
    // /<summary>对联广告</summary>
    this.items = [];
    this.name = id;
    this.arrAd = [];

    this.clone = function (s) {
        // /<summary>克隆一个广告对象</summary>
        var o = new Image();
        o.src = s.src;
        o.title = s.title;
        o.link = s.link;
        o.notes = s.notes;
    };

    this.initMove = function () {
        if (this.arrAd.length > 0) {
            try {
                this.arrAd[0].style.top = "-100px";
                this.arrAd[0].style.visibility = 'visible';
                this.arrAd[1].style.top = "-100px";
                this.arrAd[1].style.visibility = 'visible';
                this.moveLayers(this);
            }
            catch (e) {
            }
        }
    };

    this.moveLayers = function (sender) {
        try {
            if (sender.arrAd && sender.arrAd.length > 0) {
                var x = 5; // 左右边距
                var y = 100; // 顶距
                var st = document.documentElement.scrollTop;
                var cw = document.documentElement.clientWidth;
                var y = st + y;
                sender.arrAd[0].style.top = y + "px";
                sender.arrAd[0].style.left = x + "px";
                sender.arrAd[1].style.top = y + "px";
                sender.arrAd[1].style.left = cw - sender.items[1].clientWidth - x
					+ "px";
            }
        }
        catch (e) { }
    };

    this.init = function (data) {
        // /<summary>初始化</summary>
        // /<param name="sid" type="Integer">广告分类id</param>
        // /<param name="fn" type="String">实例名</param>
        // /<param name="speed" type="Integer">滚动刷新频率:毫秒</param>
        // /<param name="isHorizontally" type="Boolean">是否是垂直滚动</param>
        this.items = data;
        if (this.items.length == 0)
            return;

        // 两侧广告不成对时，自动补齐；超过两个时，删除多余对象；
        if (this.items.length == 1) {
            this.items.push(this.clone(this.items[0]));
        } else if (this.items.length > 2) {
            this.items.splice(2, this.items.length - 2);
        }

        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i]) {
                var a = document.createElement("A");
                a.appendChild(this.items[i]);
                a.href = a.firstChild.link;
                a.target = '_blank';
                a.title = a.firstChild.title;

                var div = document.createElement("DIV");
                div.appendChild(a);
                document.body.appendChild(div);
                this.arrAd.push(document.body.lastChild);
                div.style.position = 'absolute';
                div.style.zIndex = 1;
                div.style.top = '-100px';
                div.style.visibility = 'visible';

                var btn = document.createElement("A");
                btn.onclick = function () {
                    this.parentNode.style.display = 'none';
                };
                btn.className = 'close';
                btn.title = '关闭';
                btn.href = 'javascript:';
                div.appendChild(btn);
            }
        }
        this.initMove();
        setTimeout(this.name + ".initMove()", 600);
        sender = this;
        window.onscroll = function () {
            sender.moveLayers(sender);
        };
    };
}

function TAdMarquee(id, container) {
    this.items = [];
    this.rows = [];
    this.delay = 4000;
    this.height = 40;
    this.width = null;
    this.name = id;
    this.marqueeInterval = [];
    this.index = 0;
    this.colCount = 8;
    this.container = container;

    this.init = function (data) {
        // /<summary>初始化</summary>
        // /<param name="oContainer" type="DOM Object">div容器</param>
        // /<param name="sid" type="Integer">广告分类id</param>
        // /<param name="fn" type="String">实例名</param>
        if (!this.container)
            return;
        this.items = data;
        if (this.items.length == 0)
            return;
        // this.height = this.container.offsetHeight;
        this.width = this.container.offsetWidth;
        var k = 0;
        for (var i = 0; i < this.items.length; i++) {
            k = Math.floor(i / this.colCount);
            if (!this.rows[k])
                this.rows[k] = '';
            this.rows[k] += '<a href="' + this.items[i].link + '" title="' + this.items[i].title
					+ '" target="_blank"><img src="' + this.items[i].src
					+ '"/></a>';
        }

        this.container.innerHTML = '<div id="marqueeBox" style="overflow:hidden;height:'
				+ (this.height + 2)
				+ 'px" onmouseover="clearInterval('
				+ this.name
				+ '.marqueeInterval[0])" onmouseout="'
				+ this.name
				+ '.marqueeInterval[0]=setInterval(\''
				+ this.name
				+ '.play()\','
				+ this.name
				+ '.delay)"><div>'
				+ this.rows[this.index] + '</div></div>';

        this.marqueeInterval[this.index] = setInterval(this.name + ".play()",
				this.delay);
    };

    this.play = function () {
        this.index++;
        var rowIndex = this.index % this.rows.length;
        if (this.container.childNodes.length == 1) {
            var div = document.createElement("DIV");
            div.innerHTML = this.rows[rowIndex];
            this.container.appendChild(div);
            jQuery(div).css("width", this.width + "px").css("height", (this.height + 2) + "px").css("owerflow", "hidden").css("clear", "both");
        } else {
            this.container.childNodes[0].innerHTML = this.rows[rowIndex];
            this.container.appendChild(this.container.childNodes[0]);
            this.container.scrollTop = 0;
        }
        clearInterval(this.marqueeInterval[1]);
        this.marqueeInterval[1] = setInterval(this.name + ".scrollMarquee()",
				20);
    };

    this.scrollMarquee = function () {
        this.container.scrollTop += 3;
        if (this.container.scrollTop % this.marqueeHeight == (this.marqueeHeight - 1)) {
            clearInterval(this.marqueeInterval[1]);
        }
    };
}

function TAdScoll(id, container) {
    this.items = [];
    this.name = id;
    this.container = container;

    this.play = function () {
        picrun_ini();
    };

    this.init = function (data) {
        // /<summary>初始化</summary>
        // /<param name="oContainer" type="DOM Object">div容器</param>
        // /<param name="sid" type="Integer">广告分类id</param>
        // /<param name="fn" type="String">实例名</param>
        if (!this.container)
            return;
        this.items = data;
        if (this.items.length == 0)
            return;

        var str = '';
        for (var i = 0; i < this.items.length; i++) {
            str += '<div class="box-img"><img name="' + i + '" src="'
					+ this.items[i].src + '" onmouseover="' + this.name
					+ '.showMsg(this)" onmouseout="' + this.name
					+ '.removeMsg(this)" onclick="' + this.name
					+ '.showImageBox(this);return false"/><p><a href="'
					+ this.items[i].link + '" target="_blank" title="'
					+ this.items[i].title + '">' + this.items[i].title
					+ '</a></p></div>';
        }
        this.container.innerHTML = str;
        $('#ad3').show();
        this.play();
    };

    this.showMsg = function (sender) {
        MENU.hideSubmenu(); // 引用tatbjs.js
        if (this.items[sender.name].notes && this.items[sender.name].notes != "null") {
            var o = $('#pic_show_l')[0];
            var divMsgBg = document.createElement("DIV");
            divMsgBg.setAttribute("id", "adScollMsgBg");
            divMsgBg.setAttribute("class", "msgBg1");
            o.appendChild(divMsgBg);

            var divMsg = document.createElement("DIV");
            divMsg.setAttribute("id", "adScollMsg");
            divMsg.setAttribute("class", "msg1");
            divMsg.innerHTML = this.items[sender.name].notes;
            o.appendChild(divMsg);
        }
    };

    this.removeMsg = function () {
        // 去除提示信息
        var o = $('#pic_show_l')[0];
        if (o.lastChild.id == 'adScollMsg') {
            o.removeChild(o.lastChild);
            o.removeChild(o.lastChild);
        };
    };

    this.showImageBox = function (sender) {
        MENU.hideSubmenu();
        msgImg = $("#msgBoxImg")[0];
        var img = msgImg.childNodes[1].childNodes[0];
        var tmp = sender.src.split('/');
        img.src = $FILE_HOST + '/images/400x0/' + tmp[tmp.length - 1];
        img.alt = '点击关闭';
        msgImg.style.display = '';

        var p = $('P', msgImg)[0];
        p.innerHTML = '<a href="' + this.items[sender.name].link
				+ '" target="_blank" >' + this.items[sender.name].notes
				+ ' <b style="color:red">[了解更多...]</b></a>';
    };
}

function HiddenImgBox() {
    msgImg = $("#msgBoxImg")[0];
    msgImg.style.display = 'none';
}

function ShowPopupWin() {
    // /<summary>弹出窗口</summary>
    var win = GetData('/Ad/GetPopupWin.ajax?_dc='
			+ (new Date()).getTime().toString());
    if (win == null || win.Win_IsLock || !win.Win_Content)
        return;
    using("/js/src/bgiframe.js");
    using("/js/src/weebox.css");
    using("/js/src/weebox.js");

    switch (win.Win_Type) {
        case 1: // 拖动窗口
            $.weeboxs.open(win.Win_Content, {
                title: win.Win_Title,
                width: win.Win_Width,
                height: win.Win_Height,
                showCancel: false,
                okBtnName: '关闭'
            });
            break;
        case 2: // 飘动窗口
            $.weeboxs.open(win.Win_Content, {
                title: win.Win_Title,
                width: win.Win_Width,
                height: win.Win_Height,
                showCancel: false,
                okBtnName: '关闭'
            });
            break;
        default: // 新弹出窗口
            var doc = '\r<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link href="/css/style.css" type="text/css" rel="stylesheet" />'
					+ ' <base target="_blank" /></head><body>'
					+ win.Win_Content + '</body></html>\n';
            var winname = window
					.open(
							'',
							'_blank',
							'height='
									+ win.Win_Height
									+ ',width='
									+ win.Win_Width
									+ ',top=200,left=300,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
            winname.document.open('text/html', 'replace');
            winname.document.writeln(doc);
            winname.document.title = win.Win_Title;
            break;
    }
}

function TCommonAd(id, container) {
    // 通用广告
    this.container = container,
    this.name = id,
    this.init = function (data) {
        bbbbbbbbbbbbbbbbbbbbbbb = this;
        for (var i = 0; i < data.length; i++) {
            var a = document.createElement("A");
            a.appendChild(data[i]);
            a.href = data[i].link;
            a.title = data[i].title;
            this.container.appendChild(a);
        }
    }
}
// **************=================================*****************************

var Speed_1 = 30; // 速度(毫秒)
var Space_1 = 20; // 每次移动(px)
var PageWidth_1 = 960; // 翻页宽度
var interval_1 = 10000; // 翻页间隔时间
var fill_1 = 0; // 整体移位
var MoveLock_1 = false;
var MoveTimeObj_1;
var MoveWay_1 = "right";
var Comp_1 = 0;
var AutoPlayObj_1 = null;
function GetObj(objName) {
    if (document.getElementById) {
        return eval('document.getElementById("' + objName + '")');
    } else {
        return eval('document.all.' + objName);
    }
}
function AutoPlay_1() {
    clearInterval(AutoPlayObj_1);
    AutoPlayObj_1 = setInterval('ISL_GoDown_1();ISL_StopDown_1();', interval_1);
}
function ISL_GoUp_1() {
    if (MoveLock_1)
        return;
    clearInterval(AutoPlayObj_1);
    MoveLock_1 = true;
    MoveWay_1 = "left";
    MoveTimeObj_1 = setInterval('ISL_ScrUp_1();', Speed_1);
}
function ISL_StopUp_1() {
    if (MoveWay_1 == "right") {
        return
    };
    clearInterval(MoveTimeObj_1);
    if ((GetObj('ISL_Cont_1').scrollLeft - fill_1) % PageWidth_1 != 0) {
        Comp_1 = fill_1 - (GetObj('ISL_Cont_1').scrollLeft % PageWidth_1);
        CompScr_1();
    } else {
        MoveLock_1 = false;
    }
    AutoPlay_1();
}
function ISL_ScrUp_1() {
    if (GetObj('ISL_Cont_1').scrollLeft <= 0) {
        GetObj('ISL_Cont_1').scrollLeft = GetObj('ISL_Cont_1').scrollLeft
				+ GetObj('list1_1').offsetWidth;
    }
    GetObj('ISL_Cont_1').scrollLeft -= Space_1;
}
function ISL_GoDown_1() {
    clearInterval(MoveTimeObj_1);
    if (MoveLock_1)
        return;
    clearInterval(AutoPlayObj_1);
    MoveLock_1 = true;
    MoveWay_1 = "right";
    ISL_ScrDown_1();
    MoveTimeObj_1 = setInterval('ISL_ScrDown_1()', Speed_1);
}
function ISL_StopDown_1() {
    if (MoveWay_1 == "left") {
        return
    };
    clearInterval(MoveTimeObj_1);
    if (GetObj('ISL_Cont_1').scrollLeft % PageWidth_1
			- (fill_1 >= 0 ? fill_1 : fill_1 + 1) != 0) {
        Comp_1 = PageWidth_1 - GetObj('ISL_Cont_1').scrollLeft % PageWidth_1
				+ fill_1;
        CompScr_1();
    } else {
        MoveLock_1 = false;
    }
    AutoPlay_1();
}
function ISL_ScrDown_1() {
    if (GetObj('ISL_Cont_1').scrollLeft >= GetObj('list1_1').scrollWidth) {
        GetObj('ISL_Cont_1').scrollLeft = GetObj('ISL_Cont_1').scrollLeft
				- GetObj('list1_1').scrollWidth;
    }
    GetObj('ISL_Cont_1').scrollLeft += Space_1;
}
function CompScr_1() {
    if (Comp_1 == 0) {
        MoveLock_1 = false;
        return
    }
    var num, TempSpeed = Speed_1, TempSpace = Space_1;
    if (Math.abs(Comp_1) < PageWidth_1 / 2) {
        TempSpace = Math.round(Math.abs(Comp_1 / Space_1));
        if (TempSpace < 1) {
            TempSpace = 1;
        }
    }
    if (Comp_1 < 0) {
        if (Comp_1 < -TempSpace) {
            Comp_1 += TempSpace;
            num = TempSpace;
        } else {
            num = -Comp_1;
            Comp_1 = 0;
        }
        GetObj('ISL_Cont_1').scrollLeft -= num;
        setTimeout('CompScr_1()', TempSpeed);
    } else {
        if (Comp_1 > TempSpace) {
            Comp_1 -= TempSpace;
            num = TempSpace;
        } else {
            num = Comp_1;
            Comp_1 = 0;
        }
        GetObj('ISL_Cont_1').scrollLeft += num;
        setTimeout('CompScr_1()', TempSpeed);
    }
}
function picrun_ini() {
    GetObj("list2_1").innerHTML = GetObj("list1_1").innerHTML;
    GetObj('ISL_Cont_1').scrollLeft = fill_1 >= 0
			? fill_1
			: GetObj('list1_1').scrollWidth - Math.abs(fill_1);
    GetObj("ISL_Cont_1").onmouseover = function () {
        clearInterval(AutoPlayObj_1);
    };
    GetObj("ISL_Cont_1").onmouseout = function () {
        AutoPlay_1();
    };
    AutoPlay_1();
}

// **************=================================*****************************
// #ad1 对联广告 #ad1
var ad1 = new TAdCouplet('ad1');
LoadAd("120x480", '2a380c09ad4a409fa7556f986a991d94', ad1);

// #ad2 横幅切换广告
var ad2 = new TAdSlide("ad2", $('#ad2')[0]);
LoadAd("950x100", 'b5a362cf502249b182a3618151c1b3bc', ad2);

// #ad3 顶部滚动
var ad3 = new TAdScoll('ad3', $('#list1_1')[0]);
LoadAd("0x120", '9089d69f4aad4da7a1c2ba5aa63d98ef', ad3);

// #ad4 左侧版心广告(3:1)
var ad4 = new TCommonAd('ad4',$('#ad4')[0]);
LoadAd("210x70", '63aa70e6027744caa6c0d68b4c1f8303', ad4);

// #ad5 底部链接(3:1)
var ad5 = new TAdMarquee('ad5', $('#ad5')[0]);
LoadAd("120x40", '94a4b7aaca0c4ee0b30ccb019dc802d8', ad5);

// #ad6 右侧底部(3:1)
var ad6 = new TCommonAd('ad6', $('#ad6')[0]);
LoadAd("147x45", 'd3e2c5aacc3d4add910f244021a5bc21', ad6);
// ShowPopupWin();

