var aBlocks=[];
var oEraser;

function TextScanner(){
	var oSpan=document.getElementsByTagName('span');
	var _l=oSpan.length - 1;
	var _block, _span, _pos;
	
	do{
		_span=oSpan[_l];
		if(_span.className && _span.className == 'erase'){
			_pos=getAbsolutePos(_span);
			_pos.w=_span.offsetWidth;
			_pos.h=_span.offsetHeight;
			_block=new EraserBlock(_pos);
			if(window.opera)
				_span.insertBefore(_block.oPtr, _span.firstChild);
			else
				_span.appendChild(_block.oPtr);
			aBlocks.push(_block);
			_span.style.position='relative';
		}
	}while(_l--)
}

function ReposErases(){
	for(var i=0; i<aBlocks.length; i++){
		aBlocks[i].Repos();
	}
}

function SetEraseMode(evt, iMode){
	if(!oEraser)
		return;
	
	if((evt=checkEvent(evt))){
		if(iMode){
			oEraser.Show();
			document.getElementById('NapkinLay').style.visibility='hidden';
			document.getElementById('NapkinPart').style.display='none';
			_move(evt);
			addEvent(document, 'mousemove', _move);
			addEvent(window, 'resize', ReposErases);
			idEraser=setInterval('_e()', 20);
		}
		else{
			oEraser.Hide();
			document.getElementById('NapkinLay').style.visibility='visible';
			removeEvent(document, 'mousemove', _move);
			clearInterval(idEraser);
			if(!aBlocks.length)
				ShowErasedPage();
		}
	}
}

function EraseAll(){
	var l=aBlocks.length-1;
	if(l){
		do{
			aBlocks[l].Destroy();
			aBlocks.rem(l);
		}while(l--);
	}
	clearInterval(idEraser);
	ShowErasedPage();
}

function InitEraserPage(){
	var sPageErased=getCookie('page_erased');
	if(!sPageErased){
		CreateEraser();
		setCookie('page_erased', 1, new Date(2030, 0, 1), '/');
	}
	else{
		ShowErasedPage();
	}
}

function ShowErasedPage(){
	addClass(document.getElementById('number2'), 'hidden');
	removeClass(document.getElementById('PageErased'), 'hidden');
	addClass(document.getElementById('PageNotErased'), 'hidden');
	document.getElementById('NapkinLay').style.visibility='visible';
	document.getElementById('NapkinLay').className='';
	oEraser=null;
}

function ShowNotErasedPage(){
	removeClass(document.getElementById('number2'), 'hidden');
	addClass(document.getElementById('PageErased'), 'hidden');
	removeClass(document.getElementById('PageNotErased'), 'hidden');
	document.getElementById('NapkinLay').style.visibility='hidden';
	document.getElementById('NapkinPart').style.display='block';
}

function OpenAnotherPack(){
	ShowNotErasedPage();
	CreateEraser();
}

function CreateEraser(){
	PackPart.Init();
	PackTop.Init();
	if(!oEraser)
		oEraser=new Eraser();
	TextScanner();
	document.getElementById('NapkinLay').className='can-click';
}

var Point=function(x, y){
	this.x=x;
	this.y=y;
}

var Shape=function(x, y, width, height){
	this.x=x;
	this.y=y;
	this.w=width;
	this.h=height;
}

/* ------------------------------------ */

var Eraser=function(){
	this.aInitShape=[
		new Point(2,80),
		new Point(77,1),
		new Point(175,61),
		new Point(167,74),
		new Point(176,85),
		new Point(111,151),
		new Point(85,136),
		new Point(67,155)
	];
	
	this.bAutoPilot=false;
	
	this.oPtr=document.getElementById('Eraser');
	this.w=this.oPtr.offsetWidth;
	this.h=this.oPtr.offsetHeight;
	
	this.oBBox={x:0, y:0, w:this.oPtr.offsetWidth, h: this.oPtr.offsetHeight};
	
	this.SaveShape(0,0);
}

Eraser.prototype.SaveShape=function(x, y){
	this.oShape=[];
	var _p;
	for(var i=0; i<this.aInitShape.length; i++)
		this.oShape.push(new Point(this.aInitShape[i].x+x, this.aInitShape[i].y+y));
}

Eraser.prototype.Move=function(evt){
	if((evt=checkEvent(evt))){
		this.MoveTo(evt.pageX, evt.pageY);
	}
}

Eraser.prototype.MoveTo=function(_x, _y){
	_x-=74;
	_y-=77;
	
	this.oPtr.style.left=_x+'px';
	this.oPtr.style.top=_y+'px';
	
	this.oBBox.x=_x;
	this.oBBox.y=_y;
	
	this.SaveShape(_x, _y);
	
	if(this.bAutoPilot)
		_e_auto();
}

Eraser.prototype.Show=function(){
	this.oPtr.style.visibility='visible';
}

Eraser.prototype.Hide=function(){
	this.oPtr.style.visibility='hidden';
}

Eraser.prototype.InitEaseIn=function(oPnt, f){
	this.tX=oPnt.x/100 * document.body.offsetWidth;
	this.tY=oPnt.y;
	this.f=f;
	this.cX=this.oPtr.offsetLeft;
	this.cY=this.oPtr.offsetTop;
	this.onAnimate=this.EaseIn;
}

Eraser.prototype.EaseIn=function(){
	this.cX=(this.tX-this.cX)*this.f + this.cX;
	this.cY=(this.tY-this.cY)*this.f + this.cY;
	
	if(Math.abs(this.tY-this.cY) < 2 && Math.abs(this.tX-this.cX) < 2){
		this.cX=this.tX;
		this.cY=this.tY;
		this.onAnimationOver();
	}
	
	this.MoveTo(this.cX, this.cY);
}

Eraser.prototype.AutoPilot=function(){
	if(this.idTimer)
		clearInterval(this.idTimer);
	this.iApStage=0;
	this.bAutoPilot=true;
	this.MoveTo(.16 * document.body.offsetWidth, 388);
	this.Show();
	this.onAnimationOver=function(){
		this.SetStage();
	}
	this.SetStage();
	this.idTimer=setInterval('EraserAutoPilot()', 30);
}

Eraser.prototype.SetStage=function(){
	switch(this.iApStage){
		case 0:
			this.InitEaseIn(new Point(85,383), .3);
			break;
		case 1:
			this.InitEaseIn(new Point(33,435), .3);
			break;
		case 2:
			this.InitEaseIn(new Point(80,540), .3);
			break;
		case 3:
			this.InitEaseIn(new Point(32,635), .3);
			break;
		case 4:
			this.InitEaseIn(new Point(85,836), .3);
			break;
		default:
			clearInterval(this.idTimer);
			this.bAutoPilot=false;
			this.Hide();
			this.MoveTo(10,10);
			EraseAll();
			
	}
	this.iApStage++;
}

Eraser.prototype.onAnimte=function(){
	return true;
}

function EraserAutoPilot(){
	oEraser.onAnimate();
}

function _move(evt){
	oEraser.Move(evt);
}

function _e(){
	var l=aBlocks.length-1;
	if(l == -1){
		clearInterval(idEraser);
		//~ ShowErasedPage();
		return;
	}
	do{
		if(IsOverlap(aBlocks[l], oEraser)){
			aBlocks[l].Erase(oEraser);
		}
		if(aBlocks[l].CanDestroy()){
			aBlocks[l].Destroy();
			aBlocks.rem(l);
		}
	}while(l--);
}

function _e_auto(){
	var l=aBlocks.length-1;
	if(l == -1){
		return;
	}
	do{
		if(IsOverlap(aBlocks[l], oEraser)){
			aBlocks[l].Destroy();
			aBlocks.rem(l);
		}
	}while(l--);
}

var idEraser;

/* ------------------------------------ */

var EraserBlock=function(oShape){
	this.oPtr=document.createElement('div');
	this.oPtr.className='e';
	this.oPtr.style.width=oShape.w+'px';
	this.oPtr.style.height=oShape.h+'px';
	
	this.oCont=document.createElement('div');
	this.oCont.className='cc';
	this.oPtr.appendChild(this.oCont);
	
	this.oShape=oShape;
	this.bTesselated=false;
	this.aCells=[];
	
	this.Tesselate();
}

EraserBlock.prototype.iYThreshold=135;

EraserBlock.prototype.CanDestroy=function(){
	return (!this.bTesselated || this.aCells.length) ? false : true;
}

EraserBlock.prototype.Destroy=function(){
	this.oPtr.parentNode.removeChild(this.oPtr);
}

EraserBlock.prototype.Repos=function(){
	var oPoint=getAbsolutePos(this.oPtr.parentNode);
	
	for(var i=0; i<this.aCells.length; i++){
		this.aCells[i].Repos(oPoint);
	}
	
	this.oShape.x=oPoint.x;
	this.oShape.y=oPoint.y;
}

EraserBlock.prototype.Tesselate=function(){
	this.aCells=[];
	var _shape=new Shape(0, 0, this.oShape.w, 1);
	for(var i=0; i<this.oShape.h; i++, _shape.y=i){
		this.AddCell(_shape, this.oShape);
	}
	this.bTesselated=true;
}

EraserBlock.prototype.AddCell=function(oCellShape, oParentShape){
	var _cell=new EraserCell(oCellShape, oParentShape);
	this.aCells.push(_cell);
	this.oCont.appendChild(_cell.oPtr);
}

EraserBlock.prototype.Erase=function(oEraserPtr){
	//~ oEraserPtr -- ��������� �� ��������, ����� �������� �� ���������� � �������
	var _shape=oEraserPtr.oShape;
	var _parent_x=this.oShape.x;
	var _parent_y=this.oShape.y;
	var _w=this.oShape.w;
	var _h=this.oShape.h;
	var _bb_y=oEraserPtr.oBBox.y;
	
	/* 
	��� ������� ������� ��������, ������� ��� �� ���������� ������ 
	� ��� ��������� ������ �������: ������ ��� ��� ��������.
	��� �������� ���� y:135 ����� ���� �� ����� 2 �����������, ���� - �� ����� 4,
	������� ��� ����������� ������ ��� � �������
	*/
	
	var _intersections, _max_intersections, _cell, _csh, _xp, _p1, _p2, _inside_poly;
	var _prev=_shape.length-1;
	var _min_x, _max_x;
	var i=this.aCells.length-1;
	
	do{
		_cell=this.aCells[i];
		_csh=_cell.oShape;
		_max_intersections=( (_shape.y-_bb_y) < this.iYThreshold ) ? 2 : 4;
		_intersections=[];
		for(var j=0; (j<_shape.length) && (_intersections.length < _max_intersections); j++){
			_p1=_shape[j];
			_p2=_shape[_prev];
			if((_xp = IsLinesCross(_csh.x, _csh.y, _csh.x+_csh.w, _csh.y+_csh.h, _p1.x, _p1.y, _p2.x, _p2.y))){
				_intersections.push(_xp);
			}
			_prev=j;
		}
		
		var _DBG_w;
		
		_inside_poly=InsidePolygon(_shape, _csh);
		switch(_intersections.length){
			case 0: // ������� �� ���������� �������
				if(_inside_poly){ // ...�� ��������� � ���
					_cell.Destroy();
					this.aCells.rem(i);
					continue;
				}
				break;
			case 1:
				if(_inside_poly) // ������� ����� ��������� � ��������� ������
					_cell.Erase(_intersections[0].x - _parent_x, _csh.w - _intersections[0].x + _csh.x);
				else // ������� ����� ������ ���������
					_cell.Erase(_csh.x - _parent_x, _intersections[0].x - _csh.x);
				break;
			case 2:
				_min_x=Math.min(_intersections[0].x, _intersections[1].x);
				_max_x=Math.max(_intersections[0].x, _intersections[1].x);
				if(!_inside_poly){ // ����� ������� �� ��� �����
					this.AddCell(new Shape(_max_x - _parent_x, _csh.y - _parent_y, _csh.w - _max_x + _csh.x, 1), this.oShape);
					_cell.Erase(_csh.x - _parent_x, _min_x - _csh.x);
				}
				else{ // ����� �������� ��������, ���� ������� ���������� ��� �������� �����
					_cell.Erase(_min_x - _parent_x, _max_x - _min_x);
				}
				break;
			case 3:
				_intersections.sort(SortPoints);
				if(_inside_poly){
					this.AddCell(new Shape(_intersections[2].x - _parent_x, _csh.y - _parent_y, _csh.w - _intersections[2].x + _csh.x, 1), this.oShape);
					_cell.Erase(_intersections[0].x - _parent_x, _intersections[1].x - _intersections[0].x);
				}
				else{
					this.AddCell(new Shape(_intersections[1].x - _parent_x, _csh.y - _parent_y, _intersections[2].x - _intersections[1].x, 1), this.oShape);
					_cell.Erase(_csh.x - _parent_x, _intersections[0].x - _csh.x);
				}
				break;
			case 4:
				_intersections.sort(SortPoints);
				this.AddCell(new Shape(_intersections[1].x - _parent_x, _csh.y - _parent_y, _intersections[2].x - _intersections[1].x, 1), this.oShape);
				this.AddCell(new Shape(_intersections[3].x - _parent_x, _csh.y - _parent_y, _csh.w - _intersections[3].x + _csh.x, 1), this.oShape);
				_cell.Erase(_csh.x - _parent_x, _intersections[0].x - _csh.x);
				break;
		}
		if(_cell.CanDestroy()){
			_cell.Destroy();
			this.aCells.rem(i);
		}
	}while(i--);
}

/* ------------------------------------ */

var EraserCell=function(oShape, oParentShape){
	this.oPtr=document.createElement('div');
	this.oPtr.className='c';
	this.oPtr.style.width=oShape.w+'px';
	this.oPtr.style.height=oShape.h+'px';
	this.oPtr.style.left=oShape.x+'px';
	this.oPtr.style.top=oShape.y+'px';
	
	this._s=this.oPtr.style;
	
	
	this.oShape={x: oShape.x+oParentShape.x, y: oShape.y+oParentShape.y, w: oShape.w, h: oShape.h};
	this.oParentShape=oParentShape;
}

EraserCell.prototype.Destroy=function(){
	this.oPtr.parentNode.removeChild(this.oPtr);
}

EraserCell.prototype.Erase=function(x, w){
	this._s.left=x+'px';
	this._s.width=w+'px';
	this.oShape.x=x+this.oParentShape.x;
	this.oShape.w=w;
}

EraserCell.prototype.CanDestroy=function(){
	return (this.oShape.w <= 1);
}

EraserCell.prototype.Destroy=function(){
	this.oPtr.parentNode.removeChild(this.oPtr);
}

EraserCell.prototype.Repos=function(oPoint){
	this.oShape.x=this.oShape.x - this.oParentShape.x + oPoint.x;
	this.oShape.y=this.oShape.y - this.oParentShape.y + oPoint.y;
}
