import mx.data.encoders.Num;
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 mx.transitions.easing.*
       
class templateBook.mvctemplate.TableContentsView extends AbstractView {
	
var inter                                      //handler for setInterval
var __xml:XML                                  //xml data
var __chapters:XMLNode                         //list chapters
var mc_current:MovieClip                       //current movieclip
var mc_select:MovieClip                        //select movieclip
var mc_list:TableContentsList                  //movieclip list
var current_chapter_node:XMLNode               //current chapter
var over_select:MovieClip                      //movieclip mouse over
var title:TextField                            //title
var tween1:Tween                               //tween
	
///////////////////////////////////////////////////////////

function TableContentsView(){
   _visible=false
}

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

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

function onLoad() {
	
setSize()
	
mc_select.onPress = Delegate2.create(this, onPressSelect)
mc_select.onRelease=Delegate2.create(this,onReleaseSelect)
mc_select.onRollOver = Delegate2.create(this,onRollOverSelect)
mc_select.onRollOut = Delegate2.create(this,onRollOutSelect)

/////title label and color
if(Configuration.CHAPTER_TITLE_LABEL!=undefined){
title.text = Configuration.CHAPTER_TITLE_LABEL
}
title.textColor = Configuration.CHAPTER_TITLE_COLOR


///background color for title
if(Configuration.CHAPTER_TITLE_COLOR_BACKGROUND!=undefined){
NewColor.setColor(mc_current.bcg, Configuration.CHAPTER_TITLE_COLOR_BACKGROUND);
}


/////color select button
if(Configuration.CHAPTER_BUTTON_BCG_COLOR!=undefined){
NewColor.setColor(mc_select.bcg, Configuration.CHAPTER_BUTTON_BCG_COLOR)
}
////color arrow select button
if(Configuration.CHAPTER_BUTTON_ARROW_COLOR != undefined) {
NewColor.setColor(mc_select.symbol,Configuration.CHAPTER_BUTTON_ARROW_COLOR)	
}

//loadXML()

var m:BookModel = BookModel(this.getModel())
__chapters = m.date_chapter

}

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

function setSize() {
	var m:BookModel = BookModel(this.getModel())
    var w:Number = Configuration.CHAPTER_TITLE_WIDTH;
	
	//bacground
	var bcg:MovieClip = mc_current.bcg
	var mc_mask:MovieClip = mc_current.bcg.mc_mask;
	var mc_end:MovieClip = mc_current.bcg.end;
	//bcg._width = w;
	mc_mask._width = w;
	mc_end._x = w;
	
	
	
	
	
	//text label
	var t:TextField = mc_current.t
	t._width = w-t._x*2-22
	
	
	//btn select
	mc_select._x = w - mc_select._width - 2
	mc_select.mc_area._width = w;
	
	
}

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

function onPressSelect() {
	if (mc_list._currentframe==undefined) {
		create_list();	
	}else {
      	remove_list()
	}
}

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

function onReleaseSelect() {
	
}

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

function onRollOverSelect() {
over_select = mc_select.over.attachMovie("_btn_over_select", "_btn_over_select_", 1)

////color bcg
if(Configuration.CHAPTER_BUTTON_BCG_COLOR_OVER!=undefined){
NewColor.setColor(mc_select.bcg, Configuration.CHAPTER_BUTTON_BCG_COLOR_OVER);
}

///color symbol
if(Configuration.CHAPTER_BUTTON_ARROW_COLOR_OVER!=undefined){
NewColor.setColor(mc_select.symbol, Configuration.CHAPTER_BUTTON_ARROW_COLOR_OVER);
}

}

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

function onRollOutSelect() {
over_select.removeMovieClip();
	
////color arrow select button
if(Configuration.CHAPTER_BUTTON_ARROW_COLOR != undefined) {
NewColor.setColor(mc_select.symbol,Configuration.CHAPTER_BUTTON_ARROW_COLOR)	
}
}

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

function remove_list() {
	mc_list.removeMovieClip()
	///un blockade
	var m:BookModel=BookModel(this.getModel())
	m.blockade_page_over = false
	m.blockade_page_press = false
}

/////////////////////////////////////////////////////////////
  
function create_list() {
	var m:BookModel=BookModel(this.getModel())
	mc_list = TableContentsList(this.attachMovie("_table_contents_list", "_table_contents_list_", 4343, { book_model:m } ));
	mc_list.table_contents=this
	mc_list.create(__chapters, current_chapter_node.attributes.id);
	
	///blockade
	m.blockade_page_over = true
	m.blockade_page_press = true
	
}

/////////function only for test////////////////////////////////////////////////////

function loadXML() {
   __xml = new XML()
   __xml.ignoreWhite = true
   __xml.onLoad = Delegate2.create(this, onLoadXml);
   __xml.load("xml/chapters1.xml");
}

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

function onLoadXml() {
	var m:BookModel=BookModel(this.getModel())
	__chapters = __xml.firstChild
}

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

function change_swfAdress(obj) {
	var m:BookModel=BookModel(this.getModel())
	if (Configuration.REVERSE_BOOK == "false") {
	////////right
	var nr_right:Number = obj.nr_right
	////////left
	var nr_left:Number = obj.nr_left
	////////nr page	
	if(nr_right!=undefined){
	var nr_page:Number = nr_right
	}else {
	var nr_page:Number = nr_left
	}
	}else {
	////////right
	var nr_right:Number = obj.nr_right
	////////left
	var nr_left:Number =  obj.nr_left
	////////nr page	
	if(nr_left!=undefined){
	var nr_page:Number = nr_left
	}else {
	var nr_page:Number = nr_right
	}	
	nr_page=(m.getPageLength()+1)-nr_page
	}
	///////////get node from xml
	var chapter_id=	m.__pages[nr_page].chapter_id
	var chapter_node:XMLNode = getNodeChapter(chapter_id);
	var label:String = chapter_node.attributes.label
	////current node chapter
	current_chapter_node = chapter_node
	////add label for mc_current
	mc_current.t.htmlText = "<b>"+label.toUpperCase()+"</b>";
	mc_current.t.textColor = Configuration.CHAPTER_DISPLAY_COLOR
}

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

function getNodeChapter(nr_page_:Number):XMLNode {
	for (var i = 0; i < __chapters.childNodes.length; i++) {
		var node:XMLNode = __chapters.childNodes[i]
		var id:Number = Number(node.attributes.id);
		if (id == nr_page_) {
			return node;
		}
	}
}

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

function onResize() {
	var m:BookModel=BookModel(this.getModel())
	if(m.change_positions_book()==false){
	this._x=10
	this._y = m.__height - 50
	}
}

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

function onFaceBook() {
	clearInterval(inter);
	inter = setInterval(this, 'shov', 1300);
}

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

function shov() {
	clearInterval(inter);
	_alpha = 0
	_visible = true	
	tween1.stop()
	tween1 = new Tween(this, '_alpha', Strong.easeInOut, 0, 100, 1, true);
}

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


}

