var img;
var lang = {
	PresToSizeUp: 'Нажмите, чтобы приблизить',
	PresToSizeOut:'Нажмите, чтобы уменьшить'
}
Effect.DefaultOptions.fps = 100;

var imgLogoPreload = new Image;
imgLogoPreload.src = '/img/logoHover.png';


Event.observe(self, 'load', selfLoad);
function selfLoad(){
	// картинко на главной страничьке
	if( $('userPic') ){
		Event.observe($('userPic'), 'click', userPicSize);
		img = $('userPic').firstDescendant();
		img.isSmall = true;
		img.isMoving = false;
		$('userPic').setAttribute('title', lang.PresToSizeUp);
		img.absolutize();
	}

	// добавляем текст
	if ( $('overToDoForm') )
	{
		var btn = $('overToDoForm').select('[type="button"]')[0];
		btn.observe('click', toDoFormSendData).enable();
		$('toDoText').observe('keypress', function(e){
			if( e.ctrlKey && ((e.keyCode == 0xA)||(e.keyCode == 0xD)) ) toDoFormSendData(e);
		});
	}
}

function userPicSize(event){
	Event.stop(event);
	var duration = 1;

	//if (img.isMoving) return;
	if( img.isMoving )
	{
		var queue = Effect.Queues.get('userPicMove');
		queue.each(function(effect) {
			effect.cancel(); 
			duration = (effect.currentFrame * duration) / effect.totalFrames;
		});
	}

	if(img.isSmall) {
		new Effect.Morph(img, {
		  duration: duration,
		  queue: { position: 'end', scope: 'userPicMove' },
		  style: {
			width: '533px',
			height: '800px'
		  },
		  beforeStart: function(){
			img.isMoving = true;
			img.setAttribute('src', 'img/userPicBig.jpg');
		  },
		  afterUpdate: userPicSizeafterUpdate,
		  afterFinish: function(){
			img.isMoving = false;
			img.setAttribute('src', 'img/userPicBig.jpg');
			$('userPic').setAttribute('title', lang.PresToSizeOut);
		  }
		});
	} else {
		new Effect.Morph(img, { 
		  duration: duration,
		  queue: { position: 'end', scope: 'userPicMove' },
		  style: {
			width: '200px',
			height: '301px'
		  },
	    beforeStart: function(){
			img.isMoving = true;
		  },
	    //afterUpdate: userPicSizeafterUpdate,
		afterFinish: function(){
			img.isMoving = false;
			img.setAttribute('src', 'img/userPic.jpg');
			$('userPic').setAttribute('title', lang.PresToSizeUp);
		  }
		});
	}

	img.isSmall = !img.isSmall;
}

function userPicSizeafterUpdate(effect)
{
	img.scrollTo();
}


// отсылаем данные форма
function toDoFormSendData(e){
	var textElm = $('toDoText');
	var text = $('toDoText').getValue();
	$('errorStd').removeClassName('error');
	if (text.length<3)
	{
		$('errorStd').update("Сообщение слишком короткое:(").show().addClassName('error');
		return;
	}
	$('errorStd').update("Идёт отправка данных...").show().addClassName('info');
	$('overToDoForm').select('[type="button"]')[0].disable();

	var sendData = $H({ toDoText: text }).toJSON();

	new Ajax.Request('/xFase/toDoBox.php',
	{
		method:'post',
		parameters: {formData: sendData},
		onSuccess: function(transport){
			var response = transport.responseText.evalJSON();
			$('toDoTextBox').update( $H(response).get('toDoTextBox') );
			$('overToDoForm').hide();
		}
	});
}