import mx.transitions.easing.Strong;
import templateBook.mvctemplate.*
import templateBook.mvc.*
import templateBook.I.*
import flash.display.BitmapData
import templateBook.util.Observable
import templateBook.Configuration
import mx.transitions.Tween

import templateBook.SWFAddress
import templateBook.SWFAddressEvent

 
class templateBook.mvctemplate.BookView extends AbstractView {

var __width:Number                //width sheet
var __height:Number               //height sheet
var __blockade:Boolean=false      //blockade sheet
var __pages:Array                 //array sheet
var __interFace                   //handle for setInterval
var tweenAlpha:Tween              //tween alpha
var inter_select_image:Number     //handle for setInterval
var tweenInBook:Tween             //tween for book


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

function BookView() {
	
__width = Configuration.PAGE_WIDTH+Configuration.FRAME_SHEET*2
__height = Configuration.PAGE_HEIGHT + Configuration.FRAME_SHEET * 2


}

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

function onLoad(){
   this._visible = false
   this._alpha=0
}

////////////////////////////////events model////////////////////////////////////////////////////////////

function onLoadBackground() {
	var m:BookModel = BookModel(this.getModel())
	m.startLoadPages()
}

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

function faceBook() {
	var m:BookModel = BookModel(this.getModel())
	clearInterval(__interFace)
	this._visible = true
    
  this._alpha = 100 
  var x = this._x
  this._x=Stage.width+500
  tweenInBook=new Tween(this, "_x", mx.transitions.easing.Strong.easeOut,this._x,x,.5, true);	
  tweenInBook.onMotionFinished=Delegate2.create(this,end_faceBook)
  
}

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

function end_faceBook() {
	var m:BookModel = BookModel(this.getModel())
	m.dispatchEvent( { target:this, type:"onFaceBook" } );
}

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

function onResize() {
	/*
	var m:BookModel = BookModel(this.getModel())
	
	if (Configuration.BOOK_X == "center") {
		this._x=m.__width/2
	}else {
		this._x=Configuration.BOOK_X
	}
	
	if (Configuration.BOOK_Y == "center") {
		this._y=Math.round(m.__height/2-( Configuration.PAGE_HEIGHT/2 +Configuration.FRAME_SHEET))
	}else {
		this._y=Configuration.BOOK_Y
	}
	/*/
	this._x=Configuration.PAGE_WIDTH
}

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

function shov(){
	this._visible=true
}

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

function hide(){
	this._visible=false
}

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

function onLoadingRight() {
	var m:BookModel = BookModel(this.getModel())
	m.__currentRight.StartAnimationSheet()
}

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

function onLoadingLeft() {
	var m:BookModel = BookModel(this.getModel())
	m.__currentLeft.StartAnimationSheet()
}

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

public function addSheet(nr:Number, nr2):SheetView {  
  var m:BookModel=BookModel(this.getModel())
  var symmetry=getSymetria(nr)  ////if symmetry=-1 ==> page right ; if symmetry=1 ==> page left
  var dep=symmetry*nr 
  if (__pages[nr].sheet==undefined&& nr >= 1&&__pages[nr].thumb != undefined&&this.getInstanceAtDepth(dep) == undefined) {
			  
	 if (nr2==1) {
		///trace("addSheet z Book Model = " + nr + "  dep = " + dep)
	}else {
		///trace("addSheet  = " + nr + "  dep = " + dep)
	}
	  
  var linkage="_sheet"///(symmetry==-1) ? "_sheet_left" : "_sheet_right"
  var _sheet:SheetView = SheetView(this.attachMovie(linkage, "_sheet" + dep, dep))
  _sheet.setModel(getModel())
  _sheet.setModel(m)
  m.addEventListener("onTweenPageEnd", _sheet)
  m.addEventListener("onSheetBackStart",_sheet)
  m.addEventListener("onSheetBackEnd", _sheet)
  m.addEventListener("onStartDragSheet", _sheet)
  m.addEventListener("onLoadingLeft", _sheet)
  m.addEventListener("onLoadingRight", _sheet)
  m.addEventListener("onNextPage", _sheet)
  m.addEventListener("onPrevPage", _sheet)
    
  m.__pages[nr].sheet = _sheet
  if (symmetry == -1) {
	m.__pages[nr+1].sheet = _sheet  
  }else {
	  m.__pages[nr-1].sheet = _sheet  
  }
  
  _sheet.symmetry = symmetry
   _sheet.__book=this
  _sheet.setModel(m)
  _sheet.cacheAsBitmap=true
  _sheet.__deph=dep
  _sheet.__nr=nr

  if(_sheet.symmetry==-1){ ///if right page
    _sheet.rightwing()
	_sheet.setPositionRight()
  }else{  ///left
	_sheet.leftwing()
	_sheet.setPositionLeft()
  }
  
  }else{
	this.getInstanceAtDepth(dep)._visible = true
	__pages[nr].sheet._visible=true
  }
  
 _sheet.ini();
 return _sheet 
}

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

  function setCurent() {
		 
	var m:BookModel=BookModel(this.getModel())
	var oldCurrentLeft = m.__currentLeft
	var oldCurrentRight = m.__currentRight

	//////////////////OLD
	
	////right
	var target=this
	var max=target.getNextHighestDepth()-1  ///max deph
	var left:SheetView=target.getInstanceAtDepth(max) 
	m.__currentLeft=left
	/////////////////////////left
	max+=1
	max*=-1
	if(max==0){ ////if first page
		max=-1
		m.__currentLeft=undefined
	}
	var right:SheetView=target.getInstanceAtDepth(max)
	m.__currentRight = right

	////////////////////////////////////////////NEW
	var nr_left:Number
	var nr_right:Number
	var sheet_left:SheetView
	var sheet_right:SheetView
	var adres:String=m.getAdres()
	if (adres.indexOf("-") >= 0) {
	nr_left=adres.split("-")[0]	
	nr_right = adres.split("-")[1]
	}else {
		
		if(adres=="1"){
		nr_right = 1		
		}else {
		nr_left=m.getPageLength()				
		}
	}
		
	////right sheet
	sheet_right=m.__pages[nr_right].sheet
		
	////left sheet
	sheet_left=m.__pages[nr_left].sheet
	
	if(m.getNumberNotLoadedLeft()!=0&&m.getNumberNotLoadedRight()!=0){
	if (m.__currentLeft.__nr > oldCurrentLeft.__nr || m.__currentRight.__nr > oldCurrentRight.__nr) {
		///trace("rightLoading = true")
		m.__rightLoading=true
	}else {
		///trace("rightLoading = false")
		m.__rightLoading=false
	}
	}
}

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

function removeAll(flaga:Boolean){
	var m:BookModel=BookModel(this.getModel())
	for(var i in this){
		var sheet:SheetView = SheetView(this[i])
		remove(sheet)
	}
	m.dispatchEvent({target:this,type:"onRemoveAll"})
}

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

private function remove(sheet:SheetView) {
	var m:BookModel=BookModel(this.getModel())
	  if (sheet.symmetry==-1) {
		  SheetView.loadedPages[sheet.__nr] = undefined
		  SheetView.loadedPages[sheet.__nr + 1] = undefined
	  }else {
		  SheetView.loadedPages[sheet.__nr] = undefined
		  SheetView.loadedPages[sheet.__nr -1] = undefined
	  }
      sheet.StopAnimationSheet()
	  sheet.removeLoading()
	  sheet.swapDepths(100)
	  sheet.removeMovieClip()
	  
	  m.__pages[sheet._next_page.nr].page = undefined
	  m.__pages[sheet._mc_main.nr].page = undefined
	  
}

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

private function onSheetBackEnd(obj_:Object) {
	
	var mc:SheetView=SheetView(obj_.target)
   var m:BookModel=BookModel(this.getModel())
   mc.__sheetPosition="down"
   m.__blockade=false
   mc.setDepth()
 }

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

private function addMaxSheet(strona){
	
	/*
	var tab=[]
	for(var i in this){
		var sheet:SheetView=this[i]
		if(typeof(sheet)=="movieclip"){
		var obj=new Object()
		obj.sheet=sheet
		tab.push(obj)
		}
	}
	tab.sortOn("page",Array.NUMERIC)
	if(strona=="R"){
	   var value=tab[tab.length-1]
	}else{
	   var value=tab[0]
	}
	var sheet:SheetView=value.sheet
	sheet.addSheet()
	
	return sheet
	/*/
	
	}
	
/////////////////////////////////////////////////////////////////////////////////////////////

function onTweenPageEnd(obj_:Object) {
	var m:BookModel = BookModel(this.getModel())
	var sheet_:SheetView = SheetView(obj_.target)
	var automat_speed:Boolean = Boolean(obj_.automat_speed)
	sheet_.__sheetPosition="down"
     
  if (sheet_.symmetry == -1) {  ////rotate right - left
	sheet_.symmetry=1
    sheet_.__nr++
    sheet_.leftwing()
	
  }else { ///left-rigt
	sheet_.symmetry=-1
    sheet_.__nr--
    sheet_.rightwing()
  }
  setCurent()
  sheet_.shadowVisible(false)
  m.__blockade=false
  m.__currentLeft.checkLoaded()
  m.__currentRight.checkLoaded()
  sheet_.tweenMouse=undefined

  
 ///////////////////////////////////////////manul drag sshet
  
  if(m.manual==true){
    	 	 
  if (Configuration.REVERSE_BOOK == "false") {
	    
        if (m.__currentRight.__nr != undefined) {	
		m.setPage(m.__currentRight.__nr)
		}else {
		m.setPage(m.__currentLeft.__nr)
		}
	
  }else {
	  
	    if(m.__currentRight.__nr!=undefined){	  
	    m.setPage(   (m.getPageLength()-m.__currentRight.__nr) +1  )
		}else {
		m.setPage(  (m.getPageLength() - m.__currentLeft.__nr) + 1  )
		}
		  
  }
     m.select_loadImage()
  
  }
  

 ////if start from function automat_speed in SheetView 
if(automat_speed!=true){ 
cleaning()
}
}

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

   public function cleaning() {
	  
	 if(Configuration.LOAD_ALL_PAGES=="false"){
	    var array_remove:Array=[] 
	    var m:BookModel = BookModel(this.getModel())
		var currentRight:Number = m.__currentRight.__nr
		var currentLeft:Number = m.__currentLeft.__nr
		var difference:Number = Configuration.MAX_DIFFERENCE_LOADED_SHEET

		////right
	    for (var i = currentRight + difference+1 ; i < m.__pages.length ; i++) {
			var page:PageView = PageView(__pages[i].page)
			if (page != null) {
				array_remove.push(page)			
			}
		}
		
		/////left
		var start_i:Number = currentLeft - difference
		if (currentLeft>difference) {
				
		for (var i = 0 ; i < start_i ; i++) {
			var page:PageView = PageView(__pages[i].page)
			if (page != null) {
				array_remove.push(page)			
			}
		}
		}
			
		//////////////remove
		for (var i in array_remove) {
			var page:PageView = PageView(array_remove[i])
			
			page.dispoze()
			
			//page.sheet.removeMovieClip()
			page.sheet._visible=false
			
		}
			
	 }
	 
  }
	
////////////////////////////////////////////////////////////////////////////////////////////

   private function getSymetria(nr:Number){if(nr%2==0){return 1;}else{ return -1;}}

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

   public function onSetPage(obj:Object) {
	  var m:BookModel = BookModel(this.getModel())
	  if(m.__blockade==false){
	  __pages = m.__pages
	  if(m.createBook==false){
      this.firstgotoPage(obj.nr)
	  m.createBook=true
	  }else {
	     gotoPage(obj.nr)  
	  }
	  }
	  
	 
   }

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

private function gotoPage(nr_:Number) {
	var m:BookModel = BookModel(this.getModel())
	var current:Number = m.getCurrentNumber()
	var goto_nr:Number = nr_
	if (m.__currentLeft.__nr == goto_nr || m.__currentRight.__nr == goto_nr) {
		return;
	}
	if(goto_nr>__pages.length-1){goto_nr=__pages.length-1}
	if (goto_nr < 1) { goto_nr = 1 }
	
////////////////////////////////////////////////////////////////////////////////////////////////

	if (goto_nr > current) {
	var goto_nr2:Number = goto_nr
	if ( (goto_nr2 %= 2) == 0) {
		goto_nr+=1		
	}
	if((goto_nr-current)>2){	
	var old_sheet
	for (var i = current+1 ; i < goto_nr - 2 ; i++) {
	var page:PageView = m.getPage(i)	
	var sheet:SheetView=page.sheet
	if (old_sheet != sheet) {	
	sheet.automat_speed()
	sheet.StopAnimationSheet()
	sheet._visible = false
	}
	old_sheet=sheet
	}
	sheet._visible=true
		
	addSheet((goto_nr - 2))
	m.__currentRight=m.__pages[(goto_nr - 2)].sheet
	}
	m.nextPage()

	} else if(goto_nr < current){
//////////////////////////////////////////////////////////////////////////////////////////////////
	if ((current - goto_nr) > 2) {
	var old_sheet=undefined
	for (var i = current - 2; i > goto_nr+1  ; i--) {
	var page:PageView = m.getPage(i)	
	var sheet:SheetView=page.sheet
	
	if (old_sheet != sheet) {	
	sheet.automat_speed()
	sheet.StopAnimationSheet()
	sheet._visible = false
	///trace("sheet prev= "+m.getPage(i).sheet)
	}
	old_sheet=sheet
	}
	sheet._visible=true
	///trace("left goto_nr = "+goto_nr)
	addSheet((goto_nr +1))
	m.__currentLeft=m.__pages[(goto_nr +1)].sheet
	}
	m.prevPage()
	
     }
}

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

function onMouseUp() {
	var m:BookModel = BookModel(this.getModel())
	//  * FOR TEST * //
	//if (_root.nic == undefined) {
	//m.getPage(3).sheet.automat_speed()
	///_root.nic = 1
	///}
	///trace("nr = " + m.getPage(12).sheet._visible)
	///m.getPage(12).sheet._visible=false
	///	m.getPage(test_nr).sheet._alpha = 0
	//m.getPage(test_nr).sheet.speed_move_sheet()	
	//trace("vidsi = " + m.getPage(11).sheet._visible)
	////m.getPage(11).sheet._visible = false
	
}
	
///////////////////////////////////////////////////////////////////////////////////////////////	
	
	private function firstgotoPage(nr_:Number) {
		var m:BookModel = BookModel(this.getModel())
		removeAll()

		var nr:Number = nr_
		
		if(nr>__pages.length-1){nr=__pages.length-1}
		if (nr < 1) { nr = 1 }
		
		if (nr == 1) {
						
		    var a:SheetView = addSheet(nr)
			var a:SheetView = addSheet(nr+2)
			
			for (var i = 1; i < m.__pages.length; i++) {
							
				if (  (i % 2) != 0 ) {
					///addSheet(i)
				}
			}
	
		}else if (nr==__pages.length-1) {
			  var a:SheetView = addSheet(nr)
		}else{
			if( (nr%2)==0){
				var a:SheetView=addSheet(nr)
				var a:SheetView=addSheet(nr+1)
			}else{
				var a:SheetView=addSheet(nr)
				var a:SheetView = addSheet(nr - 1)
				///var a:SheetView=addSheet(nr+2)
			}
				
		}
		
		setCurent()
				
		m.__rightLoading=true
		m.loadImage(nr)
		
	}
	
//////////////////////////////////////////////////////////////////////////////////////////

function interval_select_image() {
	clearInterval(inter_select_image)
	
	var m:BookModel = BookModel(this.getModel())
	m.select_loadImage()
}


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

function onLoadInit(target:MovieClip) {
	var m:BookModel = BookModel(this.getModel())
	m.select_loadImage()
		
	//clearInterval(inter_select_image)
    ///inter_select_image=setInterval(this,'interval_select_image',100)
	//m.select_loadImage()
		
	/*
	var m:BookModel = BookModel(this.getModel())
    var nr = m.loadedPageNr///target._parent.nr
	var nrLeft = m.__currentLeft.__nr
	var nrRight = m.__currentRight.__nr
 
  var notLoadedRight:Number = m.getNumberNotLoadedRight()
  var notLoadedLeft:Number=m.getNumberNotLoadedLeft()
    
  if (m.getPage(nrLeft).loaded == false && nrLeft != undefined) {
	  m.loadImage(nrLeft)
  }else if (m.getPage(nrRight).loaded==false&&nrRight!=undefined) {
	  m.loadImage(nrRight)
  }else if (m.__rightLoading == true) {
	  if(notLoadedRight>0){
	   m.loadImage(notLoadedRight)
	  }else {
		  
		  m.loadImage(notLoadedLeft)
	  }
 }else if (m.__rightLoading == false) {
	  if(notLoadedLeft>0){
	   m.loadImage(notLoadedLeft)
	  }else {
		    m.loadImage(notLoadedRight)
	  }
 }
  
  
  ////////////////////zaladowalem ostatnio strone
  if (nr == m.getPageLength()) {
	    trace("zaladowałem ostatnio strone  = "+nr)
	    m.__rightLoading = false
	    m.loadImage(notLoadedLeft)
  }
  
  ////////////////////zaladowalem pierwsza strone
  if (nr == 1) {
	  trace("zaladowalem pierwsza strone = "+nr)
	  m.__rightLoading = true
	  m.loadImage(notLoadedRight)
  }
  
  /*/
	
}

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



}

