import templateBook.mvctemplate.*;
import templateBook.I.*
import templateBook.util.Observable
import flash.display.BitmapData
import templateBook.Configuration
import templateBook.templateMp3.mvcMp3.Mp3Model;
import TextField.StyleSheet
import templateBook.SWFAddress;
import templateBook.SWFAddressEvent;


class templateBook.mvctemplate.BookModel extends Observable{

var soundSheet:Sound                       //sound for page in book
var __container:Book	                   //container for Book
var __pages:Array                          //list pages
var __chapter:Array                        //chapter for book                      
var date_chapter:XML                       //date chapters
var date_books:XML                         //date for book  
private var __nrPage:Number                //nr page
var __currentRight:SheetView               //current right page
var __currentLeft:SheetView                //current left page
var __background:String                    //background for book
var __width:Number                         //stage width
var __height:Number                        //stage height
var __blockade:Boolean = false             //blockade for bending the corner
var __position:String                      //position toolbar (only one position now )  
var __xml:XML                              //primary xml
var __hornVisible:Boolean=true             //visible area for mouse ( on corner - class AreaHornView.as )
private var __zagniezdzone                 //private structure used to manufacture xml
var __rightLoading:Boolean=true            //if right page onLoad
var __player:Mp3Model                      //module player mp3
var player_mp3_loaded:Boolean=false        //if module player loaded
var volume                                 //volume for player mp3
var toolsHeight:Number                     //height for toolbar
var toolsPosition:String                   //up OR down ( now only down )
var lightBox                               //lightbox module
var toolsPositionY:Number = 15             //position y for icon in toolbar
var listPages:Array                        //list pages
static var __instance:BookModel            //instancje BookNodel
var thumbWidth:Number                      //thumb widh in list
var thumbHeight:Number                     //thumb height in list  
var __shovLightBox:Boolean=false           //show or hide lightbox

////////////////zoom variables
var __zoom:Number                          //first zoom
var minZoom:Number                         //min zoom
var maxZoom:Number                         //max zoom
var speedZoom:Number                       //speed zoom
var stepZoom:Number                        //step zoom

////////autoScale (if change size browser)
var autoScaleMin:Number = 10               //autoscale min
var autoScaleMax:Number = 100              //autoscale max
var autoSizeMarginW:Number = 70            //margin horizontal
var autoSizeMarginH_up:Number = 5          //margin vertical - up
var autoSizeMarginH_down:Number = 5        //margin vertical - down

////margin for tween book
var margin_widthCurrent:Number = 50        //correction width
var margin_heightCurrent:Number = 0        //correction height


var __printPage:Number                     //number for print page
var __cssStyle:StyleSheet                  //css style
var createBook:Boolean = false             //if create book it createBook=true
var listener_change_swfAdress:Function     //listener for swfAddress
var currentPageLoading:Array               //current loading page
var manual:Boolean = false                 //if you manually change the page with the mouse
var focusNumber:Boolean=false              //enabled/disabled keyboard
var loadedPage:Number = 0                  //page that is loaded
public var loadedPageNr:Number             //page number that is loaded
var max_difference_loaded_sheet:Number     //number of pages that is loaded in the background
var load_all_pages:Number                  //whether all the pages are loaded 
var key:Boolean=false                      //whether the key is pressed on the keyboard
var load_blockade:Boolean=false            //lock load
public var now_loaded:Boolean=false        //now the page load
var addres:String                          //address in browser
var blockade_page_over:Boolean = false     //blockade page over
var blockade_page_press:Boolean = false    //blockade page press
var fla                                    //timeline in book.fla
var current_book:String;                   //current book




///////////////////////////////////CONSTRUKTOR//////////////////////////////

function BookModel(book_:Book) {

if(Configuration.BUTTON_BROWSER_ENABLED=="true"){
SWFAddress.setHistory(true)	
}else {
SWFAddress.setHistory(false)	
}
	
if(Configuration.TOOLS_VISIBLE == "true") {
toolsHeight = 62	
}else{
toolsHeight = 0
}
toolsPosition = Configuration.TOOLS_POSITION


/////load pages
max_difference_loaded_sheet = Number(Configuration.MAX_DIFFERENCE_LOADED_SHEET)
load_all_pages = Number(Configuration.LOAD_ALL_PAGES)

	
minZoom = Number(Configuration.ZOON_MIN)
maxZoom = Number(Configuration.ZOOM_MAX)
speedZoom = Number(Configuration.ZOOM_SPEED)
stepZoom = Number(Configuration.ZOOM_STEP)


thumbWidth = Number(Configuration.THUMB_WIDTH)
thumbHeight = Number(Configuration.THUMB_HEIGHT)

	
__instance=this	
listPages=[]	
	

this.__container=book_
Stage.addListener(this)

if(Configuration.SUPPORT_KEYBOARD=="true"){
Key.addListener(this)
}

}

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

function show_list_book() {
	 dispatchEvent({target:this,type:"onShowListBook"})	
}

function hide_list_book() {
	 dispatchEvent({target:this,type:"onHideListBook"})	
}

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

function shovLightBox(xml_:String) {
	    __shovLightBox=true
		dispatchEvent({target:this,type:"onShovLightBox",xml:xml_})
}

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

function hideLightBox() {
	__shovLightBox = false
	dispatchEvent({target:this,type:"onHideLightBox"})
}

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

static function getInstance() {
	return __instance
}

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

function addListPage(index_:Number,page_:PageView) {
	   	listPages[index_]=page_
}

////////////////////////////css style

function setStyleCss(css_) {
	__cssStyle=css_
}

function getStyleCss() {
	return __cssStyle
}

/////////////////////////////////////////formular

function shovForm() {
	choosePrintHide()
    dispatchEvent({target:this,type:"onShovForm"})
}

function hideForm() {
	__container.formular.hide()	
}


/////////////////////////////////////////////shov Thumbs

function shovThumbs() {
	 dispatchEvent({target:this,type:"onShovThumbs"})
}

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

function hideThumbs(slow_:Boolean) {
	dispatchEvent({target:this,type:"onHideThumbs",slow:slow_})
}

/////////////////////////////////////////////////////////////////print

function choosePrintPage() {
	 hideForm()
	 dispatchEvent({target:this,type:"onChoosePrint"})
}

function choosePrintHide() {
	__container.printChoose.hide()
}

function printPage(nr_:Number) {
		__printPage = nr_
		dispatchEvent({target:this,type:"onSetPrintPage"})
}

function getPrintPage() {
	return __printPage	
}

////////////////////////////////////////////////////////////end print

function allVisibleBook():Boolean {
	if ( visible_horizontal()==true && visible_vertical()==true ) {
		return true;
	}else {
		return false;
	}
}

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

function change_positions_book() {
	/*
	 * //if widthBook > widthArea OR heightBook > heightArea
	 */
	 if ( visible_horizontal()==false || visible_vertical()==false  ) {
	       return true;
	}else {
		 return false;
	 }
}

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

function visible_horizontal() {
	if ( (getWidthCurrent()+margin_widthCurrent  ) < getAreaWidth() ) {
		return true
	}else {
		return false
	}
}

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

function visible_vertical() {
	if ( (getHeightCurrent()+margin_heightCurrent) < getAreaHeight() ) {
		return true
	}else {
		return false
	}
}

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

function resize() {
		this.dispatchEvent({target:this,resize:true,type:"onResize"})
}

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

function autoSizeBook() {
		
	var wArea:Number = getAreaWidth() - autoSizeMarginW
	var hArea:Number = getAreaHeight() - (autoSizeMarginH_up+autoSizeMarginH_down)
		
	var resize:Resize = new Resize(wArea, hArea, getWidthCurrent(), getHeightCurrent())
	var obj:Object = resize.min()
		
	setWidthCurrent(obj.w, autoScaleMin, autoScaleMax)
	
	center()
}

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

function centerX(scaleX_) {
	return int(__width/2-getWidthCurrent(scaleX_)/2)
}

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

function centerY(scaleY_) {
	/*
	if(toolsPosition=="down"){
	return int(  (__height - toolsHeight)/2- getHeightCurrent(scaleY_)/2    )
	}else {
	return int(  (__height+toolsHeight)/2- getHeightCurrent(scaleY_)/2)	
	}
	/*/
	
	//return int(  (__height /2 - getHeightCurrent(scaleY_) / 2 )- 0 )
		
	var wArea:Number = getAreaWidth() - autoSizeMarginW
	var hArea:Number = getAreaHeight() - (autoSizeMarginH_up + autoSizeMarginH_down)
	return int(  autoSizeMarginH_up +hArea / 2 - getHeightCurrent(scaleY_) / 2 )
	
}

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

function center() {
var container:ContainerBookView=__container.__containerBook	
container._x = centerX()
container._y = centerY()
}

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

function setWidthCurrent(value_:Number,min_:Number,max_:Number) {
	var width:Number = Number(Configuration.PAGE_WIDTH) * 2
	var scale:Number = 100 * value_ / width
	
	/////korekte scale
	if (min_ != undefined) {
		if (scale < min_) {
			scale=min_
		}
	}
	if (max_ != undefined) {
		if (scale > max_) {
			scale=max_
		}
	}
		
	this.zoomTo(scale)
	
}

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

function setHeightCurrent(value_:Number,min_:Number,max_:Number) {
	var height:Number = Number(Configuration.PAGE_HEIGHT) 
	var scale:Number = 100 * value_ / height
	
	/////korekte scale
	if (min_ != undefined) {
		if (scale < min_) {
			scale=min_
		}
	}
	if (max_ != undefined) {
		if (scale > max_) {
			scale=max_
		}
	}
		
	this.zoomTo(scale)
	
}

///////////height book/////////////////////////////////////////////////////////////////////////////

function getHeightCurrent(scale_) {
	var __yscale:Number=this.__container.__containerBook._yscale
	var scale = (scale_ == undefined) ? __yscale : scale_
	var newHeight:Number=Configuration.PAGE_HEIGHT * (scale / 100)
	return newHeight
}

////////////width book/////////////////////////////////////////////////////////////////////////////////

function getWidthCurrent(scale_) {
	var __xscale:Number=this.__container.__containerBook._xscale
	var scale = (scale_ == undefined) ? __xscale : scale_
	var newWidth:Number=2*Configuration.PAGE_WIDTH * (scale/ 100)
    return 	newWidth
}

//////////////width area for book////////////////////////////////////////////////////////////////////////////////////////

function getAreaWidth() {
	return __width
}

//////////////width area for book////////////////////////////////////////////////////////////////////////////////////////

function getAreaHeight() {
	return __height-toolsHeight
}

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

function doublePage(nr_:Number):Boolean {
	  /*
	  var prevThumb:String = page_.node.previousSibling.attributes.thumb
	  
	  if (page_.node.attributes.thumb == prevThumb) {
		 return true	  
	  }else {
		  return false
	  }
	  /*/
		  
	  var prevThumb:String = __pages[nr_ -1].thumb
	   if (__pages[nr_].thumb == prevThumb) {
		  return true	  
	  }else {
		  return false
	  }
}

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

function korekteZoom() {
	
	if (__zoom > maxZoom) {
		__zoom=maxZoom
	}
	if (__zoom < minZoom) {
		__zoom=minZoom
	}
}

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

function zoomIn(zoom_:Number) {
	__zoom += (zoom_ == undefined) ? stepZoom : zoom_
	korekteZoom()
	
	dispatchEvent({target:this,type:"onChangedZoom"})
} 

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

function zoomTo(zoom_:Number) {
	__zoom = Math.round(zoom_)
	if (__zoom % 2 != 0 ) {
		__zoom-=1
	}
	dispatchEvent({target:this,type:"onChangedZoom"})
}

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

function zoomOut(zoom_:Number) {
	__zoom += (zoom_ == undefined) ? -stepZoom : -zoom_
    korekteZoom()
	dispatchEvent({target:this,type:"onChangedZoom"})
}

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

function init_swfAdress() {
	
}

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

function onLoadProgress(page:PageView, bytesLoaded:Number, bytesTotal:Number) {
	var nr:Number = page.nr
	dispatchEvent({page:page,bytesLoaded:bytesLoaded,bytesTotal:bytesTotal,type:"onLoadProgressPage"})
}

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

function endLoadPages() {
dispatchEvent({target:this,type:"onEndLoadPages"})
}

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

public function startLoadPages() {
				
	var nr:Number
	if (SWFAddress.getPathNames().length==0||SWFAddress.getPathNames().length==1) { ////if empty or it's no nomber
		nr=Number(Configuration.START_FIRST_PAGE)
	}else {
			
	var value:String = SWFAddress.getPath().substr(1)
	var value:String = SWFAddress.getPathNames()[SWFAddress.getPathNames().length-1]
	var nr:Number
	
	if (value.indexOf("-")>=0) {
		nr = Number(value.split("-")[1])
	}else {
		nr = Number(value)
	}
		
	}
		
	setPage((nr))
			
	/////////////////////////////////////////////////////////
	listener_change_swfAdress = Delegate2.create(this, this.change_swfAdress)
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE,listener_change_swfAdress)
	
	/////////////////////////
	change_swfAdress()
	
	////////////////
    dispatchEvent({target:this,type:"onStartLoadPages"})

}

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

function getAdres() {
	return SWFAddress.getPathNames()[0];	
}

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

function change_swfAdress(event:SWFAddressEvent) {
	
	var value:String = SWFAddress.getPath().substr(1)
	var value:String = SWFAddress.getPathNames()[SWFAddress.getPathNames().length-1]
	var nr:Number

	/////////////////////////////////////////////////
	if (value.indexOf("-")>=0) {
		nr = value.split("-")[1]
	}else{
		nr=Number(value)
	}
	//////////////////////////////////////////////////
			
	//////////number view
	if (SWFAddress.getPathNames()[1] == undefined) {
	__container.__numberPage.face_current_page("1")
 	//__container.__tools.__numberPageView.face_current_page("1")
	}else{
	///__container.__tools.__numberPageView.face_current_page(SWFAddress.getPathNames()[0])
	__container.__numberPage.face_current_page(SWFAddress.getPathNames()[SWFAddress.getPathNames().length - 1])
	}
	
	//////////current page left and right
	var nr_left:Number
	var nr_right:Number
	/////if not symbol "-"
	if (value.indexOf("-") == -1) {
		
		if (Configuration.REVERSE_BOOK == "false") { /////if NO reverse BOOK
		
		if(value=="1"){
		nr_right = 1
		}else {
		nr_left = Number(value)
		}
		
		}else { ///// if reverse BOOK
			
		if (value == "1") {
			nr_left = 1
		}else {
		nr_right = Number(value)
		}
			
		}
	
	}else { ///if ist symbol "-"
		nr_left = value.split("-")[0]
		nr_right=value.split("-")[1]
	}
		
	dispatchEvent({target:this,type:"change_swfAdress",nr_left:nr_left,nr_right:nr_right})
	
	/////////////////////////if manual tween page
	if (manual == false) {
	setPage2(Number(nr))
	}else {
		
	}
	manual = false
		
	////////title page
	//SWFAddress.setTitle(Configuration.TITLE_BOOK_IN_BROWSER+" "+value);
	var title_browser:String = current_book + " " + value
	if (value == undefined) {
	var title_browser:String = current_book
	}
	SWFAddress.setTitle(title_browser);
		
	//////change book
	var name_book:String = SWFAddress.getPathNames()[0];
	if (name_book != current_book) {
		fla.change_book()
	}
	
	
}

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

function setPage(nr_:Number) {
	
				
	if (isNaN(nr_)) {
		return;
	}
			
	///////////////////////////////////////////////////////
	if (nr_ == 1) {
	addres="1"
	}
	else if (nr_==getPageLength()) {
	addres=String(getPageLength())	
	}
	else if (nr_ % 2 == 0) {
	
	if(Configuration.REVERSE_BOOK=="false"){	
	addres = String(nr_) + "-" + (nr_ +1)
	}else {
	addres =  (nr_ +1)+ "-" +	String(nr_)
	}
	
	}else if (nr_ % 2 > 0) {
		
	if(Configuration.REVERSE_BOOK=="false"){	
	addres = (nr_ -1) + "-" + String(nr_)	
	}else {
	addres = String(nr_)+ "-" + (nr_ -1)
	}
	
	}
	//////////////////////////////////////////////////////
	
	
	if (nr_ > getPageLength()) {
		addres=String(getPageLength())
	}else if (nr_<1) {
		addres="1"		
	}
	
	
	if (SWFAddress.getPathNames()[1] == undefined) {
		addres=Configuration.START_FIRST_PAGE
	}
		
	SWFAddress.setValue(current_book+"/"+addres);
		
	if(hitTestHorn()){
	__container.__areaHorn.reset()
	}
		
}

///////////////////Page///////////////////////////////////////////////////////

function setPage2(nr_:Number):Void {
	
if (Configuration.REVERSE_BOOK == "true") {
		nr_=(getPageLength()-nr_)+1
	}
		
this.dispatchEvent({nr:Number(nr_),target:this,type:"onSetPage"})	

}

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

function dispoze() {
	SWFAddress.removeEventListener(SWFAddressEvent.CHANGE, listener_change_swfAdress)
		
	__player.Stop()  ////player book
	lightBox.Stop() ////video in lightbox
	
	SheetView.loadedPages=[]
	
	__instance=undefined
}

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


function onKeyUp () {
	key=false
}

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

function hitTestHorn():Boolean {
	if (this.__container.__areaHorn.hitTest(_root._xmouse, _root._ymouse, true)) {
		return true;
	}else {
		return false;
	}
	
}

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

function onKeyDown (){
	
	if (focusNumber == false) {
				
	///////////////////////////////////////////change Page
	key = true
	
	if (Key.isDown(Key.LEFT)) {
		prev()
	}
	
	if (Key.isDown(Key.RIGHT)) {
		next()
	
	}
	
	if (Key.isDown(Key.UP)) {
		nextChapter()
	}
	
	if (Key.isDown(Key.DOWN)) {
		prevChapter()
	}

	/////////////////////zooom In and Out
	if (Key.getCode()==107) {
		zoomIn()
	}else if (Key.getCode()==109) {
		zoomOut()
	}
	
	////litera J - ladowanie
	if (Key.getCode() == "74") {
		//__container.__prev.hide()
			__container.__prev.onSetPage()
	}

	}
	
}

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

function getPage(nr_:Number):PageView {
	return __pages[nr_].page
}

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

function getNumberNotLoadedRight():Number {
	var nr:Number = this.__currentRight.__nr
	if (nr != undefined && nr != null) {
		
	///////////////////////////////////////////////////////////////	
	var length:Number
	if (Configuration.LOAD_ALL_PAGES == "false") {
		length=nr+Number(Configuration.MAX_DIFFERENCE_LOADED_SHEET)+1
	}else {
		length=this.__pages.length
	}
	////////////////////////////////////////////////////////
		
	for (var i = nr; i < length ; i++) {
		var page:PageView=PageView(this.__pages[i].page)		
		var loaded:Boolean = page.loaded
		if (loaded !=true) {
			return i;
		}
	}
		
	}
	return 0
}

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

function getNumberNotLoadedLeft():Number {
	var nr:Number = this.__currentLeft.__nr
	if (nr != undefined && nr != null) {
		
	////////////////////////////////////////////////////////	
	var length:Number
	if (Configuration.LOAD_ALL_PAGES == "false") {
		length=nr-Number(Configuration.MAX_DIFFERENCE_LOADED_SHEET)
	}else {
		length=1
	}
	///////////////////////////////////////////////////////////
		
	for (var i = nr; i >=length; i--) {
		var page:PageView=PageView(this.__pages[i].page)		
		var loaded:Boolean = page.loaded
		if (loaded !=true) {
			return i;
		}
	}
		
	return 0;
	}
	
}

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

function itsMaxRightLoading(nr) {
	if (nr - __currentRight.__nr >= max_difference_loaded_sheet && __currentRight != undefined) {
			return true;	
	}else {
		return false;
	}
}

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

function itsMaxLeftLoading(nr) {
     if (__currentLeft.__nr - nr >= max_difference_loaded_sheet && __currentLeft != undefined) {
		 return true		 
	 }else {
		 return false;
	 }
}

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

function select_loadImage() {
		
	if (now_loaded == true) {
		return;
	}
				
	var nr = loadedPageNr///target._parent.nr
	var nrLeft = __currentLeft.__nr
	var nrRight = __currentRight.__nr

  var notLoadedRight:Number = getNumberNotLoadedRight()
  var notLoadedLeft:Number = getNumberNotLoadedLeft()
    
  
  if (getPage(nrLeft).loaded == false && nrLeft != undefined) {
	 loadImage(nrLeft)
  }else if (getPage(nrRight).loaded==false&&nrRight!=undefined) {
	  loadImage(nrRight)
  }else if (__rightLoading == true) {
	  if(notLoadedRight>0){
	   loadImage(notLoadedRight)
	  }else {
		loadImage(notLoadedLeft)
	  }
 }else if (__rightLoading == false) {
	  if(notLoadedLeft>0){
	   loadImage(notLoadedLeft)
	  }else {
	   loadImage(notLoadedRight)
	  }
 }
    
  ////////////////////zaladowalem ostatnio strone
  if (nr == getPageLength()) {
	   /// trace("zaladowałem ostatnio strone  = "+nr)
	    __rightLoading = false
	    loadImage(notLoadedLeft)
  }
  
  ////////////////////zaladowalem pierwsza strone
  if (nr == 1) {
	  //trace("zaladowalem pierwsza strone = "+nr)
	  __rightLoading = true
	  loadImage(notLoadedRight)
  }	
   	
}

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

function loadImage(nr_:Number) {
	
if(load_blockade==false){
		
	if(nr_>=1&&nr_<=getPageLength()){
	
	var page:PageView = PageView(__pages[nr_].page)
		
	if (page == null) {
	
		if (nr_ > __currentRight.__nr) {
			
			if(nr_%2==0){
	            var s:SheetView = __container.__bookView.addSheet( (nr_-1),1)
	            loadImage(nr_)
			}else {
	            var s:SheetView = __container.__bookView.addSheet(nr_,1)
	            loadImage(nr_)
			}
			
	}else if (nr_ < __currentLeft.__nr) {
			
			if(nr_%2==0){
	           var s:SheetView = __container.__bookView.addSheet(nr_,1)
	           loadImage(nr_)
			}else {
	           var s:SheetView = __container.__bookView.addSheet( (nr_+1),1)
	           loadImage(nr_)
			}
	}
	return;
	}
	
	page.loadImage()
	dispatchEvent({target:page,type:"onLoadImage"})
	
	}
	
}
}

//////////////////////////////change browser////////////////////////////////////////////

private function onResize() {  
	
	if (Stage["displayState"] == "fullScreen") {
			this.setSize(Stage.width,Stage.height)
	}else{
			
	if(Configuration.STAGE_WIDTH=="Stage.width"&&Configuration.STAGE_HEIGHT=="Stage.height"){
	this.setSize(Stage.width,Stage.height)
	}else {
		this.setSize(Configuration.STAGE_WIDTH,Configuration.STAGE_HEIGHT)
	}

	}
}

///////////////////changed volume in mp3player//////////////////////////////////////////////////////////

function onChangedVolume() {
	var volume1:Number=__player.volumeGlobal
	var volume2:Number=__player.getOldVolume()
	var new_volume = (volume1 > volume2||volume2==undefined) ? volume1 : volume2
	//if (new_volume != undefined) {
		volume=volume1
	///}
	this.dispatchEvent({target:this,type:"onChangedVolumeGlobal"})
}

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

function set mutePlayer(boolean_:Boolean) {
	if (boolean_ == true) {  ////disabled sound
		__player.muteSound=false
		
	}else {  /////enabled sound
		if(__player.click_speaker_off==false){
		__player.muteSound=true
		}
	}
}

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

function setDate(xml_:XML):Void {
	__xml=xml_
	this.__pages=[]
			
	var array:Array = getHasChildNodes(xml_.firstChild,0)
		
	for (var i = 0; i < array.length; i++) {
		var object:Object = array[i]
		object.node.attributes.id = i + 1
		object.node.attributes.index = object.index
				
		if (!object.node.hasChildNodes()) {  ////it is sheet attrib
		      var _sheetAttrib:SheetAttrib = new SheetAttrib(object)
			 _sheetAttrib.id_chapter=__chapter.length-1
			 __pages.push(_sheetAttrib)
			 object.node.attributes.nrSheet=__pages.length
		}
	}
		
	this.__pages.unshift("")
}

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

function shovBook() {
	__container.__bookView.faceBook()
}

//////////////////////chapter////////////////////////////////////////////////////

function setChapter(nr_:Number) {
	var node:XMLNode=__xml.firstChild.childNodes[nr_-1].firstChild
	var nrSheet = node.attributes.nrSheet	
	setPage2(nrSheet)
}

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

function nextChapter() {
	if (this.__blockade == false) {
	var nextNode = getChapter().nextSibling 
	if(nextNode!=null){
	var nrSheet = nextNode.firstChild.attributes.nrSheet
	setPage2(nrSheet)
	}
	}
}

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

function prevChapter() {
   if (this.__blockade == false) {
	var prevNode = getChapter().previousSibling
	if(prevNode!=null){
	var nrSheet = prevNode.firstChild.attributes.nrSheet
	setPage2(nrSheet)
	}
	}
}

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

public function getChapter():XMLNode{	
	var nrRight = __currentRight.__nr
	var nrLeft = __currentLeft.__nr
	var nr=(nrRight) ? nrRight : nrLeft
	var attr:ChapterAttrib = (__pages[nr])
	var node:XMLNode = attr.node.parentNode
	return node
}

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

public function getChapterLength():Number {
	return __chapter.length	
}

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

 function nextPage(target_):Void {
			
	if (target_ == "sheet") {
		manual = true
	}
	 	 
	this.dispatchEvent( { target:target_, type:"onNextPage" } )	
	
}

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

function next() {
		
	if (Configuration.REVERSE_BOOK == "true") {
	var nr:Number = (getPageLength() - __currentLeft.__nr) + 1
	setPage(nr+2)
	}else{
	setPage(__currentRight.__nr+2)
	}
	
	/*
	///////for new arrow :)
	var prev:PrevButtonView = __container.__prev
	var next:NextButtonView = __container.__next
	if(Configuration.REVERSE_BOOK=="false"){
	
	if (__currentRight.__nr==(getPageLength()-1)) {
		next.hide()
	}
	}else {
		if (__currentLeft.__nr == 2) {
			prev.hide()
		}
	}
	/*/
	
}

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

function prev() {
	
	if (Configuration.REVERSE_BOOK == "true") {
		var nr:Number = (getPageLength() - __currentRight.__nr) - 1
	    setPage(nr+1)
	}else {
		setPage(__currentLeft.__nr-1)
	}
	
	/*
	///////for new arrow :)
	var prev:PrevButtonView = __container.__prev
	var next:NextButtonView = __container.__next	
	if(Configuration.REVERSE_BOOK=="false"){
	
	if (__currentLeft.__nr == 2) {
		prev.hide()
	}
	}else {
		
		trace(__currentRight.__nr)
		if (__currentRight.__nr==(getPageLength()-1)) {
		    next.hide()
	     }
				
	}
	/*/
	
}

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

function getCurrentNumber():Number {
	var left:Number=__currentLeft.__nr
	var right:Number=__currentRight.__nr
	
	if ( right != undefined) {
		return right
	}else if (right == undefined) {
		return left+1
	}
	
}

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

function prevPage(target_):Void {
	if (target_ == "sheet") {
		manual=true
	}
	this.dispatchEvent( { target:target_, type:"onPrevPage" } )	
}

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

function setBackground(background_:String):Void{
	this.__background=background_
	this.dispatchEvent({target:this,type:"onChangeBackground"})	
}

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

function getBackground() {
	return __background
}

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

function setSize(width_:Number,height_:Number):Void{
	if(width_=="Stage.width"){
	 __width=Stage.width
	}else{
	__width=width_
	}
	
	if(height_=="Stage.height"){
	__height=Stage.height
	}else{
	__height=height_			
	}
	
	this.dispatchEvent( { target:this, type:"onResize" } )
}

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

function getPageLength(){
	return this.__pages.length-1	
}

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

private function private_getHasChildNodes(child:XMLNode,item_:Number):Array{ 
	var len = child.childNodes.length;
	var old:MovieClip;
    	
	if(Configuration.REVERSE_BOOK=="true"){
	var i:Number = len - 1
	}else {
	var i:Number = 0
	}
	
	for (var ii = 0; ii < len; ii++) {
		var attrib:Object = child.childNodes[i].cloneNode(true).attributes
		attrib.index = i
		attrib.node = child.childNodes[i]
		attrib.item = item_
		
		this.__zagniezdzone.push(attrib)
	
	    if (child.childNodes[i].hasChildNodes()) {
			
		       var new_item:Number
			   new_item=item_+1
			   private_getHasChildNodes(child.childNodes[i],new_item);
		
		}
		
		if (Configuration.REVERSE_BOOK == "true") {
			i--
		}else{
		   i++
		}
    }
	
	
	return this.__zagniezdzone
}

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

public function getHasChildNodes(child:XMLNode,item_:Number){  
	this.__zagniezdzone=[]
	return this.private_getHasChildNodes(child,item_)
}
  
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function loadToContainer(str_:String) {
	__container.__containerClip.loadToContainer(str_)	
}

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

function removeContainer(str_:String) {
	__container.__containerClip.removeContainer(str_)	
}

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

function change_book(address_:String) {
	SWFAddress.setValue(address_);
	
	
}

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


}