function download_icon()
{

	delay='';
	frames='';
	for(var item in delays) 
		{
		var value = delays[item];
		delay+=","+value;
		}
	for(var item in animation_matrix) 
		{
		var value = animation_matrix[item];
		frames+=","+value;
		}
	$('#frame_count').val(frames);
	$('#iteration_count').val(delay);
	document.getElementById('download_icon').setAttribute('action','/create_icon/');
	$('#download_icon').submit();
	
}

function publish_icon()
{

	delay='';
	frames='';
	for(var item in delays) 
		{
		var value = delays[item];
		delay+=","+value;
		}
	for(var item in animation_matrix) 
		{
		var value = animation_matrix[item];
		frames+=","+value;
		}
	$('#frame_count').val(frames);
	$('#iteration_count').val(delay);
	document.getElementById('download_icon').setAttribute('action','/publish_icon/');
	$('#download_icon').submit();
	}


var USE_ANIMATION=1;
var CURSOR_PATH="/images/favico/cursors";
var MAX_FRAMES=100;
var ICON_COLUMNS=16;
var ICON_ROWS=16;
var DEFAULT_DELAY_SECS=1;
var DELAY_LIST=[5,10,20,30,40,50,60,70,80,90,100,150,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000];
var LAST_COLOR_FIELDS=6;
var CANVAS_REDRAW_INTERVAL=2000;
var large_box;
var small_box;
var context;
var favicon_matrix;
var mover_started;
var start_mover_row;
var start_mover_column;
var last_mover_row;
var last_mover_column;
var moved_image_changed;
var canvas_supported;
var last_used_colors;
var current_frame;
var animation_matrix;
var delays;
var iterations;
var frame_count;
var animated_frame;

function initialize_ordinary_page()
	{
	}

function initialize_create_page()
	{
	initialize_last_colors();
	initialize_color_picker();
	initialize_mover();
	current_frame=1;
	initialize_boxes();
	initialize_animation();
	if(USE_ANIMATION){frame_duration_changed();
	
	if(frame_count>1){toggle_animation_controls(1)}}
	if(jQuery("#animate_preview")[0]!='undefined' && jQuery("#animate_preview")[0]!=undefined){
		if(typeof (jQuery("#favicon_canvas")[0].getContext)=="function"){
			canvas_supported=1;
			context=jQuery("#favicon_canvas")[0].getContext("2d")
		}
	}
	write_hidden_fields_to_tables();
	jQuery("#big_icon").bind("mousedown",mouse_down_on_icon);
	
	if(canvas_supported){copy_to_page_favicon()}
	}

function initialize_mover()
	{
	mover_started=0;
	start_mover_row=0;
	start_mover_column=0;
	last_mover_row=0;
	last_mover_column=0;
	moved_image_changed=1;
	favicon_matrix=[];
	for(var A=1;
	A<=ICON_ROWS;
	A++){favicon_matrix[A]=[]}
	}

function initialize_animation()
	{
	animation_matrix=[];
	delays=[];
	
	if(jQuery("#frame_count")){frame_count=jQuery("#frame_count").val();
	for(var E=1;
	E<=frame_count;
	E++){animation_matrix[E]=[];
	for(var D=1;
	D<=ICON_ROWS;
	D++){animation_matrix[E][D]=[];
	var C="#"+get_pixel_field_name(D,E);
	
	if(jQuery(C)[0]){var B=jQuery(C)[0].value;
	for(var A=1;
	A<=ICON_COLUMNS;
	A++){
	if(B.match(/^trans/)){B=B.replace(/^trans/,"");
	animation_matrix[E][D][A]="trans"}else{
	if(B.match(/^[\dA-F]{6}/i)){animation_matrix[E][D][A]=B.match(/^[\dA-F]{6}/i);
	B=B.replace(/^[\dA-F]{6}/i,"")}else{alert("parsing error: "+B)}}}}else{for(var A=1;
	A<=ICON_COLUMNS;
	A++){animation_matrix[E][D][A]="trans"}}}
	if(jQuery("#delays-f"+E).val()){delays[E]=get_closest(jQuery("#delays-f"+E).val())}else{delays[E]=100*DEFAULT_DELAY_SECS}}}else{initialize_frame(1)}
	if(USE_ANIMATION){display_animation_controls();
	initialize_small_animation()}
	}

function initialize_boxes()
	{
	large_box=[];
	small_box=[];
	for(var B=1;
	B<=ICON_ROWS;
	B++){large_box[B]=[];
	small_box[B]=[];
	for(var A=1;
	A<=ICON_COLUMNS;
	A++){large_box[B][A]=jQuery("#big-r"+B+"c"+A);
	small_box[B][A]=jQuery("#small-r"+B+"c"+A)}}
	}

function initialize_frame(C)
	{
	animation_matrix[C]=[];
	for(var B=1;
	B<=ICON_ROWS;
	B++){animation_matrix[C][B]=[];
	for(var A=1;
	A<=ICON_COLUMNS;
	A++){animation_matrix[C][B][A]="trans"}}delays[C]=100;
	frame_count=C
	}

function initialize_last_colors()
	{
	last_used_colors=[];
	for(var A=0;
	A<LAST_COLOR_FIELDS+1;
	A++){last_used_colors[A]="trans"}
	}

function mouse_down_on_icon(A)
	{
	if(A.which!=1){return }jQuery("#big_icon").bind("mousemove",mouse_move_on_icon);
	jQuery("#page_body").bind("mousemove",mouse_move_on_body);
	jQuery("#big_icon").bind("mouseup",mouse_up_on_icon);
	change_color(A);
	return(false)
	}

function mouse_up_on_icon(A)
	{

	mover_started=0;
	jQuery("#big_icon").unbind("mousemove",mouse_move_on_icon);
	jQuery("#page_body").unbind("mousemove",mouse_move_on_body);
	return(false)
	}

function mouse_move_on_icon(A)
	{

	change_color(A);
	return(false)
	}

function mouse_move_on_body(A)
	{

	mover_started=0;
	jQuery("#big_icon").unbind("mousemove",mouse_move_on_icon);
	jQuery("#page_body").unbind("mousemove",mouse_move_on_body);
	return(false)
	}

function change_color(B)
	{
	var C=parseInt((B.pageY-jQuery("#big_icon").offset().top)/20,10)+1;
	var A=parseInt((B.pageX-jQuery("#big_icon").offset().left)/22,10)+1;
	
	if((C<1)||(A<1)||(C>ICON_ROWS)||(A>ICON_COLUMNS)){return }
	if(jQuery("#pipette")[0].checked){process_pipette_clicked_on_table(C,A)}else{
	if(jQuery("#transparency")[0].checked){process_erasor_clicked_on_table(C,A)}else{
	if(jQuery("#mover")[0].checked){process_mover_clicked_on_table(C,A)}else{process_pen_clicked_on_table(C,A)}}}
	}

function process_mover_clicked_on_table(E,B)
	{
	
	if(!mover_started){mover_started=1;
	
	if(moved_image_changed){start_mover_row=E;
	start_mover_column=B;
	for(var D=1;
	D<=ICON_ROWS;
	D++){for(var A=1;
	A<=ICON_COLUMNS;
	A++){favicon_matrix[D][A]=animation_matrix[current_frame][D][A]}}moved_image_changed=0}else{start_mover_row=start_mover_row-last_mover_row+E;
	start_mover_column=start_mover_column-last_mover_column+B}}else{
	if((last_mover_row!=E)||(last_mover_column!=B)){for(var D=1;
	D<=ICON_ROWS;
	D++){for(var A=1;
	A<=ICON_COLUMNS;
	A++){var C=D+start_mover_row-E;
	var F=A+start_mover_column-B;
	
	if((C>0)&&(C<=ICON_ROWS)&&(F>0)&&(F<=ICON_COLUMNS)){colorize_field(D,A,favicon_matrix[C][F])}else{colorize_field(D,A,"trans")}}}}}last_mover_row=E;
	last_mover_column=B
	}

function process_pen_clicked_on_table(C,B)
	{
	moved_image_changed=1;
	var A=jQuery("#color_hex_string").text();
	colorize_field(C,B,A);
	last_used_colors[0]=A
	}

function process_erasor_clicked_on_table(B,A)
	{
	moved_image_changed=1;
	colorize_field(B,A,"trans");
	last_used_colors[0]="trans"
	}

function colorize_field(C,B,A)
	{
	colorize_field_in_icons(C,B,A);
	if(animation_matrix[current_frame]!='undefined' && animation_matrix[current_frame]!=undefined){
		if(animation_matrix[current_frame][C]!='undefined' && animation_matrix[current_frame][C]!=undefined){
			if(animation_matrix[current_frame][C][B]!='undefined' && animation_matrix[current_frame][C][B]!=undefined){
				animation_matrix[current_frame][C][B]=A
			}
		}
	}
	}

function colorize_field_in_icons(C,B,A)
	{
	
	if(A=="trans"){large_box[C][B].css("backgroundImage",'url("/images/favico/cell_background.png")');
	if((frame_count==1)||(!jQuery("#animate_preview")[0].checked)){
		small_box[C][B].css("backgroundColor","#FFFFFF")
	}
	if(canvas_supported){
		context.clearRect(B-1,C-1,1,1)}
	}else{
		if(large_box[C]!='undefined' && large_box[C]!=undefined){
			if(large_box[C][B]!='undefined' && large_box[C][B]!=undefined){
				large_box[C][B].css("backgroundColor","#"+A);
				large_box[C][B].css("backgroundImage","");
				if((frame_count==1)||(!jQuery("#animate_preview")[0].checked)){
					small_box[C][B].css("backgroundColor","#"+A)
				}
				if(canvas_supported){
					context.fillStyle="#"+A;
					context.fillRect(B-1,C-1,1,1)}
				}
			}
		}
	}

function copy_to_page_favicon()
	{
	var B=jQuery("#page_favicon")[0];
	var A=B.cloneNode(true);
	A.setAttribute("href",context.canvas.toDataURL());
	B.parentNode.replaceChild(A,B);
	window.setTimeout(copy_to_page_favicon,CANVAS_REDRAW_INTERVAL)
	}

function process_pipette_clicked_on_table(C,B)
	{
	moved_image_changed=1;
	if(large_box[C][B].css("backgroundImage")!="none"){jQuery("#pipette")[0].checked=0;
	jQuery("#transparency")[0].checked=1}else{jQuery("#pipette")[0].checked=0;
	jQuery("#transparency")[0].checked=0;
	var A=animation_matrix[current_frame][C][B]+"";
	set_hex_color(A)}update_after_checkbox_change()
	}

function get_pixel_field_name(B,A)
	{
	return("points-r"+B+"f"+A)
	}

function get_x_coordinate(B)
	{
	var A=0;
	if(B.pageX){A=B.pageX}else{
	if(B.clientX){A=B.clientX+document.body.scrollLeft+document.documentElement.scrollLeft}}return(A)
	}

function get_y_coordinate(A)
	{
	var B=0;
	if(A.pageY){B=A.pageY}else{
	if(A.clientY){B=A.clientY+document.body.scrollTop+document.documentElement.scrollTop}}return(B)
	}

function erasor_checked()
	{
	if(jQuery("#transparency")[0].checked){jQuery("#pipette")[0].checked="";
	jQuery("#mover")[0].checked=""}update_after_checkbox_change();
	change_last_used_colors()
	}

function erasor_tool_clicked()
	{
	if(jQuery("#transparency")[0].checked){jQuery("#transparency")[0].checked=""}else{jQuery("#transparency")[0].checked=1}erasor_checked()
	}

function pipette_checked()
	{
	if(jQuery("#pipette")[0].checked){jQuery("#transparency")[0].checked="";
	jQuery("#mover")[0].checked=""}update_after_checkbox_change()
	}

function pipette_tool_clicked()
	{
	if(jQuery("#pipette")[0].checked){jQuery("#pipette")[0].checked=""}else{jQuery("#pipette")[0].checked=1}pipette_checked()
	}

function mover_tool_clicked()
	{
	if(jQuery("#mover")[0].checked){jQuery("#mover")[0].checked=""}else{jQuery("#mover")[0].checked=1}mover_checked()
	}

function mover_checked()
	{
	if(jQuery("#mover")[0].checked){jQuery("#pipette")[0].checked="";
	jQuery("#transparency")[0].checked=""}update_after_checkbox_change()
	}

function pen_selected()
	{
	jQuery("#pipette")[0].checked="";
	jQuery("#mover")[0].checked="";
	jQuery("#transparency")[0].checked="";
	update_after_checkbox_change()
	}

function update_after_checkbox_change()
	{
	if(jQuery("#pipette")[0].checked){jQuery("#big_icon").css("cursor",'url("'+CURSOR_PATH+'/pipette.cur"), pointer')}else{
	if(jQuery("#mover")[0].checked){jQuery("#big_icon").css("cursor",'url("'+CURSOR_PATH+'/mover.cur"), pointer')}else{
	if(jQuery("#transparency")[0].checked){jQuery("#big_icon").css("cursor",'url("'+CURSOR_PATH+'/eraser.cur"), pointer')}else{jQuery("#big_icon").css("cursor",'url("'+CURSOR_PATH+'/pen.cur"), pointer')}}}
	if(jQuery("#transparency")[0].checked){jQuery("#big_current_color_box").css("backgroundImage",'url("/images/favico/cell_background.png")')}else{jQuery("#big_current_color_box").css("backgroundImage","")}
	}

function next_frame()
	{
	if(current_frame<MAX_FRAMES){current_frame++}
	if(current_frame>frame_count){current_frame=1}write_hidden_fields_to_tables()
	}

function previous_frame()
	{
	current_frame--;
	
	if(current_frame<1){
	if(jQuery("#iterations").val()!=1){current_frame=frame_count}else{current_frame=1}}write_hidden_fields_to_tables()
	}

function write_hidden_fields_to_tables()
	{
	jQuery("#frame_no").text(current_frame);
	jQuery("#frame_count_display").text(frame_count);
	display_animation_controls();
	for(var C=1;
	C<=ICON_ROWS;
	C++){for(var B=1;
	B<=ICON_COLUMNS;
	B++){var A=animation_matrix[current_frame][C][B];
	colorize_field_in_icons(C,B,A)}}
	}

function write_hidden_fields()
	{
	jQuery("#hidden_fields").empty();
	append_hidden_field("hidden_fields","frame_count",frame_count);
	
	if(USE_ANIMATION){append_hidden_field("hidden_fields","iter_count",jQuery("#iterations").val())}else{append_hidden_field("hidden_fields","iter_count",1)}for(var D=1;
	D<=frame_count;
	D++){for(var C=1;
	C<=ICON_ROWS;
	C++){var B="";
	for(var A=1;
	A<=ICON_COLUMNS;
	A++){B+=animation_matrix[D][C][A]}append_hidden_field("hidden_fields",get_pixel_field_name(C,D),B)}append_hidden_field("hidden_fields","delays-f"+D,delays[D])}
	}

function append_hidden_field(A,B,C)
	{
	jQuery("#"+A).append('<input type="hidden" name="'+B+'" value="'+C+'">')
	}

function download_cursor()
	{
	jQuery("#action_flag").val("download_cursor");
	write_hidden_fields();
	jQuery("#download_icon").submit()
	}

function frame_duration_changed()
	{
	delays[current_frame]=jQuery("#frame_duration option:selected").val();
	return(true)
	}

function display_animation_controls()
	{
	if(frame_count>current_frame){jQuery("#animation_next_text").text("Следующий");
	jQuery("#animation_next_arrow").css("display","inline");
	jQuery("#loop_next").css("display","none")}else{
	if(frame_count==current_frame){
	if((jQuery("#iterations").val()!=1)&&(frame_count!=1)){jQuery("#animation_next_text").text("Первый");
	jQuery("#animation_next_arrow").css("display","inline");
	jQuery("#loop_next").css("display","inline")}else{jQuery("#animation_next_arrow").css("display","none")}}}
	if(current_frame>1){jQuery("#animation_previous_text").text("Предыдущий");
	jQuery("#animation_previous_arrow").css("display","inline");
	jQuery("#loop_previous").css("display","none")}else{
	if((jQuery("#iterations").val()==1)||(frame_count==1)){jQuery("#animation_previous_arrow").css("display","none")}else{jQuery("#animation_previous_text").text("Последний");
	jQuery("#animation_previous_arrow").css("display","inline");
	}}jQuery("#frame_duration").val(delays[current_frame]);
	jQuery("#frame_count_display").text(frame_count);
	jQuery("#frame_no").text(current_frame)
	}

function toggle_animation_controls(A)
	{
	
	
	if(A)
	{
	
	jQuery("#animation_frame").slideDown("slow",
	function()
	{
	jQuery("#animation_link_show").hide();
	jQuery("#animation_link_hide").show()});
	display_animation_controls()}else{
	if(frame_count>1){
	if(confirm("Do you really want to delete all frames except this one?")){
	if(current_frame>1){animation_matrix.splice(1,current_frame-1)}
	if(frame_count>current_frame){animation_matrix.splice(2)}frame_count=1;
	current_frame=1}else{return }}jQuery("#animation_frame").slideUp("slow",
		function()
		{
		jQuery("#animation_link_hide").hide();
		jQuery("#animation_link_show").show()})}return 
		}
	
function copy_previous_frame()
	{
	if(current_frame>1){for(var B=1;
	B<=ICON_ROWS;
	B++){for(var A=1;
	A<=ICON_COLUMNS;
	A++){colorize_field(B,A,animation_matrix[current_frame-1][B][A])}}}
	}

function clear_frame()
	{
	for(var B=1;
	B<=ICON_ROWS;
	B++){for(var A=1;
	A<=ICON_COLUMNS;
	A++){colorize_field(B,A,"trans")}}
	}

function append_new_frame()
	{
	frame_count++;
	initialize_frame(frame_count);
	delays[frame_count]=delays[current_frame];
	current_frame=frame_count;
	clear_frame();
	display_animation_controls()
	}

function delete_frame()
	{
	
	if(frame_count<2){clear_frame();
	return }
	if(confirm("Do you really want to delete this frame?")){frame_count--;
	animation_matrix.splice(current_frame,1);
	
	if(frame_count<current_frame){current_frame=frame_count}for(var B=1;
	B<=ICON_ROWS;
	B++){for(var A=1;
	A<=ICON_COLUMNS;
	A++){colorize_field_in_icons(B,A,animation_matrix[current_frame][B][A])}}display_animation_controls()}
	}

function get_closest(A)
	{
	
	var B=DELAY_LIST[0];
	for(var C=0;
	C<DELAY_LIST.length;
	C++){
	if(A>=DELAY_LIST[C]){B=DELAY_LIST[C]}else{
	if(A<DELAY_LIST[C]){break}}}return(B)
	}

function update_small_animation()
	{
	if(jQuery("#animate_preview")[0]!='undefined' && jQuery("#animate_preview")[0]!=undefined){
		if(jQuery("#animate_preview")[0].checked){
			if(animated_frame>frame_count){
				animated_frame=1
			}
		for(var C=1;C<=ICON_ROWS;C++){
			for(var B=1;B<=ICON_COLUMNS;B++){
				var A=animation_matrix[animated_frame][C][B];
				if(A=="trans"){
					small_box[C][B].css("backgroundColor","#FFFFFF")
				}else{
					small_box[C][B].css("backgroundColor","#"+A)
				}
			}
		}
	}
	animated_frame++;
	window.setTimeout(update_small_animation,delays[animated_frame-1]*10)}
	}

function initialize_small_animation()
	{
	animated_frame=1;
	update_small_animation()
	}

function animate_preview_checked()
	{
	
	if(jQuery("#animate_preview")[0].checked){animated_frame=1;
	update_small_animation()}else{write_hidden_fields_to_tables()}
	}

function change_last_used_colors()
	{
	for(var A=1;
	A<LAST_COLOR_FIELDS+1;
	A++){
	if(last_used_colors[0]==last_used_colors[A]){return }}for(var B=LAST_COLOR_FIELDS+1;
	B>0;
	B--){last_used_colors[B]=last_used_colors[B-1]}for(var B=0;
	B<LAST_COLOR_FIELDS;
	B++){
	if(last_used_colors[B+1]=="trans"){jQuery("#current_color_box_"+B).css("backgroundImage",'url("/images/favico/cell_background.png")')}else{jQuery("#current_color_box_"+B).css("backgroundImage","");
	jQuery("#current_color_box_"+B).css("background","#"+last_used_colors[B+1])}}
	}

function last_color_clicked(A)
	{
	if(last_used_colors[A+1]=="trans"){jQuery("#transparency")[0].checked=1}else{jQuery("#transparency")[0].checked=0;
	set_hex_color(last_used_colors[A+1])}update_after_checkbox_change()
	}

function post_comment()
	{
	jQuery("#comments_form").show("slow");
	jQuery("#comment_link").hide();
	jQuery("#comments_textarea").focus()
	};

