if ( typeof window.FD == "undefined" ) {
window.undefined = window.undefined;

/* ---------------------------------------------------------------
	
	FD.JS -- 4digit Javascript Library
	Copyright (c) fourdigit Co., Ltd. All Rights Reserved.
		
--------------------------------------------------------------- */

var FD = function (a) {
	a = a || document;
	if (window == this)
		return new FD(a);
	if ( typeof a == "string")
		return new FD().find(a);
};

/* ------------------------------

	FD(element) FUNCTIONS

------------------------------ */

FD.fn = FD.prototype = {
	find: function (e) {
		if(e == 'window')
			var r = window; 
		else if(e == 'body')
			var r = document.body;
		else if(e == 'document')
			var r = document;
		else if(/#/.test(e))
			var r = document.getElementById(e.substr(1,e.length));

		if (!r) return;

		//addEvent
		r.addEvent = function (eventType, func, cap) {
			if(cap == ("" || undefined)) cap = false;
			if (this.addEventListener)
				this.addEventListener (eventType, func, cap);
			else if (this.attachEvent)
				this.attachEvent ('on'+eventType, func);
			else
				return;
		};
		
		//object Position {X,Y}
		r.pos = FD.findPos(r);

		//AJAX read
		//text
		r.readText = function (url) {
			var ajax = new FD.ajax( url );
			ajax.load('text');
			ajax.onComplete = function(val){
				r.innerHTML = val;
			}
		}
		//xml
		r.readXML = function (url) {
			var ajax = new FD.ajax( url );
			ajax.load('xml');
			ajax.onComplete = function(val){
				r.innerHTML = val.firstChild.nodeValue;
			}
		}

		// :)
		r.hello = 'world';

		return r;
	}
};


/* ------------------------------

	FD.JS FUNCTIONS

------------------------------ */

//ajax
FD.ajax = function  (url, query, method) {
	this.url = url;
	if( typeof query == 'String' )
		this.query = query;
	else
		this.query = '';
	if( query == ("GET" || "POST") )
		this.method = method;
	else
		this.method = "GET";
};
FD.ajax.prototype.load = function (format) {
	try{this.req = new XMLHttpRequest();} catch(e) {
	try{this.req = new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {
	try{this.req = new ActiveXObject("Microsoft.XMLHTTP");}
	catch(e){
		alert("ブラウザ未対応です。");
		return;
	}}}
	this.req.open(this.method, this.url, true);
	var copy = this;
	this.req.onreadystatechange = function(){
		if(copy.req.readyState == 4 || copy.req.status == 200){
			if(format != 'text') {
				copy.completeF(copy.req.responseXML.lastChild);
			} else {
				var get_response_text = function ( req ) {
					var text = req.responseText;
					if ( FD.browser.webkit ) {
						var esc = escape( text );
						if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
							text = decodeURIComponent( esc );
						}
					}
					return text;
				};
				var ret = get_response_text(copy.req);
				copy.completeF(ret);
			}
		}
	};
	this.req.send(this.query);
};
FD.ajax.prototype.completeF = function (val){ if (this.onComplete) this.onComplete(val) };
FD.ajax.prototype.connectF = function (val){ if (this.onConnect) this.onConnect(val) };
FD.ajax.prototype.errorF = function (val){ if (this.onError) this.onError(val) };


//etc
FD.extend = FD.fn.extend = function() {
	var target = arguments[0], a = 1;
	if ( arguments.length == 1 ) {
		target = this;
		a = 0;
	}
	var prop;
	while (prop = arguments[a++])
		for ( var i in prop ) target[i] = prop[i];
	return target;
};
FD.extend({
		// browser discrimination
		browser : {
			msie : /msie/.test(navigator.userAgent.toLowerCase()),
			gecko : /gecko/.test(navigator.userAgent.toLowerCase()),
			webkit : /webkit/.test(navigator.userAgent.toLowerCase()),
			opera : window.opera,
			mac : /mac/.test(navigator.userAgent.toLowerCase()),
			win : /win/.test(navigator.userAgent.toLowerCase())
		},
		// window size
		window : {
			height : function () { return document.body.clientHeight || innerHeight },
			width : function () { return document.body.clientWidth || innerWidth }
		},
		// isFunction
		isFunction : function( fn ) {
			return !!fn && typeof fn != "string" && !fn.nodeName && 
				typeof fn[0] == "undefined" && /function/i.test( fn + "" );
		},
		//object Position
		findPos : function (obj) {
			var posX = posY = 0;
			if (obj.offsetParent) {
				posX = obj.offsetLeft;
				posY = obj.offsetTop;
				while (obj = obj.offsetParent) {
					posX += obj.offsetLeft;
					posY += obj.offsetTop;
				}
			}
			return {X:posX, Y:posY};
		}
});

/* FD.JS END */ }