package classes.matuto.core
{
	
	
	/* 
	   
	   Classe responsavel por registra o site 
	   no Matuto Framworks, para que ele possa 
	   fazer todo gerenciamento, ele faz a 
	   conexao com o sitemaps.
	   
	   autor: gustavo cosme
	   data:  18/maio/2011
	
	*/
	
	
	import classes.matuto.events.MatutoEvent;
	import classes.matuto.model.Model;
	import classes.matuto.ui.scroll.Scroll;
	import classes.matuto.utils.display.DisplayObjectUtils;
	import classes.matuto.utils.display.LibraryExternal;
	
	import flash.display.*;
	import flash.utils.setTimeout;
	import classes.matuto.midia.KeyBoardManager;
	
	
	public class Index extends Sprite
	{
		
		private var site				:SiteBase;
		private var preloaderDefault	:PreloaderDefault;
		
		private var barraMenor			:Sprite;
		private var barraMaior			:Sprite;
		private var mascara				:Sprite;
		private var scroll				:Scroll;
		
		public static var ref			:Index;
		
		public var  estaNoStage			:Boolean = false;
		

		public function Index()
		{
			super();
			
			if(this.parent as Stage)
			{
				init();
				estaNoStage = true;
				
			}else{
			
				estaNoStage = false;
			  
			}
		}
		
		public function init():void
		{
			Matuto.onInit = INICIALIZE_CONFIG_INDEX;
			Matuto.init();		
		}
		
		protected function INICIALIZE_CONFIG_INDEX():void
		{
			
	       if(Matuto.getConfig.getGateway)
		   {
			   INICIALIZE_AMF(Matuto.getConfig.getNomePHP,Matuto.getConfig.getGateway);
		   }
		   
		   INICIALIZE_SITE();
		   
		}
		
		protected  function INICIALIZE_SITE():void
		{		
			site  =  LibraryExternal.getLibrarySymbol('Site') as SiteBase;
			addChild(site);
			Matuto.setSite(this, stage);
			KeyBoardManager.init(stage);

			
			validationScroll();
			
		}
		
		private function validationScroll():void 
		{
			
			var validacao:String 	= Matuto.getConfig.getScrollStatus;
						
			if (validacao=="true")
			{
			
				var corMaior	:uint  	= Matuto.getConfig.getScrollColorMaior;
				var corMenor	:uint  	= Matuto.getConfig.getScrollColorMenor;
				var largura		:Number = Matuto.getConfig.getScrollWidth;
				var heightMenor	:Number = Matuto.getConfig.getScrollHeightMenor;
				
				barraMaior   = DisplayObjectUtils.getRecShape(corMaior, largura, stage.stageHeight);
				barraMenor   = DisplayObjectUtils.getRecShape(corMenor, largura, heightMenor);
				mascara 	 = DisplayObjectUtils.getRecShape(0x00FF40, stage.stageWidth, stage.stageHeight);

				
				addChild(barraMaior);
				addChild(barraMenor);
				addChild(mascara);
				
				site.mask = mascara;
				
				Matuto.addEventListener(MatutoEvent.RESIZE, onResize);
				onResize(null);
			}
			
		}
		
		private function onResize(e:MatutoEvent):void 
		{
			barraMaior.x 		= stage.stageWidth - barraMaior.width;
			barraMenor.x 		= stage.stageWidth - barraMenor.width;
			barraMaior.height 	= stage.stageHeight;
			
			mascara.width		= stage.stageWidth;
			mascara.height		= stage.stageHeight;
			
			if(scroll)
			{
			  scroll = null;
			}
			
			scroll 				= new Scroll(site, barraMenor, barraMaior, mascara);
			addChild(scroll);
			scroll.setBorder	= 10;
		
		}
		
		public function upadateScroll(altura:Number, border:Number = 10 ):void
		{
			setTimeout(function():void{
			
				scroll.setBorder = border;
				scroll.setAltura = altura;
			
			},1000);
		}
		
		public function resetScroll():void
		{
			scroll.upDatePosition(barraMenor.height);
		}
		
		protected function INICIALIZE_AMF(nome:String,gateway:String):void
		{
			Model.INIT_AMF(nome,gateway);
		}
		
		public function get getSite():SiteBase
		{
			return site;
		}
		
	}//end class
	
}