import mx.data.types.Str;
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;
     

class templateBook.mvctemplate.ContainerBookView extends AbstractView {
			
/////////////////////////////////////////////////////////////////////////////////////////

var tweenX:Tween                 //tween X
var tweenY:Tween                 //tween Y
var tweenScaleX:Tween            //tween for scale X
var tweenScaleY:Tween            //tween for scale Y
var __oefZoom:MovieClip          //onEnterFrame for zoom
var __oefMove:MovieClip          //onEnterFrame for move
var widthBook:Number             //width book
var heightBook:Number            //height book
var widthArea:Number             //width area
var heightArea:Number            //height area
var newPosMoveX:Number           //position X container book
var newPosMoveY:Number           //position Y container book

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

public function defaultController (model:Observable):Controller {
  return null////new BackgroundController(model)
 }
 
/////////////////////////////////////////////////////////////////////////////////////////

function ContainerBookView(){
	
}
 
/////////////////////////////////////////////////////////////////////////////////////////

function onLoad() {
	var m:BookModel = BookModel(this.getModel())
	__oefZoom = this.createEmptyMovieClip("_oefZoom", 100)
	__oefMove = this.createEmptyMovieClip("_oefMove", 200)
	this._xscale = m.__zoom
	this._yscale = m.__zoom	
	m.center()
}

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

function onMouseMove() {
	var m:BookModel = BookModel(this.getModel())
	setVariable()
	
	if (m.change_positions_book()) {  //if widthBook > widthArea OR heightBook > heightArea
				
		if(m.__container.__target._ymouse<m.getAreaHeight()){
		__oefMove.onEnterFrame = Delegate2.create(this, oef_move)
		}
		
	}else {
	}
}

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

function setVariable() {
	var m:BookModel = BookModel(this.getModel())
	widthBook = m.getWidthCurrent() + m.margin_widthCurrent
	heightBook = m.getHeightCurrent() + m.margin_heightCurrent
	widthArea = m.getAreaWidth()
	heightArea = m.getAreaHeight()
}

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

function oef_move() {
		
	var m:BookModel = BookModel(this.getModel())
	///delete __oefZoom.onEnterFrame
	
	setVariable()
	var margin:Number=150
	var mouseCont:MovieClip=m.__container.__target
		
	var marginH:Number = widthBook - widthArea
	var marginV:Number = heightBook - heightArea
	
	var mouseXProp:Number = (mouseCont._xmouse) / (widthArea)
	
	if(m.toolsPosition=="down"){
	var mouseYProp:Number = mouseCont._ymouse / heightArea
	}else {
	var mouseYProp:Number = (mouseCont._ymouse-m.toolsHeight) /  heightArea
	}
			
	if (mouseYProp >1) {
		mouseYProp=1
	}
	
	var tools:ToolsView = m.__container.__tools
	var hitTestTools:Boolean=!tools.hitTest(_root._xmouse,_root._ymouse,true)
	
	if (hitTestTools) {
		if(marginH>0){
	       newPosMoveX = margin-(mouseXProp * (marginH+margin*2))
		}else {
		  newPosMoveX = m.centerX()	
		}
		if (marginV > 0) {
			if(m.toolsPosition=="down"){
	        newPosMoveY = margin - (mouseYProp * (marginV + margin * 2))
		    }else {
			newPosMoveY = m.toolsHeight+margin - (mouseYProp * (marginV + margin * 2))	
			}
		}else {
		  newPosMoveY = m.centerY()	
		}
	}
	
	this._x += Math.round((newPosMoveX - this._x) / 3)
	this._y += Math.round((newPosMoveY - this._y) / 3)
		
	m.resize()
}

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

function onChangedZoom() {
	var m:BookModel = BookModel(this.getModel())
	zoomTo(m.__zoom)
	//onMouseMove()
}

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

function zoomTo(scale_:Number) {
	delete __oefMove.onEnterFrame
	//__oefZoom.onEnterFrame=Delegate2.create(this,oef_zoom,scale_)
	oef_zoom(scale_)
}

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

function oef_zoom(scale_) {
	
	var m:BookModel = BookModel(this.getModel())
	var easing:Number = 1
		
	////new scale
	var newXScale:Number =  scale_
	var newYScale:Number =  scale_
	
	///////point mouse	
	var __mouseX:Number = this._xmouse
	var __mouseY:Number = this._ymouse
	
	///////LOCAL TO GLOBAL 1
	var obj1:Object = Converter.convert( { x:__mouseX, y:__mouseY }, this)
		
	var deltaScaleX:Number =	Math.round(newXScale - this._xscale)
	var deltaScaleY:Number =	Math.round(newYScale - this._yscale)
		
	this._xscale += Math.round(deltaScaleX / easing)
	this._yscale += Math.round(deltaScaleY / easing)
	
	////////korekte position X and Y
	var newX:Number = m.centerX(newXScale)
	var newY:Number = m.centerY(newYScale)
	var deltaX:Number = (  newX  - this._x)
	var deltaY:Number = (   newY - this._y)
	this._x += Math.round(deltaX / easing)
	this._y += Math.round(deltaY / easing)
	
	
	///////LOCAL TO GLOBAL 2
	var obj2:Object = Converter.convert( { x:__mouseX, y:__mouseY }, this)
	///////////////
	var tools:ToolsView = m.__container.__tools
	var hitTestTools:Boolean = tools.hitTest(_root._xmouse, _root._ymouse, true)
	if(hitTestTools==false){
	this._x +=  Math.round(obj1.x - obj2.x);
	this._y +=  Math.round(obj1.y - obj2.y);
	}
		
		
	m.resize()
	
}

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

function onResize(obj:Object) {
if(obj.resize!=true){	
var m:BookModel = BookModel(this.getModel())
delete __oefMove.onEnterFrame
//delete __oefZoom.onEnterFrame
m.autoSizeBook()
m.center()
}
}

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

}

