if(!com) var com = new Object();
if(!com.jcity) com.jcity = new Object();

/* com.jcity.element
*******************************************************************************/
com.jcity.element = {
	anchor : {
		onLoad : function(){
			var anchors = document.getElementsByTagName("a");
			var anchor;
			var targets = Array();
			var target;
			var module = this;

			targets = getProperties("popup"); /*** refer to "objects.prop" ***/

			for(var i=0; anchor=anchors[i]; i++){
				if(anchor.className.match(/external/i)){
					anchor.onclick = function(){
						return module.openWindowExternal(this);
					}
					anchor.onkeypress = function(event){
						if(com.jcity.event.key.isKeyPressEnter(event)){
							return module.openWindowExternal(this);
						}
						return true;
					}
				}else if(anchor.className.match(/track\-back/i)){
					anchor.onclick = function(){
						if(window.opener && !window.opener.closed){
							window.opener.location.href = this.href;
							return false;
						}
						return true;
					}
					anchor.onkeypress = function(event){
						if(com.jcity.event.key.isKeyPressEnter(event)){
							if(window.opener && !window.opener.closed){
								window.opener.location.href = this.href;
								return false;
							}
						}
						return true;
					}
				}else if(anchor.className.match(/close/i)){
					anchor.href = "javascript: window.close();";
				}else if(anchor.className.match(/track\-back\-close/i)){
					if(window.opener && !window.opener.closed){
						anchor.href = "javascript: window.opener.location.href='" + anchor.href + "'; window.close();";
					}
				}else if(anchor.className.match(/topagetop/i)){
					anchor.onclick = function(){
						return com.jcity.util.scroll.toPageTop();
					}
					anchor.onkeypress = function(event){
						if(com.jcity.event.key.onKeyPress(event)){
							return com.jcity.util.scroll.toPageTop();
						}
						return true;
					}
				}else if(anchor.className.match(/toAnchor-/i)){
					anchor.onclick = function(){
						return com.jcity.util.scroll.toAnchor(module.getAnchorName(this));
					}
					anchor.onkeypress = function(event){
						if(com.jcity.event.key.onKeyPress(event)){
							return com.jcity.util.scroll.toAnchor(module.getAnchorName(this));
						}
						return true;
					}
				}else{
					for(var j=0; target=targets[j]; j++){
						if(anchor.href.match(target.pattern)){
							anchor.window_prop = target;

							anchor.onclick = function(){
								return module.openWindow(this);
							}
							anchor.onkeypress = function(event){
								if(com.jcity.event.key.isKeyPressEnter(event)){
									return module.openWindow(this);
								}
								return true;
							}
						}
					}
				}
			}
		},
		openWindow : function(anchor){
			return com.jcity.util.openWindow(anchor, anchor.window_prop);
		},
		openWindowExternal : function(anchor){
			return com.jcity.util.openWindowExternal(anchor);
		},
		getAnchorName : function(anchor){
			return anchor.className.replace(/toAnchor-/i, "");
		}
	},
	image : {
		preLoad	: new Array(),
		onLoad : function(){
			var images = document.getElementsByTagName("img");
			var inputs = document.getElementsByTagName("input");
			var over = new Array();
			var pattern = new Array();
			var img;

			pattern[0] = /rollover/i;
			pattern[1] = /\.(jpe?g|gif|png)$/i;
			pattern[2] = /\-over\.(jpe?g|gif|png)$/i;

			for(var i=0; i<images.length; i++){
				if(images[i].className.match(pattern[0])){
					over[over.length] = images[i]; 
				}
			}

			for(var i=0; i<inputs.length; i++){
				if(inputs[i].type == "image" && inputs[i].className.match(pattern[0])){
					over[over.length] = inputs[i];
				}
			}

			for(var i=0; img=over[i]; i++){
				img.defaultsrc = img.src;
				if(!img.src.match(pattern[2])){
					var prefix = img.defaultsrc.replace(pattern[1], "");
					var suffix = img.defaultsrc.match(pattern[1])[0];
					img.oversrc = prefix + "-over" + suffix;

					var j = this.preLoad.length;
					this.preLoad[j] = com.jcity.util.newImage(img.oversrc);

					img.onmouseover = function(){ this.src = this.oversrc; }
					img.onmouseout = function(){ this.src = this.defaultsrc; }
				}
			}
		}
	},
	form : {
		onLoad : function(){
			var forms = document.getElementsByTagName("form");
			var form;

			for(var i=0; form=forms[i]; i++){
				var buttonStatus = true;
				if(!form.className.match(/external/i)){
					form.onsubmit = function(){
						var inputs = this.getElementsByTagName("input");
						var input;
						for(var j=0; input=inputs[j]; j++){
							if(input.type == "submit" || input.type == "button"){
								input.style.color = "#999";
								input.disabled = true;
							}
						}
						if(buttonStatus){
							buttonStatus = false;
							return true;
						}else{
							return false;
						}
					}
				}
			}
		}
	},
	input : {
		onLoad : function(){
			var inputs = document.getElementsByTagName("input");
			var textareas = document.getElementsByTagName("textarea");
			var target = new Array();
			var input;

			for(var i=0; i<inputs.length; i++){
				if(inputs[i].type == "text" || inputs[i].type == "password" || inputs[i].type == "button"){
					target[target.length] = inputs[i];
				}
				if(inputs[i].type == "button" && inputs[i].className.match(/close/i)){
					var button = inputs[i];
					button.onclick = function(){
						window.close();
					}
					button.onkeypress = function(event){
						if(com.jcity.event.key.isKeyPressEnter(event)){
							window.close();
						}
					}
				}
			}
			for(var i=0; i<textareas.length; i++){
				target[target.length] = textareas[i];
			}

			for(var i=0; input=target[i]; i++){
				if(com.jcity.sys.conf.column.swapBgColor){
					input.onfocus = function(){
						this.defaultbgcolor = this.style.backgroundColor;
						this.defaultcolor = this.style.color;
						this.style.backgroundColor = com.jcity.sys.conf.column.backgroundColor;
						this.style.color = com.jcity.sys.conf.column.color;
						com.jcity.util.selectText(this);
					}
					input.onblur = function(){
						this.style.backgroundColor = this.defaultbgcolor;
						this.style.color = this.defaultcolor;
					}
				}else{
					input.onfocus = function(){
						com.jcity.util.selectText(this);
					}
				}
				if(input.className.match(/select/i)){
					com.jcity.util.focusObject(input);
				}
				if(input.className.match(/disabled/i)){
					input.style.color = "#999";
					if(input.type == "text"){
						input.style.backgroundColor = "#ddd";
					}
					input.disabled = true;
				}
			}
		}
	},
	label : {
		onLoad : function(){
			var labels = document.getElementsByTagName("label");
			var label;
			var userAgent;
			var safari = ((userAgent = navigator.userAgent.split("AppleWebKit/")[1]) ? userAgent.split("(")[0] : 0) >= 124;
			var module = this;

			for(var i=0; label=labels[i]; i++){
				label.onclick = function(){
					var target = module.getTarget(this);
					if((safari && target.type == "checkbox") || target.type == "radio"){
						module.ticked(target);
					}else if(target.type == "text" || target.type == "password" || target.type == "textarea"){
						com.jcity.util.focusObject(target);
					}
				}
				label.onkeypress = function(event){
					if(com.jcity.event.key.isKeyPressEnter(event)){
						var target = module.getTarget(this);
						if(safari && target.type == "checkbox"){
							module.ticked(target);
						}
					}
				}
			}
		},
		getTarget : function(label){
			return com.jcity.util.getObject(label.htmlFor);
		},
		ticked : function(target){
			target.checked = !(target.checked);
		}
	}
}

/* com.jcity.util
*******************************************************************************/
com.jcity.util = {
	openWindow : function(anchor, prop){
		var win;
		var position;

		document.body.style.cursor = "wait";
		position = (prop.top >= 0) ? ", top = " + prop.top : "";
		position += (prop.left >= 0) ? ", left = " + prop.left : "";

		win = window.open(anchor, prop.window_name, "width = " + prop.width + ", height = " + prop.height + ", directories = " + prop.directories + ", channelmode = " + prop.channelmode + ", fullscreen = " + prop.fullscreen + ", location = " + prop.location + ", menubar = " + prop.menubar + ", scrollbars = " + prop.scrollbars + ", status = " + prop.status + ", toolbar = " + prop.toolbar + ", resizable = " + prop.resizable + position);

		document.body.style.cursor = "auto";
		return this.focusWindow(win);
	},
	openWindowExternal : function(anchor){
		var win = window.open(anchor, "external");
		return this.focusWindow(win);
	},
	openPopup : function(page_name){
		var target;
		var targets = getProperties("popup");
		for(var i=0; target=targets[i]; i++){
			if(page_name.match(target.pattern)){
				return this.openWindow(page_name, target);
			}
		}
		return true;
	},
	focusWindow : function(win){
		if(win){
			win.focus();
			return false;
		}
		return true;
	},
	getObject : function(objectId){
		return document.all ? document.all(objectId) : document.getElementById ? document.getElementById(objectId) : document.layers[objectId];
	},
	newImage : function(src){
		if(document.images){
			var result = new Image();
			result.src = src;
			return result;
		}
		return "";
	},
	focusObject : function(target){
		target.focus();
		this.selectText(target);
	},
	selectText : function(target){
		if(com.jcity.sys.conf.column.highlight || target.className.match(/highlight/i)) target.select();
	},
	transferTo : function(href){
		location.href = href;
	},
	scroll : {
		getScrollLeft : function(){
			return document.body.parentNode.scrollLeft ? document.body.parentNode.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
		},
		getScrollTop : function(){
			return document.body.parentNode.scrollTop ? document.body.parentNode.scrollTop : document.body.scrollTop ? document.body.scrollTop : window.pageYOffset ? window.pageYOffset : 0;
		},
		getClientHeight : function(){
			return window.innerHeight;	//StandardMode
		},
		getAnchorPosition : function(anchorId){
			var anchor = com.jcity.util.getObject(anchorId);
			var position = new Object();

			if(!anchor) return 0;

			if(document.all){
				position.y = anchor.offsetTop;
				for(var i=0; anchor = anchor.offsetParent; i++){
					position.y += anchor.offsetTop;
				}
			}else if(document.getElementById){
				position.y = anchor.offsetTop;
			}else if(document.layers){
				position.y = document.anchors[anchorId].y;
			}else{
				position.y = 0;
			}
			return position.y;
		},
		scrollToDestination : function(moveDown, toX, toY, currentX, currentY){
			var height = this.getClientHeight();
			var scrollTimer;

			if(!toY) toY = 0;
			if(!toX) toX = 0;
			if(!currentX) currentX = this.getScrollLeft();
			if(!currentY) currentY = this.getScrollTop();

			currentX = toX;
			currentY += Math.floor((toY - currentY) * 0.2);
			if(currentY < 0) currentY = 0;
			scrollTo(currentX, currentY);
			scrollTimer = setTimeout("com.jcity.util.scroll.scrollToDestination(" + moveDown + "," + toX + "," + toY + "," + currentX + "," + currentY + ")", 16);
			var modulus = moveDown ? 5 : 0;
			if(Math.floor(Math.abs(currentY - toY) < modulus) || toY == currentY) clearTimeout(scrollTimer);
		},
		toPageTop : function(){
			this.scrollToDestination(0, 0, 0);
			return false;
		},
		toAnchor : function(toAnchor){
			var toY = this.getAnchorPosition(toAnchor);
			var pageEnd = this.getAnchorPosition("footer");

			if(pageEnd > 0){
				var range = pageEnd - this.getClientHeight();
				if(range < toY) toY = range;
			}

			this.scrollToDestination((toY > this.getScrollTop()) ? 1 : 0, 0, toY);
			return false;
		}
	},
	drag : {
		dragObject : null,
		offsetX : 0,
		offsetY : 0,
		mouseX : 0,
		mouseY : 0,
		zindex : 1,
		alpha : function(dragObject){
			dragObject.style.filter = "alpha(opacity=80)";
			dragObject.style.opacity = 0.8;
		},
		onLoad : function(){
			var elements = document.getElementsByTagName("div");
			var element;
			var module = this;

			for(var i=0; element=elements[i]; i++){
				if(element.className.match(/dragobject/i)){
					this.init(element);
					element.onmousedown = this.start;
					element.onmousemove = this.during;
					element.onmouseup = this.end;
				}
			}
		},
		init : function(element){
			element.style.position = "absolute";
			element.style.left = (element.x || element.offsetLeft || element.style.left) + "px";
			element.style.top = (element.y || element.offsetTop || element.style.top) +  "px";
			element.style.cursor = "move";
		},
		start : function(){
			var module = com.jcity.util.drag;
			module.offsetX = parseInt(this.style.left, 10) - module.mouseX;
			module.offsetY = parseInt(this.style.top, 10) - module.mouseY;
			this.style.zIndex = module.zindex += 1;
			module.alpha(this);
			module.dragObject = this;
			return false;
		},
		during : function(e){
			var module = com.jcity.util.drag;
			if(e){
				module.mouseX = e.pageX;
				module.mouseY = e.pageY;
			}else{
				module.mouseX = event.clientX;
				module.mouseY = event.clientY;
			}
			if(module.dragObject){
				module.dragObject.style.left = (module.mouseX + module.offsetX) + "px";
				module.dragObject.style.top = (module.mouseY + module.offsetY) + "px";
				module.alpha(module.dragObject);
				return false;
			}
			return true;
		},
		end : function(){
			var module = com.jcity.util.drag;
			module.dragObject.style.filter = "alpha(opacity=100)";
			module.dragObject.style.opacity = 1.0;
			module.dragObject = null;
		}
	}
}

/* com.jcity.sys
*******************************************************************************/
com.jcity.sys = {
	conf : "",
	onLoad : function(){
		this.conf = getConf(); /*** refer to "sys.conf" ***/
	}
}

/* com.jcity.event
*******************************************************************************/
com.jcity.event = {
	key : {
		isKeyPressEnter : function(event){
			if(event && event.type == "keypress" && (event.which == 13 || event.which == 32)){
				return true;
			}else{
				return false;
			}
		}
	},
	preLoad : function(){
		document.write('<script type="text/javascript" src="/shared/js/js.conf"><\/script>');
		document.write('<script type="text/javascript" src="/shared/js/objects.prop"><\/script>');
		document.write('<script type="text/javascript" src="/shared/js/bt.js"><\/script>');
		// document.write('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"><\/script>');
	},
	onLoad : function(){
		for(var root in com){
			for(var app in com[root]){
				for(var module in com[root][app]){
					if(com[root][app][module].onLoad){
						com[root][app][module].onLoad();
					}
				}
			}
		}
		/* Google Analytics ÍÑ¤ÎÀßÄê
		==============================================*/
		if(com.jcity.sys.conf.analysis.google){
			_uacct = com.jcity.sys.conf.analysis.google_uacct;
			urchinTracker();
		}
	}
}

/* Add Function
*******************************************************************************/
String.prototype.ltrim = function(){
	return this.replace(/^\n|^\s*/, "");
}
String.prototype.rtrim = function(){
	return this.replace(/\s*$|\n$/, "");
}
String.prototype.trim = function(){
	return this.ltrim().rtrim();
}

/*=============================================================================
   Load Module
 =============================================================================*/
com.jcity.event.preLoad();
window.onload = function(){
	com.jcity.sys.onLoad();
	com.jcity.event.onLoad();
}
