

if(typeof WidzzyC == 'undefined') var WidzzyC = {};
WidzzyC.library_version = 0.01;

if(
	typeof WidzzyC.version == 'undefined'
	|| WidzzyC.version < WidzzyC.library_version
) {
	(function() {
		this.version = WidzzyC.library_version;

		// Core Dom
		this.$ = function(elem){
			return (typeof elem == 'string') ? document.getElementById(elem) : elem;
		}
		this.$C = function(name,tag,node) {
			if(node == null) node = document;
			if(tag == null) tag = '*';
			var elems = node.getElementsByTagName(tag);
			var pattern = new RegExp('(?:^|\\s)' + name + '(?:\\s|$)');
			var found = [];
			for(var i = 0; i < elems.length; i++) 
				if(pattern.test(elems[i].className)) found.push(elems[i]);
			return found;
		}
		this.getScriptsByUrl = function(url) {
			var scripts = this.$T('script');
			var found = [];
			var regex = new RegExp('(?:^|\\s)' + url + '(?:\\s|$)' );
			for(var i = 0; i < scripts.length; i++)
				if(regex.test(scripts[i].src)) found.push(scripts[i]);
			return found;
		}
		this.loadLibrary = function(url) {
			if(this.getScriptsByUrl(url).length > 0) return;
			var elem = document.createElement('script');
			elem.type = 'text/javascript';
			elem.src = url;
			document.body.appendChild(elem);
		}
		this.$T = function(tag){ return document.getElementsByTagName(tag);};

		// Core Security
		this.escapeJS = function(source) {
			return source.replace(/\\/g,'\\\\').replace(/"/g,'\\"')
				.replace(/'/g,"\\'").replace(/\//g,'\\/');
		}
		this.escapeHTML = function(source) {
			return (typeof source == 'string') ? source.replace(
					/(&)(?!(?:[a-z0-9]{2,7}|#[0-9]{2,5}|#x[0-9a-f]{2,5});)/gi,
					'&amp;'
				).replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&#34;')
				.replace(/'/g,'&#39;').replace(/`/,'&#96') : source ;
		}
		this.stripTags = function(source) {
			return source.replace(/<\/?[^>]+>/gi,'');
		}

		this.addEvent = function(elem,type,fn,useCapture) {
			if(elem.addEventListener) {
				elem.addEventListener(type, fn, useCapture);
				return true;
			} else if(elem.attachEvent) {
				return elem.attachEvent('on' + type, fn);
			} else {
				elem['on' + type] = fn;
			}
		}

		this.isMacGecko = function () {
			if (navigator) {
				if (navigator.userAgent) {
					if (navigator.userAgent.indexOf('Gecko/') != -1) {
						if (navigator.userAgent.indexOf('Mac OS') != -1) {
							return true;
						}
					}
				}
			}
			return false;
		}

	}).apply(WidzzyC);


	WidzzyC.Widget = function(){};
	WidzzyC.Widget.prototype = {
		width: 0,
		height: 0,
		swf_path: '',
		arrow: 'always',
		transparent: true,
		play: true,
		loop: false,
		menu: false,
		quality: 'high',
		bgcolor: '#000000',
		id: '',
		flash_vars: '',
		wrapperId: '',
		getObjectId: function(){ return this.id + 'Object'; },
		getEmbedId: function(){ return this.id + 'Embed'; },
		getTag: function() {
			var es = WidzzyC.escapeHTML;
			var trans_obj = (this.transparent) ? '<param name="wmode" value="transparent" />' : '';
			var trans_emb = (this.transparent) ? 'wmode="transparent" ' : '';
			return [
'<object id="' + this.getObjectId() + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ',
'name="' + this.getObjectId() + '" width="' + this.width +'" height="' + this.height + '" ',
'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" >',
'<param name="allowScriptAccess" value="' + this.arrow + '" />',
trans_obj,
'<param name="movie" value="' + es(this.swf_path) + '" />',
'<param name="loop" value="' + this.loop + '" />',
'<param name="menu" value="' + this.menu + '" />',
'<param name="quality" value="' + this.quality + '" />',
'<param name="bgcolor" value="' + this.bgcolor + '" />',
'<param name="FlashVars" value="' + es(this.flash_vars) + '" />',
'<embed id="' + this.getEmbedId() + '" name="' + this.getEmbedId() + '" ',
'src="' + es(this.swf_path) + '" loop="' + this.loop + '" menu="' + this.menu + '" quality="' + this.quality + '" bgcolor="' + this.bgcolor + '" ',
'swLiveConnect="true" width="' + this.width + '" height="' + this.height + '" ',
'FlashVars="' + es(this.flash_vars) + '" ',
trans_emb,
'allowScriptAccess="' + this.arrow + '" type="application/x-shockwave-flash" ',
'pluginspage="http://www.macromedia.com/go/getflashplayer" />',
'</object>'
			].join('');
		},
		load: function () {
			var tags = this.getTag();
			if(this.wrapperId) {
				WidzzyC.$(this.wrapperId).innerHTML = tags;
			} else {
				var elem = document.createElement('div');
				elem.innerHTML = tags;
				document.body.appendChild(elem);
			}
		},
		getAvailableElement: function(){
			var el;
			return (el = WidzzyC.$(this.getEmbedId()))? el: WidzzyC.$(this.getObjectId());
		}
	}

	if (!WidzzyC.LazyTask) WidzzyC.LazyTask = {};
	(function(){
		this.queue = new Array();
		this.interval = 500;
		this.timer = undefined;

		//task:{ action: function_object_name, args: array_of_arguments };
		this.register = function(task) {
			this.queue.push(task);
			var _self = this;
			this.timer = setTimeout(
				function(){ return _self.tryExecusion.apply(_self);},
				_self.interval
			);
		}

		this.remove = function(task) {
			for(var i = 0; i < this.queue.length; i++) 
				if(this.queue[i].method == task.method
					&& this.queue[i].args == task.args
				) this.queue.splice(i,1);
		}

		this.tryExecusion = function() {
			if(this.timer) clearTimeout(this.timer);
			var next_queue = [];
			while(this.queue.length > 0) {
				var task = this.queue.shift();
				var method = this.getMethod(task.action);
				if(typeof method == 'undefined') {
					next_queue.push(task);
				} else {
					var obj = this.getBindTarget(task.action);
					if(task.result) {
						task.result = method.apply(obj, task.args);
					} else {
						method.apply(obj,task.args);
					}
				}
			}
			if(next_queue.length > 0) {
				this.queue = next_queue;
				var _self = this;
				this.timer = setTimeout(
					function(){ return _self.tryExecusion.apply(_self);},
					_self.interval
				);
			}
		}

		this.getMethod = function(name) {
			var method;
			try {
				method = eval(name);
			} catch(e) {
				method = undefined;
			}
			return method;
		}

		this.getBindTarget = function(name) {
			var obj;
			try {
				obj = eval(name.replace(/.[^.]+$/,''));
			} catch(e) {
				obj = window;
			}
			return obj;
		}
	}).apply(WidzzyC.LazyTask);
}




if(typeof WidzzyCManager == 'undefined') {
	var WidzzyCManager = function(){};
	WidzzyCManager.prototype = {
		count: 0,
		interval: 100,
		contextWrapper: '',
		base_id: '',
		path: '',
		width: 0,
		height: 0,
		arrow: 'sameDomain',
		transparent: false,
		play: true,
		loop: false,
		menu: 0,
		quality: 'high',
		bgcolor: '#000000',
		widgets_vars: [],
		widgets_swfs: [],

		appendWidget: function(swf, vars, logStr){
			document.write(
				'<div class="' + this.contextWrapper + '" id="' + this.contextWrapper + this.count + '">' + logStr + '</div>'
			);
			this.widgets_vars[this.count] = vars;
			this.widgets_swfs[this.count] = swf;
			this.count++;
		},
		onload: function() {
			for(var i = 0; i < this.count; i++) {
				var widget = new WidzzyC.Widget();

				widget.wrapperId = this.contextWrapper + i;
				widget.id = this.base_id + i + '-';

//				widget.swf_path = this.path;
				widget.swf_path = this.widgets_swfs[i];
				widget.width = this.width;
				widget.height = this.height;
				widget.arrow = this.arrow;
				widget.transparent = this.transparent;
				widget.play = this.play;
				widget.loop = this.loop;
				widget.menu = this.menu;
				widget.quality = this.quality;
				widget.bgcolor = this.bgcolor;

				widget.flash_vars = this.widgets_vars[i];

				widget.load();
			}
		},
		complete: function() {
			if(this.done) return;
			this.done = true;
			if(!typeof this.done == 'undefined') {
				clearInterval(this.timer);
				this.timer = null;
			}
			this.onload();
		},
		watch: function() {
			var _self = this;
			var ua = navigator.userAgent.toLowerCase();
			if(ua.match(/webkit|safari|khtml/)){ // Safari & KHTML
				_self.timer = setInterval(
					function() { 
						if(document.readyState.match(/loaded|complete/)) {
							return _self.complete.apply(_self);
						}
					},50);
			} else if(document.addEventListener) {
				document.addEventListener(
					"DOMContentLoaded",
					function(){ return _self.complete.apply(_self);},
					false
				);
			}
			/*@cc_on @*/
			/*@if (@_win32) @*/
			else if(ua.match(/msie/) && !ua.match(/opera/)) {
				document.write("<scr" + "ipt id=__WidzzyC_ie_init defer=true " + "src=//:><\/script>");
				document.getElementById('__WidzzyC_ie_init').onreadystatechange = function() {
					if(this.readyState != 'complete') return;
					_self.complete();
				};
			}
			/*@end @*/
			else {
				addEvent(window,"load",_self.complete);
			}
		}
	}
}




// for customization
if(typeof caduceus == 'undefined') {
	var caduceus = new WidzzyCManager();
	(function(){

		var noCache = true;
		var noCacheParam = noCache ? '?' + new Date() : '';

		this.open = function(w, h, url){
			// play log
			var elm = document.createElement('img');
			elm.src = 'http://www.new-blood.jp/play.php?id=7bdc57ac2c054ad054d40d2efca8a1bd038a230e&t='+(new Date());
			elm.width = '1';
			elm.height = '1';
			elm.style.display = 'block';
			elm.style.visibility = 'hidden';
			document.body.appendChild(elm);

			// url = floatingSwfPath[id] + noCacheParam;

			url = url || 'http://www.new-blood.jp/swf/cad_game.swf';

			if (url.indexOf('http') == -1) {
				return false;
			}

			url = url + noCacheParam;

			w = w || 400;
			h = h || 550;
			// id = id || 'CaduceusWidget-panel';
			var id = 'CaduceusWidget-panel';

			var root_url = 'http://www.new-blood.jp/lib/widget/';
			var panelId = 'WidzzyC-panel';

			// load library
			WidzzyC.loadLibrary(root_url + 'bytefx_OS.js' + noCacheParam);
			WidzzyC.loadLibrary(root_url + 'dom.js' + noCacheParam);
			WidzzyC.loadLibrary(root_url + 'effects.js' + noCacheParam);

			var floating = new WidzzyC.Widget();

			floating.width = w;
			floating.height = h;
			floating.swf_path = url;
			floating.wrapperId = id;

			floating.id = "CaduceusWidgetFloating";
			floating.flash_vars = 'id=7bdc57ac2c054ad054d40d2efca8a1bd038a230e&domain=http://www.new-blood.jp/';

			WidzzyC.LazyTask.register({
				action: "WidzzyC.Effects.centering",
				args: [floating]
			});


			if (! (window.opera || /webkit/.test(navigator.userAgent.toLowerCase())) ) {
				var timer = setInterval(checkSwf, 60);
			}

			function checkSwf() {
				var panelElm = document.getElementById(panelId);

//				if (panelElm && panelElm.getElementsByTagName('object').length) {
				if (document.getElementById('CaduceusWidgetFloatingObject')) {

					clearInterval(timer);

					var speed = 20;
					var isMSIE = /*@cc_on!@*/false;
					if (isMSIE) {
						speed = 33;
					}

/*
					var valance = document.getElementById('valance');
					if (valance) {
						WidzzyC.LazyTask.register({
							action: "bytefx_caduceus.fade",
							args: [valance, 0, 60, speed]
						});
					}
*/

					if (! WidzzyC.isMacGecko()) {
						setTimeout(function () {
							WidzzyC.LazyTask.register({
								action: "bytefx_caduceus.fade",
								args: [panelElm, 0, 99.9, speed]
							});
						}, 20);

					} else {
						// mac firefox
						setTimeout(function () {
							WidzzyC.LazyTask.register({
								action: "bytefx_caduceus.fade",
								args: [panelElm, 0, 100, speed]
							});
						}, 20);

						setTimeout(function () {
							var valance = document.getElementById('valance');
							valance.style.display = 'none';
						}, 600);
					}
				}
			}
		}


		this.close = function(){

			var panelId = 'WidzzyC-panel';

			var speed = 20;
			var isMSIE = /*@cc_on!@*/false;
			if (isMSIE) {
				speed = 33;
			}

			if (! (window.opera || /webkit/.test(navigator.userAgent.toLowerCase()) || WidzzyC.isMacGecko()) ) {

				WidzzyC.LazyTask.register({
					action: "bytefx_caduceus.fade",
					args: [document.getElementById(panelId), 99.9, 0, speed]
				});

/*
				setTimeout(function () {
					WidzzyC.LazyTask.register({
						action: "bytefx_caduceus.fade",
						args: [document.getElementById('valance'), 60, 0, speed]
					});
				}, 300);
*/

				setTimeout(function () {
					WidzzyC.LazyTask.register({
						action: "WidzzyC.Effects.end",
						args: []
					});
				}, 1500);

			} else {
				//opera & safari
				WidzzyC.LazyTask.register({
					action: "WidzzyC.Effects.end",
					args: []
				});
			}
		}


		this.introduction = function(){
			location.href = 'http://www.new-blood.jp/introduction?id=7bdc57ac2c054ad054d40d2efca8a1bd038a230e';
		}

		// widget params
		this.contextWrapper = 'CaduceusWidgetWrapper';
		this.base_id = 'Caduceus-widget-';
		this.path = 'http://www.new-blood.jp/swf/cad_result.swf' + noCacheParam;
		this.width = 160;
		this.height = 260;
		this.arrow = 'always';
		this.transparent = true;
		this.play = true;
		this.loop = false;
		this.menu = false;
		this.quality = 'high';
		this.bgcolor = '#000000';
	}).apply(caduceus);

	caduceus.watch();
}

(function () {
	try {
		var request = encodeURIComponent(document.URL);
		var referrer = encodeURIComponent(document.referrer);
	} catch (e) {
		var request = escape(document.URL);
		var referrer = escape(document.referrer);
	}

			caduceus.appendWidget(
		'http://www.new-blood.jp/swf/cad_result.swf',
		'id=7bdc57ac2c054ad054d40d2efca8a1bd038a230e&domain=http://www.new-blood.jp/&name=kamijo&gender=2&blood=2&result_ac=0&result_sp=0&current_url=' + request,

				'<img style="visibility:hidden;display:block" width="1" height="1" src="http://www.new-blood.jp/blank.gif?id=7bdc57ac2c054ad054d40d2efca8a1bd038a230e&req=' + request + '&ref=' + referrer + '&t=1280482190" />'
				);




})();

