﻿/*
*  易佰js库
* 
* 平时常用的js代码段、库、类
* 
* Copyright (c) 2009 柏富强（bofuqiang@gmail.com）
* 代码虽然简单，但在使用或修改时还请保留本人的版权信息.
* 
*
* 使用实例 ： js：new Yibai.Maquree("maquree_obj_id",1,50,"up",10);
*
*/



/*
* 
* 添加当前浏览页到收藏夹
*
* ----------------------------------------------------------------------
* 
* 参数：
*   title: 收藏夹的显示标题；
*
* ----------------------------------------------------------------------
*
* 使用实例 ： js：addBookmark("测试收藏夹");
*
*/

function addBookmark() {

    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("msie 8") > -1) {
        external.AddToFavoritesBar(document.URL, document.title, 'slice'); //IE8
    } else {
        try {
            window.external.addFavorite(document.URL, document.title);
        } catch (e) {
            try {
                window.sidebar.addPanel(document.title, document.URL, ""); //firefox
            } catch (e) {
                alert("加入收藏失败，请使用Ctrl+D进行添加");
            }
        }
    }

}


/*
* 
* 将当前浏览页设为浏览器首页
*
*/

function setHomePage() {
    document.body.style.behavior = 'url(#default#homepage)';
    document.body.setHomePage(document.location);
}


/*
* 易佰js基类
* 
* 
* 编写日期：2010-2-9
* 
* Revision: V1.0
* 
*
* 使用实例 ： js：var objclass = YibaiClass.create();
*
*
*/


var YibaiClass =
{
    create: function() {
        return function() {
            this.ready.apply(this, arguments);
        }
    }
}


/*
* js 字幕实现类
* 
*
* Copyright (c) 2009 柏富强（bofuqiang@gmail.com）
* 代码虽然简单，但在使用或修改时还请保留本人的版权信息.
* 
* 编写日期：2010-2-9
* 
* Revision: V1.0
* ----------------------------------------------------------------------
* 
* 参数：
*   objid: 要实现字幕滚动效果的容器的id，容器一般为div、ul；
*   step: 指定时间内移动的像素大小；单位像素
*   speed: 字幕滚动的时间间隔，单位毫秒
*   dircetion：字幕滚动的方向；只支持 "up"-上、"left"-左，默认"left";
*   nodeNum: 容器内要实现字幕的最低子节点数，小于nodeNum会自动添加；
*
* ----------------------------------------------------------------------
*
* 使用实例 ： js：new Yibai.Maquree("maquree_obj_id",1,50,"up",10);
*
*/

var YibaiMaquree = YibaiClass.create();

YibaiMaquree.prototype =
{

    ready: function(objid, step, speed, dircetion, nodeNum) {
        this.element = document.getElementById(objid);
        var obj = this.element;

        var c_nodes = obj.childNodes.length;
        if (c_nodes == 0)
            return;

        this.step = step;
        this.speed = speed;
        this.dircetion = dircetion;

        while (obj.childNodes.length < nodeNum) {
            var i;
            for (i = 0; i < c_nodes; i++) {
                var clonenode = obj.childNodes[i].cloneNode(true);
                obj.appendChild(clonenode);
            }
        }
        this.element.setAttribute("onmouseover", function() { obj.setAttribute("stop", "1"); });
        this.element.setAttribute("onmouseout", function() { obj.setAttribute("stop", "0"); });

        this.scroll();
    },
    scroll: function() {

        var i_stop = this.element.getAttribute("stop");
        var obj = this.element;
        if (i_stop != "1") {
            var h;
            if (this.dircetion == "left" || this.dircetion == "") {
                h = parseInt(obj.firstChild.style.marginLeft) * -1;
            }
            else if (this.dircetion = "up") {
                h = parseInt(obj.firstChild.style.marginTop) * -1;
            }

            if (isNaN(h)) h = 0;
            if (h == 0) {
                var clonenode = obj.firstChild.cloneNode(true);

                if (this.dircetion == "left" || this.dircetion == "") {
                    obj.firstChild.style.marginLeft = "-1px";
                }
                else if (this.dircetion == "up") {
                    obj.firstChild.style.marginTop = "-1px";
                }

                obj.appendChild(clonenode);
            }
            else {
                if (this.dircetion == "left" || this.dircetion == "") {
                    if (h > 0 && h < obj.firstChild.offsetWidth) {
                        obj.firstChild.style.marginLeft = (h + this.step) * -1 + "px";
                    }
                    else
                        obj.removeChild(obj.firstChild);
                }
                else if (this.dircetion == "up") {
                    if (h > 0 && h < obj.firstChild.offsetHeight) {
                        obj.firstChild.style.marginTop = (h + this.step) * -1 + "px";
                    }
                    else
                        obj.removeChild(obj.firstChild);
                }


            }
        }
        var _self = this;
        setTimeout(function() { _self.scroll() }, this.speed);
    }
}

