// window event controller.
var windowEvents = {};
window.addEvent = function () {
	if (arguments.length != 2) {
		return false;
	} else {
		var eventType = arguments[0];

		if (!windowEvents[eventType])
			windowEvents[eventType] = new Array();
		windowEvents[eventType].push(arguments[1]);

		window[arguments[0]] = function (e) {
			var objEvent = e || window.event;

			if (!!windowEvents['on' + objEvent.type] && windowEvents['on' + objEvent.type].length > 0) {
				for (var i=0; i<windowEvents['on' + objEvent.type].length; i++) {
					windowEvents['on' + objEvent.type][i]();
				}
			}
		}

		return true;
	}
}

var Browser = function () {};
Browser.prototype.isIE = (navigator.userAgent.indexOf('MSIE')>=0 && document.all);
Browser.prototype.isIE6 = (navigator.userAgent.indexOf('MSIE 6.')>=0  && document.all);
Browser.prototype.isFirefox = (navigator.userAgent.indexOf('Firefox')>=0 || navigator.userAgent.indexOf('IceWeasel')>=0);
Browser.prototype.isSafari = (navigator.userAgent.indexOf('Safari')>=0);
Browser.prototype.isSafari3 = (navigator.userAgent.indexOf('Safari')>=0 && navigator.userAgent.indexOf('Version/3')>0);
Browser.prototype.isOpera = (!Browser.prototype.isIE&&(navigator.userAgent.indexOf('Opera')>=0));
Browser.prototype.isMozilla = (!Browser.prototype.isIE && !Browser.prototype.isFirefox && !Browser.prototype.isSafari && !Browser.prototype.isOpera && (navigator.userAgent.indexOf('Mozilla')>=0));
var objBrowser = new Browser();

var ELEMENT_NODE = 1;
var ATTRIBUTE_NODE = 2;
var TEXT_NODE = 3;

// extend Object. inspired by Prototype.
Object.extend = function(target, source) {
	for (var property in source)
		target[property] = source[property];
	return target;
}

// extend Elements.
Object.extend(Object, {
	createHTMLElement: function (tag, data) {
		var objElement = document.createElement(tag);

		for (prop in data) {
			switch (prop.toLowerCase()) {
			case 'classname':
				objElement.className = data[prop];
				break;
			case 'onclick':
				objElement.onclick = data[prop];
				break;
			default:
				objElement.setAttribute(prop, data[prop]);
				break;
			}
		}

		return objElement;
	},

	calcOptimizedImageSize: function (argWidth, argHeight, boxWidth, boxHeight, resizeFlag) {
		if (!resizeFlag) resizeFlag = 'reduce';

		if (!boxWidth && !boxHeight) {
			return array(argWidth, argHeight);
		} else if (!!boxWidth && !boxHeight) {
			if (argWidth > boxWidth) {
				newWidth = boxWidth;
				newHeight = Math.ceil(argHeight * newWidth / argWidth);
			} else {
				newWidth = argWidth;
				newHeight = argHeight;
			}
		} else if (!boxWidth && !!boxHeight) {
			if (argHeight > boxHeight) {
				newHeight = boxHeight;
				newWidth = Math.ceil(argWidth * newHeight / argHeight);
			} else {
				newWidth = argWidth;
				newHeight = argHeight;
			}
		} else {
			if (argWidth > boxWidth) {
				newWidth = boxWidth;
				newHeight = Math.ceil(argHeight * newWidth / argWidth);
			} else {
				newWidth = argWidth;
				newHeight = argHeight;
			}

			if (newHeight > boxHeight) {
				tempHeight = newHeight;
				newHeight = boxHeight;
				newWidth = Math.ceil(newWidth * newHeight / tempHeight);
			}
		}

		if (argWidth * argHeight > newWidth * newHeight) {
			if (resizeFlag == 'reduce' || resizeFlag == 'both') {
				imgWidth = newWidth;
				imgHeight = newHeight;
			} else {
				imgWidth = argWidth;
				imgHeight = argHeight;
			}
		} else if (argWidth * argHeight == newWidth * newHeight) {
			imgWidth = argWidth;
			imgHeight = argHeight;
		} else if (argWidth * argHeight < newWidth * newHeight) {
			if (resizeFlag == 'enlarge' || resizeFlag == 'both') {
				imgWidth = newWidth;
				imgHeight = newHeight;
			} else {
				imgWidth = argWidth;
				imgHeight = argHeight;
			}
		}

		return {width:imgWidth, height:imgHeight};
	},

	get: function (id) {
		return document.getElementById(id);
	},

	getChildNodes: function () { // [0]:object, [1]:tag name, [2]:class name, [3]:only element
		if (Object.isElement(arguments[0]) != 1)
			return [];
		if (arguments[0].childNodes.length == 0)
			return [];

		if (!arguments[1])
			var targetNodeName = null;
		else
			var targetNodeName = arguments[1].toLowerCase();

		var targetClassName = (arguments.length < 3) ? null : arguments[2];
		var onlyElement = (arguments[3] == undefined) ? true : !!arguments[3];

		var resultArray = new Array();

		if (onlyElement == true) {
			for (var i=0; i<arguments[0].childNodes.length; i++) {
				if (Object.isElement(arguments[0].childNodes[i])) {
					if (targetNodeName == null) {
						if (targetClassName == null || arguments[0].childNodes[i].className == targetClassName)
							resultArray.push(arguments[0].childNodes[i]);
						else
							continue;
					} else {
						if (arguments[0].childNodes[i].nodeName.toLowerCase() == targetNodeName) {
							if (targetClassName == null || arguments[0].childNodes[i].className == targetClassName)
								resultArray.push(arguments[0].childNodes[i]);
							else
								continue;
						} else {
							continue;
						}
					}
				} else {
					continue;
				}
			}
		} else {
			resultArray = arguments[0].childNodes;
		}

		return resultArray;
	},

	getFirstChild: function () { // [0]:object, [1]:boolean
		if (Object.isElement(arguments[0]) != 1)
			return null;
		if (arguments[0].childNodes.length == 0)
			return null;

		var onlyElement = (arguments[1] == undefined) ? true : !!arguments[1];

		for (var i=0; i<arguments[0].childNodes.length; i++) {
			if (onlyElement) {
				if (Object.isElement(arguments[0].childNodes[i]))
					return arguments[0].childNodes[i];
			} else {
				return arguments[0].childNodes[0];
			}
		}

		return null;
	},

	getLastChild: function () { // [0]:object, [1]:boolean
		if (Object.isElement(arguments[0]) != 1)
			return null;
		if (arguments[0].childNodes.length == 0)
			return null;

		var onlyElement = (arguments[1] == undefined) ? true : !!arguments[1];

		for (var i=arguments[0].childNodes.length-1; i>=0; i++) {
			if (onlyElement) {
				if (arguments[0].childNodes[i].isElement())
					return arguments[0].childNodes[i];
			} else {
				return arguments[0].childNodes[arguments[0].childNodes.length-1];
			}
		}

		return null;
	},

	getOffset: function () {
		var objRoot = null;
		var currentObj = document.getElementById(arguments[0]);
		var bLoop = !!arguments[1];
		var data = {left:0, top:0, width:0, height:0}

		data.width = currentObj.offsetWidth;
		data.height = currentObj.offsetHeight;

		switch (bLoop) {
		case true:
			while (!!currentObj && currentObj.nodeName.toLowerCase() != 'body') {
				data.top += currentObj.offsetTop;
				data.left += currentObj.offsetLeft;
				currentObj = currentObj.offsetParent;
			}
			break;
		case false:
			data.top = currentObj.offsetTop;
			data.left = currentObj.offsetLeft;
			break;
		}

		return data;
	},

	getPreviousSibling: function () {
		if (Object.isElement(arguments[0]) != 1)
			return null;

		var onlyElement = (arguments[1] == undefined) ? true : !!arguments[1];
		var objNodeList = this.parentNode.getChildNodes(null, null, onlyElement);

		for (var i=0; i<objNodeList.length; i++) {
			if (objNodeList[i] == arguments[0]) {
				if (!!objNodeList[i - 1])
					return objNodeList[i - 1];
				else
					return null;
			}
		}

		return null;
	},

	getNextSibling: function () {
		if (Object.isElement(arguments[0]) != 1)
			return null;

		var onlyElement = (arguments[1] == undefined) ? true : !!arguments[1];
		var objNodeList = Object.getChildNodes(arguments[0].parentNode, null, null, onlyElement);

		for (var i=0; i<objNodeList.length; i++) {
			if (objNodeList[i] == arguments[0]) {
				if (!!objNodeList[i + 1])
					return objNodeList[i + 1];
				else
					return null;
			}
		}

		return null;
	},

	getParentNode: function () { // [0]:object, [1]:tag name
		if (Object.isElement(arguments[0]) == false || arguments.length != 2)
			return null;

		var currentNode = arguments[0];
		var targetName = arguments[1].toLowerCase();

		while (currentNode != undefined) {
			if (currentNode.nodeName.toLowerCase() == targetName)
				return currentNode;
			else
				currentNode = currentNode.parentNode;
		}

		return null;
	},

	getRealStyle: function () {
		var realstyle = null;

		if (Object.isElement(arguments[0]) && arguments.length == 2) {
			var element = arguments[0];
			var stylename = arguments[1];

			if (element.currentStyle)
				realstyle = element.currentStyle[stylename];
			else
				realstyle = window.getComputedStyle(element, null)[stylename];
		}

		return realstyle;
	},

	getScreenSize: function () {
		if (document.all) {
			// IE4+ or IE6+ in standards compliant
			screenW  = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
			screenH = (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
			screenS = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		} else {
			// TODO: bug in Opera.
			// Non-IE
			screenW = window.innerWidth;
			screenH = window.innerHeight;
			screenS = window.pageYOffset;
		}

		// Core code from - quirksmode.org
	    if (window.innerHeight && window.scrollMaxY) {
	        wholeW = document.body.scrollWidth;
			wholeH = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
			wholeW = document.body.scrollWidth;
			wholeH = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			wholeW = document.body.offsetWidth;
			wholeH = document.body.offsetHeight;
		}

		return {
			display:{
				width:screenW,
				height:screenH
			},
			whole:{
				width:wholeW,
				height:wholeH
			}
		}
	},

	getOpacity: function (obj) {
		return Object.getRealStyle(obj, 'opacity');
	},

	setOpacity: function (obj, value) {
		if (typeof(obj) == 'string')
			obj = document.getElementById(obj);

		// If it's 100, set it to 99 for Firefox.
		if (navigator.userAgent.indexOf('Firefox') != -1) {
			if (value == 100) { value = 99.9999; } // This is majorly retarded
		}

		// Multi-browser opacity setting
		obj.style.filter = 'alpha(opacity=' + value + ')'; 	// IE/Win
		//obj.KhtmlOpacity = (value / 100);            		// Safari 1.1 or lower, Konqueror
		//obj.MozOpacity = (value / 100);              		// Older Mozilla+Firefox
		obj.style.opacity = (value / 100);                 	// Safari 1.2, Firefox+Mozilla
	},

	isArray: function () {
		return "join" in arguments[0];
	},

	isElement: function () {
		return arguments[0] && arguments[0].nodeType == ELEMENT_NODE;
	}
});

// extend String.
Object.extend(String.prototype, {
	isEmpty: function () {
		if (this == undefined || this == null)
			return true;
		else
			return this.trim() == "";
	},

	trim: function () {
		return this.replace(/(^\s*|\s*$)/g, "");
	}
});



//fimily site
function familyArea() {
	var scroll = {time:1, start:0, change:0, duration:25, timer:null};
	var originaltime = scroll.time;
	var objIndex = 0;
	var objWrap = document.getElementById('familyArea');
	var objBtn = objWrap.getElementsByTagName('h2')[0];
	var objUL = objWrap.getElementsByTagName('ul')[0];
	var objLI = objUL.getElementsByTagName('li');

	this.initialize = function () {
		objWrap.style.top = - objUL.offsetHeight + 14 + 'px';
		objWrap.style.paddingTop = objUL.offsetHeight - 15 + 'px';
		objBtn.onclick= function () {
			//alert(objIndex);
			if (objIndex == 0) {
				objUL.style.zIndex = '9999';
				startScroll(objUL.offsetHeight, 0);
				objIndex = 1;
				return false;
			}
		}
		objUL.onclick= function () {
			if (objIndex == 1) {
				objUL.style.zIndex = '1';
				startScroll(0, objUL.offsetHeight);
				objIndex = 0;
			}
		}
		for (var i=0; i<objLI.length; i++) {
			objLI[i].getElementsByTagName('a')[0].onmouseover = function () {
				this.className = 'visible';
			}
			objLI[i].getElementsByTagName('a')[0].onmouseout= function () {
				this.className = '';
			}
		}
	}

	var startScroll = function (start, end) {
		if (scroll.timer != null) {
			clearInterval(scroll.timer);
			scroll.timer = null;
		}
		scroll.start = start;
		scroll.change = end - start;
		scroll.timer = setInterval(scrollVertical, 15);
	}

	var scrollVertical = function () {
		if (scroll.time > scroll.duration) {
			clearInterval(scroll.timer);
			scroll.time = originaltime;
			scroll.timer = null;
		} else {
			objUL.style.top = sineInOut(scroll.time, scroll.start, scroll.change, scroll.duration) + 'px';
			scroll.time++;
		}
	}

	var sineInOut = function (t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	}
}