var SystemY = {
	conf: new Array(),//[["imgSwapper"]],//
	
	addInitApps: function(){
		this.conf.push(arguments);
	},
	
	start: function(){
		SystemY.initialize();
	},
	
	initialize: function(){
		var apps = this.conf;
		for (var i = 0; i < apps.length; i++){
			var app = apps[i];
			var args = new Array();
			for(var j = 1; j< app.length; j++){
				args.push(app[j]);
			}
			this[app[0]].initialize(args);
			
		}
	},
	
	getElementsByClassName: function(className, tagName, parentElement){
		if (Prototype.BrowserFeatures.XPath) {
			var q = ".//" + (tagName || "*") + "[contains(concat(' ', @class, ' '), ' " + className + " ')]";
			return document._getElementsByXPath(q, parentElement);
		}else{
			var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
			var elements = [], child;
			for (var i = 0, length = children.length; i < length; i++){
				child = children[i];
				if (Element.hasClassName(child, className)){
					elements.push(Element.extend(child));
				}
			}
			return elements;
		}
	},
	
	getPageScroll: function(){
		var xScroll, yScroll;
		if (self.pageYOffset) {
			xScroll = self.pageXOffset;
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			xScroll = document.documentElement.scrollLeft;
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollLeft;
			yScroll = document.body.scrollTop;
		}

		var PageScroll = {x:xScroll, y:yScroll}; 
		return PageScroll;
	},

	getPageSize: function(){
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}

		var PageSize = {pw:pageWidth, ph:pageHeight, ww:windowWidth, wh:windowHeight}; 
		return PageSize;
	},

	getElementPosition:function(element) {
		var offsetTrail = (typeof element == 'string') ? $(element) : element;
		var offsetLeft = 0;
		var offsetTop = 0;

		while (offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}

		if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != "undefined") {
			offsetLeft += document.body.leftMargin;
			offsetTop += document.body.topMargin;
		}

		return ({x: offsetLeft, y: offsetTop});
	},

	// Methods
	dirToMenu: new Object(),
	imgSwapper: new Object(),
	scroller: new Object()
	
};

Object.extend(SystemY.dirToMenu, {
	type: false,
	rootID: "",
	className: "dirToMenu",
	suffix: "_on",
	extension: ["html","php"],
	
	initialize: function(args){//type, rootID, className, suffix, extension[]
		if(typeof(args[0]) == "boolean"){
			this.type = args[0];
		}
		if(typeof(args[1]) == "string" && args[1] != ""){
			this.rootID = args[1];
		}
		if(typeof(args[2]) == "string" && args[2] != ""){
			this.className = args[2];
		}
		if(typeof(args[3]) == "string" && args[3] != ""){
			this.suffix = args[3];
		}
		if(typeof(args[4]) == "object" && 0 in args[4]){
			this.extension = args[4];
		}
		
		this.start();
	},

	start: function(){
		var path = document.location.pathname;
		for(var i = 0; i < this.extension.length; i++){
			path = path.replace("." + this.extension[i], "");
		}
		path = path.replace(/index$/, "");
		if(path.substr(path.length-1) == "/"){
			var key = path + "index";
		}else{
			var key = path;
		}
		var hostname = document.location.hostname;
		var port = document.location.port;
		if(navigator.appVersion.indexOf("MSIE 5") == -1){
			var elements = SystemY.getElementsByClassName(this.className, "", (this.rootID || ""));
			//var elements = this.getElementsByClassName(this.className, (this.rootID || ""));
		}else{
			var elements = SystemY.getElementsByClassName(this.className, "ul", (this.rootID || ""));
			elements.concat(SystemY.getElementsByClassName(this.className, "li", (this.rootID || "")));
			//var elements = this.getElementsByClassName(this.className, (this.rootID || ""));
		}
		var typeFlag = "list";
		if(this.type){
			typeFlag = "topCategory";
		}
		for (var i = 0; i < elements.length; i++){
			var element = elements[i];
			var child = element.firstChild;
			if(typeFlag == "topCategory" && child.nodeName.toLowerCase() == "a"){
				var target = child.href.replace(/\?.*/, "");
				target = target.replace(/#.*/, "");
				for(var j = 0; j < this.extension.length; j++){
					target = target.replace("." + this.extension[j], "");
				}
				target = target.replace(/index$/, "");
				if(child.nodeName.toLowerCase() == "a" && location.href.indexOf(target) != -1){
					var gChild = child.firstChild;
					if(gChild.nodeName.toLowerCase() == "img"){
						gChild.src = gChild.src.replace(/(\.gif|\.jpg|\.png)/, this.suffix + "$1");
						child.className = child.className.replace(/[\b]*imgSwapper/, "");
					}		
				}
				continue;
			}
			
			if(child.nodeName.toLowerCase() == "a" && child.href.indexOf(path) != -1){
				var target = child.href.replace(/https?\:\/\//, "");
				target = target.replace(hostname, "");
				target = target.replace(port, "");
				target = target.replace(/\?.*/, "");
				target = target.replace(/#.*/, "");
				for(var j = 0; j < this.extension.length; j++){
					target = target.replace("." + this.extension[j], "");
				}
				if(target.substr(target.length-1) == "/"){
					target += "index";
				}
				
				if(target == key){
					var gChild = child.firstChild;
					if(gChild.nodeName.toLowerCase() == "img"){
						gChild.src = gChild.src.replace(/(\.gif|\.jpg|\.png)/, this.suffix + "$1");
					}
					
					element.replaceChild(child.firstChild.cloneNode(true),child);
					
					var sChild = elements[i + 1];
					if(sChild && sChild.firstChild.nodeName.toLowerCase() != "a"){
						sChild.style.display = "block";
						sChild.className += " " + this.className + "Open";
					}


					if(element.parentNode.className.indexOf(this.className) != -1){
						element.parentNode.style.display = "block";
						
						var uncle = this.previous(element.parentNode);
						if(uncle.firstChild && uncle.firstChild.nodeName.toLowerCase() == "img"){
							uncle.firstChild.src = uncle.firstChild.src.replace(/(\.gif|\.jpg|\.png)/, this.suffix + "$1");
							uncle.className = uncle.className.replace(/[\b]*imgSwapper/, "");
						}
						this.ancestorReturn(element.parentNode);
					}
				}

			}else if(child.nodeName.toLowerCase() != "a" && element.className.indexOf(this.className + "Open") == -1){
				element.style.display = "none";
			}
		}
	},
	
	getElementsByClassName: function(className, target){
		var element=($(target) || document);
		var elements = new Array();
		return this.getNextChild(className, element, elements);
	},
	
	getNextChild: function(className, target, elements){
		me = target.firstChild;
		if(me){
			if(me.className && me.className.indexOf(className) != -1){
				elements.push(me);
				elements.concat(this.getNextSibling(className, target, elements));
			}else{
				elements.concat(this.getNextSibling(className, target, elements));
			}
		}else{
			return elements;
		}
	},
	
	getNextSibling: function(className, target, elements){
		me = target.nextSibling;
		if(me){
			if(me.nodeType == 3){
				if(me.className && me.className.indexOf(className) != -1){
					elements.push(me);
					elements.concat(this.getNextChild(className, target, elements));
				}else{
					elements.concat(this.getNextChild(className, target, elements));
				}
			}else{
				elements.concat(this.getNextSibling(className, target, elements));
			}
		}else{
			return elements;
		}
	},
	
	ancestorReturn: function(gChild){
		var me = gChild.parentNode.parentNode;
		if(me && me.className.indexOf(this.className) != -1){
			me.style.display = "block";
			var uncle = this.previous(me);
			if(uncle.firstChild && uncle.firstChild.nodeName.toLowerCase() == "img"){
				uncle.firstChild.src = uncle.firstChild.src.replace(/(\.gif|\.jpg|\.png)/, this.suffix + "$1");
				uncle.className = uncle.className.replace(/[\b]*imgSwapper/, "");
			}
			this.ancestorReturn(me);
		}
	},
	
	previous: function(target){
		if(target.previousSibling && target.previousSibling.nodeType == 1){
			return target.previousSibling;
		}else if(target.previousSibling && target.previousSibling.nodeType == 3){
			return this.previous(target.previousSibling);
		}
	}
	
});

Object.extend(SystemY.imgSwapper, {
	className: "imgSwapper",
	suffix: "_on",
	preload: new Array(),
	
	initialize: function(args){//className, suffix
		if(typeof(args[0]) == "string" && args[0] != ""){
			this.className = args[0];
		}
		if(typeof(args[1]) == "string" && args[1] != ""){
			this.extension = args[1];
		}
		this.start();
	},
	
	start: function(){
		var elements = SystemY.getElementsByClassName(this.className, "a", "");
		for (var i = 0; i < elements.length; i++){
			var element = elements[i];
			var child = element.firstChild;
			if(child.nodeName.toLowerCase() == "img"){
				var outImg = child.src;
				var overImg = outImg.replace(/(\.gif|\.jpg|\.png)/, this.suffix + "$1");
				this.preload["imgSwapper"+i] = { over: new Image(), out: new Image() };
				this.preload["imgSwapper"+i].over.src = overImg;	
				this.preload["imgSwapper"+i].out.src = outImg;
				element.id = "imgSwapper"+i;
				element.onmouseover = function(){ SystemY.imgSwapper.over(this); return false; }
				element.onmouseout = function(){ SystemY.imgSwapper.out(this); return false; }
			}
		}
	},
	
	over: function(target){target.firstChild.src=this.preload[target.id].over.src},
	out: function(target){target.firstChild.src=this.preload[target.id].out.src}
	
});

Object.extend(SystemY.scroller, {
	className: "scroller",
	sp:6,
	fps:50,
	timer:0,
	
	initialize: function(args){//className, suffix
		if(typeof args[0] == "string" && args[0] != ""){
			this.className = args[0];
		}
		if(typeof args[1] == "number" && args[1] != ""){
			this.sp = args[1];
		}
		if(typeof args[2] == "number" && args[2] != ""){
			this.fps = args[2];
		}
		this.start();
	},
	
	start: function(){
		var elements = SystemY.getElementsByClassName(this.className, "a", "");
		for (var i = 0; i < elements.length; i++){
			var element = elements[i];
			element.href = "javascript:void(0);";
			Event.observe(element, 'click', SystemY.scroller.scrollStart, false);
		}
		var elements = SystemY.getElementsByClassName(this.className + "To", "a", "");
		for (var i = 0; i < elements.length; i++){
			var element = elements[i];
			element.rel += "#" + element.href.split("#")[1];
			element.href = "javascript:void(0);";
			Event.observe(element, 'click', function(){ SystemY.scroller.scrollTo(this) } , false);
		}
	},
	
	scrollStart: function(e){
		//var element = Event.element(e);
		SystemY.scroller.pageScroll(0, 0);
		return false;
	},
	
	scrollTo: function(e){
		var target = e.rel.split("#");
		if($(target[1])){
			var pos = SystemY.scroller.getScrollToPos(target[1]);
			switch(target[0]){
				case "xy":
					SystemY.scroller.pageScroll(pos.x, pos.y);
					break;
				case "x":
					SystemY.scroller.pageScroll(pos.x, 0);
					break;
				default:
					SystemY.scroller.pageScroll(0, pos.y);
					break;
			}
		}
		return false;
	},
	
	getScrollToPos: function(target){
		var pos = SystemY.getElementPosition(target);
		var w = SystemY.getPageSize();
		pos.y = (w.ph - pos.y > w.wh)?pos.y:w.ph - w.wh;
		pos.x = (w.pw - pos.x > w.ww)?pos.x:w.pw - w.ww;
		return pos;
	},
	
	getScrollLeft: function(){
		if(navigator.userAgent.indexOf("MSIE") != -1) {
			return (document.compatMode=="CSS1Compat")?document.documentElement.scrollLeft:document.body.scrollLeft;
		}else{
 		 return (window.pageXOffset) ? window.pageXOffset : 0;
 		}
	},
	
	getScrollTop: function(){
		if(navigator.userAgent.indexOf("MSIE") != -1) {
			return (document.compatMode=="CSS1Compat")?document.documentElement.scrollTop:document.body.scrollTop;
		}else{
			return (window.pageYOffset) ? window.pageYOffset : 0;
		}
	},
	
	pageScroll: function(toX, toY, cuX, cuY){
		clearTimeout(this.timer);
		toX = (!toX || toX < 0) ? 0 : toX;
		toY = (!toY || toY < 0) ? 0 : toY;
		cuX = (!cuX) ? 0 + this.getScrollLeft() : cuX;
		cuY = (!cuY) ? 0 + this.getScrollTop() : cuY;
		if(cuX > toX){
			var mX = Math.floor(cuX + (toX - this.getScrollLeft()) / this.sp);
			if(mX < toX) {mX = toX;}
		}else{
			var mX = Math.ceil(cuX + (toX - this.getScrollLeft()) / this.sp);
			if(mX > toX) {mX = toX;}
		}
		if(cuY > toY){
			var mY = Math.floor(cuY + (toY - this.getScrollTop()) / this.sp);
			if(mY < toY) {mY = toY;}
		}else{
			var mY = Math.ceil(cuY + (toY - this.getScrollTop()) / this.sp);
			if(mY > toY) {mY = toY;}
		}
		window.scrollTo(mX, mY);
		if(mX != toX || mY != toY) {
			this.timer = setTimeout("SystemY.scroller.pageScroll("+toX+","+toY+"," +mX+","+mY+")",Math.floor(1000 / this.fps));
		}
	}
});


document.observe('dom:loaded', SystemY.start);

//SystemY.addInitApps("dirToMenu", true);
SystemY.addInitApps("imgSwapper");
SystemY.addInitApps("scroller");
