var ie = (function(){
	var undef, v = 3, div = document.createElement('div');
	while (
		div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
		div.getElementsByTagName('i')[0]
	);
	return v> 4 ? v : undef;
}());
var callJS;
(function(){

	callJS = function(funcname,args)
	{
		eval(funcname + "(" + String(args) + ");");
	};

	// a.swf
	var a;
	// a.swfコンテナ
	var contentA;
	// top.swf
	var t;
	// top.swfコンテナ
	var contentTop;
	// ドラッグ用オフセット位置
	var ofsA={x: 0, y:0};

	// body
	var $body;
	// document
	var $doc;
	// マウス座標
	var mouse={x: 0, y: 0};

	/**
	 * flashオブジェクト取得
	 */
	function getMovie(moviename)
	{
		if (navigator.appName.indexOf("Microsoft") != -1 && ie != 9) {
			return window[moviename];
		}
		else {
			return document[moviename];
		}
	}

	/**
	 * Aドラッグ開始
	 */
	function startDragA(x,y)
	{
		ofsA.x = x;
		ofsA.y = y;
		$doc.bind("mousemove",dragA);
		$doc.bind("mouseup",stopDragA);
	}

	/**
	 * Aドラッグ停止
	 */
	function stopDragA(e)
	{
		var contentTopPos = contentTop.position();
		// Aセット
		var ret = t.trySetA(e.pageX - ofsA.x - contentTopPos.left + 50, e.pageY - ofsA.y - contentTopPos.top + 50);

		if (ret) {
			// A移動停止
			clearInterval(moveTimerID);

			a.setMotion(1);
			// 隠す
			contentA.css("display","none");
		}
		else {
			a.setMotion(2);
		}

		$doc.unbind("mousemove",dragA);
		$doc.unbind("mouseup",stopDragA);
	}

	/**
	 * Aドラッグ
	 */
	function dragA(e)
	{
		var x = e.pageX - ofsA.x;
		var y = e.pageY - ofsA.y;

		if (0 <= x && x <= $doc.width() - contentA.width()) {
			contentA.css("left", x + "px");
		}
		if (0 <= y && y <= $doc.height() - contentA.height()) {
			contentA.css("top" , y + "px");
		}
	}

	/**
	 * A飛び出し
	 */
	function accidentA()
	{
		var contentTopPos = contentTop.position();
		contentA.css("left",contentTopPos.left + 787);
		contentA.css("top", contentTopPos.top  + 218);

		a.setMotion(3);
		a.setAspect(false);

		// 飛び出しアニメ
		accidentTimerID = setInterval(accidentAanime,50);
	}
	
	/**
	 * A飛び出しアニメ
	 */
	var accidentSp={x: 45, y:-30};
	var g=5;
	var accidentTimerID;
	function accidentAanime()
	{
		var contentTopPos = contentTop.position();
		var contentAPos   = contentA.position();
		var bodyw = $body.width();

		// 壁
		if (bodyw < contentAPos.left + contentA.width()) {
			contentA.css("left", (bodyw - contentA.width()) + "px");
			// 方向転換
			a.setAspect(true);
			accidentSp.x *= -0.3;
			accidentSp.y = -10;
		}
		// 停止
		else if (contentTopPos.top + 280 < contentAPos.top) {
			a.setMotion(2);
			clearInterval(accidentTimerID);

			// マウス追従移動開始
			setTimeout(function(){
				moveTimerID = setInterval(moveA,50);
			}
			,100);
		}
		else {
			// 移動
			contentA.css("left", (contentAPos.left + accidentSp.x) + "px");
			contentA.css("top",  (contentAPos.top  + accidentSp.y) + "px");

			// 加速
			accidentSp.y += g;
		}
	}

	/**
	 * A移動
	 */
	var moveSp=1;
	var constantSp=2;
	var moveTimerID;
	function moveA()
	{
		var contentAPos   = contentA.position();
		var bodyw = $doc.width();
		var bodyh = $doc.height();

		// Aとマウス座標の距離
		var disX = mouse.x - (contentAPos.left + contentA.width() / 2);
		var disY = mouse.y - (contentAPos.top + contentA.height() / 2);
		var dis = Math.sqrt(Math.pow(disX,2) + Math.pow(disY,2));

		if (dis < 30) {
			return;
		}

//		var sp = dis / (3000 / 50) + moveSp;

		// Aとマウス座標の角度
		var rad = Math.atan2(disY,disX);

		// 移動方向分解
		var spx = constantSp * Math.cos(rad);
		var spy = constantSp * Math.sin(rad);
//		var spx = sp * Math.cos(rad);
//		var spy = sp * Math.sin(rad);

		var tmpx = contentAPos.left - spx;
		var tmpy = contentAPos.top  - spy;

		// A方向セット
		a.setAspect(spx >= 0);

		// X軸左移動
		if (spx >= 0) {
			// 左端でない場合
			if (0 < tmpx) {
				contentA.css("left", tmpx + "px");
			}
		}
		// X軸右移動
		else {
			// 右端でない場合
			if (bodyw > tmpx + contentA.width()) {
				contentA.css("left", tmpx + "px");
			}
		}

		// Y軸上移動
		if (spy >= 0) {
			// 上端でない場合
			if (0 < tmpy) {
				contentA.css("top", tmpy + "px");
			}
		}
		// Y軸下移動
		else {
			// 下端でない場合
			if (bodyh > tmpy + contentA.height()) {
				contentA.css("top", tmpy + "px");
			}
		}
	}

	//***************************************
	// onLoad Area
	//***************************************
	$(function(){

		a = getMovie("aSWF");
		contentA = $("#flashContentA");
		t = getMovie("topSWF");
		contentTop = $("#flashContentTop");
		$body = $("body");
		$doc  = $(document);

		$body.bind("mousemove",function(e)
		{
			mouse = {x: e.pageX, y: e.pageY};
		});
	});

}());


