/**
 * @author Egor Hmelyoff (egor@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 */

function Block(obj){
	this.obj = jQuery(obj);
	this.init();
};

Block.prototype.init = function() {
	
	this.free = true;
	this.frames = 7;
	this.parking = false;
	
	/* Состояния вращения */
	this.state = [
		[ 0,-1],
		[ 1, 0],
		[ 0, 1],
		[-1, 0]
	]
	
	/* Направление по умолчанию */
	this.direction = 0;
	
	/* Скрываем все состояния */
	this.obj.find('img').hide();
	
	this.setDirection(0);
	
};

Block.prototype.destroy = function() {
	this.obj.hide("slow");
}

Block.prototype.setDirection = function(i) {
	this.direction=i;
/*	if(this.direction >= 4)
		this.direction = 0
	if(this.direction < 0)
		this.direction = 3*/
		
	this.obj.find('img').hide().end().find('.s'+(this.direction+1)).show();
};

Block.prototype.getDirection = function(i) {
	return this.direction;
};

Block.prototype.setPosition = function(pos) {

	clearInterval(this.idMove);
	
	if(pos){
		this.left = pos.left;
		this.top = pos.top;
	}
	var _my = this;
	var _pos = this.getPosition({ left: this.left, top: this.top })
	this.obj.css({ left: _pos.left, top: _pos.top });
/*	setTimeout(function(){ _my.obj.css({ left: _pos.left }); }, Snake.time/2)*/
};

Block.prototype.setPositionAnimate = function() {
	
	clearInterval(this.idMove);
	
	var _my = this;
	
	var pos = this.getPosition({ left: this.left, top: this.top })
	var _pos = this.getPosition({ left: this._left, top: this._top })
	var _dx = Math.round((pos.left-_pos.left)/this.frames);
	var _dy = Math.round((pos.top-_pos.top)/this.frames);
	
	this.obj.css({ left: _pos.left+_dx, top: _pos.top+_dy });
	
	this.i = 1;
	this.idMove = setInterval(function(){
		_my.i++
		if(_my.i < _my.frames){
			_my.obj.css({ left: _pos.left+_dx*_my.i, top: _pos.top+_dy*_my.i })
		} else {
			clearInterval(_my.idMove);
			_my.obj.css({ left: pos.left, top: pos.top })
		}
	}, Math.floor(Snake.time/this.frames))
	
/*	var _pos = this.getPosition({ left: this.left, top: this.top })
	this.obj.animate({ left: _pos.left, top: _pos.top }, Snake.time-10, 'linear', function(){
		jQuery(this).css({ left: _pos.left, top: _pos.top });
	});*/
};

Block.prototype.clear = function() {
	clearInterval(this.idMove);
}

Block.prototype.move = function() {
	
	this._left = this.left;
	this._top = this.top;
	
	this.left = this.state[this.direction][0]*1 + this.left;
	this.top = this.state[this.direction][1]*1 + this.top;
	
	if(this.left >= Snake.width){
		this.left = 0;
		this._left = -1
	}
	if(this.top >= Snake.height){
		this.top = 0;
		this._top = -1;
	}
	if(this.left < 0){
		this.left = Snake.width-1;
		this._left = Snake.width;
	}
	if(this.top < 0){
		this.top = Snake.height-1;
		this._top = Snake.height;
	}
	
	if($.browser.msie){
		this.setPosition();
	} else {
//		this.setPosition();
		this.setPositionAnimate();
	}

};

Block.prototype.pseudoMove = function(dir) {
	var left = this.state[(dir != undefined) ? dir : this.direction][0]*1 + this.left;
	var top = this.state[(dir != undefined) ? dir : this.direction][1]*1 + this.top;
	if(left >= Snake.width)
		left = 0;
	if(top >= Snake.height)
		top = 0;
	if(left < 0)
		left = Snake.width-1;
	if(top < 0)
		top = Snake.height-1;
	return { left: left, top: top };
}

Block.prototype.moveOut = function() {
	this.left = - this.state[this.direction][0]*1 + this.left;
	this.top = - this.state[this.direction][1]*1 + this.top;
	
	if(this.left >= Snake.width){
		this.left = 0;
		this._left = -1
	}
	if(this.top >= Snake.height){
		this.top = 0;
		this._top = -1;
	}
	if(this.left < 0){
		this.left = Snake.width-1;
		this._left = Snake.width;
	}
	if(this.top < 0){
		this.top = Snake.height-1;
		this._top = Snake.height;
	}
	
	this.setPosition();
};


Block.prototype.getPosition = function(obj) {
	return { left: obj.left*Snake.blockWidth, top: obj.top*Snake.blockHeight }
};

Block.prototype.setMoved = function(){
	this.free = false;
	this.obj.css({ zIndex: 3 });
}



