﻿/**
* 무비클립 클래스 생성자
* 
* 작성자 : 홍준수
* container : 이미지가 담길 컨테이너 ( jquery객체 )
* url : 이미지 경로 혹은 이미지 배열
* num : url이 경로일 경우에 갯수지정
* frameDelay : 초당 프레임수 
* callBack : 로딩완료시 호출될 콜백함수
*
*/

function MovieClip ( container , url , num , frameDelay , callBack , num2 )
{
	//debug ( "무비클립 생성자" );
	this.reload ( container , url , num , frameDelay , callBack , num2 );
}

MovieClip.prototype.reload = function( container , url , num , frameDelay , callBack , num2 )
{
	//debug ( "무비클립 이미지 로딩시작");
	this.url = url;
	this.num = num;
	this.num2 = num2;
	

	if ( num2 ) this.num = parseInt ( num / num2 );

	this.container = container;
	this.clear();

	this.currentFrame = 1;
	this.interval = 0;
	this.frameDelay = 1000 / frameDelay;
	this.callBack = callBack;
	this.realFrame = 0;

	this.prevMc = null;
	this.targetFrame;

	this.appendStr = "";

	//debug ( "무비클립 : " + url + " : " + num );

	if ( typeof ( url ) == "string" )
	{
		this.preloadImages();
	}
	else if ( typeof ( url ) == "object" )
	{
		this.preloadImagesWithArray();
	}
}

// public

/**
* 무비클립을 플레이시킵니다.
*/

MovieClip.prototype.play = function()
{
	this.intervalStart();
}

/**
* 무비클립을 정지시킵니다.
*/

MovieClip.prototype.stop = function()
{
	clearInterval ( this.interval );
}

/**
* 지정한 프레임부터 재생시킵니다.
* @param num 프레임
*/

MovieClip.prototype.gotoAndPlay = function ( num )
{
	this.currentFrame = num;
	this.intervalStart();
}

/**
* 지정한 프레임으로 이동후 정지시킵니다.
* @param num 프레임
*/

MovieClip.prototype.gotoAndStop = function( frame )
{
	var prevF = frame;
	
	this.currentFrame = frame;
	

	if ( frame < 1 || frame > this.num )
	{
		frame = ( frame % this.num ) + 1;
	}

	if ( frame < 0 ) frame = this.num + frame;
	if ( parseInt ( frame ) == 0 ) frame = 1;
//	debug ( ":: " + this.currentFrame + " , " + frame );
	
//	this.frameMove();

//	debug ( "원래 : " + prevF + " , 변환 : " + frame );

	this.container.find(".img_"+parseInt(this.prevMc)).css ( "display" , "none" );
	this.container.find(".img_"+parseInt(frame) ).css ( "display" , "block" );
	this.prevMc = parseInt ( frame );
}

/**
*
* 지정한 프레임으로 순차적으로 이동후 정지시킵니다.
* @param num 타겟 프레임
*/

MovieClip.prototype.moveToFrame = function( num , callBack )
{

	this.currentFrame = parseInt ( this.prevMc );
	this.targetFrame = num;
	this.intervalStartMoveToFrame();
	this.moveToFrameCallBack = callBack;

//	debug ( "moveToFrame시작 : " + num + " , f:" + this.currentFrame );
}

MovieClip.prototype.clear = function()
{
	//debug ( "무비클립 클리어" );

	this.callBack = null;
	var img = this.container.find(".img_"+ parseInt(this.currentFrame));

	this.container.find("img").detach().remove();
	this.container.empty();
	clearInterval ( this.interval );

	this.container.append ( img );

}

/**
* 
*/

MovieClip.prototype.next = function()
{
	this.targetFrame = this.num;
	this.intervalStartMoveToFrame();
}

MovieClip.prototype.prev = function()
{
	this.targetFrame =1
	this.intervalStartMoveToFrame();
}

MovieClip.prototype.frameTween = function( num )
{
}

// private

MovieClip.prototype.preloadImages = function()
{
	var str = "";
	var imageAr = [ ];

	var plusNum = 1;
	if ( this.num2 ) 
	{
		plusNum = this.num2;
//		debug ( "plusNum : " + plusNum );
	}

	//debug ( this.num + " , " + plusNum );


	var count = 1;

	for ( var i = 1 ; i <= this.num ; i++ )
	{
		var imageURL;
		if ( count < 1000 ) imageURL = this.url + "0" + count + ".png";
		if ( count < 100 ) imageURL = this.url + "00" + count + ".png";
		if ( count < 10 ) imageURL = this.url + "000" + count + ".png";

		imageAr.push ( imageURL );
		count += plusNum;
		str += "<img src='" + imageURL + "' class='img_" + i + "' style='display:none' />";
	}

	this.appendStr = str;

	if ( $.browser.version == "6.0" )
	{

	}
	else
	{
		this.container.empty();
		this.container.append ( this.appendStr );
	}

	//this.loadComplete();

	var iphone = false;

	if ( iphone )
	{
		this.loadComplete();
	}
	else
	{
		$.imgpreload( imageAr , { all:$.proxy ( this.loadComplete , this ) } );
	}


	imageAr = null;
}

MovieClip.prototype.preloadImagesWithArray = function()
{
	var str = "";
	for ( var i = 0 ; i <= this.url.length ; i++ )
	{
		str += "<img src='" + this.url[i] + "' class='img_" + (i+1) + "' style='display:none' />";
	}

	if ( iphone)
	{
		this.loadComplete();
	}
	else
	{
		$.imgpreload( this.url , { all:$.proxy ( this , "loadComplete" ) } );
	}

	imageAr = null;
	this.container.empty();
	this.container.append ( str );
	this.num = this.url.length;
}

MovieClip.prototype.loadComplete = function()
{
	if ( $.browser.version == "6.0" )
	{
		this.container.empty();
		this.container.append ( this.appendStr );
	}

	$("body").animate ( { top:0 } , { duration:500 , complete:$.proxy ( this , "loadComplete2" ) } );

}

MovieClip.prototype.loadComplete2 = function()
{
	if ( this.callBack ) this.callBack.apply ( null , [this] );
}

MovieClip.prototype.intervalStartMoveToFrame = function()
{
	clearInterval ( this.interval );
	this.interval = setInterval (  $.proxy ( this , "frameMoveToFrame" ) , this.frameDelay );
}

MovieClip.prototype.intervalStart = function()
{
	clearInterval ( this.interval );
	this.interval = setInterval (  $.proxy ( this , "frameMove" ) , this.frameDelay );
}

MovieClip.prototype.intervalStop = function()
{
	clearInterval ( this.interval );
}

MovieClip.prototype.frameMoveToFrame = function()
{
	this.container.find(".img_"+this.prevMc).css ( "display" , "none" );
	this.container.find(".img_"+this.currentFrame).css ( "display" , "block" );
	this.prevMc = this.currentFrame;

//	debug ( this.currentFrame + ' , ' + this.targetFrame );

	if ( this.currentFrame > this.targetFrame )
	{
		this.currentFrame --;
	}
	else if ( this.currentFrame < this.targetFrame )
	{
		this.currentFrame ++;
	}
	else if ( this.currentFrame == this.targetFrame )
	{
		this.intervalStop();
		if ( this.moveToFrameCallBack ) 
		{
			this.moveToFrameCallBack();
			this.moveToFrameCallBack = null;
		}
	}
}

MovieClip.prototype.frameMove = function()
{
	this.container.find(".img_"+this.prevMc).css ( "display" , "none" );
	this.container.find(".img_"+this.currentFrame).css ( "display" , "block" );
	this.prevMc = this.currentFrame;
	
	this.currentFrame++;
	if ( this.currentFrame > this.num )
	{
		this.currentFrame = 1;
	}
}
