var textarea_default = "Enter your text to show up on the model's sign";
var captions = new Array( "", "Create custom iphone wallpaper<br />by clicking on the image", "Enter your Text", "Your custom Wallpaper" );
var image_current = 0;
rotate( '' );
pop_page( 1 );
//dirtyIt();

/*
 * toggles help display off and on
 *
 */
function toggleHelp()
{
	if ( $( 'help' ).style.display == 'block' )
	{ 
		// turn off the help
		$( 'help' ).style.display = 'none';
	}
	else
	{
		// turn on the help
		$( 'help' ).style.display = 'block';
	}
}

/*
 * easy id retrieval
 *
 * @param  string				id of DOM element object
 *
 * @return  object				DOM element node object
 *
 */
function $( objid )
{
	return document.getElementById( objid );
}

/*
 * set display thumb image
 *
 */
function rotate( direction )
{

	if ( direction == 'left' )
	{
		// decrement
		if ( image_current > 0 ) image_current -= 1;
		
		if ( image_current > 0 ) $( 'arrow_right' ).className = 'arrow_on';
		else $( 'arrow_left' ).className = 'arrow_off';

	}
	else if ( direction == 'right')
	{
		// increment
		if ( image_current < 9 ) image_current += 1;

		if ( image_current < 9 ) $( 'arrow_left' ).className = 'arrow_on';
		else $( 'arrow_right' ).className = 'arrow_off';
	}

	// insert selected image
	$( 'thumbz' ).src = "fsimages/" + thumbs_array[ image_current ];
}

/*
 * open text and image for display
 *
 * @param  integer				what page to show
 *
 * @return  boolean				true 
 *
 */
function pop_page( pageno )
{
	// close all steps
	$( 'step_one' ).style.display = "none";
	$( 'step_two' ).style.display = "none";
	$( 'step_three' ).style.display = "none";

	// unload image
	$( 'fansign' ).src = '';

	// set caption
	$( 'titler' ).innerHTML = captions[ pageno ];
	
	if ( pageno == 1 ) 
	{
		// open step one
		$( 'step_one' ).style.display = "block";

		return true;
	}

	if ( pageno == 2 ) 
	{
		// open step two
		$( 'step_two' ).style.display = "block";

		return true;
	}

	var sel_image = $( "imagelist" );
	var user_text = $( "usertext1" ).value + "{{nl}}" +
	                $( "usertext2" ).value + "{{nl}}" +
					$( "usertext3" ).value;

	var selection = thumbs_array[ image_current ];

/*
	var crlf      = String.fromCharCode(13,10);
	var nwln      = String.fromCharCode(10);
	var cgrt      = String.fromCharCode(13);
	
	var brnwln    = "{{nl}}";
	
	// replace unpassable chars
	while (user_text.indexOf(crlf) > 0)
		user_text = user_text.replace(crlf,brnwln);

	while (user_text.indexOf(nwln) > 0)
		user_text = user_text.replace(nwln,brnwln);

	while (user_text.indexOf(cgrt) > 0)
		user_text = user_text.replace(cgrt,brnwln);
*/	
	// load image
	$( 'fansign' ).src = "fansign_img.php?img=" + selection + "&text=" + user_text;
	
	setTimeout( 'show_img()', 200 );
	
	return true;
}

/*
 * show image after slight delaye
 *
 */
function show_img()
{
	// open step three
	$( 'step_three' ).style.display = "block";

	return true;
}

/*
 * remove default text from textarea
 *
 */
function cleanIt()
{
	if ( $( 'user_text' ).value == textarea_default )
	{
		$( 'user_text' ).value = '';	
		$( 'user_text' ).className = 'user_text_on';
	}
}

/*
 * put default text to textarea
 *
 */
function dirtyIt()
{
	if ( $( 'user_text' ).value == '' )
	{
		$( 'user_text' ).value = textarea_default;	
		$( 'user_text' ).className = 'user_text_off';
	}
	else if ( $( 'user_text' ).value == textarea_default )
	{
		$( 'user_text' ).className = 'user_text_off';
	}
	else
	{
		$( 'user_text' ).className = 'user_text_on';
	}
	
}
