/*
*  易佰js库
* 
* 平时常用的js代码段、库、类
* 
* Copyright (c) 2009 柏富强（bofuqiang@gmail.com）
* 代码虽然简单，但在使用或修改时还请保留本人的版权信息.
* 
*
* 使用实例 ： js：new Yibai.Maquree('maquree_obj_id',1,50,'up',10);
*
*/



/*
* 
* 添加当前浏览页到收藏夹
*
* ----------------------------------------------------------------------
* 
* 参数：
*   title: 收藏夹的显示标题；
*
* ----------------------------------------------------------------------
*
* 使用实例 ： js：addBookmark('测试收藏夹');
*
*/

function addBookmark(title) {

    window.external.addToFavoritesBar(document.URL, title);
}


/*
* 
* 将当前浏览页设为浏览器首页
*
*/

function setHomePage() {
    document.body.style.behavior = 'url(#default#homepage)';
    document.body.setHomePage(document.location);
    return false;
}


/*
* 易佰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.3
* ----------------------------------------------------------------------
* 
* 参数：
*   objid: 要实现字幕滚动效果的容器的id，容器一般为div、ul；
*   step: 指定时间内移动的像素大小；单位像素
*   speed: 字幕滚动的时间间隔，单位毫秒
*   dircetion：字幕滚动的方向；只支持 'up'-上、'left'-左，默认'left';
*   nodeNum: 容器内要实现字幕的最低子节点数，小于nodeNum会自动添加；
*
* ----------------------------------------------------------------------
*
* 使用实例 ： js：new YibaiMaquree('maquree_obj_id',1,50,'up',10);
*
* 本字幕必须使用Jquery库
*/

var YibaiMaquree = YibaiClass.create();

YibaiMaquree.prototype =
{

		ready: function(objid, step, speed, dircetion, nodeNum) {
				this.itime = null; 
				this.element = $('#' + objid);
				var obj = this.element;

				if (obj.children().length == 0)
						return;

				this.step = step;
				this.speed = speed;
				if (dircetion == '')
						dircetion = 'left';
				this.dircetion = dircetion.toLowerCase();

				while (obj.children().length < nodeNum) {
						var i;
						for (i = 0; i < nodeNum; i++) {
								var clonenode = obj.children().eq(i).clone(true);
								obj.children(':last-child').after(clonenode);
						}
				}
				obj.bind('mouseover', function() { $(obj).attr('stop', '1'); });
				obj.bind('mouseout', function() { $(obj).attr('stop', '0'); });
				this.scroll();
		},
		scroll: function() {
				var obj = this.element;
				if (this.itime )
						clearTimeout(this.itime);
				var h;

				var i_stop = obj.attr('stop');
				var child = obj.children(':first');

				if (i_stop != '1') {
						switch (this.dircetion) {
								case 'left':
										h = parseInt(child.css('marginLeft')) * -1;
										break;
								case 'up':
										h = parseInt(child.css('marginTop')) * -1;
										break;
								default:
										h = parseInt(child.css('marginLeft')) * -1;
										break;
						}
						if (isNaN(h)) h = 0;

						switch (this.dircetion) {
								case 'left':
										//h = child.scrollLeft();
										if (h == 0) {
												obj.children(':last').after(child.clone(true));
												//												child.scrollLeft(this.step);
												//												document.title =h;
												child.css('marginLeft', '-1');
												//child.animate({ marginLeft: "-=" + this.step }, this.speed);
										}
										else if (h >= child.outerWidth()) {
												child.remove();
										}
										else {
												//child.animate({marginLeft:"-="+this.step }, this.speed);
												//child.scrollLeft(this.step);
												child.css('marginLeft', -(h + this.step));
										}
										break;
								case 'up':
										if (h == 0) {
												obj.children(':last').after(child.clone(true));
												child.css('marginTop', '-1')
												//child.animate({ marginTop: "-=" + this.step }, this.speed);	
										}
										else if (h >= child.outerHeight()) {
												child.remove();
										}
										else {
												//child.animate({ marginTop: "-=" + this.step }, this.speed);
												child.css('marginTop', (h + this.step) * -1);
										}
										break;
								default:
										if (h == 0) {
												obj.children(':last').after(child.clone(true));
												child.css('marginLeft', this.step);
												//child.animate({ marginLeft: "-=" + this.step }, this.speed);

										}
										else if (h >= child.outerWidth()) {
												child.remove();
										}
										else {
												child.css('marginLeft', (h + this.step) * -1);
												//child.animate({ marginLeft: "-=" + this.step }, this.speed);
										}
										break;
						}

				}
				if (!this) return;
				var _self = this;
				this.itime = setTimeout(function() { _self.scroll() }, this.speed);

		}
}

var g$ = function(id) {
		return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
		create: function() {
				return function() {
						this.initialize.apply(this, arguments);
				}
		}
}
Object.extend = function(destination, source) {
		for (var property in source) {
				destination[property] = source[property];
		}
		return destination;
}
function addEventHandler(oTarget, sEventType, fnHandler) {
		if (oTarget.addEventListener) {
				oTarget.addEventListener(sEventType, fnHandler, false);
		} else if (oTarget.attachEvent) {
				oTarget.attachEvent("on" + sEventType, fnHandler);
		} else {
				oTarget["on" + sEventType] = fnHandler;
		}
};
var Scroller = Class.create();
Scroller.prototype = {
	initialize: function (idScrollMid, options) {
		var oThis = this, oScrollMid = g$(idScrollMid);
		var oScroller = oScrollMid.parentElement.parentElement;
		//var oScroller = oScrollMid.parentElement;
		//alert(oScroller);
		this.SetOptions(options);
		this.Dircetion = this.options.Dircetion || ["left"]; //方向 
		this.scroller = oScroller; //对象 
		this.speed = this.options.Speed; //速度 
		this.timer = null; //时间 
		this.pauseHeight = 0; //定高 
		this.pauseWidth = 0; //定宽 
		this.pause = 0; //定高(宽) 
		this.side = 0; //参数 
		//用于上下滚动
		this.heightScroller = parseInt(oScroller.style.height) || oScroller.offsetHeight;

		this.heightList = oScrollMid.offsetHeight;
		//用于左右滚动 
		this.widthScroller = parseInt(oScroller.style.width) || oScroller.offsetWidth;
		this.widthList = oScrollMid.offsetWidth;
		//js取不到css设置的height和width 
		oScroller.style.overflow = "hidden";
		oScrollMid.parentElement.appendChild(oScrollMid.cloneNode(true));
		oScrollMid.parentElement.appendChild(oScrollMid.cloneNode(true));
		addEventHandler(oScroller, "mouseover", function () { oThis.Stop(); });
		addEventHandler(oScroller, "mouseout", function () { oThis.Start(); });
		this.Start();
	},
	//设置默认属性 
	SetOptions: function (options) {
		this.options = {//默认值 
			Step: 1, //每次变化的px量 
			Speed: 20, //速度(越大越慢) 
			Side: ["left"], //滚动方向:"up"是上，"down"是下，"left"是左，"right"是右 
			PauseHeight: 0, //隔多高停一次 
			PauseWidth: 0, //隔多宽停一次 
			//当上下和左右一起使用时必须设置PauseHeight和PauseWidth来设置转向位置 
			PauseStep: 3000//停顿时间(PauseHeight或PauseWidth大于0该参数才有效) 
		};
		Object.extend(this.options, options || {});
	},
	//转向 
	Turn: function () {
		//通过设置方向数组的排列来转向 
		this.Dircetion.push(this.Dircetion.shift().toLowerCase());
	},
	//上下滚动 
	ScrollUpDown: function () {
		this.pause = this.pauseHeight;
		this.scroller.scrollTop = this.GetScroll(this.scroller.scrollTop, this.heightScroller, this.heightList,
this.options.PauseHeight);
		this.pauseHeight = this.pause;
		var oThis = this;
		this.timer = window.setTimeout(function () { oThis.Start(); }, this.speed);
	},
	//左右滚动 
	ScrollLeftRight: function () {
		this.pause = this.pauseWidth;
		var ss;
		ss = this.GetScroll(this.scroller.scrollLeft, this.widthScroller, this.widthList, this.options.PauseWidth);
		//document.title = ss;
		this.scroller.scrollLeft = ss;
		this.pauseWidth = this.pause;
		var oThis = this;
		this.timer = window.setTimeout(function () { oThis.Start(); }, this.speed);
	},
	//获取设置滚动数据 
	GetScroll: function (iScroll, iScroller, iList, iPause) {
		var iStep = this.options.Step * this.side;

		if (this.side > 0) {
			if (iScroll >= (iList * 2 - iScroller)) { iScroll -= iList; }
		} else {
			if (iScroll <= 0) { iScroll += iList; }
		}

		this.speed = this.options.Speed;
		if (iPause > 0) {
			if (Math.abs(this.pause) >= iPause) {
				this.speed = this.options.PauseStep; this.pause = iStep = 0; this.Turn();
			} else {
				this.pause += iStep;
			}
		}
		//document.title = iScroll + iStep;

		if ((iScroll + iStep) > iList)
			return iStep;

		//		if ((iScroll + iStep) > il)
		//			return iScroller.offsetWidth - iScroll + iStep;


		return (iScroll + iStep);
	},
	//开始 
	Start: function () {
		//方向设置 
		switch (this.Dircetion[0].toLowerCase()) {
			case "right":
				if (this.widthList < this.widthScroller) return;
				this.side = -1;
				this.ScrollLeftRight();
				break;
			case "left":
				if (this.widthList < this.widthScroller) return;
				this.side = 1;
				this.ScrollLeftRight();
				break;
			case "down":
				if (this.heightList < this.heightScroller) return;
				this.side = -1;
				this.ScrollUpDown();
				break;
			case "up":
			default:
				if (this.heightList < this.heightScroller) return;
				this.side = 1;
				this.ScrollUpDown();
		}
	},
	//停止 
	Stop: function () {
		clearTimeout(this.timer);
	}
}; 


/*
* js 漂浮广告类
* 
*
* Copyright (c) 2009 柏富强（bofuqiang@gmail.com）
* 代码虽然简单，但在使用或修改时还请保留本人的版权信息.
* 
* 编写日期：2010-2-9
* 
* Revision: V1.0
* ----------------------------------------------------------------------
* 
* 参数：
*   id: 要漂浮的的容器的id，容器一般为div、ul；
*   step: 指定时间内移动的像素大小；单位像素
*   speed: 字幕滚动的时间间隔，单位毫秒
*
* ----------------------------------------------------------------------
*
* 使用实例 ： js：new Yibai.adShow('ad_obj_id',1,50);
*
*/

var adShow = YibaiClass.create();

adShow.prototype =
    {
        ready: function(id, step, speed) {
            this.element = document.getElementById(id);
            this.pageSize = getPageSize();
            this.defaultPos =
            {
                x: parseInt(this.element.style.left),
                y: parseInt(this.element.style.top)
            }
            this.stepX = step;
            this.stepY = step;
            this.speed = speed;

            var t = this.action();
            var _self = this;

            this.element.setAttribute('onmouseover', function() { obj.setAttribute('stop', '1'); });
            this.element.setAttribute('onmouseout', function() { obj.setAttribute('stop', '0'); });

        },
        action: function() {

            var i_stop = this.element.getAttribute('stop');
            if (i_stop != '1') {
                var _x = parseInt(this.element.style.left);
                var _y = parseInt(this.element.style.top);

                if (_x <= this.defaultPos.x) {
                    this.stepX = Math.abs(this.stepX)
                }

                if (_y <= this.defaultPos.y) {
                    this.stepY = Math.abs(this.stepY)
                }

                if (_x + this.element.offsetWidth >= this.pageSize.SW) {
                    this.stepX = -this.stepX;
                }

                if (_y + this.element.offsetHeight >= this.pageSize.SH) {
                    this.stepY = -this.stepY;
                }

                this.element.style.left = _x + this.stepX + 'px';
                this.element.style.top = _y + this.stepY + 'px';
            }

            var _self = this;

            setTimeout(function() { _self.action() }, this.speed);


        }
    }


//获取scrollTop
function getScrollTop() {
    var _scrollPos;
    _scrollPos = (typeof window.pageYOffset != 'undefined') ? window.pageYOffset :
        (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') ? document.documentElement.scrollTop :
        (typeof document.body != 'undefined') ? document.body.scrollTop : 0;
    return _scrollPos;
}

//获取页面及屏幕可见区域尺寸
function getPageSize() {
    var _pw = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth);
    var _ph = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    var _sw = (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
    var _sh = (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
    return { PW: _pw, PH: _ph, SW: _sw, SH: _sh }
}


// 设置刷新时间
function refreshTime (objid) {
    setInterval(objid+'.innerText="当前时间： "+new Date().toLocaleString()', 1000); 
}



