// cjs_lib_all.js ver0.02 2004-03-12
// -- description
// misc library
// -- note
// global value
//    CJS_userAgent (useragent detector object)
//    CJS_windowListener (window event manager object)
//    CJS_style (stylesheet manager object)
//    CJS_cookie (cookie manager object)
//    CJS_image (image manager object)
//    CJS_windowMaker (window maker object)
// -- todo
// auto resizer for nn4 (need?)

document.write ("");

// [prototype]
// [Array | 2004-02-27]
if (typeof (Array.prototype.pop) == "undefined") Array.prototype.pop = function () {
	return this.splice (-1)[0];
};
if (typeof (Array.prototype.push) == "undefined") Array.prototype.push = function () {
	for (var i = 0; i < arguments.length; i++) this [this.length] = arguments [i];
	return this.length;
};
if (typeof (Array.prototype.shift) == "undefined") Array.prototype.shift = function () {
	return this.splice (0, 1)[0];
};
Array.prototype.splice = function (start, deleteCount) {
	if (!arguments.length) return start;
	start = isNaN (start = parseInt (start)) ? 0 : (start < 0 ? Math.max (0, this.length + start) : Math.min (this.length, start));
	if (arguments.length < 2) {
		var removeItems = this.slice (start);
		this.length = start;
		return removeItems;
	}
	deleteCount = isNaN (deleteCount = parseInt (deleteCount)) ? 0 : Math.max (0, deleteCount);
	var removeItems = this.slice (start, start + deleteCount);
	var endItems = this.slice (start + deleteCount);
	this.length = start;
	for (var i = 2; i < arguments.length; i++) this [start - 2 + i] = arguments [i];
	for (var i = 0; i < endItems.length; i++) this [start + arguments.length - 2 + i] = endItems [i];
	return removeItems;
};
Array.prototype.unshift = function () {
	var i = arguments.length;
	while (i--) this.splice (0, 0, arguments [i]);
	return this.length;
};
Array.prototype.copy = function () {
	return this.slice ();
};
Array.prototype.indexOf = function (value, position) {
	for (var i = isNaN (position = parseInt (position)) ? 0 : Math.min (Math.max (position, 0), this.length - 1);i < this.length; i++) if (this [i] == value) return i;
	return -1;
};
Array.prototype.lastIndexOf = function (value, position) {
	var i = isNaN (position = parseInt (position)) ? this.length : Math.min (Math.max (position + 1, 0), this.length);
	while (i--) if (this [i] == value) return i;
	return -1;
};

// [RegExp | 2004-02-27]
if (typeof (RegExp.prototype.test) == "undefined") RegExp.prototype.test = function (string) {
	return string.match (this) != null;
};
// [/prototype]

// [ParamString | 2004-02-27]
// -- description
// paramater string object
// -- usage
// .rawString = raw string
// .separator = separator
// .getRawString() = get raw string
// .getSeparator() = get separator
// .get(n) = get value named n
// .set(n,v) = set v named n
// .remove(n) = remove paramater named n
function _CJS_paramaterString (rawString,separator) {
	this.rawString=rawString;
	this.separator=separator;
	this.getRawString = function (){
		return this.rawString;
	}
	this.getSeparator = function (){
		return this.separator;
	}
	this.get = function (n) {
		return new RegExp (this.separator + n + "=([^" + this.separator + "]*)").test (this.rawString.replace (/^\??/, this.separator)) ? unescape (RegExp.$1) : null;
	};
	this.set = function (n, v) {
		var oldValue = this.get (n, this.separator);
		this.rawString=this.rawString.replace (/^\??/, this.separator).replace (oldValue != null ? new RegExp (this.separator + n + "=" + oldValue) : /$/, this.separator + n + "=" + escape (v)).replace (new RegExp ("^" + this.separator + "+"), "");
		return this.rawString;
	};
	this.remove = function (n) {
		this.rawString=this.rawString.replace (/^\??/, this.separator).replace (new RegExp (this.separator + n + "=[^" + this.separator + "]*", "g"), "").slice (1);
		return this.rawString;
	};
	this.names = function () {
		return this.rawString.replace (/^\??/, this.separator).replace(new RegExp ("=[^" + this.separator + "]*","g"), "").match(new RegExp ("[^" + this.separator + "]+","g"));
	}
	return this;
}
// -- make paramater string object
function cjs_makeParamaterString(rawString,separator){
	return _CJS_paramaterString (rawString,separator);
}
// [/ParamString | 2004-02-27]

// [UserAgent | 2004-03-02]
// -- description
// useragent detector object
// -- usage
// .os.isWin = bool
// .os.isMac = bool
// .os.isUnknown = bool
// .browser.isIe = bool
// .browser.isGecko = bool
// .browser.isSafari = bool
// .browser.isOpera = bool
// .browser.isNav4 = bool
// .browser.isUnknown = bool
// .version = float
function _CJS_userAgent () {
	var o = navigator.appVersion;
	var p = navigator.userAgent;
	var q = /Opera/.test (p);
	this.os = { isWin : /Win/.test (o), isMac : /Mac/.test (o) };
	this.browser = { isIe : (/MSIE/.test (p) && !q), isGecko : /rv:/.test (p), isSafari : /Safari/.test (p), isOpera : q, isIcab : /iCab/.test (p), isNav4 : Boolean (document.layers) };
	this.version = parseFloat (/Opera[^\d]([\d\.]+)/.test (p) ? RegExp.$1 : (/(MSIE |rv:|Safari\/)([\d\.]+)/.test (p) ? RegExp.$2 : (/Mozilla\/([\d\.]+)/.test (p) ? RegExp.$1 : o)));
	var r = [this.os, this.browser];
	while (r.length) {
		var s = r.shift ();
		var t = true;
		for (i in s) if (s [i]) {
			t = false;
			break;
		}
		s.isUnknown = t;
	}
	return this;
};
// make global CJS_userAgent
function cjs_makeUserAgent () {
	return new _CJS_userAgent;
}
var CJS_userAgent = cjs_makeUserAgent();
// [/UserAgent | 2004-03-02]

// [WindowListener | 2004-03-02]
// -- description
// window event manager object
// -- usage
// .listeners = window listener object
// .add(event, func) = add func to event
// .remove(event, func) = remove func from event
// .clear(event) = clear all func in event
// event = [load|unload|resize|...]
// -- note
// <body onload="***"> can't remove...?
function _CJS_windowListener (){
	this.listeners={};
	this.o = (CJS_userAgent.browser.isOpera && CJS_userAgent.version>=7)?document.body:window;
	this.add = function (event, listener){
		if(!this.listeners[event]){
			if(this.o[event]){
				this.listeners[event]=[this.o[event]];
			}else{
				this.listeners[event]=[];
			}
		}
		this.listeners[event].push(listener);
		var func = "";
		func +="var event='" + event + "';\n";
		func +="for (var i=0;i<CJS_windowListener.listeners[event].length;i++) {\n";
		func +="(typeof(CJS_windowListener.listeners[event][i])=='string')?eval(CJS_windowListener.listeners[event][i]):CJS_windowListener.listeners[event][i]();\n";
		func +="}\n";
		this.o[event] = new Function(func);
		return;
	};
	this.remove = function (event, listener) {
		if(!this.listeners[event]){
			if(this.o[event]){
				this.listeners[event]=[this.o[event]];
			}else{
				this.listeners[event]=[];
			}
		}
		if (this.listeners[event]) {
			var pos = this.listeners[event].lastIndexOf (listener);
			if (pos != -1) this.listeners[event].splice (pos, 1)[0];
		}
		return;
	};
	this.clear = function (event) {
		var o=(CJS_userAgent.browser.isOpera && CJS_userAgent.version>=7)?document.body:window;
		this.listeners[event]=[];
		this.o[event]= new Function("");
		return;
	};
}
// make global CJS_windowListener
function cjs_makeWindowListener () {
	return new _CJS_windowListener;
}
var CJS_windowListener = cjs_makeWindowListener();
// [/WindowListener | 2004-03-02]

// [Style | 2004-03-12]
// -- description
// print path/***.css
// -- usage
// .set(param) = paramater(css file with os, browser) setting
// .print(path,pram) = print <link> html (param is optional)
// -- example
// CJS_style.set("win=win.css&***=***");
// CJS_style.print("./");
//  OR
// CJS_style.print("./","win=win.css&***=***");
// -- note
// param is [os].css, [os]_[browser].css
//    os: win,mac,unknown
//    browser: ie,gecko,safari,opera,icab,unkown
//    default: default(if not moatch)
// full param
//    win=win.css
//    mac=mac.css
//    unknown=unknown.css
//    win_ie=win_ie.css
//    win_gecko=win_gecko.css
//    win_opera=win_opera.css
//    win_nav4=win_nav4.css
//    win_unknown=win_ie.css
//    mac_ie=mac_ie.css
//    mac_safari=mac_safari.css
//    mac_gecko=mac_gecko.css
//    mac_opera=mac_opera.css
//    mac_nav4=mac_nav4.css
//    mac_unknown=unknown.css
//    unknown_unknown=unknown.css
//    default=default.css
// read all matched css order by param
//    win=win.css&win_ie=win_ie.css
//    read win.css, then win_ie.css
// if not match, then default
//    win=win.css&win_ie=win_ie.css&default=defautl.css
//    if mac, do nothing
// if not match, do nothing(not set default)
//    win=win.css&win_ie=win_ie.css
//    if mac, do nothing
function _CJS_style() {
	this.types;
	this.set = function (param) {
		this.types = new cjs_makeParamaterString(param, "&");
	}
	this.print = function (path, param) {
		var isNeverMatch = true;
		var css
		var types=(param)?new cjs_makeParamaterString(param, "&"):this.types
		if(!path){
			path = "./";
		}
		var names = types.names();
		for(var i=0;i<names.length;i++){
			var t = names[i].split("_");
			var isMatch = true;
			if(t[0]){
				isMatch &= eval("CJS_userAgent.os.is" + t[0].charAt(0).toUpperCase() + t[0].substring(1).toLowerCase());
			}
			if(t[1]){
				isMatch &= eval("CJS_userAgent.browser.is" + t[1].charAt(0).toUpperCase() + t[1].substring(1).toLowerCase());
			}
			if(isMatch && (css=types.get(names[i]))){
				isNeverMatch = false;
				document.open ();
				document.write ('<link rel="stylesheet" type="text/css" href="' + path + css + '">'+"\n");
				document.close ();
			}
		}
		if(isNeverMatch && (css=types.get("default"))){
			document.open ();
			document.write ('<link rel="stylesheet" type="text/css" href="' + path + css + '">'+"\n");
			document.close ();
		}
	};
}
// make global CJS_style
function cjs_makeStyle () {
	return new _CJS_style;
}
var CJS_style = new _CJS_style();
// [/Style | 2004-03-12]

// [Image | 2004-03-12]
// -- description
// image manager object
// add .preload() to windowListener[onload]
// -- usage
// .add(image_path,image_path) = add preload images(image_path)
// .preload() = load preloadimages(array)
// .change(name,path_to_image) = change image (onMouseOver)
// .restore() = restore changed image (onMouseOut)
// .getList() = return image object(or objects)
// -- example
// <a onMouseOver="CJS_image.change('img1','1.gif')" onMouseOut="CJS_image.restore()"><img name="img1"></a>
// <a onMouseOver="CJS_image.change('img1','1.gif','img2','2.gif')" onMouseOut="CJS_image.restore()"><img name="img1"></a>
// <input type="image" onMouseOver="CJS_image.change(this,'1.gif')" onMouseOut="CJS_image.restore()"><img name="img1"></a>
// -- note
// cannot change other <input type="image"> (can change self image only)
function _CJS_image() {
	this.preloadImages = [];
	this.restoreImages = [];
	this.add = function () {
		for (var i = 0; i < arguments.length; i++) this.preloadImages.push (arguments [i]);
		return this.preloadImages.length;
	};
	this.preload = function () {
		var images = arguments.length ? arguments : this.preloadImages;
		for (var i = 0; i < images.length; i++) (new Image).src = images [i];
		return true;
	};
	this.change = function () {
		var length = this.restoreImages.length;
		for (var i = 0; i < arguments.length; i += 2) {
			var images = this.getList (arguments [i]);
			while (images.length) {
				var image = images.shift ();
				this.restoreImages.unshift (image, image.src);
				image.src = arguments [i + 1];
			}
		}
		return (this.restoreImages.length > length);
	};
	this.restore = function () {
		if (arguments.length) {
			var length = this.restoreImages.length;
			for (var i = 0; i < arguments.length; i++) {
				var images = this.getList (arguments [i]);
				for (var j = 0; j < images.length; j++) {
					var index = this.restoreImages.indexOf (images [j]);
					if (index != -1) {
						var pair = this.restoreImages.splice (index, 2);
						pair [0].src = pair [1];
					}
				}
			}
			return (this.restoreImages.length < length);
		}
		while (this.restoreImages.length) this.restoreImages.shift ().src = this.restoreImages.shift ();
		return true;
	};
	this.getList = function (object) {
		var images = document.images;
		if (object.constructor == RegExp) {
			var list = [];
			for (var i = 0; i < images.length; i++) {
				var image = images [i];
				if (object.test (image.name)) list.push (image);
			}
			return list;
		}
		var image = typeof (object) == "string" ? images [object] : object;
		return image ? [image] : [];
	};
	return this;
}
// make global CJS_image
function cjs_makeImage () {
	return new _CJS_image;
}
var CJS_image = new _CJS_image();
CJS_windowListener.add("onload", new Function ("CJS_image.preload()"));
// [/Image | 2004-03-12]

// [Cookie | 2004-03-12]
// -- description
// get cookie value all(as string)
// -- usage
// .rawString = raw cookie string
// .getRawString() = get raw string
// .get(n) = get value named n
// .set(n, v, msecToExpiration, path, domain, secure) = set v named n with misc paramater
// .remove(n, path, domain) = remove paramater named n with misc paramater
function _CJS_cookie(){
	this.rawString = document.cookie;
	// -- get cookie value named n
	this.getRawString = function () {
		return this.rawString;
	}
	this.get = function (n) {
		return new cjs_makeParamaterString(this.rawString.replace (/\s/g, ""),";").get (n);
	}
	// -- set cookie value named n(with msecToExpiration, path, domain, secure)
	this.set = function (n, v, msecToExpiration, path, domain, secure) {
		if (msecToExpiration) {
			var date = new Date;
			date.setTime (date.getTime () + msecToExpiration);
		}
		document.cookie = n + "=" + escape (v) + (msecToExpiration ? ("; expires=" + date.toGMTString ()) : "") + (path ? ("; path=" + path) : "") + (domain ? ("; domain=" + domain) : "") + (secure ? "; secure" : "");
		this.rawString = document.cookie;
	}
	// -- remove cookie named n (with path, domain)
	this.remove = function (n, path, domain) {
		return this.set (n, "", -1, path, domain);
		this.rawString = document.cookie;
	}
}
// make global CJS_cookie
function cjs_makeCookie(){
	return new _CJS_cookie();
}
var CJS_cookie = cjs_makeCookie();
// [/Cookie | 2004-03-12]

// [Window | 2004-03-12]
// -- description
// window maker(open)
// -- usage
// .open(url,name,width,height,features,align,dx,dy) = open new window
//    url = url
//    name = window name
//    width = window width
//    height = window height
//    features = 7 digit(0|1) = menubar, toolbar, directories, location, status, scrollbars, resizable
//    align = window align = lt, t, rt, l, c, r, lb, b, rb
//    dx = move +x after align
//    dy = move +y after align
// .openFullScreen(url,name,features,ie) = open full window
//    url = url
//    name = window name
//    features = 7 digit(0|1) = menubar, toolbar, directories, location, status, scrollbars, resizable = use for not ie fullscreen
//    ie = 0|1 = use ie fullscreen
function _CJS_windowMaker(){
	this.open = function (url,name,width,height,features,align,dx,dy){
		if(!name) name="";
		var s = (width ? ("width=" + width) : "") + (height ? (",height=" + height) : "") + this._parseFeature(features);
		if(align){
			var sw=this._getScreenWidth();
			var sh=this._getScreenHeight();
			var x=0;
			var y=0;
			if(align=="t"||align=="c"||align=="b"){
				x = (sw)?(sw-width)/2:0;
			}else if(align=="rt"||align=="r"||align=="rb"){
				x = (sw)?sw-width:0;
			}
			if(align=="l"||align=="c"||align=="r"){
				y = (sh)?(sh-height)/2:0;
			}else if(align=="lb"||align=="b"||align=="rb"){
				y = (sh)?sh-height:0;
			}
			if(dx){
				x+=dx;
			}
			if(dy){
				y+=dy;
			}
			s += ",left=" + x + ",top=" + y + ",screenX=" + x + ",screenY=" + y;
		}
		return window.open(url,name,s);
	}
	this.openFullScreen = function (url,name,features,ie){
		if(!name) name="";
		if (CJS_userAgent.browser.isIe&&ie) {
			return window.open(url,name,"fullscreen=1" + this._parseFeature(features));
		}
		var s="top=0,left=0,screenX=0,screenY=0" + this._parseFeature(features);
		var sw=this._getScreenWidth();
		var sh=this._getScreenHeight();
		if(sw>0 && sh>0){
			s+=",outerWidth="+sw+",outerHeight="+sh;
		}
		var win = window.open(url,name,s);
		if(sw>0 && sh>0){
			win.resizeTo(sw,sh);
		}
		if(win.moveTo) win.moveTo(0,0);
		//if(CJS_userAgent.browser.isOpera&&CJS_userAgent.version>=7) alert("Press F11 for fullscreen.");
		return win;
	}
	// parse features digit to string
	this._parseFeature = function (features){
		var s="";
		var ss = ["menubar", "toolbar", "directories", "location", "status", "scrollbars", "resizable"];
		if (features) {
			for (var i = 0; i < ss.length; i++) s += "," + ss [i] + "=" + features.charAt (i);
		}
		return s;
	}
	// get screen width(if not avaliable return 0)
	this._getScreenWidth = function () {
		return (!self["screen"])?0:(screen.availWidth)?screen.availWidth:(screen.width)?screen.width:0;
	}
	// get screen height.open(if not avaliable return 0)
	this._getScreenHeight = function () {
		return (!self["screen"])?0:(screen.availHeight)?screen.availHeight:(screen.height)?screen.height:0;
	}
}
// make global CJS_windowMaker
function cjs_makeWindowMaker(){
	return new _CJS_windowMaker;
}
var CJS_windowMaker = cjs_makeWindowMaker();
// [/Window | 2004-03-12]

// [Redirect | 2004-03-02]
// -- description
// redirect object
// -- usage
// .url = redirect url
// .msec = redirect delay (msec)
// .start() = start redirect (with delay)
// -- note
// mac ie doesn't work setTimeout well, so use setInterval
function _CJS_redirect(url,msec){
	if(!msec){
		msec = 0;
	}
	this.url=url;
	this.msec=msec;
	if(CJS_userAgent.os.isMac && CJS_userAgent.browser.isIe){
		this.start = new Function('setInterval(\'location.href="'+url+'"\',' + this.msec + ')');
	}else{
		this.start = new Function('setTimeout(' + new Function('location.href="'+url+'"') + ',' + this.msec + ')');
	}
}
// -- make redirect object (url, msec delay)
function cjs_makeRedirect(url,msec){
	return new _CJS_redirect(url,msec);
}
// var CJS_redirect = cjs_makeRedirect("http://***",10*1000);
// [/Redirect | 2004-03-02]
