package modules.noticias
{
	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;
	
	public class Noticias extends MovieClip 
	{
		
		private var _scroll:ScrollController;
		private var interna:NoticiasInterna
		private var container	:	Sprite;
		
		
		public function Noticias() 
		{
			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);
			
			alpha = 0;
			x = 300;
			TweenMax.to(this,.6,{x:0,autoAlpha:1,ease:Exponential.easeInOut,onComplete:function(){
			}});
			
			container = new Sprite();
			container.y = 118;
			container.x = 10;
			addChild(container);
			
			initContainerScroll();
			
			
		}
		
		private function initContainerScroll():void 
		{
			Model.XML_CONNECT("http://gfv.com.br/santajoana/portalmedico/noticias.xml",onCompleteXml);
		}
		
		
		private function onCompleteXml(e:XML)
		{
		
			var list:XMLList = e.data;
			//trace(list);
			
			//trace(list[0].title);
			//trace(list[0].date);
			//trace(list[0].text);
		
			var thumb:NoticiasItem;
			var position:int = 0;
			
			for each(var i:Object in list)
			{
				thumb = new NoticiasItem();
				thumb.y = (thumb.height + 10) * position;
				thumb.setModel(i);
				container.addChild(thumb);
				position++;
				
				thumb.addEventListener(Event.SELECT, onSelectThumb);
			}
			
			
			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 select:NoticiasItem = e.currentTarget as NoticiasItem;
		   
		   if(_scroll)
		   _scroll.pauseScrolling();

		   TweenMax.to(container,.5,{x:-200,autoAlpha:0,ease:Exponential.easeInOut,onComplete:function(){}});
		   NoticiasInterna.MODEL = select.getParams;
		   interna = new NoticiasInterna();
		   interna.addEventListener(Event.CLOSE, onCloseInterna);
			addChild(interna);
		  
		}
		
		private function onCloseInterna(e:Event):void 
		{
			TweenMax.to(container,.5,{x:10,autoAlpha:1,ease:Exponential.easeInOut,onComplete:function(){}});
		   _scroll.resumeScrolling();

		}
		
	}//END CLASS

}