﻿/*-------------------------------------'
'                                      '
'   ================================   '
'   Coded By Terry @ Qinxin Studio     '
'   http://wjhen.3322.org/             '
'--------------------------------------'
'   > File: default.js                 '
'   > Module Version Number: 1.0.0     '
'   > Released: Aug. 11, 2007          '
'   > Modified:                        '
'-------------------------------------*/

// 取标签
function getElement(name) {
	if (document.all) {
		return document.all(name);
	}
	else {
		if (document.getElementById(name) == null) {
			return document.getElementsByName(name)[0];
		}
		else {
			return document.getElementById(name);
		}
	}
}


var title = document.title;
var url = document.location.href;
// 收藏夹
function bookmark() {
	if (document.all) {
		window.external.AddFavorite(url, document.title);
	}
	else if (window.sidebar) {
		window.sidebar.addPanel(title, url, "")
	}
	else if (window.opera && window.print ) {
		var mbm = document.createElement('a');
		mbm.setAttribute('rel', 'sidebar');
		mbm.setAttribute('href', url);
		mbm.setAttribute('title', title);
		mbm.click();
	}
}
// 首页
function setHomePage() {
	if (document.all) {
		this.homepage.style.behavior = "url(#default#homepage)";
		this.homepage.sethomepage(url);
	}
	else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
		} 
		catch (e) { 
			alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'"); 
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage',url);
	}
}

// 选项卡
function tab(tid,hx,box,s,pr){
	var hxs = getElement(tid).getElementsByTagName(hx);
	var boxs = getElement(tid).getElementsByTagName(box);
	if(!pr){
		go_to(0);
		yy();
	}
	else {
		go_to(pr);
		yy();
	}
	function yy(){
		for(var i=0;i<hxs.length;i++){
			hxs[i].temp=i;
			hxs[i][s]=function(){
				go_to(this.temp);
			}
		}
	}
	function go_to(pr){
		for(var i=0;i<hxs.length;i++){
			if(!hxs[i].tmpClass){
				hxs[i].tmpClass=hxs[i].className+" pr1984_com";
				boxs[i].tmpClass=boxs[i].className+" pr1984_com";
			}
			if(pr==i){
				hxs[i].className=hxs[i].tmpClass+" up";
				boxs[i].className=boxs[i].tmpClass+" up";
			}
			else {
				hxs[i].className=hxs[i].tmpClass;
				boxs[i].className=boxs[i].tmpClass;
			}
		}
	}
}

// 模拟marquee
function scrollingAD(_id, _width, _height, _bgColor, _direction, _delay, _pauseTime, _size, _isHover) {
	this.id              = _id;
	this.width           = _width;
	this.height          = _height;
	this.bgColor         = _bgColor;
	this.direction       = _direction;
	this.delay           = _delay;
	this.pauseTime       = _pauseTime;
	this.size            = _size;
	this.object          = null;
	this.isMove          = true;
	
	if((this.id == "") || (this.id == null)) {
		alert("必须输入对象的 ID 才能创建滚动区域！");
		this.isMove = false;
		return false;
	}
	
	if(document.getElementById(this.id)) {
		this.object = document.getElementById(this.id);
	} else {
		window.alert("滚动区域创建失败！\n请确认 " + this.id + " 是否拼写错误。");
		this.isMove = false;
		return false;
	}
}

scrollingAD.prototype.checkNumber = function(_attribute, defaultValue) {
	if(isNaN(_attribute)) {
		return defaultValue;
	} else {
		return ((typeof(parseInt(_attribute)) == "number") ? parseInt(_attribute) : defaultValue);
	}
}

scrollingAD.prototype.move = function() {
	if(this.isMove == false) return false;
	
	var defaultWidth           = 200;
	var defaultHeight          = 50;
	var defaultDelay           = 20;
	var defaultPauseTime       = 2000;
	var defaultIsHover         = true;
	var defaultBgColor         = "transparent";
	var defaultDirection       = "up";
	
	this.width          = this.checkNumber(this.width, defaultWidth);
	this.height         = this.checkNumber(this.height, defaultHeight);
	this.delay          = this.checkNumber(this.delay, defaultDelay);
	this.pauseTime      = this.checkNumber(this.pauseTime, defaultPauseTime);
	this.isHover        = (typeof(this.isHover) == "boolean") ? this.isHover : defaultIsHover;
	
	if(this.direction == "left") {
		this.size       = this.checkNumber(this.size, this.width);
	} else {
		this.size       = this.checkNumber(this.size, this.height);
	}

	if((this.bgColor == null) || (typeof(this.bgColor) == undefined)) {
		this.bgColor = defaultBgColor;
	} else {
		this.bgColor = this.bgColor;
	}

	if((this.direction == null) || (typeof(this.direction) == undefined)) {
		this.direction = defaultDirection;	
	} else {
		this.direction = (this.direction.search(/(^up$)|(^left$)/gi) != -1) ? this.direction.toLowerCase() : defaultDirection;
	}

	// 创建滚动区域；
	with(this.object) {
		style.display         = "block";
		style.width           = this.width + "px";
		style.height          = this.height + "px";
		style.overflow        = "hidden";
		style.backgroundColor = this.bgColor;
	}

	if(this.direction == "up") {
		this.object.innerHTML = "<div id=\"" + this.id + "_mirror\">" + this.object.innerHTML + "</div>" + "<div id=\"" + this.id + "_mirror_2\">" + this.object.innerHTML + "</div>";
	} else {
		this.object.innerHTML = "<span id=\"" + this.id + "_mirror\">" + this.object.innerHTML + "</span>" + "<span id=\"" + this.id + "_mirror_2\">" + this.object.innerHTML + this.object.innerHTML + "</span>";
	}

	if(getElement(this.id)) {
		var evalString;
		var _id = this.id;
		var _delay = this.delay;
		var _size = this.size;
		var _pauseTime = this.pauseTime;

		if(this.direction == "up") {
			evalString = _id + "_timer = window.setInterval(function() {scrollToUp(\"" + _id + "\", " + _delay + ", " + _size + ", " + _pauseTime + ", 0) }, " + _delay + ")";
			eval(evalString);

			this.object.onmouseover = function() {
				evalString = "clearInterval(" + _id + "_timer);";
				eval(evalString);
			};

			this.object.onmouseout = function() {
				evalString = _id + "_timer = window.setInterval(function() {scrollToUp(\"" + _id + "\", " + _delay + ", " + _size + ", " + _pauseTime + ", 0) }, " + _delay + ")";
				eval(evalString);
			};
		} else {
			evalString = _id + "_timer = window.setInterval(function() {scrollToLeft(\"" + _id + "\", " + _delay + ", " + _size + ", " + _pauseTime + ", 0) }, " + _delay + ")";
			eval(evalString);

			this.object.onmouseover = function() {
				evalString = "clearInterval(" + _id + "_timer);";
				eval(evalString);
			};

			this.object.onmouseout = function() {
				evalString = _id + "_timer = window.setInterval(function() {scrollToLeft(\"" + _id + "\", " + _delay + ", " + _size + ", " + _pauseTime + ", 0) }, " + _delay + ")";
				eval(evalString);
			};
		}
	} else {
		return false;
	}

	function pixelToNum(_string)	{
	//该函数用于去掉数值后面的px，并将之转化为数字。
		if(_string.slice(_string.length - 2) == "px") {
			return parseInt(_string.slice(0, (_string.length - 2)));
		} else  {
			return _string;
		}
	}

	function scrollToLeft(_id, _delay, _size, _pauseTime, _s) {
		var obj = getElement(_id);
		var mirror = getElement(_id + "_mirror");
		var mirror2 = getElement(_id + "_mirror_2");

		/*if (mirror2.offsetLeft - obj.scrollLeft <= 0) {
			obj.scrollLeft -= mirror.offsetWidth;
		} else {
			obj.scrollLeft++;
		}*/
		if (obj.scrollWidth / 2 - obj.scrollLeft <= 0) {
			obj.scrollLeft = 0;
		} else {
			obj.scrollLeft++;
		}
	}

	function scrollToUp(_id, _delay, _size, _pauseTime, _s) {
		var obj = getElement(_id);
		var mirror = getElement(_id + "_mirror");
		var mirror2 = getElement(_id + "_mirror_2");

		/*if (mirror2.offsetTop - obj.scrollTop <= 0) {
			obj.scrollTop -= mirror.offsetHeight;
		} else {
			obj.scrollTop++;
		}*/
		if (obj.scrollHeight / 2 - obj.scrollTop <= 0) {
			obj.scrollTop = 0;
		} else {
			obj.scrollTop++;
		}
	}
}
