<?php
//--------------------------------------//
//			Declare Functions			//
//--------------------------------------//


// These functions rely upon several globals being declared
// in the calling script as follows:
//	global $movie ;
//	global $moviewidth;
//	global $movieheight;
//	global $movieframerate;
//
//	global $font ;
//	global $fontsize ;
//
//	global $red ;
//	global $blue ;
//	global $green ;
//

define ( MNG_TRANS_LEFT, 0x00 ) ;
define ( MNG_TRANS_RIGHT, 0x01 ) ;
define ( MNG_TRANS_TOP, 0x02 ) ;
define ( MNG_TRANS_BOTTOM, 0x03 ) ;
define ( MNG_TRANS_DIAG_BL, 0x04 ) ;
define ( MNG_TRANS_DIAG_BR, 0x05 ) ;
define ( MNG_TRANS_DIAG_TL, 0x06 ) ;
define ( MNG_TRANS_DIAG_TR, 0x07 ) ;
 
// easing functions based on those at robertpenner.com
function easeInQuad ($t, $b, $c, $d) {
$t/=$d;
	return $c*$t*$t + $b;
};
function easeOutQuad ($t, $b, $c, $d) {
$t/=$d;
	return -$c *($t)*($t-2) + $b;
};

// basic actionscript control of playback using mouse
$strAction="
if(!init){
	init=true;
	stopped=false;
	controls = {
		onMouseDown: function () {
			if(!stopped){
				stop();
				stopped=true;
			}else{
				play();
				stopped=false;
			}
		}
	};
	Mouse.addListener(controls);
}
";
//$movie->add(new SWFAction($strAction));

function fade_in_pic($file, $duration, $x = 0, $y = 0, $a = 1){
	global $movie ;
	
	$img = new SWFBitmap(fopen($file,"rb"));
	$pic=$movie->add($img); 
	$pic->moveTo( $x, $y );

	$cnt=1; $startpos=0; $offset=1;
	while($cnt <= $duration){
		$inc=$a * easeInQuad ($cnt++, $startpos, $offset, $duration);
		$pic->multColor(1,1,1,$inc);
		$movie->nextFrame();
	}

	return $pic ;
}

function fade_out_pic($pic, $duration){
	global $movie ;
	
	$cnt=1; $startpos=1; $offset=-1;
	while($cnt <= $duration){
		$inc=easeOutQuad ($cnt++, $startpos, $offset, $duration);
		$pic->multColor(1,1,1,$inc);
		$movie->nextFrame();
	}
	$movie->remove($pic);
}

function set_color( $r = 0xdb, $g = 0xe3, $b = 0xf8 ) {
	global $red ;
	global $blue ;
	global $green ; 
	
	$red = $r ;
	$green = $g ;
	$blue = $b ;
}

function set_fontsize( $s = 15 ) {
	global $fontsize ;
	
	$fontsize = $s ;
}

function fade_in($item, $duration, $parent, $a=1){
	
	$cnt=1; $startpos=0; $offset=1;
	while($cnt <= $duration){
		$inc=$a * easeOutQuad ($cnt++, $startpos, $offset, $duration);
		$item->multColor(1,1,1,$inc);
		$parent->nextFrame();
	}
}

function fade_out($item, $duration, $parent){
	
	$cnt=1; $startpos=1; $offset=-1;
	while($cnt <= $duration){
		$inc=easeOutQuad ($cnt++, $startpos, $offset, $duration);
		$item->multColor(1,1,1,$inc);
		$parent->nextFrame();
	}
	$parent->remove($item);
}

function skip_frames($n, $parent=0){
	global $movie ;

	if ($parent == 0) $parent = $movie ;
	for ( $i = 0 ; $i < $n ; $i++ ) {
		$parent->nextFrame();
	}
}

function skip_time($seconds, $parent=0){
	global $movie ;
	global $movieframerate ;

	if ($parent == 0) $parent = $movie ;
	
	$framecount = $seconds * $movieframerate ;
	for ( $i = 0 ; $i < $framecount ; $i++ ) {
		$parent->nextFrame();
	}
}

function add_text( $x, $y, $str, $parent, $url = '' ){
	global $font ;
	global $fontsize ;
	global $red ;
	global $blue ;
	global $green ; 

	$t = new SWFText();
	$t->setFont($font);
	$t->setColor($red, $green, $blue);
	$t->setHeight($fontsize);
	$t->setSpacing(1.0);
	$t->addString($str);
	
	$i = $parent->add($t);
	//$i->moveTo($x, $y + $t->getAscent());
	$i->moveTo($x, $y);

	if ($url <> '') {
	}
	
	return $i ;
}

function move_text( $handle, $off_x, $off_y, $duration, $parent ){

	for ( $i = 0 ; $i < $duration ; $i++ ) {
		$handle->move ( ($off_x/$duration), ($off_y/$duration) ) ;
		$parent->nextFrame();
	}
}

function scroll_lines( $horizontal, $vertical, $duration = 2, $strings ){
	// default duration is 2 seconds. If duration = 0, the text will be left on screen.
	// can cope with 0-5 lines
	global $movie ;
	global $movieheight ;
	global $movieframerate ;

	// Add 3 scrolling lines
	set_fontsize(12);
	$voffset = 20 ;
	$rolling_offset = 0 ;
	$cnt = 0 ;
	
	// Set internal timesteps
	$scrollTime = 20 ;
	$spacing = 4 ;
	
	foreach( $strings as $str ) {
		$spr1 = new SWFSprite();
		$text1 = add_text( $horizontal, $vertical+$rolling_offset+$movieheight, $str, $spr1 );
		move_text( $text1, 0, 0 - $movieheight, $scrollTime, $spr1 ) ;
		$sprite[$cnt++] = $movie->add($spr1);
		skip_frames($spacing);
		$rolling_offset += $voffset ;
	}
	
	skip_frames($scrollTime-($cnt*$spacing));
	
	// Second text is across - replace with static text
	
	$voffset = 20 ;
	$rolling_offset = 0 ;
	$cnt = 0 ;
	foreach( $strings as $str ) {
		$movie->remove($sprite[$cnt]);
		$text[$cnt++] = add_text( $horizontal, $vertical+$rolling_offset, $str, $movie );
		skip_frames($spacing);
		$rolling_offset += $voffset ;
	}

	skip_frames($scrollTime-($cnt*$spacing));

	// Leave on screen for $dur seconds, then remove
	
	$cnt = 0 ;
	if ($duration>0) {
		skip_frames($movieframerate*$duration);
		foreach( $strings as $str ) {
			//$movie->remove($text[$cnt++]);
			fade_out($text[$cnt++], 6, $movie);
		}
		$box=new SWFShape(); 
		$box->setRightFill(255,255,255);
		$box->movePenTo(0,0); 
		$box->drawLine(300,0);  
		$box->drawLine(0,20); 
		$box->drawLine(-300,0); 
		$box->drawLine(0,-20); 
		//$b=$movie->add($box);
		//$b->moveTo(0,$h+48);
		//fade_in($b,3,$movie);
		//$movie->remove($b);
	}
}

function WipeImage($parent, $filename, $seconds=2, $edge=MNG_TRANS_LEFT){
	global $moviewidth;
	global $movieheight;
	global $movieframerate;
	
	// Set direction variables
	// The box is twice screen size so offset accordingly
	switch ($edge) {
		case MNG_TRANS_TOP : 
			$startX = 0 - $moviewidth / 2 ;
			$startY = 0 - 2 * $movieheight ;
			$deltaX = 0 ;
			$deltaY = 1 ;
			break ;
		case MNG_TRANS_BOTTOM : 
			$startX = 0 - $moviewidth / 2 ;
			$startY = $movieheight ;
			$deltaX = 0 ;
			$deltaY = -1 ;
			break ;
		case MNG_TRANS_LEFT : 
			$startX = 0 - 2 * $moviewidth ;
			$startY = 0 - $movieheight / 2 ;
			$deltaX = 1 ;
			$deltaY = 0 ;
			break ;
		case MNG_TRANS_RIGHT : 
			$startX = $moviewidth ;
			$startY = 0 - $movieheight / 2 ;
			$deltaX = -1 ;
			$deltaY = 0 ;
			break ;
	}
	
	// make mask
	$toothShape =new SWFShape();
	$toothShape->setRightFill(0xff, 0x00, 0x00);
	$toothShape->drawLine(2*$moviewidth,0); 
	$toothShape->drawLine(0,2*$movieheight); 
	$toothShape->drawLine(0-2*$moviewidth,0); 
	$toothShape->drawLine(0,0-2*$movieheight); 
	
	$i=$parent->add($toothShape);
	$i->moveTo($startX,$startY); 
	$i->setMaskLevel(255);
	
	$img = new SWFBitmap(fopen($filename,"rb"));
	$pic=$parent->add($img); 

	$framecount = ($movieframerate * $seconds) ;
	for ($y=0;$y<$framecount;$y++){
		$i->move(($moviewidth/$framecount)*$deltaX,($movieheight/$framecount)*$deltaY);
		skip_frames(1);
	}

	$parent->remove($i);
	return ($pic ) ;
}

function DiagWipeImage($parent, $filename, $seconds=2, $edge=MNG_TRANS_LEFT){
	global $moviewidth;
	global $movieheight;
	global $movieframerate;
	
	// Set direction variables
	// The box is twice screen size so offset accordingly
	$toothShape =new SWFShape();
	$toothShape->setRightFill(0xff, 0x00, 0x00);

	switch ($edge) {
		case MNG_TRANS_DIAG_TL : 
			$startX = 0 ;
			$startY = 0 ;
			$deltaX = 0 ;
			$deltaY = 2 ;
			$toothShape->drawLine($moviewidth,0-$movieheight); 
			$toothShape->drawLine(0,0-2*$movieheight); 
			$toothShape->drawLine(0-$moviewidth,0); 
			$toothShape->drawLine(0,3*$movieheight); 
			break ;
		case MNG_TRANS_DIAG_TR : 
			$startX = $moviewidth ;
			$startY = 0 ;
			$deltaX = 0 ;
			$deltaY = 2 ;
			$toothShape->drawLine(0-$moviewidth,0-$movieheight); 
			$toothShape->drawLine(0,0-2*$movieheight); 
			$toothShape->drawLine($moviewidth,0); 
			$toothShape->drawLine(0,3*$movieheight); 
			break ;
		case MNG_TRANS_DIAG_BL : 
			$startX = 0 ;
			$startY = $movieheight ;
			$deltaX = 0 ;
			$deltaY = -2 ;
			$toothShape->drawLine($moviewidth,$movieheight); 
			$toothShape->drawLine(0,2*$movieheight); 
			$toothShape->drawLine(0-$moviewidth,0); 
			$toothShape->drawLine(0,0-3*$movieheight); 
			break ;
		case MNG_TRANS_DIAG_BR : 
			$startX = $moviewidth ;
			$startY = $movieheight ;
			$deltaX = 0 ;
			$deltaY = -2 ;
			$toothShape->drawLine(0-$moviewidth,$movieheight); 
			$toothShape->drawLine(0,2*$movieheight); 
			$toothShape->drawLine($moviewidth,0); 
			$toothShape->drawLine(0,0-3*$movieheight); 
			break ;
	}
	
	// make mask
	
	$i=$parent->add($toothShape);
	$i->moveTo($startX,$startY); 
	$i->setMaskLevel(255);
	
	$img = new SWFBitmap(fopen($filename,"rb"));
	$pic=$parent->add($img); 

	$framecount = ($movieframerate * $seconds) ;
	for ($y=0;$y<$framecount;$y++){
		$i->move(($moviewidth/$framecount)*$deltaX,($movieheight/$framecount)*$deltaY);
		skip_frames(1);
	}

	$parent->remove($i);
	return ($pic ) ;
}

function StripeImage($parent, $filename, $stripes=8, $seconds=2, $edge=MNG_TRANS_LEFT){
	global $moviewidth;
	global $movieheight;
	global $movieframerate;
	
	// Set direction variables and make mask
	$toothShape =new SWFShape();
	$toothShape->setRightFill(0xff, 0x00, 0x00);

	switch ($edge) {
		case MNG_TRANS_TOP : 
			$startX = 0 ;
			$startY = 0 - 3 * $movieheight ;
			$deltaX = 0 ;
			$deltaY = 1 ;
			$toothShape->drawLine(0,$movieheight);  // Always allow a large block following to prevent rounding gaps
			$toothShape->drawLine(0,$movieheight); 
			for ($x=0;$x<$stripes;$x++){
				$toothShape->drawLine(0, $movieheight);
				$toothShape->drawLine($moviewidth / $stripes / 2, 0);
				$toothShape->drawLine(0, 0 - $movieheight);
				$toothShape->drawLine($moviewidth / $stripes / 2, 0);
			}
			$toothShape->drawLine(0,0-$movieheight); 
			$toothShape->drawLine(0,0-$movieheight); 
			$toothShape->drawLine(0-$moviewidth,0); 
			break ;
		case MNG_TRANS_BOTTOM : 
			$startX = 0 ;
			$startY = 4 * $movieheight ;
			$deltaX = 0 ;
			$deltaY = -1 ;
			$toothShape->drawLine(0,0-$movieheight); 
			$toothShape->drawLine(0,0-$movieheight); 
			for ($x=0;$x<$stripes;$x++){
				$toothShape->drawLine(0, 0-$movieheight);
				$toothShape->drawLine($moviewidth / $stripes / 2, 0);
				$toothShape->drawLine(0, $movieheight);
				$toothShape->drawLine($moviewidth / $stripes / 2, 0);
			}
			$toothShape->drawLine(0,$movieheight); 
			$toothShape->drawLine(0,$movieheight); 
			$toothShape->drawLine(0-$moviewidth,0); 
			break ;
		case MNG_TRANS_LEFT : 
			$startX = 0 - 3 * $moviewidth ;
			$startY = 0 ;
			$deltaX = 1 ;
			$deltaY = 0 ;
			$toothShape->drawLine($moviewidth,0); 
			$toothShape->drawLine($moviewidth,0); 
			for ($x=0;$x<$stripes;$x++){
				$toothShape->drawLine($moviewidth, 0);
				$toothShape->drawLine(0, $movieheight / $stripes / 2);
				$toothShape->drawLine(0 - $moviewidth, 0);
				$toothShape->drawLine(0, $movieheight / $stripes / 2);
			}
			$toothShape->drawLine(0-$moviewidth,0); 
			$toothShape->drawLine(0-$moviewidth,0); 
			$toothShape->drawLine(0,0-$movieheight); 
			break ;
		case MNG_TRANS_RIGHT : 
			$startX = 4 * $moviewidth ;
			$startY = 0 ;
			$deltaX = -1 ;
			$deltaY = 0 ;
			$toothShape->drawLine(0-$moviewidth,0); 
			$toothShape->drawLine(0-$moviewidth,0); 
			for ($x=0;$x<$stripes;$x++){
				$toothShape->drawLine(0-$moviewidth, 0);
				$toothShape->drawLine(0, $movieheight / $stripes / 2);
				$toothShape->drawLine($moviewidth, 0);
				$toothShape->drawLine(0, $movieheight / $stripes / 2);
			}
			$toothShape->drawLine($moviewidth,0); 
			$toothShape->drawLine($moviewidth,0); 
			$toothShape->drawLine(0,0-$movieheight); 
			break ;
	}
	
	// move mask
	
	$i=$parent->add($toothShape);
	$i->moveTo($startX, $startY);
	$i->setMaskLevel(255);
	
	$img = new SWFBitmap(fopen($filename,"rb"));
	$pic=$parent->add($img); 

	$framecount = ($movieframerate * $seconds) ;
	for ($y=0;$y<$framecount;$y++){
		$i->move(($moviewidth/$framecount)*$deltaX*2,($movieheight/$framecount)*$deltaY*2);
		skip_frames(1);
	}

	$parent->remove($i);
	return ($pic ) ;
}


?>