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.TableContentsList extends MovieClip {
	
var container:MovieClip                         //container list
var container_big:MovieClip                     //container 
var book_model:BookModel                        //instance module book
var mc_mask:MovieClip                           //mask
var  __scroll:ScrollComponents                  //components scroll
var bcg:MovieClip                               //background
var marginV:Number = 5                          //margin vertical
var marginH:Number = 5                          //margin horizontal
var bcg_width:Number                            //background width
var bcg_height:Number = 129                     //background height
var table_contents:TableContentsView            //instance table contents view
var maxRow:Number                               //maximum row
var array_rows:Array                            //array with rows
var __node:XMLNode                              //rows data
 
///////////////////////////////////////////////////////////

function TableContentsList() {
	maxRow=Configuration.CHAPTER_LIST_MAX_ROWS   
}

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

function onLoad() {
	Mouse.addListener(this)
	////color bcg
	if(Configuration.CHAPTER_LIST_BCG_COLOR!=undefined){
	NewColor.setColor(bcg, Configuration.CHAPTER_LIST_BCG_COLOR)
	}
	
	
}

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

function onMouseWheel(delta_) {
	var delta:Number=13
	if(this.bcg.hitTest(_root._xmouse,_root._ymouse,true)){
	if (delta_ > 0) {
		__scroll.setScrollPosition(__scroll.getScrollPosition()+delta)
	}else {
		__scroll.setScrollPosition(__scroll.getScrollPosition()-delta)
	}
}
}

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

function create(node_:XMLNode, select_:Number) {
	bcg_width = Number(Configuration.CHAPTER_LIST_WIDTH);
	
	__node=node_
	array_rows=[]
	container = this.createEmptyMovieClip("mc_container", 1).createEmptyMovieClip("mc_container2", 1);
	container_big = container._parent
	container_big._x = marginH
	container_big._y = marginV
	bcg._width = bcg_width
	var old_row:MovieClip
	for (var i = 0; i < node_.childNodes.length; i++) {
	var node:XMLNode = node_.childNodes[i]
	var row:MovieClip = container.attachMovie("_row_list_chapter", "_row_list_chapter_" + i, i);
	array_rows.push(row);
	row.node = node
	row.t.border = false;
	row.t.htmlText = "<b>" + node.attributes.label + "<b>";
	row.t._width = bcg_width - 20;
	row._y = (!old_row) ? 0 : old_row._y + old_row._height //+2
	row.onRollOver = Delegate2.create(this, onRollOverRow, row);
	row.onRollOut = Delegate2.create(this, onRollOutRow, row);
	row.onPress = Delegate2.create(this, onPressRow, row);
	if (node.attributes.id == select_) {
		row.enabled = false
		row.t.textColor=Configuration.CHAPTER_LIST_TEXT_ACTIVE
	}else {
		row.t.textColor=Configuration.CHAPTER_LIST_TEXT_NORMAL
	}

	old_row=row	
	}
	addScroll()
	this._y = -bcg._height-4
}
 
////////////////////////////////////////////////////////////

function getPageNumber(id_chapter_:Number):Number {
	var m:BookModel=book_model
	for (var i = 0; i < m.__xml.firstChild.childNodes.length;i++){
		var node_book:XMLNode = m.__xml.firstChild.childNodes[i]
		var chapter_id:Number = Number(node_book.attributes.chapter_id)
		if (chapter_id == id_chapter_) {
			return (i+1)			
		}
}	
}

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

function onPressRow(row) {
	var node:XMLNode = row.node
	var id = node.attributes.id
	book_model.setPage(getPageNumber(id))
	table_contents.remove_list();
}

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

function onRollOverRow(row:MovieClip) {
	////rectangle
	var mc_line:MovieClip = row.createEmptyMovieClip("mc_line", row.t.getDepth() - 10)
	var w:Number=bcg_width-15
	Drawing.rectangle(mc_line, 0, 0,w,19, [Configuration.CHAPTER_LIST_RECTANGLE_COLOR, 100]);
	mc_line._y = 0
	mc_line._x = -marginH
	
	//text
	row.t.textColor = Configuration.CHAPTER_LIST_TEXT_ACTIVE;
	/*
	mc_line.lineStyle(1, Configuration.CHAPTER_LIST_TEXT_NORMAL, 100)
	mc_line.moveTo(0, 0)
	mc_line.lineTo(row.t.textWidth+2, 0)
	mc_line._y = row.t.textHeight
	/*/
	
}

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

function onRollOutRow(row:MovieClip) {
	//text
	row.t.textColor = Configuration.CHAPTER_LIST_TEXT_NORMAL;
	row.mc_line._visible=false
	row.mc_line.removeMovieClip();
}

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

function scrolling_item() {
			if (__node.childNodes.length > maxRow) {
				return true
			}else {
				return false
			}
}

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

function addScroll() {
	////create mask
	mc_mask = this.createEmptyMovieClip("mcMask", 23123)
	mc_mask._x = -marginH;
	var wMask:Number = bcg._width// - marginH * 2
	if(scrolling_item()){
	var hMask:Number = array_rows[maxRow - 1]._y + array_rows[maxRow - 1]._height//bcg._height-marginV*2
	}else {
	var hMask:Number = array_rows[array_rows.length-1]._y + array_rows[array_rows.length-1]._height//bcg._height-marginV*2	
	}
	Drawing.rectangle(mc_mask,marginH,marginV, wMask, hMask, [0xFF0000,50]);
	container_big.setMask(mc_mask);
	////bcg size
	bcg._height = mc_mask._height + 2 * marginV
	if(scrolling_item()){
	///////////////set property for scroll
	__scroll = ScrollComponents(this.attachMovie("_scrollv1", "_scrollv1_", 45));
	__scroll.position="V" ////  V or H
	__scroll._x=bcg_width-10//container._x+container._width -5 ///position x
	__scroll._y=container._y+15  //// position y
	__scroll.pressBackground=true  ///// onPressBackgroundScroll (false/true)
	__scroll.height = mc_mask._height+20 ///width scroll
	__scroll.width = 1       /////height scroll
    __scroll.firstValue=0 /////first Position
    __scroll.lastValue=-container._height+mc_mask._height-20    /////last position
	__scroll.proportion = 0.5 ///( 0....1)
	__scroll.speedScroll=5   ////speed scroll
	__scroll.offsetArrow=40; 
	__scroll.spaceArea=7
	__scroll.arrowVisible=false    ////arrow visible (true/false)
	////color ( if color = "" , is NOT colorable)
	
	if(Configuration.CHAPTER_COLOR_SCROLL_BCG!=undefined){
	__scroll.colorBackground = Configuration.CHAPTER_COLOR_SCROLL_BCG   ///color background
	}

	if(Configuration.CHAPTER_COLOR_SCROLL!=undefined){
	__scroll.colorScrollOut = Configuration.CHAPTER_COLOR_SCROLL    ////color ScrollOut
	}
	//__scroll.colorScrollOver="0x999999"     ///color ScrollOver
	__scroll.colorArrow="0xFFFFFF"           ////color arrow
	__scroll.colorArrowBackground="0x747474"  /////color arrow background
	__scroll.addEventListener("onChangeScroll",this)   ////add events change scroll
	__scroll.addEventListener("onPressScroll",this)    ////add events press scroll
	__scroll.addEventListener("onReleaseScroll",this)  ////add events release scroll
	__scroll.setScrollPosition(130)   ///// set current scroll position 
	
	}
}

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

function onChangeScroll() {
var value = __scroll.getScrollPosition()   ////current scroll Position
container._y = value
}

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

function remove() {
	table_contents.remove_list()
}

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

}

