// thumbnails of the images that are available
var available_images         = new Array();

var section_index            = document.location.search.substring(1);
if (section_index == "" || section_index == 'undefined') {
	section_index = 0;
} else {
	section_index = parseInt(section_index);
}

var buttons_titles           = new Array("left", "up", "right", "down", "-",       "+");
var buttons_names            = new Array("left", "up", "right", "down", "zoom_out", "zoom_in");
var i;
for (i = 0; i < buttons_titles.length; i++) {
	eval("var " + buttons_names[i] + "_enabled      = new Image();");
	eval(         buttons_names[i] + "_enabled.src  = '../img/button.img?text=\"" + buttons_titles[i] + "\"&width=60'");

	eval("var " + buttons_names[i] + "_disabled      = new Image();");
	eval(         buttons_names[i] + "_disabled.src  = '../img/button.img?text=\"" + buttons_titles[i] + "\"&width=60&fg_r=150&fg_g=150&fg_b=150'");
}


var VERTICAL_POSITIONS            = new Array();
VERTICAL_POSITIONS['']            = 0;
VERTICAL_POSITIONS['top']         = 0;
VERTICAL_POSITIONS['bottom']      = 1;
VERTICAL_POSITIONS['numeric']     = 2;

var HORIZONTAL_POSITIONS          = new Array();
HORIZONTAL_POSITIONS['']          = 0;
HORIZONTAL_POSITIONS['left']      = 0;
HORIZONTAL_POSITIONS['right']     = 1;
HORIZONTAL_POSITIONS['numeric']   = 2;


var loading_source           = new Image();
	loading_source.src       = 'Images/loading.gif';

available_images[0]            = new Array();
available_images[0][0]         = new AvailableImage('kids/guy.gif', 'image');

var IMAGES_DIRECTORY         = '../Images/';

var SCREEN_IMAGE_NAME        = 'screen';
var HIDDEN_IMAGE_NAME        = 'hidden_image';

var current_image_index      = 0;

var loading                  = true;
var generated                = false;








function GetSelectedValue(obj)
{
	var index  = obj.selectedIndex;
	var value  = obj.options[index].value; 
	return value;
}

function GotoPage()
{
	var obj    = document.forms[0].section;
	var value  = GetSelectedValue(obj);

	var loc    = "" + document.location;
	if (loc.indexOf("?") == -1) {
		document.location = loc + "?" + value;
	} else {
		loc = loc.substring(0, loc.indexOf("?")) + "?" + value;
		document.location = loc;
	}
}



function AvailableImage(source, alt_text)
{
	this.src       = source;
	this.alt       = alt_text;
}


function UpdateImage()
{
	loading = true;

	var img_src = available_images[section_index][current_image_index].src;
	var zoom_source = '../img/zoom.img?image_path="' + img_src + '"&depth=' + depth + '&center_x=' + center_x + '&center_y=' + center_y;

	document.images[HIDDEN_IMAGE_NAME].src = zoom_source;
	document.images[SCREEN_IMAGE_NAME].src = loading_source.src;
}



function Loaded(i)
{
	if (i > 1) {
		document.images[SCREEN_IMAGE_NAME].src = document.images[HIDDEN_IMAGE_NAME].src;
		loading = false;
		return;
	} else {
		i++;
		window.setTimeout("Loaded(" + i + ")", 100);
	}
}


function ResetImage()
{
	generated = false;
}


function SetButtonEnabled(button_name, enabled)
{
	if (document.images[button_name])
		document.images[button_name].src = eval(button_name + (enabled ? "_enabled" : "_disabled") + ".src");
}


function ChooseImage()
{
	Popup('all_images.htm', 200, 500, true);
}

function ViewResult()
{
	var form                    = document.forms[0];
	var image_path              = form.screen_source.value;

	if (image_path.indexOf('space.gif') != -1) {
		alert('Please choose some image');
		return;
	}

	var vertical_pos            = GetSelectedValue(form.text_vertical_pos);
	var horizontal_pos          = GetSelectedValue(form.text_horizontal_pos);
	var numeric_vertical_pos    = form.text_numeric_vertical_pos.value;
	var numeric_horizontal_pos  = form.text_numeric_horizontal_pos.value;
	var font                    = GetSelectedValue(form.font);
	var size_of_font            = form.font_size.value;
	var color                   = form.font_color.value;
	var fg_r                    = GetRed(color);
	var fg_g                    = GetGreen(color);
	var fg_b                    = GetBlue(color);
	var text                    = form.text.value;
	var antialias               = form.antialias.checked ? 1 : 0;
	var left                    = 0;
	var top                     = 0;

	if (size_of_font.search(/^\d+$/) == -1) {
		alert("Invalid font size value");
		return;
	}

	text = text.replace(/\\/g, "\\\\");
	text = text.replace(/\"/g, "\\\"");
	text = text.replace(/\'/g, "\\'");
	text = text.replace(/\r/g, "");
	text = text.replace(/\n/g, "\\n");

	text = escape(text);

	if (vertical_pos == 'numeric') {
		top = parseInt(numeric_vertical_pos);
		if (numeric_vertical_pos == '' || numeric_vertical_pos == 'underined') {
			alert('Please enter value for `Text Vertical Position`');
			form.text_numeric_vertical_pos.focus();
			return;
		}
		if (numeric_vertical_pos.search(/^\d+$/) == -1) {
			alert("Invalid Text Vertical Position value");
			return;
		}
	}

	vertical_pos   = VERTICAL_POSITIONS[vertical_pos];

	if (horizontal_pos == 'numeric') {
		left = parseInt(numeric_horizontal_pos);
		if (numeric_horizontal_pos == '' || numeric_horizontal_pos == 'underined') {
			alert('Please enter value for `Text Horizontal Position`');
			form.text_numeric_horizontal_pos.focus();
			return;
		}
		if (numeric_horizontal_pos.search(/^\d+$/) == -1) {
			alert("Invalid Text Horizontal Position value");
			return;
		}
	}

	horizontal_pos = HORIZONTAL_POSITIONS[horizontal_pos];

	var generated_image = '../img/cards.img?image_path="' + image_path + '"&vertical_pos=' + vertical_pos + '&horizontal_pos=' + horizontal_pos + '&font="' + font + '"&size_of_font=' + size_of_font + '&fg_r=' + fg_r + '&fg_g=' + fg_g + '&fg_b=' + fg_b + '&text="' + text + '"&antialias=' + antialias + '&left=' + left + '&top=' + top;
	document.images[HIDDEN_IMAGE_NAME].src = generated_image;
	generated = true;
	if (document.images[SCREEN_IMAGE_NAME].src != document.images[HIDDEN_IMAGE_NAME].src) {
		document.images[SCREEN_IMAGE_NAME].src = loading_source.src;
	}

}


function CheckForNumeric(obj, obj_to_set_enabled)
{
	var value = GetSelectedValue(obj);

	if (value == 'numeric') {
		obj_to_set_enabled.focus();
	}
		
}


function CheckNumeric(obj_name)
{
	eval('document.forms[0].' + obj_name + '.value  = "numeric"');
}