var xboxbp_server = "http://gow.blogdeco.jp/";
var xboxbp_linktarget = "http://gearsofwar.jp/";
//var xboxbp_ZakoEnemies = 30;	// think and enjoy!
//var xboxbp_ShowTime = 1;	// think and enjoy!
var xboxbp_img_on =						"img/onb.gif";
var xboxbp_img_ontonedown =		"img/ona.gif";
var xboxbp_timeMinAppear = 500;
var xboxbp_timeMaxAppear = 2000;	// Max-Min間隔で現れる
var xboxbp_speedFade = 34;				// fadein,fadeoutの速さ

document.write("<div id='xboxbp' style='width:180px; height: 260px; background-image:url("+xboxbp_server+"img/last.gif); text-align:left;'><a href='"+xboxbp_linktarget+"' style='border:0px;margin:0;padding:0'><img src='"+xboxbp_server+"img/180b.gif' height='178px' width='176px' style='border:none;margin-top:2px;margin-left:2px;' /></a>");

document.write("<div id='xboxbpCountdown' style='background-color:black;background-image:url("+xboxbp_server+"img/countdown1.gif); width:176px; height:40px; overflow:hidden; margin-left:2px; overflow:hidden;'>");
document.write("</div><!--xboxbpCountdown-->");

document.write("<a href='javascript:void(0);'><div id='xboxbp_start' style='background-color:black;border:none; background-image:url("+xboxbp_server+xboxbp_img_on+");width:176px;height:38px;margin-left:2px;'></div></a></div><!--xboxbp-->");

document.write("<div id='xboxbpCLast' style='position:absolute;display:none;z-index:7;height:48px;width:606px;overflow:hidden; background-image:url("+xboxbp_server+"img/hatsubai.gif)'>");
document.write("</div>");

var xboxbp_Interval_interval = 10;
var xboxbp_Interval_timer = null;
var xboxbp_Interval_tid = 0;
var xboxbp_Interval_procs = 0;
var xboxbp_Interval_proc = {};
var xboxbp_Interval_countInit = {};
var xboxbp_Interval_count = {};
var xboxbp_Interval_loop = function(){
	for( var i in xboxbp_Interval_proc ){
		xboxbp_Interval_count[i]--;
		if( xboxbp_Interval_count[i]==0 ){
			xboxbp_Interval_count[i] = xboxbp_Interval_countInit[i];
			xboxbp_Interval_proc[i]();
		}
	}
}
var xboxbp_Interval = function(){
	return{
		// t:[ms]
		set : function( func, t ){
			xboxbp_Interval_tid++;
			xboxbp_Interval_procs++;
			if(typeof func == 'string'){ func = new Function(func); }
			xboxbp_Interval_proc[xboxbp_Interval_tid] = func;
			xboxbp_Interval_countInit[xboxbp_Interval_tid] = parseInt(t/10);
			xboxbp_Interval_count[xboxbp_Interval_tid] = xboxbp_Interval_countInit[xboxbp_Interval_tid];
			if( !xboxbp_Interval_timer ){
				xboxbp_Interval_timer = setInterval(xboxbp_Interval_loop,10);
			}
			return xboxbp_Interval_tid;
		},
		del : function( timerid ){
			if( xboxbp_Interval_proc[timerid] ){
				xboxbp_Interval_proc[timerid]=undefined;
				xboxbp_Interval_countInit[timerid]=undefined;
				xboxbp_Interval_count[timerid]=undefined;
				xboxbp_Interval_procs--;
			}
			if( xboxbp_Interval_procs==0 ){
				clearInterval(xboxbp_Interval_timer);
				xboxbp_Interval_timer=null;
			}
		}
	}
}();

function xboxbp_addRule( selector, property, sheetindex, ruleindex ) {
	if( !document.styleSheets ){ return; }
  if( sheetindex == undefined ) sheetindex = 0;
  var sheet = document.styleSheets[ sheetindex ];
  if( sheet.addRule ) { //IE
      if( ruleindex == undefined ) ruleindex = sheet.rules.length;
      sheet.addRule( selector, "{" + property + "}", ruleindex );
      return ruleindex;
  }
  else if( sheet.insertRule ) { //Mozilla
      if( ruleindex == undefined ) ruleindex = sheet.cssRules.length;
      return sheet.insertRule( selector + "{" + property + "}", ruleindex );
  }
  return null;
}

xboxbp_addRule(".xboxbpLeftNum0","background-image:url("+xboxbp_server+"img/number/0.gif)");
xboxbp_addRule(".xboxbpLeftNum1","background-image:url("+xboxbp_server+"img/number/1.gif)");
xboxbp_addRule(".xboxbpLeftNum2","background-image:url("+xboxbp_server+"img/number/2.gif)");
xboxbp_addRule(".xboxbpLeftNum3","background-image:url("+xboxbp_server+"img/number/3.gif)");
xboxbp_addRule(".xboxbpLeftNum4","background-image:url("+xboxbp_server+"img/number/4.gif)");
xboxbp_addRule(".xboxbpLeftNum5","background-image:url("+xboxbp_server+"img/number/5.gif)");
xboxbp_addRule(".xboxbpLeftNum6","background-image:url("+xboxbp_server+"img/number/6.gif)");
xboxbp_addRule(".xboxbpLeftNum7","background-image:url("+xboxbp_server+"img/number/7.gif)");
xboxbp_addRule(".xboxbpLeftNum8","background-image:url("+xboxbp_server+"img/number/8.gif)");
xboxbp_addRule(".xboxbpLeftNum9","background-image:url("+xboxbp_server+"img/number/9.gif)");

xboxbp_addRule(".xboxbpLastNum0","background-image:url("+xboxbp_server+"img/number3/a0.gif)");
xboxbp_addRule(".xboxbpLastNum1","background-image:url("+xboxbp_server+"img/number3/a1.gif)");
xboxbp_addRule(".xboxbpLastNum2","background-image:url("+xboxbp_server+"img/number3/a2.gif)");
xboxbp_addRule(".xboxbpLastNum3","background-image:url("+xboxbp_server+"img/number3/a3.gif)");
xboxbp_addRule(".xboxbpLastNum4","background-image:url("+xboxbp_server+"img/number3/a4.gif)");
xboxbp_addRule(".xboxbpLastNum5","background-image:url("+xboxbp_server+"img/number3/a5.gif)");
xboxbp_addRule(".xboxbpLastNum6","background-image:url("+xboxbp_server+"img/number3/a6.gif)");
xboxbp_addRule(".xboxbpLastNum7","background-image:url("+xboxbp_server+"img/number3/a7.gif)");
xboxbp_addRule(".xboxbpLastNum8","background-image:url("+xboxbp_server+"img/number3/a8.gif)");
xboxbp_addRule(".xboxbpLastNum9","background-image:url("+xboxbp_server+"img/number3/a9.gif)");

Function.prototype.bind = function(object) {
	var __method = this;
	var __object = object;
	var __arguments = arguments;
	for( var index = 0; index < __arguments.length-1; ++index )
		__arguments[index] = __arguments[index+1];
	__arguments.length = __arguments.length-1;
	return function(){ __method.apply(__object, __arguments); }
}

var xboxbp_SimpleSound_index=0;
var xboxbp_SimpleSound = function(url){
	this.initialize(url);
};
xboxbp_SimpleSound.prototype = {
	url: '',
	idHead:'xboxbp_SimpleSound',
	id: '',
	index: 0,
	swf:xboxbp_server+'SoundPreload',
	el:null,
	play: function(){
		if( this.el.playSound ){
			this.el.playSound();
		}
	},
	initialize: function(url){
		this.id = this.idHead+xboxbp_SimpleSound_index;
		this.index = xboxbp_SimpleSound_index;
		this.url = url;
		xboxbp_SimpleSound_index++;
		var tag = [
		'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="0" height="0" id="' + this.id + '" align="middle">',
		'<param name="allowscriptaccess" value="always" />',
		'<param name="movie" value="'+this.swf+this.index+'.swf" />',
		'<param name="quality" value="high" />',
		'<param name="bgcolor" value="#ffffff" />',
		'<embed src="'+this.swf+this.index+'.swf" quality="high" bgcolor="#ffffff" width="0" height="0" name="' + this.id + '" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />',
		'</object>'
		];
		var txt = tag.join('');
		document.write(txt);
		this.el = navigator.userAgent.match(/MSIE/) ? window[this.id] : document[this.id];
		setTimeout(this.load.bind(this),100);
	},
	load: function(){
		var ele = this.el;
		if( ele.loadSound ){
			ele.loadSound( this.url );
		}else{
			setTimeout(this.load.bind(this),100);
		}
	}
};
var xboxbp_soundFire = new xboxbp_SimpleSound(xboxbp_server+"sound/CogRifleFire03.mp3");
var xboxbp_soundBoss = new xboxbp_SimpleSound(xboxbp_server+"sound/BrumakVocalScream_03.mp3");
var xboxbp_soundLogo = new xboxbp_SimpleSound(xboxbp_server+"sound/sfx_xbox_3_trim.mp3");

var xboxbp = {};
xboxbp = {
	debug : 1,
	idOverlay : 'xboxbpOverlay',
	OPACITY : 0,	// 0-1
	OVERLAY_COLOR : 'black',
	aImage : ['img/scope.gif','img/offb.gif','img/monster.gif'],
	aChild : [],
	eleOverlay : null,
	eleStart :null,
	eleOff :null,
	eleScope :null,
	eleEnding:null,
	eleEndingImg: null,
	eleBoss:null,
	eleBossImg:null,
	eleLastCounter:null,
	effectOff :null,
	effectOffDisapear:null,
	//effectBoss:null,
	effectEnding : null,
	SIZE_IMG : [[40,35],[176,38],[640,360]],	// [px] width,height
	intervalTime: 40,		//[ms]
	zakoPos : [0,0],
	mouse : [0,0],
	aPageSize: [0,0,0,0],
	aScroll: [0,0],
	scrolling: null,
	timerBossDisapear: null,
	timerAreaUpdate: null,
	timerShake: null,
	fAllDead: null,
	timeStart: null,
	timeEnd: null,
	load : function(){
		var ele = document.getElementById('xboxbp_start');
		xboxbp_addListener( ele, "click", xboxbp.start, false );
		xboxbp.eleStart = ele;
		// 2006/01/18
		//XBoxCounterLeft = new XBoxCounter(2007,0,18,'xboxbpCLeft','xboxbpLeftNum');
		//XBoxCounterLast = new XBoxCounter(2007,0,18,'xboxbpCLast','xboxbpLastNum');
		//xboxbp_addListener( window, 'unload', XBoxCounterLeft.end.bind(XBoxCounterLeft), false );
		//xboxbp_addListener( window, 'unload', XBoxCounterLast.end.bind(XBoxCounterLast), false );
		if( !xboxbp.readCookie() ){
			xboxbp.setCookie();
			xboxbp.start();
		}
	},
	start : function(){

		xboxbp.eleLastCounter = document.getElementById('xboxbpCLast');
		xboxbp.deleteObstacles();
		xboxbp.fAllDead = false;

		xboxbp_removeListener( xboxbp.eleStart, "click", xboxbp.start, false );
		xboxbp.eleStart.style.backgroundImage = 'url('+xboxbp_server+xboxbp_img_ontonedown+')';

		// overlay
		var ol;
		if( !xboxbp.eleOverlay ){
			var bd = document.getElementsByTagName('body').item(0);
			ol = document.createElement('div');
			ol.setAttribute('id',xboxbp.idOverlay);
			ol.style.display = 'none';
			xboxbp.eleOverlay = ol;
			bd.appendChild(ol);
			xboxbp.aChild.push(ol);
		}
		var sO = xboxbp.eleOverlay.style;
		xboxbp.aPageSize = xboxbp.getPageSize();
		xboxbp.aScroll = xboxbp.getScrollPosition();
		sO.position =				'absolute';
		sO.top =						'0px';
		sO.left =						'0px';
		sO.width =					'100%';
		sO.height =					xboxbp.aPageSize[1]+'px';
		sO.backgroundColor =xboxbp.OVERLAY_COLOR;
		sO.zIndex =					1;
		sO.opacity =				xboxbp.OPACITY;
		if( (navigator.userAgent.indexOf('MSIE') != -1) && (!window.opera) ){
			sO.filter="alpha(opacity=0)";
			xboxbp.eleOverlay.filters.alpha.Opacity  = xboxbp.OPACITY*100;
		}
		sO.display =				'block';

		var bd = document.getElementsByTagName('body').item(0);
		for( var i=0; i<3; i++ ){
			if( !xboxbp.aChild[xboxbp.aChild.length] ){
				var img = document.createElement('img');
				img.setAttribute('src',xboxbp_server+xboxbp.aImage[i]);
				var s = img.style;
				s.display =			'none';
				s.position =		'absolute';
				s.width =				xboxbp.SIZE_IMG[i][0]+'px';
				s.height =			xboxbp.SIZE_IMG[i][1]+'px';
				//bd.appendChild(img);
				xboxbp.aChild.push(img);
			}
		}

		// contents0: scope
		xboxbp.eleScope = xboxbp.aChild[1];
		var ss = xboxbp.eleScope.style;
		ss.top =					'0px';
		ss.left =					'0px';
		ss.zIndex = 			2;
		ss.display =			'none';
		ss.cursor='crosshair';
		bd.appendChild(xboxbp.eleScope);

		// contents1: onoff
		xboxbp.eleOff = xboxbp.aChild[2];
		var so = xboxbp.eleOff.style;
		so.top =					'-'+xboxbp.SIZE_IMG[1][1]+'px';
		so.left =					'0px';
		so.zIndex =				6;
		so.display =			'block';
		xboxbp_addListener( xboxbp.eleOff, "click", xboxbp.cancel, false );
		xboxbp.effectOff = new xboxbp_Effect(xboxbp.SIZE_IMG[1][0],xboxbp.SIZE_IMG[1][1],xboxbp.eleOff,"SLIDEDOWN","LEFTTOP",2);
		xboxbp.effectOff.start();
		bd.appendChild(xboxbp.eleOff);

		// contents2: boss
		xboxbp.eleBoss = document.createElement('div');
		xboxbp.eleBossImg = xboxbp.aChild[3];
		xboxbp.eleBoss.appendChild( xboxbp.eleBossImg );
		var sb = xboxbp.eleBoss.style;
		sb.zIndex = 					2;
		sb.position =					'absolute';
		sb.backgroundColor =	'black';
		sb.display =					'none';
		bd.appendChild(xboxbp.eleBoss);

		// contents3: ending

		xboxbp_ZakoEnemyFactory.initialize();
		xboxbp_addListener( document.body, "mousemove", xboxbp.mousemove, false );
		xboxbp_addListener( document.body, "click", xboxbp.mouseclick, false );
		//xboxbp.timerAreaUpdate = setInterval(xboxbp.updateArea,100);
		xboxbp.timerAreaUpdate = xboxbp_Interval.set(xboxbp.updateArea,100);
(xboxbp.updateArea,100);
		xboxbp.timeStart = new Date();
	},
	backToNormalCursor : function(){
		xboxbp.eleScope.src=xboxbp_server+'img/scope.gif';
	},
	updateArea : function(){
		xboxbp.aPageSize = xboxbp.getPageSize();
		xboxbp.aScroll = xboxbp.getScrollPosition();
		var s = xboxbp.eleOff.style;
		if( xboxbp.eleOff
			&& s.display=='block'
			&& !(xboxbp.effectOff.timerMove)
			&& ( !(xboxbp.effectOffDisapear) || !(xboxbp.effectOffDisapear.timerMove)) )
		{
			s.left =	xboxbp.aScroll[0]+'px';
			s.top =		xboxbp.aScroll[1]+'px';
		}
	},
	getImgSize : function( imgsize, displaysize ){
		// fits visible area
		var ret = [];
		var imgrate = imgsize[1]/imgsize[0];
		var displayrate = displaysize[3]/displaysize[2];
		if( displayrate > imgrate ){
			ret[0] = displaysize[2];
			ret[1] = ret[0]*imgrate;
		}else{
			ret[1] = displaysize[3];
			ret[0] = ret[1]/imgrate;
		}
		return ret;
	},
	mouseclick : function(e){
		xboxbp_soundFire.play();
		var x = xboxbp.getMouseX(e);
		var y = xboxbp.getMouseY(e);
		xboxbp.eleScope.src=xboxbp_server+'img/scope2.gif';
		setTimeout(xboxbp.backToNormalCursor,200);
		var livingenemies = xboxbp_ZakoEnemyFactory.shoot(x,y);
		if( !livingenemies && !xboxbp.fAllDead ){
			xboxbp.timeEnd = new Date();
			xboxbp.fAllDead = true;
			xboxbp_soundBoss.play();
			var sb = xboxbp.eleBoss.style;
			sb.left =			'0px';
			sb.top =			'0px';
			sb.width =		xboxbp.aPageSize[0]+'px';
			sb.height =		xboxbp.aPageSize[1]+'px';
			//se.width=			'100%';
			//se.height=		'100%';
			sb.display =	'block';
			var sbi = xboxbp.eleBossImg.style;
			var wh = xboxbp.getImgSize( xboxbp.SIZE_IMG[2],xboxbp.aPageSize );
			var l = Math.floor((xboxbp.aPageSize[2]-wh[0])/2);
			var t = Math.floor((xboxbp.aPageSize[3]-wh[1])/2);
			sbi.left =			l+xboxbp.aScroll[0]+'px';
			sbi.top =				t+xboxbp.aScroll[1]+'px';
			sbi.width =			wh[0]+'px';
			sbi.height =		wh[1]+'px';
			sbi.display =		'block';
			xboxbp.timerBossDisapear = setTimeout(xboxbp.bossend,500);
			xboxbp.timerShake = setTimeout(xboxbp.shakewindow,100);
		}
	},
	mousemove : function(e){
		var s = xboxbp.eleScope.style;
		s.display='block';
		var x = xboxbp.getMouseX(e);
		var y = xboxbp.getMouseY(e);
		s.left =	x-Math.floor(xboxbp.SIZE_IMG[0][0]/2)+'px';
		s.top =		y-Math.floor(xboxbp.SIZE_IMG[0][1]/2)+'px';
		xboxbp.mouse[0] = x;
		xboxbp.mouse[1] = y;
	},
	bossend: function(){
		xboxbp.timerBossDisapear = null;
		if( !xboxbp.eleEnding ){
			var bd = document.getElementsByTagName('body').item(0);
			xboxbp.eleEnding = document.createElement('div');
			xboxbp.eleEnding.style.display = 'none';
			bd.appendChild(xboxbp.eleEnding);
		}
		//var e = new xboxbp_Effect(xboxbp.aPageSize[0],xboxbp.aPageSize[1],xboxbp.eleEnding,"SLIDEDOWN","CENTER",100);
		var e = new xboxbp_Effect(xboxbp.aPageSize[0],xboxbp.aPageSize[1],xboxbp.eleEnding,"APPEAR","",7);
		xboxbp.eleEnding.innerHTML = "";
		var se = xboxbp.eleEnding.style;
		se.display=						'none';
		se.width=							xboxbp.aPageSize[0]+'px';
		se.height=						xboxbp.aPageSize[1]+'px';
		//se.width=						'100%';
		//se.height=					'100%';
		se.top =							'0px';
		se.left =							'0px';
		se.backgroundColor =	'black';
		se.zIndex =						5;
		se.position =					'absolute';
		e.aftermove = function(){
			this.timer2nd = setTimeout(this.appear.bind(this),1000);
		};
		e.appear = function(){
			this.timer2nd = null;
			var allsize = [457,450];
			var left = Math.floor((xboxbp.aPageSize[2]-allsize[0])/2)+xboxbp.aScroll[0];
			left = (left<0) ? 0 : left;
			var top = Math.floor((xboxbp.aPageSize[3]-allsize[1])/2)+xboxbp.aScroll[1];
			top = (top<0) ? 0 : top;
			var imgsize = [457,253];
			var time = xboxbp.timeEnd.getTime() - xboxbp.timeStart.getTime();
			xboxbp.eleEnding.innerHTML = '<div style="border: none; position:absolute; left:'+left+'px;top:'+top+'px; text-align:center;"><a href='+xboxbp_linktarget+' style="border:none;"><img id="xboxbpEndingImg" src="'+xboxbp_server+'img/logo.jpg" width="'+imgsize[0]+'px" height="'+imgsize[1]+'px"style="border:none;" /></a><br />'+((typeof xboxbp_ShowTime!="undefined")?'<div id="xboxbpTime" style="color:white;margin-top:100px;font-size:x-small">COMPLETE: '+time+' [ms]</div></div>':'');
			var sl = xboxbp.eleLastCounter.style;
			sl.left =				left-60+'px';
			sl.top =				top+300+'px';
			sl.display =		'block';
			xboxbp_soundLogo.play();
		};
		e.afterend = function(){
			if( this.timer2nd ){
				clearTimeout(this.timer2nd);
				this.timer2nd = null;
			}
		};
		e.start();
		xboxbp.effectEnding = e;
	},
	cancel : function(){
		xboxbp.eleLastCounter.style.display='none';
		xboxbp_removeListener( document.body, "click", xboxbp.mouseclick, false );
		xboxbp_removeListener( xboxbp.eleOff, "click", xboxbp.cancel, false );
		xboxbp_removeListener( document.body, "mousemove", xboxbp.mousemove, false );
		xboxbp.eleOverlay.style.display='none';
		xboxbp.eleScope.style.display='none';
		//xboxbp.eleOff.style.display='none';
		xboxbp.eleBoss.style.display='none';
		if( xboxbp.eleEnding ){
			xboxbp.eleEnding.style.display='none';
		}
		var eo = new xboxbp_Effect(xboxbp.SIZE_IMG[1][0],xboxbp.SIZE_IMG[1][1],xboxbp.eleOff,"SLIDEUP","LEFTTOP",-2);
		eo.aftermove = function(){
			this.ele.style.display='none';
			if( xboxbp.timerAreaUpdate ){
				//clearTimeout(xboxbp.timerAreaUpdate);
				xboxbp_Interval.del(xboxbp.timerAreaUpdate);
				xboxbp.timerAreaUpdate = null;
			}
		};
		xboxbp.effectOffDisapear = eo;
		eo.start();
		if( xboxbp.timerBossDisapear ){
			clearTimeout(xboxbp.timerBossDisapear);
			xboxbp.timerBossDisapear = null;
		}
		if( xboxbp.timerShake ){
			clearTimeout(xboxbp.timerShake);
			xboxbp.timerShake = null;
		}
		if( xboxbp.effectEnding ){
			xboxbp.effectEnding.end();
		}
		xboxbp_ZakoEnemyFactory.end();
		xboxbp.revivalObstacles();
		xboxbp.eleStart.style.backgroundImage = 'url('+xboxbp_server+xboxbp_img_on+')';
		xboxbp_addListener( xboxbp.eleStart, "click", xboxbp.start, false );
	},
	revivalObstacles : function(){
		var objs = document.getElementsByTagName('object');
		this.revivalEmbedSelect(objs);
		var eles = document.getElementsByTagName('embed');
		this.revivalEmbedSelect(eles);
		var selects = document.getElementsByTagName('select');
		this.revivalEmbedSelect(selects);
		var iframes = document.getElementsByTagName('iframe');
		this.revivalEmbedSelect(iframes);
		//this.showScrollBars();
	},
	revivalEmbedSelect : function(arr){
		var len = arr.length;
		if( len == 0 ){ return; }
		for( var i=0; i<len; i++ ){
			var ele = arr[i];
			if( ele.getAttribute('rel')=='xboxbpdeleted' ){
				//ele.setAttribute('rel')='';
				//ele.style.display='block';
				ele.style.visibility='visible';
			}
		}
	},
	showScrollBars : function(){
		document.body.style.overflow = this.scrolling;
	},
	hideScrollBars : function(){
		this.scrolling = document.body.style.overflow;
		document.body.style.overflow = 'hidden';
	},
	deleteObstacles : function(){
		var objs = document.getElementsByTagName('object');
		this.deleteEmbedSelect(objs);
		var eles = document.getElementsByTagName('embed');
		this.deleteEmbedSelect(eles);
		var selects = document.getElementsByTagName('select');
		this.deleteEmbedSelect(selects);
		var iframes = document.getElementsByTagName('iframe');
		this.deleteEmbedSelect(iframes);
		//this.hideScrollBars();
	},
	deleteEmbedSelect : function(arr){
		var len = arr.length;
		if( len == 0 ){ return; }
		for( var j=0; j<len; j++ ){
			var ele = arr[j];
			if( ele.style.visibility!='hidden' && (ele.id.search(/xboxbp/)==-1) ){
				ele.setAttribute('rel','xboxbpdeleted');
				ele.style.visibility='hidden';
			}
		}
	},
	// whole page size, including hidden area by scroll bars
	calcScrollSize : function(){
		return [document.body.scrollWidth,document.body.scrollHeight];
	},
	// visible area size, without hidden area by scroll bars
	calcWindowSize : function(){
		var width = (window.innerWidth) ? window.innerWidth :
						(document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
		var height= (window.innerHeight) ? window.innerHeight :
						(document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
		return [width,height];
	},
	getPageSize : function(){
		var a1 = xboxbp.calcScrollSize();
		var a2 = xboxbp.calcWindowSize();
		return new Array(a1[0],a1[1],a2[0],a2[1]);
	},
	getScrollPosition : function(){
		var arr = [];
		arr[0] = document.body.scrollLeft || document.documentElement.scrollLeft;
		arr[1] = document.body.scrollTop || document.documentElement.scrollTop;
		return arr;
	},
	getMouseX: function(e){
		if(window.opera)
			return (e.clientX+document.body.scrollLeft);
		else if(document.all)
			return (document.body.scrollLeft || document.documentElement.scrollLeft)+event.clientX
		else if(document.layers||document.getElementById)
			return e.pageX
	},
	getMouseY: function(e){
		if(window.opera)
			return (e.clientY+document.body.scrollTop);
		else if(document.all)
			return (document.body.scrollTop || document.documentElement.scrollTop)+event.clientY
		else if(document.layers||document.getElementById)
			return e.pageY
	},
	readCookie : function(){
		cookieData = document.cookie;
		if( cookieData.match(/xboxbp=1/) ){
			return 1;
		}else{
			return 0;
		}
	},
	setCookie : function(){
		document.cookie="xboxbp=1;expires=0";
	},
	cleanupSWFs: function(){
		if(window.opera||!document.all){return;}
		var _2d=document.getElementsByTagName("OBJECT");
		for(var i=0;i<_2d.length;i++){
			_2d[i].style.display="none";
			for(var x in _2d[i]){
				if(typeof _2d[i][x]=="function"){
					_2d[i][x]=function(){};
				}
			}
		}
	},
	prepUnload: function(){
		__flash_unloadHandler=function(){};
		__flash_savedUnloadHandler=function(){};
		if(typeof window.onunload=="function"){
			var _30=window.onunload;
			window.onunload=function(){
				xboxbp.cleanupSWFs();
				_30();
			};
		}else if(window.onunload){
			window.onunload=xboxbp.cleanupSWFs;
		}
	},
	shakewindow: function(){
		xboxbp.timerShake = null;
		var shakesize = 20; 
		var shakecount = 3;
		var quart = String.fromCharCode(34);
		var comma = String.fromCharCode(44);
		for (i=0;i<shakecount ;i++){
			eval('top.window.moveBy(0' + comma + 'shakesize);');
			eval('top.window.moveBy(shakesize' + comma + '0);');
			eval('top.window.moveBy(0' + comma + '-shakesize);');
			eval('top.window.moveBy(-shakesize' + comma + '0);');
		}
	}
};

var xboxbp_Effect = function(width,height,ele,effect,targetpos,speed,opt){
	this.initialize(width,height,ele,effect,targetpos,speed,opt);
};
xboxbp_Effect.prototype = {
	size:null,
	pos:null,
	timerMove: null,
	max:0,
	ele:null,
	bd:null,
	effect:"",
	targetpos:"",
	option:null,
	speed:5,
	opacity:0,	// 0-100
	initialize: function(width,height,ele,effect,targetpos,speed,opt){
		this.size = new Array();
		this.size[0] = width;
		this.size[1] = height;
		this.pos = new Array();
		this.ele = ele;
		this.effect = effect;
		this.targetpos = targetpos;
		this.speed = speed;
		this.option = new Object();
		this.option = opt;
	},
	start: function(){
		this.afterstart();
		switch(this.effect){
		case "SLIDEDOWN":
			switch(this.targetpos){
			case "CENTER":
				this.pos[0] = Math.floor((xboxbp.aPageSize[0]-this.size[0])/2);
				this.pos[1] = -this.size[1];
				this.max = Math.floor((xboxbp.aPageSize[1]-this.size[1])/2);
				break;
			case "LEFTTOP":
				this.pos[0] = 0;
				this.pos[1] = -this.size[1];
				this.max = 0;
				break;
			}
			break;
		case "SLIDEUP":
			this.pos[0] = 0;
			this.pos[1] = this.ele.offsetTop-xboxbp.aScroll[1];
			this.max = -this.size[1];
			break;
		case "APPEAR":
		case "DISAPPEAR":
			var init = (typeof(this.option)!="undefined") ?
				((typeof(this.option.initopacity)!="undefined") ? (this.option.initopacity) : 0 ) : 0;
			var max =  (typeof(this.option)!="undefined") ?
				((typeof(this.option.targetopacity)!="undefined")?(this.option.targetopacity):100):100;
			this.setOpacity( init );
			this.max = max;
			this.ele.style.display='block';
			break;
		}
		//this.timerMove = setInterval(this.move.bind(this),50);
		this.timerMove = xboxbp_Interval.set(this.move.bind(this),50);
	},
	isComplete: function(){
		switch(this.effect){
		case "SLIDEDOWN":
			return ( this.pos[1] >= this.max );
		case "SLIDEUP":
			return ( this.pos[1] <= this.max );
		case "APPEAR":
			return ( this.opacity >= this.max );
		case "DISAPPEAR":
			return ( this.opacity <= this.max );
		}
	},
	move: function(){
		switch(this.effect){
		case "SLIDEDOWN":
		case "SLIDEUP":
			this.pos[1] += this.speed;
			var s = this.ele.style;
			if( this.isComplete() ){
				s.top = this.max+xboxbp.aScroll[1]+'px';
				xboxbp_Interval.del(this.timerMove);
				this.timerMove = null;
				this.aftermove();
			}else{
				s.top = this.pos[1]+xboxbp.aScroll[1]+'px';
				s.display='block';
			}
			break;
		case "APPEAR":
		case "DISAPPEAR":
			this.opacity += this.speed;
			var s = this.ele.style;
			if( this.isComplete() ){
				this.setOpacity( this.max );
				xboxbp_Interval.del(this.timerMove);
				this.timerMove = null;
				this.aftermove();
			}else{
				this.setOpacity( this.opacity );
			}
			break;
		}
	},
	afterstart: function(){
		// usually, rewrite this method after instanciation
	},
	aftermove: function(){
		// usually, rewrite this method after instanciation
	},
	beforeend: function(){
		// usually, rewrite this method after instanciation
	},
	end: function(){
		this.beforeend();
		if( this.timerMove ){
			xboxbp_Interval.del(this.timerMove);
			this.timerMove = null;
		}
		this.afterend();
	},
	afterend: function(){
		// usually, rewrite this method after instanciation
	},
	// op: 0-100
	setOpacity: function(op){
		var el = this.ele;
		var o = ( op>=100 ) ? 99.9999 : op;
		el.style.opacity = o/100;
		if( (navigator.userAgent.indexOf('MSIE') != -1) && (!window.opera) ){
			el.style.filter="alpha(opacity=0)";
			el.filters.alpha.Opacity  = o;
		}
	}
};

var xboxbp_ZakoEnemyFactory = {};
xboxbp_ZakoEnemyFactory = {
	zakos: [],
	zakoNums : ((typeof xboxbp_ZakoEnemies == "undefined") ? 4 : parseInt(xboxbp_ZakoEnemies)),
	timer : null,
	initialize: function(){
		this.timer = null;
		for( var i=0; i<this.zakoNums; i++ ){
			if( !this.zakos[i] ){
				var z = new xboxbp_ZakoEnemy(i);
				this.zakos.push( z );
			}else{
				this.zakos[i].initialize(i);
			}
		}
		this.timer = xboxbp_Interval.set(this.move.bind(this),50);
	},
	shoot: function(x,y){
		var len = this.zakoNums;
		var alive = 0;
		for( var i=0; i<len; i++ ){
			alive += this.zakos[i].shoot(x,y);
		}
		if( !alive && this.timer ){
			// stop timer on all dead
			xboxbp_Interval.del( this.timer );
			this.timer = null;
		}
		return alive;
	},
	end: function(){
		var len = this.zakoNums;
		for( var i=0; i<len; i++ ){
			this.zakos[i].end();
		}
		if( this.timer ){
			xboxbp_Interval.del( this.timer );
			this.timer = null;
		}
	},
	move: function(){
		var len = this.zakoNums;
		for( var i=0; i<len; i++ ){
			this.zakos[i].move();
		}
	}
};
var xboxbp_ZakoEnemy = function(index){
	this.initialize(index);
};
xboxbp_ZakoEnemy.prototype = {
	imgSrc: ['img/ene1.gif','img/ene3.gif'],	//,'img/ene2.gif'
	imgSrcDead: 'img/blood1.gif',
	imgSize: [[43,78],[79,42]],	// only ene1,3	//,[51,77]
	imgSizeDead: [320,240],
	speed: [0,0],		//[6,10],
	eleImg: null,
	eleImgDead: null,
	pos: null,
	max: null,
	index:0,
	dir:0,
	living:1,
	hiding:1,
	timerHide:null,
	range:50,
	effectAppear:null,
	shoot: function(x,y){
		var pos = this.pos;
		var range = this.range;
		var imgSize = this.imgSize[this.index%(this.imgSrc.length)];
		var imgSizeDead = this.imgSizeDead;
		if( !this.living ){
			return 0;
		}else if( this.hiding ){
			return 1;
		}else
		if(  ( pos[0]-range+xboxbp.aScroll[0] <= x && x <= pos[0]+imgSize[0]+range+xboxbp.aScroll[0] )
			&& ( pos[1]-range+xboxbp.aScroll[1] <= y && y <= pos[1]+imgSize[1]+range+xboxbp.aScroll[1] )
			&& this.living  )
		{
			// dead
			this.living=0;
			this.eleImg.style.display='none';
			var s = this.eleImgDead.style;
			s.position =		'absolute';
			s.width =				this.imgSizeDead[0]+'px';
			s.height =			this.imgSizeDead[1]+'px';
			var max0 = xboxbp.aPageSize[2]-imgSizeDead[0];
			var max1 = xboxbp.aPageSize[3]-imgSizeDead[1];
			pos[0] = (pos[0]>max0) ? max0 : pos[0];
			pos[1] = (pos[1]>max1) ? max1 : pos[1];
			s.top =			pos[0]+'px';
			s.left =		pos[1]+'px';
			s.zIndex =	4;
			s.left =		pos[0]+parseInt((imgSize[0]-imgSizeDead[0])/2)+xboxbp.aScroll[0]+'px';
			s.top =			pos[1]+parseInt((imgSize[1]-imgSizeDead[1])/2)+xboxbp.aScroll[1]+'px';
			s.display='block';
			if( this.timerHide ){
				clearTimeout( this.timerHide );
				this.timerHide = null;
				this.effectAppear.end();
			}
			return 0;
		}else{
			return 1;
		}
	},
	end: function(){
		if( this.timerHide ){
			clearTimeout( this.timerHide );
			this.timerHide = null;
			this.effectAppear.end();
		}
		this.eleImg.style.display='none';
		this.eleImgDead.style.display='none';
	},
	move: function(){
		var pos = this.pos;
		if( !this.living ){ return; }
		var dir_diff = Math.random()*45;
		var sign = ( parseInt(Math.random()*2)==0 ) ? 1 : (-1);
		var dir = this.dir;
		dir += dir_diff*sign;
		dir = (dir>360) ? dir-360 : dir;
		var speed = this.speed[this.index%(this.imgSrc.length)];
		var offsetx = speed*Math.cos(dir*Math.PI/180);
		var offsety = speed*Math.sin(dir*Math.PI/180);

		if( pos[0]+offsetx > this.max[0] || pos[0]+offsetx < 0 ){
			offsetx = -1*offsetx;
			dir = 180-dir;
			dir = (dir<0) ? dir+360 : dir;
		}
		if( pos[1]+offsety > this.max[1] || pos[1]+offsety < 0 ){
			offsety = -1*offsety;
			dir = 360-dir;
		}
		this.dir = dir;
		pos[0] += parseInt(offsetx);
		pos[1] += parseInt(offsety);
		var s = this.eleImg.style;
		s.left = pos[0]+xboxbp.aScroll[0]+'px';
		s.top = pos[1]+xboxbp.aScroll[1]+'px';
	},
	initialize: function(index){
		if( !this.eleImg ){
			this.index = index;
			bd = document.getElementsByTagName('body').item(0);
			var img = document.createElement('img');
			img.setAttribute('id','zako'+index);
			img.setAttribute('src',xboxbp_server+this.imgSrc[index%(this.imgSrc.length)]);
			img.style.display= 'none';
			bd.appendChild(img);
			this.eleImg = img;

			var img2 = document.createElement('img');
			img2.setAttribute('id','zakoDead'+this.index);
			img2.setAttribute('src',xboxbp_server+this.imgSrcDead);
			img2.style.display = 'none';
			bd.appendChild(img2);
			this.eleImgDead = img2;
		}
		this.eleImgDead.style.display='none';
		var s = this.eleImg.style;
		var imgSize = this.imgSize[this.index%(this.imgSrc.length)];
		s.display =			'none';
		s.position =		'absolute';
		s.width =				imgSize[0]+'px';
		s.height =			imgSize[1]+'px';
		this.max = new Array();
		this.max[0] = xboxbp.aPageSize[2] - imgSize[0];
		this.max[1] = xboxbp.aPageSize[3] - imgSize[1];
		this.pos = new Array();
		this.pos[0] = parseInt(Math.random()*this.max[0]);
		this.pos[1] = parseInt(Math.random()*this.max[1]);
		// start from walls
		/*
		switch(index%4){
		case 0:
			this.pos[0] = 0;
			break;
		case 1:
			this.pos[1] = 0;
			break;
		case 2:
			this.pos[0] = this.max[0];
			break;
		case 3:
			this.pos[1] = this.max[1];
			break;
		}
		*/
		s.top =					this.pos[0]+'px';
		s.left =				this.pos[1]+'px';
		s.zIndex =			4;
		this.dir = Math.random()*360;
		this.living=1;
		this.hiding=1;
		this.effectAppear = new xboxbp_Effect(0,0,this.eleImg,"APPEAR","",xboxbp_speedFade,{initopacity:0,targetopacity:100,zako:this});
		this.effectAppear.afterstart = function(){
			if( this.effect=="APPEAR" ){
				this.option.zako.hiding=0;
				var pos = this.option.zako.pos;
				var max = this.option.zako.max;
				pos[0] = parseInt(Math.random()*max[0]);
				pos[1] = parseInt(Math.random()*max[1]);
			}
		},
		this.effectAppear.aftermove = function(){
			if( this.effect=="APPEAR" ){
				this.effect = "DISAPPEAR";
				this.option.initopacity = 100;
				this.option.targetopacity = 0;
				this.speed = -xboxbp_speedFade;
			}else{
				this.option.zako.hiding=1;
				this.effect = "APPEAR";
				this.option.initopacity = 0;
				this.option.targetopacity = 100;
				this.speed = xboxbp_speedFade;
			}
			var t = parseInt(Math.random()*(xboxbp_timeMaxAppear-xboxbp_timeMinAppear))+xboxbp_timeMinAppear;
			this.option.zako.timerHide = setTimeout(this.start.bind(this),t);
		};
		var t = parseInt(Math.random()*(xboxbp_timeMaxAppear-xboxbp_timeMinAppear))+xboxbp_timeMinAppear;
		this.timerHide = setTimeout(this.effectAppear.start.bind(this.effectAppear),t);
	}
};

var XBoxCounter = function(year,month,day,idheader,classheader){
	this.initialize(year,month,day,idheader,classheader);
};
XBoxCounter.prototype = {
	ele : null,
	target: null,
	classheader:'',
	tid:null,
	lastnumbers:null,
	initialize : function(year,month,day,idheader,classheader){
		this.ele = new Array();
		this.lastnumbers = new Array();
		this.classheader = classheader;
		this.target = new Date(year,month,day);		// 2007年1月1日 : 2007,0,1
		for( var i=0; i<10; i++ ){
			var ele = document.getElementById(idheader+i);
			if( ele ){ this.ele.push(ele); }
		}
		var today = new Date();
		if((this.target.getTime() - today.getTime()) < 0){
			for( var i=0; i<10; i++ ){
				if( this.ele[i] ){
					this.ele[i].className = this.classheader+0;
				}
			}
		}else{
			this.tid = xboxbp_Interval.set(this.interval.bind(this), 500);
		}
	},
	end : function(){
		if( this.tid ){
			xboxbp_Interval.del(this.tid);
			this.tid = null;
		}
	},
	interval : function(){
		var today = new Date();
		var secs = Math.floor((this.target-today)/1000);	// ms->s
		var nums = [];
		for( var i=0; i<10; i++ ){
			var num;
			if( !this.ele[i] ){ break; }
			switch(i){
			case 0:
				num = (secs%60)%10;
				secs -= num;
				break;
			case 1:
				num = (secs%60)%100/10;
				secs -= num*10;
				secs /= 60;	// s->min
				break;
			case 2:
				num = (secs%60)%10;
				secs -= num;
				break;
			case 3:
				num = (secs%60)%100/10;
				secs -= num*10;
				secs /= 60;	// min->hour
				break;
			case 4:
				num = secs%10;
				secs -= num;
				break;
			case 5:
				num = (secs%100)/10;
				secs -= num*10;
				break;
			case 6:
				num = (secs%1000)/100;
				secs -= num*100;
				break;
			case 7:
				num = (secs%10000)/1000;
				secs -= num*1000;
				break;
			}
			secs = (secs < 0) ? 0 : secs;
			if( this.lastnumbers[i] != num ){
				this.ele[i].className = this.classheader+num;
			}
			this.lastnumbers[i] = num;
		}
	}
};

function xboxbp_addListener(element, name, observer, useCapture) {
	if (element.addEventListener) {
		element.addEventListener(name, observer, useCapture);
	} else if (element.attachEvent) {
		element.attachEvent('on' + name, observer);
	}
}
function xboxbp_removeListener( element, name, observer, useCapture ){
	if (element.removeEventListener) {
		element.removeEventListener(name, observer, useCapture);
	} else if (element.detachEvent) {
		element.detachEvent('on' + name, observer);
	}
}
xboxbp_addListener( window, 'load', xboxbp.load, false );

/*
 * below code portions, xboxbp.prepUnload, xboxbp.cleanupSWFs are from SWFObject v1.4.4: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
if(typeof window.onbeforeunload=="function"){
	var oldBeforeUnload=window.onbeforeunload;
	window.onbeforeunload=function(){
		xboxbp.prepUnload();
		oldBeforeUnload();
	};
}else{
	window.onbeforeunload=xboxbp.prepUnload;
}
