var maxSizeX = 100;	// �ִ� ũ��
var minSizeX = 10;	// �ּ� ũ��
var curSizeX = 100;	// ���� ũ��
var maxSizeY = 80;	// �ִ� ũ��
var minSizeY = 8;	// �ּ� ũ��
var curSizeY = 80;	// ���� ũ��

var unitSizeX = 5;	// �̵� ����
var unitSizeY = 4;	// �̵� ����

var curX = 0;	// ���� ��ġ X
var curY = 0;	// ���� ��ġ Y

var prevzoomX = 0;
var prevzoomY = 0;
var prevzoomSizeX = 100;	// ���� ũ��

var onLayer = false;


function ImageChange()
{
  eval("document.getElementById('remote').style.background = 'url(/resources/img/"+curSizeX+"_"+curSizeY+".jpg)';");
  document.getElementById('remote').style.backgroundRepeat = "no-repeat";
}

function movepic(img_name, img_src)
{
  document[img_name].src = img_src;
}


function MoveUp()
{
	if( curY < unitSizeY )	// �� �̻� �ö� �� ����
		return;

	curY = curY - unitSizeY;
	document.getElementById('remote').style.position = "relative";
	document.getElementById('remote').style.top = curY+"px";;
}

function MoveDown()
{
	if( curY + curSizeY == maxSizeY)	// �� �̻� ������ �� ����
		return;

	curY = curY + unitSizeY;
	if(curY + curSizeY > maxSizeY){
	    curY = maxSizeY - curSizeY;
	}
	document.getElementById('remote').style.position = "relative";
	document.getElementById('remote').style.top = curY+"px";;
}

function MoveLeft()
{
	if( curX < unitSizeX )	// �� �̻� �������� �̵��� �� ����
		return;

	curX = curX - unitSizeX;
	document.getElementById('remote').style.position = "relative";
	document.getElementById('remote').style.left = curX+"px";;
}

function MoveRight()
{
	if( curX + curSizeX == maxSizeX)	// �� �̻� ���������� �̵��� �� ����
		return;

	curX = curX + unitSizeX;
	document.getElementById('remote').style.position = "relative";
	document.getElementById('remote').style.left = curX+"px";;
}

function Move( moveX, moveY )
{
	curX = moveX;
	curY = moveY;

	if ( curX + curSizeX > maxSizeX )
		curX = maxSizeX - curSizeX;

	if ( curY + curSizeY > maxSizeY )
		curY = maxSizeY - curSizeY;

	document.getElementById('remote').style.position = "relative";
	document.getElementById('remote').style.top = curY+"px";;
	document.getElementById('remote').style.left = curX+"px";;
}

function ZoomIn()
{
	if( curSizeX <= minSizeX )	// �� �̻� �۾��� �� ����
		return;
	
	curSizeX = curSizeX - ( unitSizeX * 2 );
	curSizeY = curSizeY - ( unitSizeY * 2 );
	document.getElementById('remote').style.width = curSizeX+"px";
	document.getElementById('remote').style.height = curSizeY+"px";

	MoveDown();
	MoveRight();
	SetDigitalZoom(0);
	ImageChange();
}

function ZoomOut()
{
	if( curSizeX >= maxSizeX )	// ���̻� Ŀ�� �� ����
		return;
	
	curSizeX = curSizeX + ( unitSizeX * 2 );
	curSizeY = curSizeY + ( unitSizeY * 2 );
	document.getElementById('remote').style.width = curSizeX+"px";
	document.getElementById('remote').style.height = curSizeY+"px";
	
	MoveUp();
	MoveLeft();

	if( curY + curSizeY > maxSizeY )
		MoveUp();

	if( curX + curSizeX > maxSizeX )
		MoveLeft();
	SetDigitalZoom(0);
	ImageChange();
}

function ZoomHome()
{
	curSizeX = maxSizeX;
	curSizeY = maxSizeY;
	curX = 0;
	curY = 0;
	document.getElementById('remote').style.width = curSizeX+"px";
	document.getElementById('remote').style.height = curSizeY+"px";
	document.getElementById('remote').style.position = "relative";
	document.getElementById('remote').style.top = curY+"px";;
	document.getElementById('remote').style.left = curX+"px";;
	
	SetDigitalZoom(0);
	ImageChange();
}

function ButtonUp()
{
    MoveUp();
	SetDigitalZoom(0);
}

function ButtonDown()
{
    MoveDown();
	SetDigitalZoom(0);
}

function ButtonLeft()
{
    MoveLeft();
	SetDigitalZoom(0);
}

function ButtonRight()
{
    MoveRight();
	SetDigitalZoom(0);
}

function ButtonLeftUp()
{
    MoveLeft();
    MoveUp();
	SetDigitalZoom(0);
}
function ButtonRightUp()
{
    MoveRight();
    MoveUp();
	SetDigitalZoom(0);
}
function ButtonLeftDown()
{
    MoveLeft();
    MoveDown();
	SetDigitalZoom(0);
}
function ButtonRightDown()
{
    MoveRight();
    MoveDown();
	SetDigitalZoom(0);
}

function RedrawDzoom()
{
	SetDigitalZoom(1);
}

function SetDigitalZoom(force)
{
    var ratio = 0;
    var relativeposX = 0;
    var relativeposY = 0;
    if((prevzoomX != curX) || (prevzoomY != curY) || (prevzoomSizeX != curSizeX) || (force == 1)){
        prevzoomX = curX;
        prevzoomY = curY;
        prevzoomSizeX = curSizeX;
        
        if(trnPlayer == PLAYER_ACTIVEX){
            // ���⼭ tvs control
    //    	infoStr = "curSize : " + curSizeX + " / curX : " + curX + " / curY : " + curY;
    //	    document.iForm.result.value = document.iForm.result.value + infoStr + "\n";
            relativeposX = Math.round((tvs_get_width()) * curX / 100);
            relativeposY = Math.round((tvs_get_height()) * curY / 80);
    //        alert("relativeposX = " + relativeposX + "relativeposY = " + relativeposY + "curSizeX = " + curSizeX);
            if(curSizeX == 100){
                tvs_digitalzoom(100, relativeposX, relativeposY);
            }
            else if(curSizeX == 90){
                tvs_digitalzoom(111, relativeposX, relativeposY);
            }
            else if(curSizeX == 80){
                tvs_digitalzoom(125, relativeposX, relativeposY);
            }
            else if(curSizeX == 70){
                tvs_digitalzoom(143, relativeposX, relativeposY);
            }
            else if(curSizeX == 60){
                tvs_digitalzoom(167, relativeposX, relativeposY);
            }
            else if(curSizeX == 50){
                tvs_digitalzoom(200, relativeposX, relativeposY);
            }
            else if(curSizeX == 40){
                tvs_digitalzoom(250, relativeposX, relativeposY);
            }
            else if(curSizeX == 30){
                tvs_digitalzoom(333, relativeposX, relativeposY);
            }
            else if(curSizeX == 20){
                tvs_digitalzoom(500, relativeposX, relativeposY);
            }
            else if(curSizeX == 10){
                tvs_digitalzoom(1000, relativeposX, relativeposY);
            }
        }
        else if(trnPlayer == PLAYER_CHROMEPLUGIN){
            var size = VXGMP_GetsizePlayer('vxgplayer1');

            relativeposX = Math.round((size.width) * curX / 100);
            relativeposY = Math.round((size.height) * curY / 80);
    //        alert("relativeposX = " + relativeposX + "relativeposY = " + relativeposY + "curSizeX = " + curSizeX);
            if(curSizeX == 100){
                VXGMP_CustomDigitalZoom('vxgplayer1', 100, relativeposX, relativeposY);
            }
            else if(curSizeX == 90){
                VXGMP_CustomDigitalZoom('vxgplayer1', 111, relativeposX, relativeposY);
            }
            else if(curSizeX == 80){
                VXGMP_CustomDigitalZoom('vxgplayer1', 125, relativeposX, relativeposY);
            }
            else if(curSizeX == 70){
                VXGMP_CustomDigitalZoom('vxgplayer1', 143, relativeposX, relativeposY);
            }
            else if(curSizeX == 60){
                VXGMP_CustomDigitalZoom('vxgplayer1', 167, relativeposX, relativeposY);
            }
            else if(curSizeX == 50){
                VXGMP_CustomDigitalZoom('vxgplayer1', 200, relativeposX, relativeposY);
            }
            else if(curSizeX == 40){
                VXGMP_CustomDigitalZoom('vxgplayer1', 250, relativeposX, relativeposY);
            }
            else if(curSizeX == 30){
                VXGMP_CustomDigitalZoom('vxgplayer1', 333, relativeposX, relativeposY);
            }
            else if(curSizeX == 20){
                VXGMP_CustomDigitalZoom('vxgplayer1', 500, relativeposX, relativeposY);
            }
            else if(curSizeX == 10){
                VXGMP_CustomDigitalZoom('vxgplayer1', 1000, relativeposX, relativeposY);
            }
        }
        else if(trnPlayer == PLAYER_WEBSOCKET){
            var size = VXGWSMP_GetsizePlayer('vxgwsplayer1');

            relativeposX = Math.round((size.width) * curX / 100);
            relativeposY = Math.round((size.height) * curY / 80);
    //        alert("relativeposX = " + relativeposX + "relativeposY = " + relativeposY + "curSizeX = " + curSizeX);
            if(curSizeX == 100){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 100, relativeposX, relativeposY);
            }
            else if(curSizeX == 90){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 111, relativeposX, relativeposY);
            }
            else if(curSizeX == 80){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 125, relativeposX, relativeposY);
            }
            else if(curSizeX == 70){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 143, relativeposX, relativeposY);
            }
            else if(curSizeX == 60){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 167, relativeposX, relativeposY);
            }
            else if(curSizeX == 50){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 200, relativeposX, relativeposY);
            }
            else if(curSizeX == 40){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 250, relativeposX, relativeposY);
            }
            else if(curSizeX == 30){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 333, relativeposX, relativeposY);
            }
            else if(curSizeX == 20){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 500, relativeposX, relativeposY);
            }
            else if(curSizeX == 10){
                VXGWSMP_CustomDigitalZoom('vxgwsplayer1', 1000, relativeposX, relativeposY);
            }
        }
	}
}

function onLayerSet( val )
{
	onLayer = val;
}

function clickLayer()
{
	clickX = window.event.offsetX;	// Ŭ���� ��ġ ( ���� ���̾� �»�� ���� ��� ��ǥ )
	clickY = window.event.offsetY;	// Ŭ���� ��ġ ( ���� ���̾� �»�� ���� ��� ��ǥ )
	clickMove ( clickX + curX, clickY + curY );
	SetDigitalZoom(0);
}

function clickTable()
{
	if( onLayer )
		return;
	
	clickX = window.event.offsetX;	// Ŭ���� ��ġ ( 100x100 ���̺� �»�� ���� ��� ��ǥ )
	clickY = window.event.offsetY;	// Ŭ���� ��ġ ( 100x100 ���̺� �»�� ���� ��� ��ǥ )
	clickMove ( clickX, clickY );
	SetDigitalZoom(0);
}

function clickMove( x, y )
{
	clickX = x;	// Ŭ���� ��ġ ( 100x100 ���̺� �»�� ���� ��� ��ǥ )
	clickY = y;	// Ŭ���� ��ġ ( 100x100 ���̺� �»�� ���� ��� ��ǥ )

	clickX = clickX - ( clickX % unitSizeX );
	clickY = clickY - ( clickY % unitSizeY );

	clickX = clickX - ( curSizeX / 2 );
	clickY = clickY - ( curSizeY / 2 );

	if( clickX < 0 )
		clickX = 0;
	if( clickY < 0 )
		clickY = 0;
	Move( clickX, clickY );
}
