import mx.controls.streamingmedia.HMiniPlayBarTray;
import mx.data.encoders.Num;
import mx.transitions.Tween;
import templateBook.mvctemplate.*
import templateBook.mvc.*
import templateBook.I.*
import flash.display.BitmapData
import templateBook.util.Observable
import templateBook.Configuration
import flash.filters.GlowFilter;
import templateBook.Angles
   
class templateBook.mvctemplate.SheetView extends AbstractView {
	private var __setMask__:Boolean=true                                             //mask
	static var loadedPages:Array;                                                    //array loaded pages 
	var __width:Number;                                                              //width page
	var __height:Number;                                                             //height page
	private var mouse_x:Number                                                       //value mouse x
	private var mouse_y:Number;                                                      //value mouse y  
	//mouse korekte
	private var diff_x:Number;                                                       //difference x
	private var diff_x2:Number;                                                      //difference x2
	private var diff_y:Number;                                                       //difference y
	private var diff_y2:Number;                                                      //diference y2
	private var mask_next_page:MovieClip                                             //mask next page
	private var mask__mc_main:MovieClip;  ///mask                                    //mask current page
	var tween_automatic:MovieClip                                                    //tween automatic
	private var __shadow:MovieClip                                                   //shadow for page
	public var _sx:Number,_sy:Number;                                                //center of the rotation
	private var rmax:Number,rmax2:Number;                                            //radius
	private var rcurrent:Number,rcurrent2:Number;                                    //current radius
	private var angle_sheet:Number;                                                  //angle sheet
	private var mouse_x0:Number, mouse_y0:Number;                                    //position mouse   
	var mask_shadow:MovieClip                                                        //mask for shadow
	private var twen1:Tween, twen2:Tween, twen3:Tween, twen4:Tween, twen5:Tween, twen6:Tween, twen7:Tween, twen8:Tween, twen9:Tween, twen10:Tween, twen11:Tween, tweenShadowAlpha:Tween;                //tween
	public var __nr:Number;                                                         //numer position in array
	private var __orginal_symmetry:Number                                           //first value variable __symmetry 
	private var __symmetry:Number;                                                  //( value -1 if to right page and value if left page )
	public var __deph:Number;                                                       //dep Sheet
	private var shadow_left:MovieClip,__shadow_right:MovieClip                      //dropShadow
	private var mcArea:MovieClip                                                    //backgroud sheet
	private var __loaded:Number=0                                                   //loading sheet end
	public var __loadingStart:Number                                                //loading image/swf start
	public var __loadingEnd:Number                                                  //loading image/swf end
    private var __shadows:Array                                                     //arrow shadow
	private var __preloader__mc_main:MovieClip                                      //preloader 
	private var __preloader_next_page:MovieClip                                     //preloader
	private var __url1:String                                                       //name first image
	private var __url2:String                                                       //name two image
	private var __onTweenPageEnd:Function;                                          //evens sheet
	private var __onTweenPageExit:Function;                                         //events sheet
	private var MULTIPLE_LOADING:Boolean=true                                       //enabled multiple loading
	private var __numberSheet:MovieClip                                             //number sheet
	private var __numberSheet2:MovieClip                                            //number sheet
	public var __sheetPosition:String="down"                                        //up or down
	public var __book:BookView                                                      //instance BookView
	var _mc_main:PageView;                                                          //instance PageView
	private var mc_main:PageView;                                                   //instance PageView
	private var nextPage:PageView                                                   //instance PageView
    var _next_page:PageView                                                         //instane PageView
	private var dir:Number=1                                                        //dir 1 or -1
	private var tween_intro:Tween                                                   //tween
	private var tweenBackground:Tween                                               //tween for background
	private var __mouseDown:Boolean                                                 //if mouse down
	public var tweenMouse:Boolean = false                                           //tween for mouse
	var __soundSheet:Sound                                                          //sound for sheet
	var mask_shadow_page:MovieClip                                                  //mask for shadow
	var time_autoTween:Number = 10                                                  //time [frame/s]      
	var easingSheet:Number = 3                                                      //easing for scheet
	var timeClick:Number = 0                                                        //time for double click
	var timeClickZoom:Number = 0                                                    //time for double click
	var offset_doubleClick:Number = 500                                             //offset double click
	
    
////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function SheetView() {
		time_autoTween = Number(Configuration.SPEED_PAGE_TWEEN)
		tween_automatic=this.createEmptyMovieClip("__mcTweenAutomatisz",-1020308)
		nextPage = PageView(this.attachMovie("_pageView", "___nextPage", -16377)) //nextPage)
		mc_main = PageView(this.attachMovie("_pageView", "__mc_main", -16383)) //mc_main
		if(loadedPages==undefined){
		loadedPages = []
		}
		__width = Number(Configuration.PAGE_WIDTH) + Number(Configuration.FRAME_SHEET) * 2
		height = Number(Configuration.PAGE_HEIGHT) + Number(Configuration.FRAME_SHEET) * 2
	}
	
//////////////////////////////////////////////////////////////////////////////////////////////////

function set height(value_) {
	__height=value_	
}

//////////////////////////////////////////////////////////////////////////////////////////////////

function get height() {
	var m:BookModel = BookModel(this.getModel())
	var book:BookView = m.__container.__bookView
	return __height	
}
	
//////////////////////////////////////////////////////////////////////////////////////////////////

function get ymouse2() {
	var m:BookModel = BookModel(this.getModel())
	return m.__container.__bookView._ymouse
}

//////////////////////////////////////////////////////////////////////////////
		
function get xmouse2() {
	var m:BookModel = BookModel(this.getModel())
	return m.__container.__bookView._xmouse
}
	
//////////////////////////////////////////////////////////////////////////////////////////////////

    public function ini():Void {
		var m:BookModel = BookModel(this.getModel())
		__shadow._visible = false
		if(m.currentPageLoading==undefined){
		m.currentPageLoading=[]
		}
		__shadows=[]
		if(symmetry==1){
			//mc_main._x=-__width
		}
				
		this.mask_shadow = this.createEmptyMovieClip("mcMaskShadow", 122288)
		mask_shadow._visible=false
		Drawing.rectangle(mask_shadow,0,0,__width*2,height,["0xFF0000",0])
					
        mask_shadow._x=-__width
        shadows_sheet(this.symmetry)
        this._mc_main._visible = true
		///_mc_main.node = getAttributesMain()
		//_next_page.node=getAttributesNext()
   		setSizePages()
        loadImages();
		//addSheet()
    }

///////////////////////////////////////////////////////////////////////////////////////////////////

public function setPositionRight() {
   _mc_main._x=0
   _mc_main._y=0
   _next_page._x=__width
}

///////////////////////////////////////////////////////////////////////////////////////////////////

public function setPositionLeft() {
   _mc_main._x=-__width
   _mc_main._y=0
   _next_page._x=-__width
   _next_page._y=height
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function createFrame_(mc:MovieClip){
	var width=__width
	var height=height
	var sizeLeft=Configuration.FRAME_SHEET+.5
	var sizeRight=Configuration.FRAME_SHEET+.5
	var sizeUp=Configuration.FRAME_SHEET+.5
	var sizeDown = Configuration.FRAME_SHEET+.5
	var color=0xFFFFFF
	
	////left
	if(Math.abs(sizeLeft)>0){
	mc.beginFill(color,100)
	mc.lineTo(sizeLeft,0)
	mc.lineTo(sizeLeft,height)
	mc.lineTo(0,height)
	mc.endFill()
	}
		
	////right
	if(Math.abs(sizeRight)>0){
	mc.beginFill(color,100)
    mc.moveTo(width-sizeRight,0)
	mc.lineTo(width,0)
	mc.lineTo(width,height)
	mc.lineTo(width-sizeRight,height)
	mc.endFill()
	}
	
	////top
	if(Math.abs(sizeUp)>0){
	mc.beginFill(color,100)
	mc.moveTo(sizeLeft,0)
	mc.lineTo(sizeLeft,sizeUp)
	mc.lineTo( (width+sizeLeft)-(sizeRight+sizeLeft),sizeUp)
	mc.lineTo( (width+sizeLeft)-(sizeRight+sizeLeft),0)
	mc.endFill()
	}
		
	////down
	if(Math.abs(sizeDown)>0){
	mc.beginFill(color,100)
	mc.moveTo(sizeLeft,height)
	mc.lineTo((width+sizeLeft)-(sizeLeft+sizeRight),height)
	mc.lineTo( (width+sizeLeft)-(sizeLeft+sizeRight),height-sizeDown)
	mc.lineTo(sizeLeft,height-sizeDown)
	mc.endFill()
	}
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////

    private function setSizePages():Void {
	   ///////////////////set Size Area
      _mc_main.mcArea._width=__width
      _mc_main.mcArea._height=height
      _next_page.mcArea._width=__width
      _next_page.mcArea._height=height	
	  _next_page.mcArea._visible=false
	  _mc_main.mcArea._visible=false
	  	  	  
	  /////////////////setSize Frame
	  var mask_space:Number= 20
	  	  
	  var __containerBcg:MovieClip=this.createEmptyMovieClip("mcContainerBackground",-324)
	  ////////////////////////////////main
	  var frame_mc_main=_mc_main.createEmptyMovieClip("containerFrame",-3).createEmptyMovieClip("frame",1)
	  ///createFrame(frame_mc_main)
	  	  
	  var mask1:MovieClip = _mc_main.createEmptyMovieClip("Mask", 78978989)
	  Drawing.rectangle(mask1, (symmetry == -1) ? 0 : -mask_space , -mask_space, __width + mask_space, height + (mask_space * 2), ["0x000000", 100])
	  	 
	 frame_mc_main._parent.setMask(mask1)
	 
	 
	  ////////////////////////////////next
	  var frame_next_page=_next_page.createEmptyMovieClip("containerFrame",-3).createEmptyMovieClip("frame",1)
	  ///createFrame(frame_next_page)
	  	  
	  var mask2:MovieClip = _next_page.createEmptyMovieClip("Mask", 456456)
	  Drawing.rectangle(mask2, (symmetry == -1) ? -mask_space : 0 , -mask_space, __width + mask_space, height + (mask_space * 2), ["0xFFFF00", 100])
	  
	frame_next_page._parent.setMask(mask2)
   
	  /////////////////////////////////glow
	  if(Configuration.GLOW_SHEET=="true"){
	  var filter:GlowFilter = new GlowFilter(0x000000,.2,10,10, 1, 1, false, false);
      var filterArray:Array = new Array();
      filterArray.push(filter);
      _mc_main.containerFrame.frame.filters = filterArray;
	  _next_page.containerFrame.frame.filters = filterArray;
      }

	 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getAttributesMain():XMLNode {
	var m:BookModel=BookModel(this.getModel())
	return m.__pages[__nr].node
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

function getAttributesNext():XMLNode {
	var m:BookModel = BookModel(this.getModel())
	if(symmetry==-1){
	return m.__pages[__nr+1].node
	}else {
	return m.__pages[__nr-1].node
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

     function onLoad() {
		 this._mc_main.mcArea.onPress=function(){}
		 this._next_page.mcArea.onPress=function(){}
	}
	 
////////////////////////////////////////////////////////////////////////////////////////////

    public function getAreaMouseDown():Boolean {
	var m:BookModel=BookModel(this.getModel())
	var margin:Number=Number(Configuration.SHEET_AREA)
	var w=Configuration.PAGE_WIDTH+Configuration.FRAME_SHEET*2
	var h = Configuration.PAGE_HEIGHT + Configuration.FRAME_SHEET * 2
	var horn:AreaHornView = m.__container.__areaHorn
	var hitTestHorn:Boolean = horn.hitTest(_root._xmouse, _root._ymouse, true)
			
	if(hitTestHorn==false){	  
	/////if NOT hitTest horn		
	if(this.symmetry==-1){ ///right page
	if( (   this._xmouse<w&&this._xmouse>(w-margin)   )  ||  (this._ymouse>0&&this._ymouse<margin&&_xmouse>w-margin)  ||  (this._ymouse<h&&this._ymouse>h-margin&&this._xmouse>w-margin)){
		return true;
	}else{
		return false	
	}
	}else{  ////left page
		if( (this._xmouse<-w+margin&&this._xmouse<(-w+margin)&&this._ymouse<h&&this._ymouse>0)||(this._ymouse>0&&this._ymouse<margin&&this._xmouse<-w+margin)||(this._ymouse<h&&this._ymouse>h-margin&&this._xmouse<-w+margin)){
		return true;
	}else{
		return false	
	}
	}
	/////if NOT hitTest horn end
		
	}else { 
		//////if hitTest Horn
		if (this.symmetry == -1) {
			if (this._xmouse > 0) {
				return true
			}else {
				return false
			}
		}else {
			if (this._xmouse < 0) {
				return true
			}else {
				return false
			}
    	}
      //////if hitTest Horn end
		
		
	}
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////

private function onPressTrue():Boolean {
	  var m:BookModel=BookModel(this.getModel())
	  var hitTestMouse= this.hitTest(_root._xmouse,_root._ymouse,true)
	  var maxDepSheet = (this.__symmetry == -1) ? m.__currentRight : m.__currentLeft
	  if (hitTestMouse && maxDepSheet == this) {
		  return true;
	  }else {
		  return false;		  
	  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////	
	
private function onMouseDown():Void {
	
var m:BookModel = BookModel(this.getModel())
	
if(m.blockade_page_press==false){
	
if(m.__shovLightBox==false&&Configuration.DRAG_PAGE=="true"){	
		
	///////////////////////normal click
	if (m.__hornVisible == true && m.__container.__containerClip.__shov == false && m.allVisibleBook() && m.__container.list_pages._visible == false) {
				
	 __mouseDown = true
	var horn:AreaHornView = m.__container.__areaHorn
	delete horn.onEnterFrame
	move_sheet(undefined, .9, getAreaMouseDown())
	
	if(getAreaMouseDown()){
	//m.__container.__tools.swapDepthsToDown()
	}
	}

	////////////////////////////////automatic page SINGLE OR DOUBLE
	if(m.allVisibleBook()&&onPressTrue()&&getAreaMouseDown()){
    	if (Configuration.SINGLE_CLICK == "true") {
			//mouseUp(1)			
		}else if (Configuration.DOUBLE_CLICK == "true") {
		   check_double_click()
		}
	}
	////////////////////////////////////////
	
}
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

function check_double_click() {
	var m:BookModel = BookModel(this.getModel())
	if(onPressTrue()&&getAreaMouseDown()){/////double_click
	if (  (getTimer() - timeClick) < this.offset_doubleClick) { ///doublce click
	mouseUp(1)
	}else {  ///single click
	}
	timeClick=getTimer()
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////

function speed_move_sheet() {
	move_sheet(true,1)
}

///////////////////////////////////////////////////////////////////////////////////////////////////

    public function move_sheet(auto_:Boolean, time_, area_:Boolean):Void {
		
	  var m:BookModel = BookModel(this.getModel())

	  if ( (onPressTrue() && area_) || auto_) {
 
	  m.dispatchEvent({target:this,type:"onStartDragSheet",nr:__nr,auto:auto_})
	  this.__sheetPosition="up"
	  this._visible=true
			
	  //if(this.MULTIPLE_LOADING==true){
		this.addSheet()		
	 // }
		
	  if (this.__loadingEnd || this.MULTIPLE_LOADING == true) {
		  m.__blockade = true
		  if (this.symmetry == -1) {
			this.swapDepths(_parent.getNextHighestDepth());
	      }
			  
	      stopTweny()
		 
		 if(symmetry==-1){
				  _sx=0		
		  }else{
			  	_sx =__width	  
		  }
			  
		var wsp_y = angle_horm()
		
		if (auto_ == true) { ///automatic
			_sy =  height
		}else {    ////not automatic
			_sy =ymouse2 + wsp_y 
			
		}
			
		if (_sy > height) {
				_sy=height
		}
		
		if (_sy < 0) {
			_sy = 0
		}
			
		if (auto_ != true) {
						
			setFirstXY()
			
			this.OEF(xmouse2, ymouse2);
			
			this.onEnterFrame = function() {
				this.OEF(xmouse2, ymouse2);
			};
			
	         this.onMouseUp = mouseUp
	    }else{
              automat(time_)
		}
		}
	 }
   };

//////////////////////////////////////////////////////////////////////////////////////////////////

function automat_speed() {
	 m.dispatchEvent( { target:this, type:"onStartDragSheet",nr:__nr,type2:"auto" } )
	 this.__sheetPosition="up"
	  if (this.symmetry == -1) {
			//this.swapDepths(_parent.getNextHighestDepth());
	     }
	 
	 if(symmetry==-1){
				  _sx=0		
		  }else{
			  	_sx =__width	  
		  }
			  
		var wsp_y = angle_horm()
		
				_sy =  height
		
			
		if (_sy > height) {
				_sy=height
		}
		
		if (_sy < 0) {
			_sy = 0
		}
		
		
	// automat(10)
		
	var m:BookModel = BookModel(this.getModel())	
	this.__sheetPosition="up"
	stopTweny()
	m.dispatchEvent({target:this,type:"onTweenPageStart"})	
		
    stopTweny()
    var __this=this
    var time=20

    //twen9 = new Tween(tween_automatic, "_x", mx.transitions.easing.Strong.easeOut,__width,-__width, time, false);
    //twen9 = new Tween(tween_automatic, "_y", mx.transitions.easing.Strong.easeOut,180+angle_horm(),0,time,false);
	
	//_x = -__width
	///_y = 0
	
	if(symmetry==-1){
	var x=-__width
	var y = 0
	}else {
	var x=__width
	var y = 0	
	}
	
	var y2 = __this.height - (Math.sin(  y * Math.PI/180  )*__this.height/3)
	
	__this.OEF(x, y2, "auto");
	
	m.dispatchEvent( { target:__this, type:"onTweenPageEnd",automat_speed:true } )
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////

private function mouseUp(par_):Void {

	var m:BookModel = BookModel(this.getModel())
	///if(m.doubleClick==false){
	var time_
	__mouseDown=false
	delete this.onMouseUp
	delete this.onEnterFrame;
	
	if (_xmouse < 0 && symmetry == -1 || _xmouse > 0 && symmetry == 1||par_==1) {
		if(symmetry==-1){
		m.nextPage("sheet")
		}
		else {
		m.prevPage("sheet")
		}
	}else {
		
		if (Configuration.SINGLE_CLICK == "true") {
			mouseUp(1)
		}else{
		pageExit(time_) 
		}
	}
	
	if (mask_shadow.hitTest(this._xmouse, this._ymouse)) {
		//this._parent._parent.swapDepths(78978978978987915)
	}
}
	 
//////////////////////////////////////////////////////////////////////////////////////////////////

private function hitTestSheet():Boolean {
	
	var m:BookModel = BookModel(this.getModel())
	var viewbook:BookView=m.__container.__bookView
	if (__symmetry == -1) {
		
		if (viewbook._xmouse > 0 && viewbook._xmouse < __width && viewbook._ymouse >= 0 && viewbook._ymouse < height) {
			return true;
		}else {
			return false;
		}
	}else {
		
		if (viewbook._xmouse < 0 && viewbook._xmouse >- __width && viewbook._ymouse >= 0 && viewbook._ymouse < height) {
			return true;
		}else {
			return false;
		}
	}
}

 ////////////////////////////////////////////////////////////////////////////////////////////
 
    private function automat(time_:Number):Void{
	soundEnabled()
	var m:BookModel = BookModel(this.getModel())	
	this.__sheetPosition="up"
	stopTweny()
	m.dispatchEvent({target:this,type:"onTweenPageStart"})	
		
    stopTweny()
    var __this=this
    var time=time_
   	
	if(symmetry==-1){
    twen9 = new Tween(tween_automatic, "_x", mx.transitions.easing.Strong.easeOut,__width,-__width, time, false);
    twen9 = new Tween(tween_automatic, "_y", mx.transitions.easing.Strong.easeOut,180,0,time,false);
	}else{
	twen9 = new Tween(tween_automatic, "_x", mx.transitions.easing.Strong.easeOut,-__width,__width, time, false);
    twen9 = new Tween(tween_automatic, "_y", mx.transitions.easing.Strong.easeOut,180,0,time,false);
	}
    twen9.onMotionChanged = function() {
	var x=__this.tween_automatic._x
	var y=__this.tween_automatic._y
	var y2=__this.height-(Math.sin(  y*Math.PI/180  )*__this.height/3)
	__this.OEF(x,y2,"auto");
    };
    twen9.onMotionFinished = function() {
		
		m.dispatchEvent( { target:__this, type:"onTweenPageEnd" } )
		
		if(Configuration.LOAD_ALL_PAGES=="false"){
		m.select_loadImage()
		}
		
	};
	
	}

////////////////////////////////////////////////////////////////////////////////////////////

private function tweenPageFinish(time_:Number):Void {
	   soundEnabled()
	    var m:BookModel = BookModel(this.getModel())	
		this.__sheetPosition="up"
		stopTweny()
		m.dispatchEvent({target:this,type:"onTweenPageStart"})
		
	    var time=(time_)? time_ : 0.7
	     ///trace(tween_automatic+"/ "+_mc_main+" / "+_next_page)
	    if(symmetry==-1){
	   	twen10 = new Tween(tween_automatic, "_x", mx.transitions.easing.Strong.easeOut, mouse_x, -__width, time, true);
		twen11 = new Tween(tween_automatic, "_y", mx.transitions.easing.Strong.easeOut, mouse_y, _sy, time, true);
		}else{
			
		twen10 = new Tween(tween_automatic, "_x", mx.transitions.easing.Strong.easeOut, mouse_x, __width, time, true);
		twen11 = new Tween(tween_automatic, "_y", mx.transitions.easing.Strong.easeOut, mouse_y, _sy, time, true);	
		}
				
		var _this = this;
		twen10.onMotionChanged = function() {
			
			_this.OEF(_this.tween_automatic._x, _this.tween_automatic._y,"auto");
		};
		twen10.onMotionFinished = function() {
			
			m.dispatchEvent({target:_this,type:"onTweenPageEnd"})
		};
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     private function setColor(mc_:MovieClip,color_):Void{
		 var k:Color=new Color(mc_)
		k.setRGB(color_)
	 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////

	public function set _onTweenPageEnd(f:Function):Void {
		__onTweenPageEnd = f;
	}
	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public function checkLoaded():Void {
		  var m:BookModel = BookModel(this.getModel())	
		  
		  if (m.__currentLeft == undefined) {
			    m.dispatchEvent( { target:this, type:"onLoadingRight" } )
				return;
		  }else if (m.__currentRight==undefined) {
			    m.dispatchEvent( { target:this, type:"onLoadingLeft" } )			  
				return;
		  }
		  
		  
	     if (symmetry == -1 && m.__currentRight.__sheetPosition == "down" && m.__blockade == false) {
			   if (m.getPage(m.__currentRight.__nr).loaded==true&&m.getPage(m.__currentLeft.__nr).loaded==true) {
				     m.dispatchEvent( { target:this, type:"onLoadingRight" } )
			 }
	     }
		 else if (symmetry == 1 && m.__currentLeft.__sheetPosition == "down" && m.__blockade == false) {
			if (m.getPage(m.__currentLeft.__nr).loaded==true&&m.getPage(m.__currentRight.__nr).loaded==true) {
			m.dispatchEvent( { target:this, type:"onLoadingLeft" } )
			 }
     	}
		
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////

	public function set symmetry(value):Void{
		__symmetry = value
		if(__symmetry==-1){  ///all right sheet
			
		_next_page=undefined
        _mc_main=undefined
	    _next_page = nextPage
		_next_page._visible=false
		_mc_main = mc_main
	    _sx=0
		}else{ ////left page
		_sx=__width		
	    _next_page=undefined
	    _mc_main=undefined
        _next_page = mc_main
		_next_page._visible=false
	    _mc_main=nextPage
		}
		this._mc_main._visible=true
     	_next_page.swapDepths(_mc_main.getDepth()+7)
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////

    public function get symmetry():Number{
		return __symmetry
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////

	public function set _onTweenPageExit(f:Function) {
		__onTweenPageExit = f;
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////

    private function loadImages():Void {
		
	var m:BookModel=BookModel(this.getModel())
	if(__orginal_symmetry==undefined){
		__orginal_symmetry= symmetry 
	}
     
    loadImage()
	onLoadStart__()
    }
	
////////////////////////////////////////////////////////////////////////////////////////////////////////	

    private function loadImage():Void {
	  var m:BookModel = BookModel(this.getModel())	

	  __loadingStart = 1
	  
	  /////load image main
	  _mc_main.sheet=this
	  _mc_main.book=__book
	  _mc_main.nr = __nr
	  m.__pages[__nr].page=_mc_main
	  m.__pages[__nr].content = _mc_main.createEmptyMovieClip("content", 1)
	 //_mc_main.loadImage()
	
	 
	 /////load image next
	  _next_page.sheet=this
	  _next_page.book=__book
	  _next_page.nr = (symmetry == -1) ? (__nr + 1) : (__nr - 1)
	   m.__pages[_next_page.nr].page = _next_page
	   m.__pages[_next_page.nr].content = _next_page.createEmptyMovieClip("content", 1)
	  // _next_page.loadImage()
		    
	  ///add number page
      if(symmetry==-1){
      //  addNumberMain(this.__nr)
      //  addNumberNext((this.__nr + 1))
		addChapter(_next_page)
	  }else{
		//addNumberMain(this.__nr)
       /// addNumberNext((this.__nr - 1))
		addChapter(_mc_main)
	  }

    }
	
////////////////////////////////////////////////////////////////////////////////////////////

function addChapter(clip_) {
	/*
	var m:BookModel=BookModel(this.getModel())
	var chapter:ChapterView = ChapterView(clip_.attachMovie("_chapter", "_chapter", 1002))
	chapter._x = Configuration.CHAPTER_X
	chapter._y = Configuration.CHAPTER_Y
	var node:XMLNode = clip_.node
	////frame chapter
	var frameChapter = Number(node.parentNode.attributes.index) + 2
	if (node.attributes.frameChapter!=undefined&&node.attributes.frameChapter.length) {
		var frameChapter=node.attributes.frameChapter
	}
	chapter.gotoAndStop(frameChapter)
	/*/
}

////////////////////////////////////////////////////////////////////////////////////////////
/*
private function addNumberMain(nr_:Number):Void {
			
	var visibleNumber = getAttributesMain().attributes.visibleNumber
	var visible = (visibleNumber == undefined || visibleNumber == "") ? Configuration.VISIBLE_NUMBER_PAGE : visibleNumber
	
	if(visible=="true"){
	var colorNumber=getAttributesMain().attributes.colorNumber
	this.__numberSheet= this._mc_main.attachMovie("NumberSheet","NumberSheet",123123123)
	this.__numberSheet.t.autoSize=true
	this.__numberSheet.t.text="  -  "+nr_+" -  "
	this.__numberSheet.t.textColor=(colorNumber==undefined||colorNumber=="") ? Configuration.COLOR_NUMBER_PAGE : colorNumber
	this.__numberSheet._x=(symmetry==-1) ? __width-this.__numberSheet._width : 0
	this.__numberSheet._y = int( (height ) - this.__numberSheet.t._height)
	
	}
}

///////////////////////////////////////////////////////////////////////////////////////////

private function addNumberNext(nr_:Number):Void{
	var visibleNumber = getAttributesNext().attributes.visibleNumber
	var visible = (visibleNumber == undefined || visibleNumber == "") ? Configuration.VISIBLE_NUMBER_PAGE : visibleNumber
	if(visible=="true"){
	var colorNumber=getAttributesNext().attributes.colorNumber
	this.__numberSheet2= this._next_page.attachMovie("NumberSheet","NumberSheet2",123123126)
	this.__numberSheet2.t.autoSize=true
	this.__numberSheet2.t.text="  -  "+nr_+"  -  "
	this.__numberSheet2.t.textColor =(colorNumber==undefined||colorNumber=="") ? Configuration.COLOR_NUMBER_PAGE :colorNumber
	this.__numberSheet2._x=(symmetry==-1) ?  0 :  __width-this.__numberSheet._width 
	this.__numberSheet2._y = int( (height ) - this.__numberSheet2.t._height )

	if (nr_ == 23) {
	//	__numberSheet2.t.textColor = "0xFF0000"
		//__numberSheet2._x=__width-this.__numberSheet._width 
	}
	}
}
/*/

///////////////////////////////////////////////////////////////////////////////////////////////////////	

function onLoadStart__():Void{
//this._mc_main.preloader=this._mc_main.attachMovie("_preloader_sheet","_preloader_sheet_",1200,{_x:__width/2,_y:height/2})
//this._next_page.preloader=this._next_page.attachMovie("_preloader_sheet","_preloader_sheet_",1200,{_x:__width/2,_y:height/2})
}

////////////////////////////////////////////////////////////////////////////////////////////

function onLoadStart(mc:MovieClip):Void{
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

function onLoadProgress(mc:MovieClip):Void{
			
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

public function addNumberLoading(nr_:Number):Void {
	var m:BookModel = BookModel(this.getModel())
	loadedPages[nr_]=undefined
	var isset:Boolean=false
	for(var i=0;i<m.currentPageLoading.length;i++){
		var value=m.currentPageLoading[i]
		if(value==nr_){
		isset=true;			
	    }
	}
	if(isset==false){
		m.currentPageLoading.push(nr_)
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

public function removeNumberLoading(nr_:Number):Void {
	var m:BookModel = BookModel(this.getModel())
	for(var i=0;i<m.currentPageLoading.length;i++){
		var value=m.currentPageLoading[i]
		if(value==nr_){
			m.currentPageLoading.splice(i,1)			
			return;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

function onLoadComplete(target:MovieClip):Void{
	var m:BookModel=BookModel(this.getModel())
	target._alpha=0	
	target.model=m
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

function onLoadInit(target:MovieClip):Void {
	var m:BookModel = BookModel(this.getModel())
	target.model=m
	target.onStopAnimationSheet()
	var nr:Number = Number(target._parent.nr)
	if (nr%2 == 0) {
	  var next:Number = nr + 1
	 }else {
	   var next:Number=nr-1
	}
	
	loadedPages[target._parent.nr]=1
	target._alpha=0
	
	///////////////position image
	if(target._parent._name=="mc_main"){
	//target._x=(__width-Configuration.FRAME_SHEET)-target._width
	//target._y=height/2-target._height/2
	}else{
	//target._x=Configuration.FRAME_SHEET
	//target._y=height/2-target._height/2
	}
				
	__loaded++
	///target._parent.preloader.removeMovieClip()
   // removeNumberLoading(target._parent.nr)
			
   if(__loaded==2){  ///loaded page (2 dep)
	__loadingEnd=1
	}
			
	if (loadedPages[nr] == 1 && loadedPages[next] == 1) { ////jesli zaladowano dwie strony to je pokaz
	
	PageView(m.__pages[nr].content._parent).frame_preloader.removeMovieClip();
	PageView(m.__pages[next].content._parent).frame_preloader.removeMovieClip();
	
	target.tween_intro.stop()
	target.tween_intro2.stop()
	target.tween_intro = new Tween(m.__pages[nr].content, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);
	target.tween_intro2 = new Tween(m.__pages[next].content, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);
	target.tween_intro.onMotionFinished = Delegate2.create(this, this.onIntroEnd, target)
	
	///////////////event (page left loaded)
		if ( (this==m.__currentLeft||this==m.__currentRight) && m.__blockade == false) {
			m.dispatchEvent( { target:this, type:"onLoadingLeft" } )
			m.dispatchEvent({target:this,type:"onLoadingRight"})
		}
	
	} else if (nr == 1) {  ///jesli to pierwsza 
		
	 PageView(m.__pages[nr].content._parent).frame_preloader.removeMovieClip();
						
			
	target.tween_intro = new Tween(m.__pages[nr].content, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);
	target.tween_intro.onMotionFinished = Delegate2.create(this, this.onIntroEnd, target)
	if (this.__nr == m.__currentRight.__nr && m.__blockade == false) {
			m.dispatchEvent({target:this,type:"onLoadingRight"})
	}	
	
	
	}else if ( nr == m.getPageLength() ) {  ///jesli to jest ostatnia strona
	 PageView(m.__pages[nr].content._parent).frame_preloader.removeMovieClip();
	m.__pages[nr].frame_preloader.removeMovieClip()	
	target.tween_intro = new Tween(m.__pages[nr].content, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);
	target.tween_intro.onMotionFinished = Delegate2.create(this, this.onIntroEnd, target)
		
	if(this.__nr==m.__currentLeft.__nr&&m.__blockade==false){
			m.dispatchEvent( { target:this, type:"onLoadingLeft" } )
	}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

private function onIntroEnd(target:MovieClip):Void{
	/////////////////////////////change background image (if loaded image)
	changeBackground(target)
	
	if(__loaded==2){
	
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

function soundEnabled() {
	if(Configuration.SOUND_SHEET_ENABLED=="true"){
	var m:BookModel = BookModel(this.getModel())
	__soundSheet = m.soundSheet
	__soundSheet.stop()
	__soundSheet.setVolume(Number(Configuration.SOUND_SHEET_VOLUME))
	__soundSheet.start()
	}
}

//////////////////////////////////////////if onLoad Image//////////////////////////////////////////////////

private function changeBackground(target:MovieClip):Void{
		
   if(Configuration.COLOR_BACKGROUND_ONLOADPAGE=="transparent"){
	target._parent.mc_bck._visible=false
	 }else{
	target._parent.mc_bck._visible=true
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

    public function removeLoading():Void{
	  if(__symmetry==-1){
	  removeNumberLoading(this.__nr)
	  removeNumberLoading((this.__nr+1))
	  }else{
	  removeNumberLoading(this.__nr)
	  removeNumberLoading((this.__nr-1))
	  }
	}

////////////////////////////////////////////////////////////////////////////////////////////

   private function addSheet():Void {
	 var m:BookModel=BookModel(this.getModel())
     if(symmetry==-1){
	       __book.addSheet(__nr+2)
	
	}else{
		
		 __book.addSheet(__nr-2)
	}
   }

////////////////////////////////////////////////////////////////////////////////////////////

  function leftwing():Void {
	setDepth()
		
	if(symmetry==__orginal_symmetry){  ///exit
	}
	else{
	}
   }

//////////////////////////////////////////////////////////////////////////////////////////

  function setDepth():Void{
	__deph=__nr*symmetry
	this.swapDepths(__deph)
  }
	
/////////////////////////////////////////////////////////////////////////////////////////////

   function rightwing():Void{
	setDepth()
		
	if(symmetry==__orginal_symmetry){
	}else{
  	}
    }
	
//////////////////////////////////////////////////////////////////////////////////////////////

   public function stopTweny():Void{
       twen1.stop(); twen2.stop(); twen3.stop(); twen4.stop(); twen5.stop(); twen6.stop(); twen7.stop(); twen8.stop(); twen9.stop();
	   twen10.stop()
	   twen11.stop()
	   tweenShadowAlpha.stop()
    }

////////////////////////////////////////////////////////////////////////////////////////////

  private function angle_horm():Number {
	  return -10 + (  (ymouse2 / height) * 20 )
  }

////////////////////////////////////////////////////////////////////////////////////////////

  private function shadows_sheet(symmetry_):Void{
	 
  for(var i in this.__shadows){
		var mc=this.__shadows[i]
		mc.removeMovieClip()
  }
  
  if (symmetry_ == -1) { ////if right sheet
   var cl=_mc_main.attachMovie("__shadow_sheet","shadow_left",10000000015)
  this.__shadows.push(cl)
   cl._height=height
  var cp=_next_page.attachMovie("__shadow_sheet","shadow_right",10000000016)
  this.__shadows.push(cp)
  cp._height=height
  cp._xscale*=-1
  cp._x=__width
  }else{ ///if left sheet
  var cp=_mc_main.attachMovie("__shadow_sheet","shadow_right",10000000017)
  this.__shadows.push(cp)
  cp._height=height
  cp._xscale*=-1
  cp._x=__width
  var cp=_next_page.attachMovie("__shadow_sheet","shadow_right",10000000018)
  this.__shadows.push(cp)
  cp._height=height
  cp._x=0
 }

}

////////////////////////////////////////////////////////////////////////////////////////////

public function shadowVisible(visible_:Boolean):Void {
	__shadow._visible=visible_	
}

////////////////////////////////////////////////////////////////////////////////////////////

 public function pageExit(time_,target_):Void {
	 var m:BookModel = BookModel(this.getModel())	
     var new_target=(target_==undefined) ? this : target_
     m.dispatchEvent({target:new_target,type:"onSheetBackStart"})
	 
	var __this = this
	
	if (tweenMouse == true) {
	setDepth()	
	stopTweny()
	var time = (time_ == undefined) ? .8 : time_
	var timeAlpha:Number=time+2
	var anim='linearTween'
	
	if(this.symmetry==-1){  ///right page
	
	if (angle_sheet<0) {
		twen1 = new Tween(__shadow, "_x", mx.transitions.easing.Strong.easeOut,__shadow._x,__width, time, true);
		twen2 = new Tween(__shadow, "_y", mx.transitions.easing.Strong.easeOut, __shadow._y, 0, time, true);
		tweenShadowAlpha=new Tween(__shadow, "_alpha", mx.transitions.easing.Strong.easeOut, __shadow._alpha,0, time+timeAlpha,true);
		twen3 = new Tween(mask_next_page, "_x", mx.transitions.easing.Strong.easeOut,mask_next_page._x,__width, time, true);
		twen4 = new Tween(mask_next_page, "_y", mx.transitions.easing.Strong.easeOut,mask_next_page._y,0, time, true);
		twen5 = new Tween(mask__mc_main, "_x", mx.transitions.easing.Strong.easeOut,mask__mc_main._x,__width, time, true);
		twen6 = new Tween(mask__mc_main, "_y", mx.transitions.easing.Strong.easeOut,mask__mc_main._y,0, time, true);
		twen7 = new Tween(_next_page, "_x", mx.transitions.easing.Strong.easeOut,_next_page._x,__width, time, true);
		twen8 = new Tween(_next_page, "_y", mx.transitions.easing.Strong.easeOut,_next_page._y,0, time, true);
		twen8.onMotionFinished = function() {
			m.dispatchEvent({target:new_target,type:"onSheetBackEnd"})
		};
	} else {
		var r = Rog(_next_page, 0, height);
		var delta_x = __width-r.x;
		var delta_y = height - r.y;
		twen1 = new Tween(__shadow, "_x", mx.transitions.easing.Strong.easeOut,__shadow._x,__width, time, true);
		twen2 = new Tween(__shadow, "_y", mx.transitions.easing.Strong.easeOut, __shadow._y, height, time, true);
		//__shadow.sheet._visible=false
		tweenShadowAlpha=new Tween(__shadow, "_alpha", mx.transitions.easing.Strong.easeOut, __shadow._alpha,0, time+timeAlpha,true);
		twen3 = new Tween(mask_next_page, "_x", mx.transitions.easing.Strong.easeOut,mask_next_page._x,__width, time, true);
		twen4 = new Tween(mask_next_page, "_y", mx.transitions.easing.Strong.easeOut,mask_next_page._y,height, time, true);
		twen5 = new Tween(mask__mc_main, "_x", mx.transitions.easing.Strong.easeOut,mask__mc_main._x,__width, time, true);
		twen6 = new Tween(mask__mc_main, "_y", mx.transitions.easing.Strong.easeOut,mask__mc_main._y,height, time, true);
		twen7 = new Tween(_next_page, "_x", mx.transitions.easing.Strong.easeOut,_next_page._x,_next_page._x+delta_x, time, true);
		twen8 = new Tween(_next_page, "_y", mx.transitions.easing.Strong.easeOut,_next_page._y,_next_page._y+delta_y, time, true);
		twen8.onMotionFinished = function() {
			m.dispatchEvent({target:new_target,type:"onSheetBackEnd"})
		};
	}
	
	}else{  ////left page
					
     if (angle_sheet<0) {
	    var dpr=Rog(_next_page, __width, height);
		var delta_x=-__width-dpr.x
		var delta_y=height-dpr.y
		twen1 = new Tween(__shadow, "_x", mx.transitions.easing.Strong.easeOut,__shadow._x,-__width, time, true);
		twen2 = new Tween(__shadow, "_y", mx.transitions.easing.Strong.easeOut, __shadow._y, height, time, true);
		tweenShadowAlpha=new Tween(__shadow, "_alpha", mx.transitions.easing.Strong.easeOut, __shadow._alpha,0, time+timeAlpha,true);
		twen3 = new Tween(mask_next_page, "_x", mx.transitions.easing.Strong.easeOut,mask_next_page._x,-__width, time, true);
		twen4 = new Tween(mask_next_page, "_y", mx.transitions.easing.Strong.easeOut,mask_next_page._y,height, time, true);
		twen5 = new Tween(mask__mc_main, "_x", mx.transitions.easing.Strong.easeOut,mask__mc_main._x,-__width, time, true);
		twen6 = new Tween(mask__mc_main, "_y", mx.transitions.easing.Strong.easeOut,mask__mc_main._y,height, time, true);
		twen7 = new Tween(_next_page, "_x", mx.transitions.easing.Strong.easeOut,_next_page._x,(_next_page._x+delta_x), time, true);
		twen8 = new Tween(_next_page, "_y", mx.transitions.easing.Strong.easeOut,_next_page._y,_next_page._y+delta_y, time, true);
		twen8.onMotionFinished = function() {
				m.dispatchEvent({target:new_target,type:"onSheetBackEnd"})
		};
      } else {
		var dpr=Rog(_next_page, __width,0);
		var delta_x=-__width-dpr.x
		var delta_y = -dpr.y
		twen1 = new Tween(__shadow, "_x", mx.transitions.easing.Strong.easeOut,__shadow._x,-__width, time, true);
		twen2 = new Tween(__shadow, "_y", mx.transitions.easing.Strong.easeOut, __shadow._y, 0, time, true);
		tweenShadowAlpha=new Tween(__shadow, "_alpha", mx.transitions.easing.Strong.easeOut, __shadow._alpha,0, time+timeAlpha,true);
		twen3 = new Tween(mask_next_page, "_x", mx.transitions.easing.Strong.easeOut,mask_next_page._x,-__width*1, time, true);
		twen4 = new Tween(mask_next_page, "_y", mx.transitions.easing.Strong.easeOut,mask_next_page._y,0, time, true);
		twen5 = new Tween(mask__mc_main, "_x", mx.transitions.easing.Strong.easeOut,mask__mc_main._x,-__width*1, time, true);
		twen6 = new Tween(mask__mc_main, "_y", mx.transitions.easing.Strong.easeOut,mask__mc_main._y,0, time, true);
		twen7 = new Tween(_next_page, "_x", mx.transitions.easing.Strong.easeOut,_next_page._x,(_next_page._x+delta_x), time, true);
		twen8 = new Tween(_next_page, "_y", mx.transitions.easing.Strong.easeOut,_next_page._y,_next_page._y+delta_y, time, true);
		twen8.onMotionFinished = function() {
				m.dispatchEvent({target:new_target,type:"onSheetBackEnd"})
		};
	    }
  }
		
		
  }else {
    	//m.dispatchEvent({target:new_target,type:"onSheetBackEnd"})
  }
}

////////////////////////get angle mouse////////////////////////////////////////////////////////////////////

    private function AngleMouse(x:Number,y:Number):Number {
		var angle_ = Math.atan2(y, x);
		return Angles.RadianToStopien(angle_);
   }

///////////////////////get angle sheet///////////////////////////////////////////////////////////////////

	private function getAngleSheet(x:Number,y:Number):Number {
		return AngleMouse(x, y)*2;
	}
	
///////////////////position mask right page/////////////////////////////////////////////////////////////////////////

	private function positionMaskLeft(angle_:Number):Object {
		var dlr = Rog(_next_page, 0, height);
		var dpr = Rog(_next_page, __width, height);
		var glr = _next_page._y;
				
		if ((angle_)>6) {
			var obj={x:((height-dlr.y)/Math.tan(Angles.StopienToRadian(angle_)))+dlr.x, y:height};
			return obj
			
		} else if (angle_<-6) {
			var obj={x:((glr/Math.tan(Angles.StopienToRadian(Math.abs(angle_))))+_next_page._x), y:0};			
			return obj
		} else {
			var _licznik = dlr.x+__width;
	    	var jedynka = (_licznik/(2*__width));
			var obj={x:(jedynka*__width), y:height};
			return obj
		}
	}
	
///////////////////////position mask left page////////////////////////////////////////////////////////////////////////////

    private function positionMaskRight(angle_:Number):Object {
		var dlr = Rog(_next_page, 0, height);
		var dpr = Rog(_next_page, __width, height);
 				
		if ((angle_)>6) {
			var gpr = Rog(_next_page, __width, 0);
			var obj={x:((gpr.x-gpr.y/Math.tan(Angles.StopienToRadian(Math.abs(angle_))))), y:0};			
			return obj
				
		} else if (angle_<-6) {
				
			if(Math.abs(angle_)>45){  ////
			var obj={x:((height-dlr.y)/Math.tan(Angles.StopienToRadian(angle_)))+dlr.x, y:height};
			return obj
			}else{  //// plynniej
			var y=height-dpr.y  
			var obj={x:dpr.x+(y/Math.tan(Angles.StopienToRadian(angle_))), y:height};
			return obj	
			}
					
		} else {
			var _licznik = dlr.x+__width;
	    	var jedynka = -.5+(_licznik/(2*__width));
			var obj={x:(jedynka*__width), y:height};
			return obj
		}
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function Rog(klip:MovieClip,rog_x:Number,rog_y:Number):Object {
		return Converter.convert({x:rog_x, y:rog_y}, klip, this);
	}
	
//////////////////////////////////////////////////////////////////////////////////////////////////	
	
	function setFirstXY() {
	_next_page._x = (symmetry==-1) ? this.__width : - __width*2
	_next_page._y = 0
	_next_page._rotation = 0	
		
	var center = Converter.convert( { x:_sx, y:_sy }, _next_page, this);
	mouse_x = center.x
	mouse_y = center.y
	}

	
///////////////////////////////////////////////////////////////////////////////////////////////

	public function OEF(__x, __y, auto_:String) {
		
		mouse_x0 = Math.min(Math.max(-__width, __x), __width)
		mouse_y0 = __y;
		Rmax();
		currentR();
		delta();
		///
		Rmax2();
		currentR2();
		delta2();
		var roz_x = diff_x2+diff_x;
		var roz_y = diff_y2+diff_y;
		////////////korekte mouse
		
		if (auto_ == undefined) {
		var center = Converter.convert( { x:_sx, y:_sy }, _next_page, this);	
        mouse_x += (  (mouse_x0-(isNaN(roz_x) ? 0 : roz_x)) - center.x) / easingSheet
		mouse_y += (  (mouse_y0 + (isNaN(roz_y) ? 0 : roz_y)  ) - center.y ) / easingSheet
		}else {
		mouse_x =   mouse_x0 - (isNaN(roz_x) ? 0 : roz_x)
		mouse_y =   mouse_y0 + (isNaN(roz_y) ? 0 : roz_y)
		}
				
		////throw X and Y
		if(symmetry==-1){
		 var Vx =__width-mouse_x;
		}else{
		 var Vx =__width+mouse_x;
		}
		var Vy = (_sy-mouse_y)*(symmetry*-1)
		angle_sheet = getAngleSheet(Vx,Vy)
		
		
		if (auto_ == "auto") {  //automatic page
						
		}else {  ///no automatic
			tweenMouse=true			
		}
		
		/////////rotation next page (_sx;_sy)
		rotate(_next_page, _sx, _sy, angle_sheet);
		
		/////change position
		var center = Converter.convert({x:_sx, y:_sy}, _next_page, this);
		_next_page._x += mouse_x-center.x
		_next_page._y += mouse_y - center.y
						
		////point of the joint of the sheet of paper with the bottom edge
		if(symmetry==-1){
		var pos_mask:Object = positionMaskLeft(angle_sheet);	
		mask_page_right(pos_mask.x, pos_mask.y, angle_sheet/2);
	
		}else{
		var pos_mask:Object= positionMaskRight(angle_sheet);	
	
		mask_page_left(pos_mask.x, pos_mask.y, angle_sheet/2);
		}
}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function mask_page_right(x, y, angle_) {
		///////////////mask _next_page
		this.mask_next_page= this.attachMovie("_mask", "_mask_next_page_", 300,{_visible:true,_alpha:20});
		this.mask_next_page._x = x///Math.max(0, x);
		this.mask_next_page._y = y;
		this.mask_next_page._rotation = angle_;
		if(__setMask__){
		_next_page.setMask(this.mask_next_page);
		}
		///////////////dropShadow
		__shadow = this.attachMovie("_shadow", "_shadow_", 301);
		__shadow._yscale=200
		__shadow._x = Math.max(0, x);
		var main_alpha:Number = Math.abs(_next_page.containerFrame.getBounds(this).xMax)
		//trace(main_alpha)
		if(main_alpha<100){
		__shadow._alpha=main_alpha
		}
		__shadow._y = y;
		__shadow._rotation = angle_;
		
		if (__setMask__) {  ///mask shadow
			var margin=0
			mask_shadow_page = this._next_page.createEmptyMovieClip("mcMaskPage", -2002)
			mask_shadow_page._visible=false
			Drawing.rectangle(mask_shadow_page, -margin, -margin, __width + margin * 2, height + margin * 2, ["0xFF0000", 0])
			__shadow.container.contact.setMask(mask_shadow);
		   __shadow.container.sheet.setMask(mask_shadow_page)
		}
		
		mask_shadow._visible=false
	    ///////////////mask _mc_main
		this.mask__mc_main = this.mask_next_page.duplicateMovieClip("_mask__mc_main_", 3002);
		if(__setMask__){
		_mc_main.setMask(mask__mc_main);
		}
		this._next_page._visible=true
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	private function mask_page_left(x, y, angle_) {
		/////////////mask _next_page
		this.mask_next_page= this.attachMovie("_mask", "_mask_next_page_", 300,{_visible:true});
		this.mask_next_page._x =x
		this.mask_next_page._y = y;
		this.mask_next_page._rotation =(angle_ +180)
		if(__setMask__){
		_next_page.setMask(this.mask_next_page);
		}
   		////////////dropShadow
		__shadow = this.attachMovie("_shadow", "_shadow_", 301);
		__shadow._yscale=200
		__shadow._xscale=-100
		__shadow._x =x
		var main_alpha:Number=Math.abs(_next_page.containerFrame.getBounds(this).xMin)
		if(main_alpha<100){
		__shadow._alpha=main_alpha
		}
		__shadow._y = y;
		__shadow._rotation = angle_
		if(__setMask__){
		    var margin=0
			mask_shadow_page = this._next_page.createEmptyMovieClip("mcMaskPage", -2002)
			mask_shadow_page._visible=false
			Drawing.rectangle(mask_shadow_page,-margin,-margin,__width+margin*2,height+margin*2,["0xFF0000",0])
			__shadow.container.contact.setMask(mask_shadow);
		   __shadow.container.sheet.setMask(mask_shadow_page)
		}
		mask_shadow._visible=false
		////////////////mask _mc_main
		this.mask__mc_main = this.mask_next_page.duplicateMovieClip("_mask__mc_main_", 3002);
		_mc_main.setMask(mask__mc_main);
		mask__mc_main._visible=true
	    this._next_page._visible=true
	}
	
////////////////////////////////////////////////////////////////////////////////////

	private function Rmax() {
		rmax = Math.sqrt((__width*__width)+((height-_sy)*(height-_sy)));
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function Rmax2() {
		rmax2 = Math.sqrt((__width*__width)+((_sy)*(_sy)));
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function currentR() {
		rcurrent = Math.sqrt((mouse_x0*mouse_x0)+(height-mouse_y0)*(height-mouse_y0));
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function currentR2() {
		rcurrent2 = Math.sqrt((mouse_x0*mouse_x0)+(mouse_y0)*(mouse_y0));
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function os_x(value_, angle_) {
		return value_*Math.cos(Angles.StopienToRadian(angle_));
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function os_y(value_, angle_) {
		return value_*Math.sin(Angles.StopienToRadian(angle_));
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function delta() {
		var dir = 1;
		var diff = Math.max(0, (-rmax+rcurrent)); 
		var angle_ = AngleMouse(mouse_x0, height-mouse_y0);
		diff_x = os_x(diff, angle_);
		diff_y = os_y(diff, angle_)*dir;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function delta2() {
		var dir = -1;
		var diff = Math.abs(Math.min(0, (rmax2-rcurrent2)));
		var angle_ = AngleMouse(mouse_x0, mouse_y0);
		diff_x2 = os_x(diff, angle_);
		diff_y2 = os_y(diff, angle_)*dir;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function rulez_for_test_(x1, y1, x2, y2, dep, kol) {
		var p = dep;
		var l = this.createEmptyMovieClip("line_test"+p, p);
		l.lineStyle(3, kol, 100);
		l.moveTo(x1, y1);
		l.lineTo(x2, y2);
	}
	
///////////////////global xmouse/////////////////////////////////////////////////////////////////////////

	public function get _sxG() {
		return Converter.convert({x:_sx, y:_sy}, _next_page, this).x;
	}
	
////////////////////global ymouse////////////////////////////////////////////////////////////////////////

	public function get _syG() {
		return Converter.convert({x:_sx, y:_sy}, _next_page, this).y;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////

	private function rotate(klip, _sx, _sy, angle_) {
		var p = {x:_sx, y:_sy};
		klip.localToGlobal(p);
		klip._rotation = angle_;
		var q = {x:_sx, y:_sy};
		klip.localToGlobal(q);
		klip._x += (p.x-q.x);
		klip._y += (p.y - q.y);
	}

///////////////////////////////////////////////////////////////////////////////////////

private function onTweenPageEnd() {
	if(__loadingEnd){
	//StartAnimationSheet()
	}
}

////////////////////////////////////////////////////////////////////////////////////////

private function onSheetBackEnd(obj:Object) {

}

//////////////////////////////////////////////////////////////////////////////////////////////////

private function onSheetBackStart(obj:Object) {
	var m:BookModel = BookModel(this.getModel())
		
	if(obj.target!="horn"&&(m.__currentLeft.__nr==__nr||m.__currentRight.__nr==__nr)){
	StartAnimationSheet()
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

private function onStartDragSheet() {
	StopAnimationSheet()
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private function onLoadingLeft(obj:Object) {
	var m:BookModel = BookModel(this.getModel())
	if (this.__nr == m.__currentLeft.__nr && this.__sheetPosition == "down") {
		// StartAnimationSheet()
    }
	}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private function onLoadingRight(obj:Object) {
	    var m:BookModel = BookModel(this.getModel())
		if (this.__nr == m.__currentRight.__nr && this.__sheetPosition == "down") {
		    /// StartAnimationSheet()
	    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function onNextPage(obj:Object) {
	var m:BookModel = BookModel(this.getModel())
    if(m.__currentRight==this){
      	if (obj.target == "sheet") {
			 this.tweenPageFinish()
		}else if (obj.target != "sheet") {
			if (m.__blockade == false) {
				 m.__currentRight.move_sheet(true,time_autoTween)
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function onPrevPage(obj:Object) {
	   var m:BookModel=BookModel(this.getModel())
       if(m.__currentLeft==this){
       	if (obj.target == "sheet") {
			 this.tweenPageFinish()
		}else if (obj.target != "sheet") {
			if (m.__blockade == false) {
			 m.__currentLeft.move_sheet(true,time_autoTween)
			}
		}
	}
}

/////////////////////events sheet ( in swf file)///////////////////////////////////////////////////////////////////

 function StartAnimationSheet() {
	var m:BookModel = BookModel(this.getModel())
	var firstPage:Boolean = (m.__currentLeft.__nr==undefined&&loadedPages[1]==1)
	var lastPage:Boolean = (m.__currentRight==undefined&&loadedPages[m.getPageLength()]==1)
	
	if( (loadedPages[m.__currentLeft.__nr]==1&&loadedPages[m.__currentRight.__nr]==1) ||  firstPage ||lastPage   ){
			_mc_main.content.onStartAnimationSheet(m)
	} 
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

public function StopAnimationSheet(m) {
	var m:BookModel=BookModel(this.getModel())
	_mc_main.content.onStopAnimationSheet()
	_next_page.content.onStopAnimationSheet()
}

/////////////////////end events sheet ( in swf file)//////////////////////////////////////////////////////


}
