package com.asm {
  import flash.display.*;
  import flash.net.*;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.text.TextField;
  import flash.net.FileReference;
  import fl.transitions.easing.*;
 import flash.utils.*;
 import flash.events.TimerEvent;
    import flash.events.Event;

	/**
	* Main Constructor
	*
	* @params
	* @return Date([yearOrTimevalue:Number], [month:Number], [date:Number], [hour:Number], [minute:Number], [second:Number], [millisecond:Number])
	*/
	public class iTimer extends MovieClip {
		private var _mSeconds	:Number;
		private var _seconds	:Number;
		private var _minutes	:Number;
		private var _hours		:Number;
		private var _days			:Number;
		private var _years		:Number;
		private var _timer		:Number;
		public  var _run 			:Timer = new Timer(50);
		private var _lastTimer:Date;
		private var _tEnd			:String;
		private var _time_modify:Boolean = false;
		private var p_obj:Object = new Object();
		private var _scopes:Object;
		private var _now:Date;
		private var _event_date:Date;
		private var _event_countdown:Number;
		
		public function iTimer():void {
			_run.addEventListener(TimerEvent.TIMER, timer);
		}
		
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function addTimer(p_scopes:Object, p_parameters:Object):Boolean {
			p_obj = p_parameters;
			_scopes = p_scopes;
			for (var prop in p_obj) { 
			 //trace("myObject."+prop+" = "+p_obj[prop]); 
			} 
	
	
			//trace("GAME" + p_scopes);
			if (!Boolean(p_scopes)) return false;
	
			var i:Number, j:Number, istr:String;
	
			var rScopes:Array; // List of objects to tween
			if (p_scopes is Array) {
				// The first argument is an array
				rScopes = p_scopes.concat();
			} else {
				// The first argument(s) is(are) object(s)
				rScopes = [p_scopes];
			}
			
			return true;
		}
		
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function init() {
			//gStart();
		}
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function gStart():void {
			trace('Timer has Started')
			set_runs();
		}
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function set_runs():void {
			//trace('_tEnd      >>>>>>>>>  '+_tEnd)
			_now = new Date();
			
			var welcome:Number = GetDaysInMonth(p_obj.months-1, p_obj.years);
			//trace('dopemmmmmmm     >>>>  ' + welcome);
			
			var sMSecond:Number;
			var sSecond	:Number;
			var sMinutes:Number;
			var sHours	:Number;
			var sDays		:Number;
			var sMonths	:Number;
			var sYear		:Number;
			
			if(p_obj.exact){
				p_obj.mseconds == undefined ? sMSecond  = _now.getMilliseconds() : sMSecond  = p_obj.mseconds;
				p_obj.seconds  == undefined ? sSecond   = _now.getSeconds() : sSecond  = p_obj.seconds;
				p_obj.minutes  == undefined ? sMinutes  = _now.getMinutes() : sMinutes = p_obj.minutes;
				p_obj.hours 	 == undefined ? sHours 	  = _now.getHours() 	: sHours   = p_obj.hours;
				p_obj.days 	   == undefined ? sDays 	  = _now.getDate() 	  : sDays    = p_obj.days;
				p_obj.months   == undefined ? sMonths   = _now.getMonth() : sMonths  = p_obj.months;
				p_obj.years    == undefined ? sYear 	  = _now.getFullYear(): sYear    = p_obj.years;
				_event_date = new Date(sYear, sMonths-1, sDays, sHours, sMinutes, sSecond, sMSecond);
			}else{
				p_obj.mseconds == undefined ? sMSecond  = _now.getMilliseconds() : sMSecond  = _now.getSeconds() + p_obj.mseconds;
				p_obj.seconds  == undefined ? sSecond   = _now.getSeconds() : sSecond  = _now.getSeconds() + p_obj.seconds;
				p_obj.minutes  == undefined ? sMinutes  = _now.getMinutes() : sMinutes = _now.getMinutes() + p_obj.minutes;
				p_obj.hours 	 == undefined ? sHours 	  = _now.getHours() 	: sHours   = _now.getHours()   + p_obj.hours;
				p_obj.days 	   == undefined ? sDays 	  = _now.getDate() 	  : sDays    = _now.getDate()     + p_obj.days;
				p_obj.months   == undefined ? sMonths   = _now.getMonth()+1 : sMonths  = _now.getMonth()+1 + p_obj.months;
				p_obj.years    == undefined ? sYear 	  = _now.getFullYear(): sYear    = _now.getFullYear()+ p_obj.years;
				_event_date = new Date(sYear, sMonths-1, sDays, sHours, sMinutes, sSecond, sMSecond);
			
			}
			
/*
			trace('sSecond : ' + sSecond);
			trace('sMinutes : ' + sMinutes);
			trace('sHours : ' + sHours);
			trace('sDays : ' + sDays);
			trace('sDays : ' + sMonths);
			trace('sYear : ' + sYear);
*/
			
			
			_event_countdown = _event_date.getTime();

			_run.start();
			
		}
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function reset_runs():void {
			
		}
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function gStop():void {
			trace('stop is working');
			_run.stop();
		}
		
	
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function reset():void {
			_run.reset();
		}
	 
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function timer(e:Event):void{
			//trace(e);
			_now = new Date();
			var _now_milli_secs:Number = _now.getTime();
			_timer = _event_countdown - _now_milli_secs;
				
			_mSeconds = Math.floor(_timer);
			_seconds  = Math.floor(_timer / 1000);
			_minutes  = Math.floor(_seconds / 60);
			_hours	  = Math.floor(_minutes / 60);
			_days		  = Math.floor(_hours / 24);
			_years	  = Math.floor(_days / 365);
			
			//Storing the remainder of this division problem
			_mSeconds%= 1000;
			_seconds %= 60;
			_minutes %= 60;
			_hours 	%= 24;
		
			//Converting numerical values into strings so that
			//we string all of these numbers together for the display
			var sec:String = _seconds.toString();
			var min:String = _minutes.toString();		
			var hrs:String = _hours.toString();
			var d:String 	 = _days.toString();
		 	var y:String 	 = _years.toString();
	
			
			
			if (sec.length < 2) {
				sec = "0" + sec;
			}
		 
			if (min.length < 2) {
				min = "0" + min;
			}
		 
			if (hrs.length < 2) {
				hrs = "0" + hrs;
			}
			
			if (y.length < 2) {
				y = "0" + y;
			}
	
			
			if(p_obj.progress != undefined){
				_progress();
			}
			if (_timer<=0) {
				_complete();
				trace("TIME IS COMPLETE");
			}
	
		}

		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		private function _progress():void {
			//var theFunction = eval(p_obj.progress);
			try {
				//trace('p_obj.progress  ' + p_obj.progress)
				_scopes[p_obj.progress]([p_obj.onCompleteParams]);
				//theFunction.apply(null, [p_obj.onCompleteParams]);
			} catch(e:Error) {
				trace('ERROR')
				//handleError(_tweenList[i], e, "onOverwrite");
			}
		}
	
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		private function _complete():void {
			//var theFunction = eval(p_obj.progress);
			reset();
			try {
				_scopes[p_obj.onComplete]([p_obj.onCompleteParams]);
				//theFunction.apply(null, [p_obj.onCompleteParams]);
			} catch(e:Error) {
				trace('ERROR' + e)
				//handleError(_tweenList[i], e, "onOverwrite");
			}
		}
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function set progress(e:String){
			p_obj.progress = e;
		}
		
		/**
		 * Needs Documenting
		 *
		 * @params
		 * @return
		 */
		public function get stime():Object{
			var t:Object = new Object();
			t.mSeconds = _mSeconds;
			t.seconds = _seconds;
			t.minutes = _minutes;
			t.hours = _hours;
			t.days = _days;
			//t.rotation = _seconds * (360/p_obj.seconds);
			t.rotation = (360/(p_obj.seconds * 1000)*_timer);
			//trace('_timer   >>>>>  ' + _timer);
			//trace('DOPE   '  + (360/(p_obj.seconds * 1000))*_timer)
			return t;
		}
		
		/**
		 * Check Leap Year
		 *
		 * @params
		 * @return
		 */
		function GetDaysInMonth(Month, Year) {
			var Days:Number;
		  if (Month==0 || Month==2 || Month==4 || Month==6 || Month==7 || Month==9 || Month==11) {
		    
		    Days = 31;
		  } else if (Month==3 || Month==5 || Month==8 || Month==10) {
		    Days = 30;
		  } else if (Month==1) {
		    if (IsLeapYear(Year)) {
		      Days = 29;
		    } else {
		      Days = 28;
		    }
		  }
		  return (Days);
		}

		/**
		 * Check Leap Year
		 *
		 * @params
		 * @return
		 */
		function IsLeapYear (Year) {
		  if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
		    return (true);
		  } else {
		 
		    return (false);
		  }
		}
	}
}