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.IconView extends AbstractView {
var widthIcon:Number           //width icon
var heightIcon:Number          //height icon
var spaceIcon:Number           //space icon
var positionX:Number           //position x
var positionY:Number           //position y
var container:MovieClip        //container for icon
var url_data_icon:String       //url for xml
var data_icon:XML              //xml data 
var dir:Number                 //1,-1,0
var orientation:String         //v Or h
var loader:MovieClipLoader     //loader
var alphaOut:Number            //alpha out
var alphaOver:Number           //alpha over
var marginX:Number             //margin X
var marginY:Number             //margin Y
var tween:Tween                //tween for alpha
var inter:Number               //handle for setInterval
	
///////////////////////////////////////////////////////////

function IconView(){

}

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

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

function onLoad() {

}

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

function onFaceBook() {
	clearInterval(inter);
	inter = setInterval(this, 'create', 1500);
}

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

function create() {
	clearInterval(inter);
	 if(Configuration.ICON_VISIBLE=="true"){
           init()	
	}	
}

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

function onLoadLogo() {
		
}

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

function init() {
	widthIcon = Number(Configuration.WIDTHICON)
	heightIcon = Number(Configuration.HEIGHTICON)
	spaceIcon = Number(Configuration.SPACEICON)
	url_data_icon = Configuration.URL_DATA_ICON
	dir = -1
	orientation = "h"
	alphaOut = Number(Configuration.ICON_ALPHAOUT)
	alphaOver = Number(Configuration.ICON_ALPHAOVER)
	marginX = Number(Configuration.ICON_MARGINX)
	marginY = Number(Configuration.ICON_MARGINY)
	loader = new MovieClipLoader()
    loader.addListener(this)
   	load_data()
    onResize()		
}

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

function onIntroEnd(){
	
}

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

function onLoadMenuTree() {
	
}

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

function onResize(){
	var m:BookModel=BookModel(this.getModel())
	this._x = m.__container.__containerBook._x+m.getWidthCurrent()-20
	this._y = m.__container.__containerBook._y	+ m.getHeightCurrent()+20
}

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

function load_data() {
	data_icon = new XML()
	data_icon.ignoreWhite = true
	data_icon.onLoad = Delegate2.create(this, onLoadData)
	data_icon.load(url_data_icon)
}

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

function onLoadData() {
	create_icon()
}

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

function getSize():Number {
	var size:Number
	var length:Number = data_icon.firstChild.childNodes.length	
	if (orientation == "h") {
		 size=(widthIcon+spaceIcon)*length-spaceIcon
	}else {
		 size=(heightIcon+spaceIcon)*length-spaceIcon	
	}
	return size;
}

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


function create_icon() {
	container = this.createEmptyMovieClip("mcContainer", 1)
	for (var i = 0; i < data_icon.firstChild.childNodes.length; i++) {
		var node:XMLNode = data_icon.firstChild.childNodes[i]
		var path:String = node.attributes.path
		var redirect:String = node.attributes.redirect
		var item:MovieClip = container.createEmptyMovieClip("mcIcon" + i, i)
		item._alpha=alphaOut
		item.redirect=redirect
		var containerItem:MovieClip = item.createEmptyMovieClip("container", 1)
		
		/////////if horizontal
		if (orientation == "h") {
			
		if(dir==1){
		item._x = i * ( (widthIcon) + spaceIcon) 
		}else if(dir==-1) {
		item._x = i * ( (-widthIcon) - spaceIcon)  -  (widthIcon)
		}else {
		item._x = i * ( (widthIcon) + spaceIcon) 	
		container._x=-getSize()/2
		}
				
		}else {
			
		/////////if vertical
		if(dir==1){
		item._y = i * ( (heightIcon) + spaceIcon) 
		}else if(dir==-1){
		item._y = i * ( (-heightIcon) - spaceIcon)  -  (heightIcon)
		}else {
		item._y = i * ( (heightIcon) + spaceIcon)	
		container._y=-getSize()/2	
		}
						
		}
			
		////create area
		var area:MovieClip = item.createEmptyMovieClip("mcArea", 11)
		Drawing.rectangle(area, 0, 0, widthIcon, heightIcon, ["0xFF0000",0])
		
         ////add event		
		item.onPress = Delegate2.create(this, onPressIcon,item)
		item.onRollOver = Delegate2.create(this, onRollOverIcon,item)
		item.onRollOut = Delegate2.create(this, onRollOutIcon,item)
		
		////load image
		//containerItem.loadMovie(path)
		loader.loadClip(path,containerItem)
	}
}

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

function onLoadComplete(target:MovieClip) {
	target._alpha=100	
}

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

function onLoadInit(target:MovieClip) {
    //target.tween('_alpha',100,0.5,'easeInOutCubic')  
	//tween.stop()
	new Tween(target,'_alpha',Strong.easeOut,0,100,1,true)
}

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


function onPressIcon(icon_:MovieClip) {
	var redirect:String = icon_.redirect
	var url:String = redirect.split(",")[0]
	var window:String = redirect.split(",")[1]
	getURL(url,window)
}

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


function onRollOverIcon(icon_:MovieClip) {
	//icon_.container.tween('_y',-10,0.6,'easeOutCubic')
	//icon_.tween('_alpha',alphaOver,0.6,'easeOutCubic')
	icon_.tween.stop()
	icon_.tween=new Tween(icon_,'_alpha',Strong.easeOut,icon_._alpha,alphaOver,1,true)
}

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

function onRollOutIcon(icon_:MovieClip) {
	//icon_.container.tween('_y',0,0.6,'easeOutCubic')
  	//icon_.tween('_alpha',alphaOut,0.6,'easeOutCubic')
	icon_.tween.stop()
	icon_.tween=new Tween(icon_,'_alpha',Strong.easeOut,icon_._alpha,alphaOut,1,true)
}

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


}

