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 flash.geom.Rectangle
import flash.geom.Point
    
class templateBook.mvctemplate.PageView extends AbstractView {
	 
var mcArea:MovieClip                             //area
var containerFrame:MovieClip                     //container for frame
var nr:Number                                    //numer page
var content:MovieClip                            //content for page
var mcMask:MovieClip                             //movie clip mask
var preloader:MovieClip                          //preloader for page
var book:BookView                                //instance BookView
var sheet:SheetView                              //instance SheetView
var __loader:MovieClipLoader                     //loader
var loaded:Boolean=false                         //if loaded page
var __numberSheet:MovieClip                      //number sheet
var shadow_left:MovieClip                        //shadow left
var shadow_right:MovieClip                       //shadow right
var contentW:Number                              //content width
var contentH:Number                              //content height
var bitmap:BitmapData                            //bitmap data
var bitmap2:BitmapData                           //bimtap data
var bitmap_pattern:BitmapData                    //bimtap data
var bitmap_smoothing:BitmapData                  //bimtap data
var radius:Number                                //radius
var now_loaded:Boolean=false                     //if now loaded page
var was_dispoze:Boolean = false                  //if dispoze bitmap
var frame_preloader:MovieClip                    //preloader frame
var timeClickZoom:Number                         //time clisk for zoom
var offset_doubleClickZoom:Number                //offset for double click
    
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function PageView() {
		radius=Number(Configuration.RADIUS)
	}
		
////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function get node() {
		  var m:BookModel = BookModel(BookModel.getInstance())
		return m.__pages[nr].node
	}
//////////////////////////////////////////////////////////////////////////////////////////////////

    function onLoad() {
		offset_doubleClickZoom = Number(Configuration.OFFSET_DOUBLECLICKZOOM)
	    addNumberPage()
		visible_unvisible_shadow()
		preloaderStart()
		create_background()
	}
	
///////////////////////////////////////////////////////////////////////////////////////////

   function create_background() {
	 var bck_mc_main = containerFrame.frame.createEmptyMovieClip("mc_bck", 0)
	 bck_mc_main.onPress = Delegate2.create(this, onPressBackground)
	 
	 ///////show mouse or hide mouse
	 if(node.attributes.redirectURL.length>0){
	   bck_mc_main.useHandCursor = true
	 }else {
	   bck_mc_main.useHandCursor = false
	 }

	 ////color background
	 var color
	 var color_global = Configuration.COLOR_BACKGROUND
	 var color_local = node.attributes.colorBackground
	 if (color_local.length > 0) {
		 color=color_local
	 }else {
		 color=color_global
	 }
	 	 
	 
	  if (this._name == "__mc_main") {
		 Drawing.curve1(bck_mc_main, Configuration.PAGE_WIDTH, Configuration.PAGE_HEIGHT, radius, color, 100);
	  }else {
		 Drawing.curve2(bck_mc_main, Configuration.PAGE_WIDTH, Configuration.PAGE_HEIGHT, radius, color, 100);  
	  }
	 
   }
	
////////////////////////////////////////////////////////////////////////////////////////////   
   
function onPressBackground() {
	var redirectURL:String = node.attributes.redirectURL
	if (redirectURL.length > 0) { 	/////redirectURL
	var url:String = redirectURL.split(",")[0]	
	var window:String = redirectURL.split(",")[1]
	getURL(url,window)
	}
	else if(Configuration.ZOOM_DOUBLE_CLICK_ENABLED=="true"){////////ZOOM - double click
	var m:BookModel = BookModel(BookModel.getInstance())
	if (  (getTimer() - timeClickZoom) < this.offset_doubleClickZoom) { ///doublce click
	if (m.allVisibleBook() == true) {
		m.zoomTo(Number(Configuration.ZOOM_MAX_DOUBLE_CLICK))
	}else {
	    m.autoSizeBook()
	    m.resize()
	}
	}
	timeClickZoom = getTimer()
	}
	///////////////////////////////END ZOOM - double click
}
      
///////////////////////////////////////////////////////////////////////////////////////////

    function addNumberPage() {
	var m:BookModel = BookModel(BookModel.getInstance())
	var visibleNumber = node.attributes.visibleNumber
	var visible = (visibleNumber == undefined || visibleNumber == "") ? Configuration.VISIBLE_NUMBER_PAGE : visibleNumber
	
	
	if(visible=="true"){  /////if visible
	var colorNumber=node.attributes.colorNumber
	this.__numberSheet= this.attachMovie("NumberSheet","NumberSheet",123123123)
	this.__numberSheet.t.autoSize = true
		
	if (Configuration.REVERSE_BOOK == "true") {
	var label_nr:Number = (m.getPageLength()-nr)+1
	}else{
	var label_nr:Number = nr
	}

	/////title for page
	var title:String = node.attributes.titlePage
	if (title == undefined) {
	   title=""	
	}
	
	/////color number page
	var color_number_page=(colorNumber==undefined||colorNumber=="") ? Configuration.COLOR_NUMBER_PAGE : colorNumber
	
	///////color title page
    var color_title_page = Configuration.COLOR_TITLE_PAGE
     
		
	__numberSheet.t.html = true
	
	if (nr % 2 == 0) { ////left page
	__numberSheet.t.autoSize=true	
	this.__numberSheet.t.htmlText = "<b><FONT COLOR='" + color_number_page + "'>" + label_nr + "</FONT></b>"
	this.__numberSheet.t2.htmlText = "<FONT COLOR='" + color_title_page + "'>   " + title + "</FONT>" ;
	__numberSheet.t2._x=__numberSheet.t._x+__numberSheet.t._width+0
	}else {    ////right page
	__numberSheet.t2.autoSize = true
	__numberSheet.t2.htmlText = "<FONT COLOR='" + color_title_page + "'>" + title + "</FONT>"
	this.__numberSheet.t.htmlText =  "<b><FONT COLOR='" + color_number_page + "'>   " + label_nr + "</b>";	
	__numberSheet.t2._x=__numberSheet.t._x-__numberSheet.t2._width+3
	}
	
      
	////korekte position
	var position_x:Number = Configuration.NUMBER_PAGE_X
	var position_y:Number = Configuration.NUMBER_PAGE_Y
		
	
	if (nr % 2 == 0) {  ////left page
	  this.__numberSheet._x = position_x//sheet.__width / 2 - this.__numberSheet._width / 2
	  this.__numberSheet._y = int( (sheet.__height ) - this.__numberSheet.t._height)-position_y
	}else {  //// right page
	  this.__numberSheet._x = int (   (sheet.__width-position_x) - this.__numberSheet.t._width  )
	  this.__numberSheet._y = int( (sheet.__height ) - this.__numberSheet.t._height)-position_y
	}
		
	}
		
	}
	
///////////////////////////////////////////////////////////////////////////////////////////

   function visible_unvisible_shadow() {
	   if (this.node.attributes.visibleShadow == "false") {
		   shadow_left._visible = false
		   shadow_right._visible=false
	   }else {
		   shadow_left._visible = true
		   shadow_right._visible = true
		   
	   }
	}
	
///////////////////////////////////////////////////////////////////////////////////////////

   function loadImage() {
	   
	   var m:BookModel = BookModel(BookModel.getInstance())
	   if (loaded == false && now_loaded == false) {
		m.now_loaded = true   		   
		now_loaded=true
		
	   __loader = new MovieClipLoader()
	   __loader.addListener(this)
	   __loader.addListener(book)
	   __loader.addListener(sheet)
	   var url = book.__pages[nr].thumb
	   __loader.loadClip(url, content)   
	   sheet.addNumberLoading(nr)
	   }
	  
	   preloaderStart()
   }
   
///////////////////////////////////////////////////////////////////////////////////////////


function create_frame() {
    var m:BookModel = BookModel(BookModel.getInstance())
	var space:Array=Configuration.BACKGROUND_PAGE_IF_VISIBLE_PRELOADER_MARGIN.split(",")
	var spaceLeft:Number = Number(space[0])
	var spaceRight:Number = Number(space[1])
	var spaceUp:Number = Number(space[2])
	var spaceDown:Number = Number(space[3])
	var w:Number = Configuration.PAGE_WIDTH-(spaceLeft+spaceRight)
	var h:Number = Configuration.PAGE_HEIGHT-(spaceUp+spaceDown)
	var color = Configuration.BACKGROUND_PAGE_IF_VISIBLE_PRELOADER.split(",")[0]
	var alpha = Configuration.BACKGROUND_PAGE_IF_VISIBLE_PRELOADER.split(",")[1]
	frame_preloader = this.createEmptyMovieClip("mcFrame_", 432432)
	frame_preloader._x = spaceLeft
	frame_preloader._y = spaceUp
	
	//Drawing.frame(frame_preloader,1, w,h, [color,alpha]);
	Drawing.rectangle(frame_preloader, 0, 0, w, h, [color, alpha]);
}

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

private function preloaderStart() {
	create_frame()
		
	/*
	preloader = this.attachMovie("_preloader_sheet", "_preloader_", 1200, { _x:book.__width/2,_y:book.__height/2 } )	
	preloader.preloader.circle.onEnterFrame = function() {
		this._rotation += 15		
		
	}
	/*/
	
var obj={}
obj.colorBackground = Configuration.PRELOADER_BACKGROUND_COLOR
obj.colorCurrent = Configuration.PRELOADER_CURRENT_COLOR
obj.preview = false
obj.color_text=Configuration.PRELOADER_TEXT_COLOR
obj.label = Configuration.PRELOADER_LABEL
obj._x = book.__width / 2
obj._y = book.__height / 2

preloader = this.attachMovie("_preloader_rectangle", "_preloaderCircle_", 432438, obj)
preloader._x = book.__width / 2 - 147 / 2
preloader._y=book.__height/2

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

function onLoadProgress (target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void {
	 var m:BookModel = BookModel(BookModel.getInstance())
	var p:Number = int(   (bytesLoaded / bytesTotal)*100  )
	preloader.setPosition(p) 
	m.onLoadProgress(this, bytesLoaded, bytesTotal)
}

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

  function onLoadInit(target) {
		  
	  var m:BookModel = BookModel(BookModel.getInstance())
	  m.now_loaded = false
	  now_loaded = false
	  		
	  m.loadedPageNr = nr
	  m.loadedPage++
	  
	  if (m.loadedPage >= m.getPageLength()) {
		 m.endLoadPages()		  
	  }
	  		  
	  preloader.setPosition(100) 
		
	  var pageW:Number = Number(Configuration.PAGE_WIDTH)
	  var pageH:Number = Number(Configuration.PAGE_HEIGHT)

	  //////add page for list pages
	    if (m.doublePage(nr) == false) {
		  bitmap = new BitmapData(pageW, pageH, true, 0xFF000000)
	      bitmap.draw(target)
	    }else{
	       ///////////if big image 
	      bitmap_pattern=new BitmapData(pageW*2, pageH, true, 0xFF000000)
	      bitmap_pattern.draw(target)
	  	 
	      bitmap2=new BitmapData(pageW,pageH, true, 0xFF000000)
	      bitmap2.copyPixels(bitmap_pattern, new Rectangle(pageW, 0, pageW, pageH), new Point(0, 0));
	   	  bitmap=bitmap2
	    }	  
	    
	  m.dispatchEvent({target:this,type:"onLoadInitPage"})

	  contentW = target._width
	  contentH = target._height
  
	  loaded=true
	  preloader.removeMovieClip()
	  //frame_preloader.removeMovieClip()
	  sheet.removeNumberLoading(nr)
	  
	  var w:Number=Number(Configuration.PAGE_WIDTH)
	  var h:Number=Number(Configuration.PAGE_HEIGHT)

	  ////////smoothing
	  var smoothing:String = (node.attributes.smoothing==undefined) ? Configuration.SMOOTHING  : node.attributes.smoothing
	  if (smoothing=="true") {
		     bitmap_smoothing = new BitmapData(contentW,contentH, true, 0xFF000000)
	         bitmap_smoothing.draw(target)
	         target.attachBitmap(bitmap_smoothing, 1, "auto", true)
	  }
		  
	  //////resize
	  var area:MovieClip = target.getMcArea()
	  var areaWidth:Number = area._width
	  var areaHeight:Number = area._height
	  
	  var newW:Number = (w * target._width) / areaWidth
	  var newH=(newW*target._height)/target._width
	  
	   	
	  if (area == undefined) {
		// target._width = w
	     ///target._height = h
	  }else {
		// target._width = newW
		 //target._height = newH
	  }
	  var prevThumb:String = node.previousSibling.attributes.thumb
	  if (m.doublePage(nr)) {
		content._x = -Number(Configuration.PAGE_WIDTH)  
		
	  }
	  create_mask()	  

	if (was_dispoze == true) {
		dispoze()
		was_dispoze=false
	}
	
  }
  
///////////////////////////////////////////////////////////////////////////////////////////

function dispoze() {
	 var m:BookModel = BookModel(BookModel.getInstance())
	 	 
	if(now_loaded==false){
	 __loader.unloadClip(content)
	 bitmap.dispose()
	 bitmap_pattern.dispose()
	 bitmap_smoothing.dispose()
	 bitmap2.dispose()
	 
    SheetView.loadedPages[nr] = 0

	 loaded = false
	 
	}else {
		was_dispoze=true		
	}
}
  
///////////////////////////////////////////////////////////////////////////////////////////
  
  function create_mask() {
	  var w:Number = Number(Configuration.PAGE_WIDTH)
	  var h:Number = Number(Configuration.PAGE_HEIGHT)
	  mcMask = this.createEmptyMovieClip("_mcMask", 2500)

	 if (this._name == "__mc_main") {
		  Drawing.curve1(mcMask, w, h, radius, 0xFF0000, 0);
	 }else {
		  Drawing.curve2(mcMask, w, h, radius, 0xFF0000, 0);
	 }

	content.setMask(mcMask)
	  
  }
  
  
 ///////////////////////////////////////////////////////////////////////////////////////////


}
