package modules.tabela
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import classes.matuto.model.Model;
	import classes.matuto.loaders.*;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import genericos.*;
	import flash.net.*;
		//Matuto
	import classes.fl.motion.easing.*;
	import classes.greensock.*;
	import classes.matuto.core.*;
	import classes.matuto.core.module.*;
	import classes.matuto.events.*;
	import classes.matuto.interfaces.*;
	import classes.matuto.model.*;
	import classes.matuto.mtween.textinho.*;
	import classes.matuto.ui.*;
	import classes.matuto.utils.display.*;
	import classes.matuto.utils.model.*;	
	import modules.home.*;
	import modules.menu.*;
	import flash.geom.*;
	import genericos.RectangleSprite;
	import com.freshplanet.lib.ui.scroll.mobile.ScrollController;
	import flash.filesystem.*;
	import flash.utils.*;
	
	public class Tabela extends MovieClip 
	{
		
		private var _scroll:ScrollController;
		private var container	:	Sprite;
		private var thumbs		:	Array = new Array();
		
		
		public function Tabela() 
		{
			super();
			
			addEventListener(Event.ADDED_TO_STAGE, add);
			addEventListener(Event.REMOVED_FROM_STAGE, remove);
		}
		
		private function remove(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, remove);
			_scroll.removeScrollControll();
			_scroll = null;
		}
		
		private function add(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, add);
			
		
			
			container = new Sprite();
			container.y = 118;
			container.x = 10;
			addChild(container);
			
			setTimeout(initContainerScroll,500);
			
			
		}
		
		private function initContainerScroll():void 
		{

			
				alpha = 0;
			x = 300;
			TweenMax.to(this,.6,{x:0,autoAlpha:1,ease:Exponential.easeInOut,onComplete:function(){
			}});
			
			  var _file:File = flash.filesystem.File.applicationDirectory.resolvePath("tabela.xml");
		  
			  if (_file.exists) 
			  {     
				  
				var stream : FileStream = new FileStream();
				stream.open(_file, FileMode.READ);
				var str : String = stream.readUTFBytes(stream.bytesAvailable);
				stream.close();
				var xml:XML = new XML(str);
				onCompleteXml(xml);
				
			  }
		 
		  
		}

		private function onCompleteXml(e:XML)
		{
		
			var list:XMLList = e.sheet.row;
		
			var thumb:TabelaItem;
			var position:int = 0;
			
			for (var i:int = 0; i < 100; i++)
			{
				
				
				if (position < 100)
				{
			
					thumb = new TabelaItem();
					thumb.y = (150) * position;
					var obj:Object = new Object();
					obj.position = position;
					obj.xml = list[i];
					thumb.setParams = obj;
					thumb.setModel(list[i]);
					container.addChild(thumb);
					position++;
					thumb.addEventListener(Event.SELECT, onSelectThumb);
					thumb.addEventListener(Event.OPEN, onOpenThumb);
					thumb.addEventListener(Event.CLOSE, onCloseThumb);
					thumbs.push(thumb);
				
				}
			}
			
			var containerViewport:Rectangle = new Rectangle(0, 118,stage.stageWidth,stage.stageHeight);
			this._scroll = new ScrollController();
			this._scroll.horizontalScrollingEnabled = false;
			this._scroll.addScrollControll(container, this, containerViewport);
			
			
			
		}
		
		private function onSelectThumb(e:Event):void 
		{
			var thumb:TabelaItem =  e.currentTarget as TabelaItem;
			
		}		
		
		private function onOpenThumb(e:Event):void 
		{
			var thumb:TabelaItem =  e.currentTarget as TabelaItem;
			onOpen(thumb);
		}		
		
		
		private function onOpen(thumb:TabelaItem):void 
		{
			
			for each(var item:TabelaItem in thumbs)
			{
				
				if (item.getParams.position > thumb.getParams.position)
				{
					TweenMax.to(item, .3, {overwrite:true,y:item.y+176,delay:0, ease:Exponential.easeInOut } );
				}
				
				
			}
			
		}
		
		private function onCloseThumb(e:Event):void 
		{
			

			var thumb:TabelaItem =  e.currentTarget as TabelaItem;
			
			for each(var item:TabelaItem in thumbs)
			{
				if (item.getParams.position > thumb.getParams.position)
				{
					TweenMax.to(item, .3, {y:item.y-176, ease:Exponential.easeInOut } );
				}
			}
		
		}
		
	
		
	}//END CLASS

}