
if(typeof(KAPPE)=='undefined') KAPPE = function(){}

KAPPE.Agent = {};
{
	KAPPE.Agent.Mac      = navigator.appVersion.indexOf('Mac',0)    != -1;
	KAPPE.Agent.Win      = navigator.appVersion.indexOf('Win',0)    != -1;
	KAPPE.Agent.IE       = navigator.appName.indexOf("Microsoft",0) != -1;
	KAPPE.Agent.NN       = navigator.appName.indexOf("Netscape",0)  != -1;
	KAPPE.Agent.Moz      = navigator.userAgent.indexOf("Gecko")     != -1;
	KAPPE.Agent.Safari   = navigator.userAgent.indexOf("Safari")    != -1;
	KAPPE.Agent.iCab     = navigator.appName.indexOf("iCab")        != -1;
	KAPPE.Agent.Opera    = window.opera ? true : false;
	KAPPE.Agent.MacIE    = (KAPPE.Agent.Mac && KAPPE.Agent.IE) ? true : false;
	KAPPE.Agent.version  = parseInt( navigator.appVersion.charAt(0) );
	KAPPE.Agent.mversion = 0;

	if (KAPPE.Agent.IE)
		KAPPE.Agent.version += 1;
	if ((navigator.appVersion.indexOf('MSIE 7.',0) != -1)  && KAPPE.Agent.Win)
		KAPPE.Agent.version  = 7;
	if ((navigator.appVersion.indexOf('MSIE 6.',0) != -1)  && KAPPE.Agent.Win)
		KAPPE.Agent.version  = 6;
	if ((navigator.appVersion.indexOf('MSIE 5.5',0) != -1) && KAPPE.Agent.Win)
		KAPPE.Agent.mversion = 5;
	if ((navigator.appVersion.indexOf('MSIE 4.',0) != -1)  && KAPPE.Agent.Mac)
		KAPPE.Agent.version  = 4;
}


KAPPE.Poy = function() {
	for( var i=0, arr=["h1","h2","h3","h4","h5","h6"]; i<arr.length; i++ ) {
		var elm = document.getElementsByTagName(arr[i]);
		if( !elm ) {
			continue;
		}
		for( var ii=0; ii<elm.length; ii++ ) {
			var _e = elm[ii];
			if( _e.className.match(/^Poy_\d\d_\d{1,}(_[0-9a-f]{6}){1,2}$/i) ) {
				var _query = {};
				
				if( _a = _e.getElementsByTagName("a")[0] ) {
					_query.href = _a.href;
				}

				if( _e.innerText ) {
					_query.q = _e.innerText;
				} else {
					_query.q = document.createTextNode(_e.textContent).textContent;
				}

				_query.q = _query.q.replace(/^\s+/g,"");
				_query.q = _query.q.replace(/\s+$/g,"");

				if( _query.q.length == 0 ) {
					continue;
				}
				var _arr = _e.className.split(/_/);
				
				_query.element = _e;
				_query.size    = _arr[1] || "20";
				_query.font    = _arr[2] || "0";
				_query.color   = _arr[3] || "000000";
				_query.bgcolor = _arr[4] || "FFFFFF";

				this.execImage(_query);
			}
		}
	}
	return this;
}



KAPPE.Poy.prototype.appendImageObject = function( _element, _img )
{
	if(typeof(_img)=="object") {
		if(KAPPE.Agent.IE) {
			_element.innerHTML = '<img src="'+_img.src+'" border="0" alt="'+_img.alt+'" border="0">';
		} else {
			var _obj = document.createElement("img");
			_obj.src    = _img.src;
			_obj.alt    = _img.alt;
			_obj.border = 0;
			_element.appendChild(_obj);
		}
	}
}

KAPPE.Poy.prototype.execImage = function(_query)
{
	var _p = [];

	for( var i=0, _a=["size","font","color","bgcolor","q"]; i<_a.length; i++ ) {
		_p.push(encodeURI(_query[_a[i]]));
	}
	var uri = "http://hinode.kappe.ne.jp/API/Poy/poy.cgi/" + _p.join("/");

	var _img = new Image;
	var _alt = _query.q;

	try {
		_img.src   = uri;
		_img.alt   = _alt;
		_img.border = 0;

		if( _img.width != "1" ) {
			if( !_query.href ) {
				
				this.appendImageObject( _query.element, _img );
			} else {
				_a = document.createElement("a");
				_a.href = _query.href;
				this.appendImageObject( _a, _img );
				_query.element.appendChild(_a);
				
			}
			for(var iii=0,_de=_query.element.childNodes;iii<_de.length-1;iii++ ) {
				_query.element.removeChild(_de[iii]);
			}
		}
	} catch(error) {
		throw error;
	}
}

