import mx.controls.streamingmedia.HMiniPlayBarTray;
import mx.transitions.Tween;
import mx.transitions.easing.*
import templateBook.mvctemplate.*
import templateBook.mvc.*
import templateBook.I.*
import flash.display.BitmapData
import templateBook.util.Observable
import templateBook.Configuration
import flash.filters.GlowFilter;
      
class templateBook.mvctemplate.ListPagesView extends AbstractView {
	 
var container:MovieClip                        //container for thumbs
var loader:MovieClipLoader                     //loader
var array:Array                                //array data
var thumbWidth:Number                          //width thumb
var thumbHeight:Number                         //height thumb
var thumbs:Array                               //array thumbs
var spaceX:Number = 25                         //space thumb
var bcg:MovieClip                              //background big
var bcg_visible:MovieClip                      //background for thumbs 
var tweenIn:Tween                              //tween show
var tweenOut:Tween                             //tween hide
var newPositionX:Number = 0                    //position x
var margin:Number=200                          //margin mouse x
var currentLeft:ThumbView                      //thumb current left
var currentRight:ThumbView                     //thumb current right
var speed:Number = 20                          //speed mouse move
var easing:Number = 3                          //easing
var colorBcg:String                            //color background
var colorLine:String                           //color line
var sizeLine:Number = 2                        //size line
var btn_close:MovieClip                        //close list

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

	function ListPagesView() {
		speed = Number(Configuration.SPEED_LIST_PAGE)
		easing = Number(Configuration.EASING_LIST_PAGE)
		
		colorBcg = Configuration.COLOR_BCG_LIST_PAGE
		colorLine = Configuration.COLOR_LINE_LIST_PAGE
						
		var m:BookModel = BookModel(BookModel.getInstance())
		thumbWidth = m.thumbWidth
		thumbHeight = m.thumbHeight
		
		thumbs=["empty"]
		container = this.createEmptyMovieClip("mcContainer", 1)
					
		loader = new MovieClipLoader()
		loader.addListener(this)
		
		//button close list
		if (Configuration.LIST_BUTTON_CLOSE_BCG_COLOR != undefined) {
			NewColor.setColor(btn_close.bcg, Configuration.LIST_BUTTON_CLOSE_BCG_COLOR);
		}
        if (Configuration.LIST_BUTTON_CLOSE_SYMBOL_COLOR != undefined) {
			NewColor.setColor(btn_close.symbol, Configuration.LIST_BUTTON_CLOSE_SYMBOL_COLOR);
		}

		_visible=false
	}
		
//////////////////////////////////////////////////////////////////////////////////////////////////

    function onLoad() {
				
		////background color
		var color = colorBcg.split(",")[0]
		var alpha = colorBcg.split(",")[1]
		NewColor.setColor(bcg_visible.bcg, color)
		bcg_visible.bcg._alpha = alpha
		
		///glow for background
		var obj=bcg_visible.filters[0]
		obj.color = Configuration.GLOW_COLOR_BCG_LIST
		bcg_visible.filters = new Array(obj)
		if(Configuration.GLOW_BCG_LIST_VISIBLE=="false"){
		bcg_visible.filters = new Array()
		}
				
		//////background big
		var color_all = Configuration.COLOR_BCG_BIG_LIST_PAGE
		var color = color_all.split(",")[0]
		var alpha = color_all.split(",")[1]
		NewColor.setColor(bcg, color);
		bcg._alpha = alpha
		
		
		bcg.onPress = function() { }
		bcg.useHandCursor=false
		createContainerForList()
		
		//event for button close
		btn_close.onPress = Delegate2.create(this, onPressButtonClose);
	}

//////////////////////////////////////////////////////////////////////////////////////////////////	
	
	function onPressButtonClose() {
		 var m:BookModel = BookModel(BookModel.getInstance())
		 m.hideThumbs(true);
	}
	
//////////////////////////////////////////////////////////////////////////////////////////////////

  function onLoadInitPage(obj:Object) {
	    	  
	  var m:BookModel = BookModel(BookModel.getInstance())
	  var page:PageView=PageView(obj.target)
	  var nr:Number = page.nr
	  var content:MovieClip = page.content
	  var attributes:Object = m.__pages[nr]
	  var thumb_list:String = attributes.thumb_list
	  	  	  
	  m.addListPage(nr - 1,page)
	  array[nr-1]=page
	  	  
	  ///////add bitmap OR load image
	  var thumb:ThumbView = ThumbView(thumbs[nr])
	  if(thumb_list==undefined){
	      thumb.addBitmap(page.bitmap)
	  }else {
		  thumb.loadImage(thumb_list)
	  }
  	  
  }
  
////////////////////////////////////////////////////////////////////////////////////////////////

function onSetPage() {
	select_current()
	
}

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

function onTweenPageEnd() {
	select_current()
}

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

function select_current(){
    var m:BookModel=BookModel(BookModel.getInstance())
	var left=(m.__currentLeft.__nr)
	var right=(m.__currentRight.__nr)
	var nrPage:Number
	
	currentLeft.unSelect()
	currentRight.unSelect()
	currentLeft = undefined
	currentRight = undefined
		
	if(left==undefined){
	currentLeft=thumbs[1]
	}else if(right==undefined){
	currentRight=thumbs[m.getPageLength()]
	}else{
	currentLeft = thumbs[left]
	currentRight = thumbs[right]
	
	}
		
	currentLeft.select()
	currentRight.select()
}

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

function createContainerForList() {
	 var m:BookModel = BookModel(BookModel.getInstance())
	 var pagesLength:Number = m.getPageLength()
	 resize()
	 var oldThumb:ThumbView
	 for (var i = 0; i < pagesLength; i++) {
		var thumb:ThumbView = ThumbView(container.attachMovie("_thumbList", "_thumbList_" + i, i))
		thumb.nr=i+1
		thumbs.push(thumb)
		
		if (oldThumb == undefined) {
			thumb._x=0
		}else {
			var doublePage:Boolean = m.doublePage(thumb.nr)
			if (doublePage == true) {
				thumb.doublePage = true
				oldThumb.doublePage = true
				thumb.bcg._alpha = 0
				oldThumb.bcg._width=thumbWidth*2
			}
			var space:Number=(doublePage==true) ? 0 : spaceX
			thumb._x = oldThumb._x + thumbWidth+space
			
		}
		thumb._x=int(thumb._x)
		oldThumb=thumb
	 }
	
}

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

function onShovThumbs() {
	shov()
}

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

function onHideThumbs(obj:Object) {
	hide(obj.slow)	
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////

function shov() {
	    var m:BookModel = BookModel(BookModel.getInstance())
	    _visible = true
		
	    var current:ThumbView
		if (currentLeft == undefined) {
			current=currentRight
		}else {
			current=currentLeft
		}
		if(current.nr==1){
		container._x = margin
		}else if (current.nr==m.getPageLength()) {
			container._x=-container._width+m.getAreaWidth()-margin
		}else {
			container._x = -current._x + m.getAreaWidth() / 2-thumbWidth
						
		}
		_alpha = 0
	    reset_tween()
		tweenIn=new Tween(this,'_alpha',Strong.easeOut,0,100,1,true)
		resize()
		
		newPositionX=container._x
		bcg_visible.onMouseMove=Delegate2.create(this,move_thumbs)
}

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

function move_thumbs() {

	if(isHitTest()){
	    bcg_visible.onMouseMove=null
		var m:BookModel = BookModel(BookModel.getInstance())
		var marginM:Number = 100 ///margin mouse
		this.onEnterFrame = function() {
		var x:Number = (  (this._xmouse - marginM) / (m.getAreaWidth() - marginM * 2)  )
		
		///////////////////////////////
		var x2:Number = _xmouse - (m.getAreaWidth() / 2) ///// [-getAreaWidth()/2;getAreaWidth()/2]
		var x3:Number = (x2/(m.getAreaWidth()/2)) * speed  ///// [-speed;speed]
		var deltaX:Number = container._width - m.getAreaWidth()
		if(isHitTest()){
		//newPositionX =  -(  deltaX  * x  )
		newPositionX +=  -x3
		}
		
		if (newPositionX > margin) {
			newPositionX=margin
		}
		
		var end=-container._width+m.getAreaWidth()-margin
		if (newPositionX < end ) {
			newPositionX= end
		}
		
		container._x += int(  (newPositionX - container._x) / easing )
		updateAfterEvent();

		}
		
	}
}

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

function isHitTest():Boolean {
	if (bcg_visible.hitTest(this._xmouse, this._ymouse, true)){
	   return true
    }else {
	   return false
    }
}

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

function reset_tween() {
		tweenIn.stop()
		tweenOut.stop()
}

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

function hide(slow_:Boolean) {
	 reset_tween()
	 	 
	 if(slow_==true){
	 tweenOut = new Tween(this, '_alpha', Strong.easeInOut, this._alpha, 0, 1, true)
	 tweenOut.onMotionFinished=Delegate2.create(this,endOut)
	 }else{
	_visible = false
	bcg_visible.onMouseMove = null
	delete this.onEnterFrame
	 }
}

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

function endOut() {
	_visible=false	
}

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

function resize() {
	 var m:BookModel = BookModel(BookModel.getInstance())
	bcg._width = m.getAreaWidth()
	bcg._height = m.__height
	container._y = m.autoSizeMarginH_up+m.getHeightCurrent()/2-thumbHeight/2
	var color = colorLine.split(",")[0]
	var alpha = colorLine.split(",")[1]
	bcg_visible.bcg._width = m.getAreaWidth()+20
	bcg_visible.bcg._height = thumbHeight +55
	bcg_visible.bcg._x=-10
	bcg_visible._y =   container._y-20//m.getAreaHeight()/2-bcg_visible._height/2	
	bcg_visible.lineStyle(sizeLine, color, alpha)
	bcg_visible.lineTo(bcg_visible.bcg._width, 0)
	bcg_visible.moveTo(0, bcg_visible.bcg._height)
	bcg_visible.lineTo(bcg_visible.bcg._width,bcg_visible.bcg._height)
	
	///position for button close
	btn_close._x = m.getAreaWidth() - btn_close._width -10
	btn_close._y = bcg_visible._y-25
}

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

function onResize() {
	 var m:BookModel = BookModel(BookModel.getInstance())
	resize()	
	
	
}

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


}
