﻿
var defW = 0;
var defH = 0;
var defScx = 0;
var defScy = 0;

var xfg = 0;
var yfg = 0;
var stX = 0;
var edX = 0;
var stY = 0;
var edY = 0;

var targetOBJ = null;

var intervalID = null;
var timerID = null;

function __init(dynname)
{
	dynamicLayerInit();

	targetOBJ = eval(dynname);

	checkWindowReSize();

	timerID = setInterval("moveset(0,0)",10);

	if(is.ie && document.compatMode=="BackCompat"){
		var wt = 500;
		//document.body.onscroll = scrollEvent;

	}else if(is.opera){
		var wt = 500;
		//document.documentElement.onscroll = scrollEvent;

	}else{
		var wt = 100;
		//intervalID = setInterval("scrollEvent()",100);
	}
	intervalID = setInterval("scrollEvent()",wt);

	shadow.alphaSet(20);
}

function checkWindowReSize(scrollfg)
{
	var wi = getInnerSize();

	if(defW != wi[0] || defH != wi[1] || scrollfg){
		defW = wi[0];
		defH = wi[1];
		var cx = wi[2] + wi[0] - Math.round(wi[0]/2);
		var cy = wi[3] + wi[1] - Math.round(wi[1]/2);

		stX = targetOBJ.x + Math.round(targetOBJ.w/2);
		stY = targetOBJ.y + Math.round(targetOBJ.h/2);
		edX = cx;
		edY = cy;

		gx = cx;
		gy = cy;
	}
}

function scrollEvent()
{
	var wi = getInnerSize();
	if(wi[2] != defScx || wi[3] != defScy){
		defScx = wi[2];
		defScy = wi[3];
		checkWindowReSize(true);
	}
}

function moveset()
{
	if(zoomID != null && (is.ie || is.opera))
		return;

	if(Math.abs(stX - edX) > 0.8){
		var p = edX - stX;
		var cx = stX + p/10;
		stX = cx;
	}else{
		xfg ++;
		var cx = edX;
	}
	if(Math.abs(stY - edY) > 0.8){
		var p = edY - stY;
		var cy = stY + p/10;
		stY = cy;
	}else{
		yfg ++;
		var cy = edY;
	}
	if(xfg > 0 && yfg > 0){
		xfg = yfg = 0;
	}else{
		cx -= Math.round(targetOBJ.w/2);
		cy -= Math.round(targetOBJ.h/2);
		targetOBJ.moveTo(cx,cy);
	}
}

window.onresize = checkWindowReSize;




var nowWd = 0;
var nowHi = 0;
var zoomID = null;
var boderSize1 = 10;
var boderSize2 = 30;

function imageLoder(fname)
{
	indicator.hide();
	photo.backImg(fname);
}

var gx = 0;
var gy = 0;

function zoomCts(tw,th,fname)
{
	var fg = 0;
	var e1 = targetOBJ;
	var e2 = base;
	var e3 = photo;
	var e4 = shadow;
	var e5 = indicator;

	if(nowWd != tw){
		var zmw = tw - nowWd;
		var dw = 0;

		if(Math.abs(zmw) < 0.5){
			dw = tw;
			nowWd = tw;
		}else{
			zmw /= 4;
			dw = Math.ceil(nowWd + zmw);
			nowWd += zmw;
		}
		e1.w = dw;
		e1.x = gx - Math.round(dw/2);

		e2.w = dw - (boderSize1*2);
		e2.x = boderSize1;

		e3.w = dw - (boderSize2*2);
		e3.x = boderSize2;

		e4.w = dw;
		e4.x = 0;

		e5.x = Math.round(e1.w / 2) - 8;

	}else{
		fg ++;
	}

	if(nowHi != th){
		var zmh = th - nowHi;
		var dh = 0;
		if(Math.abs(zmh) < 0.5){
			dh = th;
			nowHi = th;
		}else{
			zmh /= 4;
			dh = Math.ceil(nowHi + zmh);
			nowHi += zmh;
		}
		e1.h = dh;
		e1.y = gy - Math.round(dh/2);

		e2.h = dh - (boderSize1*2);
		e2.y = boderSize1;

		e3.h = dh - (boderSize2*2);
		e3.y = boderSize2;

		e4.h = dh;
		e4.y = 0;

		e5.y = Math.round(e1.h / 2) - 8;

	}else{
		fg ++;
	}

	if(fg == 2){
		clearInterval(zoomID);
		zoomID = null;
		imageLoder(fname);
	}else{
		e1.moveTo(e1.x, e1.y);
		e1.resizeTo(e1.w, e1.h);
		e2.moveTo(e2.x, e2.y);
		e2.resizeTo(e2.w, e2.h);
		e3.moveTo(e3.x, e3.y);
		e3.resizeTo(e3.w, e3.h);
		e4.moveTo(e4.x, e4.y);
		e4.resizeTo(e4.w, e4.h);
		e5.moveTo(e5.x, e5.y);
	}
}

function __zoomStart(
	nw,	// 現在の横幅
	nh,	// 現在の縦幅
	tw,	// 目的の横幅
	th,	// 目的の縦幅
	cx,	// 中心Ｘ座標
	cy,	// 中心Ｙ座標
	fname	// 読み込む画像ファイル名
){
	nowWd = nw;
	nowHi = nh;

	tw += Math.max(boderSize1,boderSize2)*2;
	th += Math.max(boderSize1,boderSize2)*2;

	gx = cx;
	gy = cy;

	photo.backImg("none");

	if(fname != "none"){
		indicator.show();
	}
	clearInterval(zoomID);
	zoomID = setInterval("zoomCts("+tw+","+th+",'"+fname+"')",10);
}

function __hideContent()
{

	targetOBJ.hide();
	indicator.hide();
	shadow.hide();
	base.hide();
	photo.hide();
	photo.backImg("none");

	var w = targetOBJ.w;
	var h = targetOBJ.h;
	var x = targetOBJ.x;
	var y = targetOBJ.y;

	__zoomStart(
		w,			// 現在の横幅
		h,			// 現在の縦幅
		60,			// 目的の横幅
		60,			// 目的の縦幅
		x + Math.round(w/2),	// 中心Ｘ座標
		y + Math.round(h/2),	// 中心Ｙ座標
		"none"
	);
}

function __showContent()
{
	targetOBJ.show();
	shadow.show();
	base.show();
	photo.show();

	indicator.hide();
}

function __showCtrlPanel()
{
	
}


