
var current_tone;
var cTone = parseInt($.cookie('current_tone')); // а есть ли что-то в куках?

if (isNaN(cTone)) { // похоже, что нет
	current_tone = 5; // default gray tone of site
	$.cookie('current_tone', current_tone, { expires: 14, path: '/'});

} else { // куки не пусты
	current_tone = cTone;
}

$(document).ready(function() {

	if (typeof viewer !== 'undefined'){
		paint(current_tone); // перекрашиваем на странице вьюрера
	} 
	
	$("#dragger").draggable({
		containment:'img#photo',
		cursor:'move'
	}).resizable({
		containment:'img#photo',
		aspectRatio: true,
		handles:"ne, se, sw, nw",
		minWidth:100,
		minHeight:100
	});

	$('#tones').hover(function () {
		$('#tone_choose').show();
	}, function () {
		paint(current_tone);
		$('#tone_choose').hide();
	});
	
	$('#tone_choose a')
		.click(function() {
			regx = new RegExp("[0-9]+");
			num = regx.exec( $(this).attr("class") );
			current_tone = num;
			/* $.cookie('current_tone', num);  */
			$.cookie('current_tone', current_tone, { expires: 14, path: '/'}); // пишем в куки
			paint(num);		
			$("#tone_choose").hide();
		})
		.mouseover(function () {
			var regx = new RegExp("[0-9]+");
			var num = regx.exec( $(this).attr("class") );
			paint(num);		
		});
	
});

var colors = new Array();
// colors:  [ a | p ]
colors[0] = [100, 50];
colors[1] = [100, 50];
colors[2] = [100, 60];
colors[3] = [100, 70];
colors[4] = [100, 80];
colors[5] = [100, 90];
colors[6] = [100, 80];
colors[7] = [100, 70];
colors[8] = [100, 60];
colors[9] = [20, 50];

function getColor(c) {
	var c = Math.round(c*2.55);
	return "rgb(" + c + ", " + c + ", " + c + ")";
}

function paint(tone) {
	// body & paragraphes
	$("body").css("background-color", $(".tone"+tone).css("background-color"));
	// link colors
	$("a").css("color", getColor( colors[tone][0] ));
	// paint text colors
	$("body, p").css("color", getColor( colors[tone][1] ));
   // underlines
   // label "photographer"
	
}

function write_address() {
	var name = '%82%DB%9E%D2%CC%DF%D8%87%9C%D7%DF%D3%D2%CE%D1%80%C8%DF%CA%DF%CC%FA%DB%D1%DF%CE%DB%C8%D7%D4%DF%DD%DF%D6%D5%D3%D0%DB%90%D9%D1%D7%9C%84%C8%DF%CA%DF%CC%FA%DB%D1%DF%CE%DB%C8%D7%D4%DF%DD%DF%D6%D5%D3%D0%DB%90%D9%D1%D7%82%95%DF%84';
	var pattern= 'AE';
	document.write(unescape(decode(name,pattern)));
}


/*
function write_address() {
	var name = '%D3%DB%D7%D6%CA%D5%84%CC%DB%CE%DB%C8%FE%DF%D5%DB%CA%DF%CC%D3%D0%DB%D9%DB%D2%D1%D7%D4%DF%94%DD%D5%D3';
	var pattern= 'AE';
	document.write(unescape('%3C'+'a' + ' hr'+'ef="' + decode(name,pattern) + '"' + '%3E'));
}
*/


/* editor functions */

function _process_thumb(_image_src)
{
	var d_offset = $("#dragger").offset();
	var p_offset = $("#photo_holder").offset();
	var _top = d_offset.top - p_offset.top;
	var _left = d_offset.left - p_offset.left;
	var _size = $("#dragger").width();
	location.href='/editor/process_thumb.html?x='+_left+'&y='+_top+'&w='+_size+'&img='+_image_src;
	return;
}

function initiate_dragger() {
	if (typeof x === "undefined") return;
	$("#dragger").css('left', x);
	$("#dragger").css('top', y);
	$("#dragger").css('width', w);
	$("#dragger").css('height', w);
}