package classes.gustavo.ui.box 
{
	import classes.greensock.*;
	import classes.matuto.core.Button;
	import classes.matuto.interfaces.IDisplay;
	import flash.display.Stage;
	import flash.events.Event;
	
	import fl.motion.easing.Exponential;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import classes.gustavo.events.EventBox;

	
	/**
	 * ...
	 * @author gustavo
	 */
	
	
	public class Box extends MovieClip
	{
		
		public static var ref			:Box;
		
		private var fotoBox				:FotoBox; 
		private var bg					:MovieClip;
		private var fechar				:Button;
		private var direita				:Button;
		private var esquerda			:Button;
		private var down				:Button;
		
		private var dados				:Array;
		private var posicao				:int;
		
		private var controle			:MovieClip;
		
		private var estaAnimadoControle	:Boolean = false;
		
		private var rootStage:Stage;
		
		public function Box() 
		{
			super();
			
			ref		   = this;
			
			bg		   = this['_bg'];
			fechar     = _fechar
			direita    = _direita
			esquerda   = _esquerda
			down	   = _down;
			
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			
		}
		
		private function onAddToStage(e:Event):void 
		{
			
			rootStage = stage;
			removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			rootStage.addEventListener(Event.RESIZE, resize);
			
			removeProps();

		}
		
		private function removeProps():void 
		{
			bg.alpha = 0;
			fechar.alpha 	= 0;
			direita.alpha 	= 0;
			esquerda.alpha 	= 0;
			down.alpha 		= 0;
		}
		
		public function init(_dados:Array,_posicao:int):void 
		{
			dados 		= _dados;
			posicao		= _posicao;
			
			initBg();
			
			fotoBox = new FotoBox();
			addChild(fotoBox);
			setChildIndex(fotoBox, numChildren - 1);
			setTimeout(fotoBox.load, 900, dados[posicao].image);
			
			estaAnimadoControle = true;
			resize();
			
			TweenMax.to(esquerda, 1.5, {autoAlpha:1, delay:1.8, ease:Exponential.easeInOut});
			TweenMax.to(direita, 1.5, {autoAlpha:1, delay:1.8, ease:Exponential.easeInOut});
			TweenMax.to(fechar, 1.5, {autoAlpha:1, delay:1.8, ease:Exponential.easeInOut});
			//TweenMax.to(down, 1.5, {autoAlpha:1, delay:1.8, ease:Exponential.easeInOut});	
		}
		
		public function addEvent():void 
		{
			fechar.addEventListener(MouseEvent.CLICK, clickFechar);
			direita.addEventListener(MouseEvent.CLICK, clickDir);
			esquerda.addEventListener(MouseEvent.CLICK, clickEsq);
			down.addEventListener(MouseEvent.CLICK, clickDown);
		}
		
		private function clickDown(e:MouseEvent):void 
		{
			var request:URLRequest = new URLRequest("http://www.selectalifestyle.com/gallery/visao/imagens/galeria/original/"+dados[posicao]);
			var localRef:FileReference = new FileReference();
			
			try
			{
				localRef.download( request );
			}
			catch (error:Error)
			{
				trace("Unable to download file.");
			} 
			
		}
		
		private function clickEsq(e:MouseEvent):void 
		{
			 posicao--;
			 if (posicao < 0)
			 posicao = dados.length-1;
	
			 updateFoto();
		}
		
		private function clickDir(e:MouseEvent):void 
		{
			 posicao++;
			 if (posicao > dados.length - 1)
			 posicao = 0;
			 
			 updateFoto();
		}
		
		public function updateFoto():void
		{
			 fotoBox.close();
			 
			 setTimeout(function():void
			 {
				 removeChild(fotoBox);
				 fotoBox = null;
				 
				 fotoBox = new FotoBox();
				 addChild(fotoBox);
				 setChildIndex(fotoBox, numChildren - 1);
				 setTimeout(fotoBox.load, 900, dados[posicao].image);
				 resize();

			 },1120);
			
		}
		
		private function clickFechar(e:MouseEvent):void 
		{
			close();
		}
	
		public function close():void 
		{			
			rootStage.removeEventListener(Event.RESIZE, resize);

			TweenMax.to(esquerda, .7, {autoAlpha:0, ease:Exponential.easeInOut});
			TweenMax.to(direita, .7, {autoAlpha:0, ease:Exponential.easeInOut});
			TweenMax.to(fechar, .7, {autoAlpha:0,y:-400,  ease:Exponential.easeInOut});
			TweenMax.to(down, .7, {autoAlpha:0, ease:Exponential.easeInOut});
			
			closeBg();
			fotoBox.close();
			
			setTimeout(function():void {
				
				dispose();
				dispatchEvent(new EventBox(EventBox.CLOSE));
				
			}, 1300);
		}
		
		private function initBg():void
		{
			TweenLite.to(bg,1,{autoAlpha:.8,ease:Exponential.easeInOut});
		}
		
				
		private function closeBg():void
		{
			TweenLite.to(bg,.8,{autoAlpha:0,delay:.7,ease:Exponential.easeInOut});
		}
		
		public function resize(e:Event=null):void 
		{
			
			bg.width 			= rootStage.stageWidth;
			bg.height 			= rootStage.stageHeight;
			
			direita.x 			= rootStage.stageWidth-50;
			esquerda.x 			= 50;
			
			direita.y			= (rootStage.stageHeight>>1)-(25);
			esquerda.y			= (rootStage.stageHeight>>1)-(25);
			
			fechar.y				= 50;
			fechar.x				= (rootStage.stageWidth)-(100);
			
			down.y					= rootStage.stageHeight-100;
			down.x					= (rootStage.stageWidth)-(100);
			
			if (fotoBox)
			{
				fotoBox.x 			= (rootStage.stageWidth >> 1);
				fotoBox.y 			= (rootStage.stageHeight >> 1);
			}
			
			
		}
		
		public function dispose():void 
		{
			removeChild(bg);
			bg = null;
			removeChild(fotoBox);
			fotoBox = null;
		}
		
	}

}