/******************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/14/2010
 *
 *ConceptsCEE class for conceptsFrame3 movie of the CEE application
 * CS5 version of com.digitec.cee.ConceptsCEE
 *
 *******************************************/
package com.digitec.cee {
	import gs.*;
	import gs.easing.*;
	import flash.display.MovieClip;
	import flash.display.Sprite
	import flash.events.*;
	//import flash.events.TextEvent;
	import flash.display.SimpleButton;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import fl.transitions.TweenEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.*;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	/////////////////////////////////////////////////

	public class ConceptsCEE extends BaseCEE {
		private var _xmlPath: String;
		private var activeCat: Number;
		//ARRAY OF CATEGORY OBJECTS
		private var catList: Array;
		private var numCategories: int;
		private var maxCategories: int = 5;
		//ARRAY OF CONCEPTS OBJECTS
		//private var conceptsListArray:Array = new Array();				
		private var conceptsXML: XML;
		private var currentConcept: int; //Current selected concept		
		private var startAnim_y: Number = 158; //for accordion animation		
		private var animSpeed: Number = 0.3; //.7 sec animation //SET ACCORDION ANIMATION SPEED
		private var conceptsArray: Array = new Array();


		private var $nc: NetConnection;
		private var $ns: NetStream;
		private var $streampath: String;
		private var $streamurl: String = MainConstants.CEE_SERVER;

		private var myFont: Font = new Font5();
		private var versionField: TextField;
		/*******************************************************************
		 * CONSTRUCTOR
		 *******************************************************************/
		public function ConceptsCEE() {
			super(MainConstants.XMLCONCEPTS);

			setupPageButtons();
		}

		/*******************************************************************
		 * FUNCTION: SETUP FRAME TEXT_FIELDS  FROM XML
		 *******************************************************************/
		override public function setFramesText(_xml: XML): void {
			//FIRST SET THE TEXT FIELDS
			this.title_tf.text = _xml.xtitle;
			this.titleInfo_tf.text = _xml.xtitleInfo;

			//this.logo1_tf.text =  _xml.xlogo1;
			//this.logo2_tf.text =  _xml.xlogo2;
			//this.version2_tf.text =  _xml.xversion2;			
			var myFormat: TextFormat = new TextFormat();
			myFormat.font = myFont.fontName;
			myFormat.size = 12;
			myFormat.color = 0x8DC269;
			//myFormat.bold = true;
			versionField = new TextField();
			versionField.defaultTextFormat = myFormat;
			versionField.embedFonts = true;
			versionField.height = 17;
			versionField.width = 50;
			versionField.background = false;
			versionField.border = false;
			versionField.multiline = true;
			versionField.wordWrap = true;
			versionField.x = 415;
			versionField.y = 581;
			versionField.htmlText = "<b>" + _xml.xversion2 + "</b>";
			loadingmenu.visible = true;
			addChild(versionField);
			this.register_tf.text = _xml.xregister;

			//GET THE  CATEGORIES FROM category NODES IN THE XML			
			//SET CATEGORY COUNT TO A MAX OF 5
			numCategories = _xml.elements("category").length();
			if (numCategories > maxCategories) {
				numCategories = maxCategories;
			}

			//CREATE AN ARRAY OF  CATEGORY OBJECTS		
			catList = new Array();
			for (var i: int = 0; i < numCategories; i++) {
				var thename: String = _xml.category[i].categoryName.toString();
				var conceptcount: int = int(_xml.category[i].conceptCount);
				var base_y: Number = this["cat" + i + "_mc"].y;
				var theCategory: Category = new Category(i, thename, base_y, conceptcount);
				catList[i] = theCategory;
				this["cat" + i + "_mc"].buttonMode = false;
				this["cat" + i + "_mc"].useHandCursor = false;
				//SET TEXT FIELD IN CATEGORY CONCEPT MOVIES BUTTONS
				this["cat" + i + "_mc"].catName_txt.text = thename; //this.conceptList_mc
				this["cat" + i + "_mc"].conceptCount_txt.text = catList[i].conceptCount + " Concepts";
				//this["cat" + i + "_mc"].catmc.addEventListener( MouseEvent.CLICK,  selectCategory );								
			}
			//LOAD THE CONCEPT LIST
			
		    var GETLISTXML = Object(parent).getDpath();
			loadConceptList(GETLISTXML + "_VE50DATA/" + MainConstants.XMLPATH + MainConstants.XMLCONCEPTS_LIST);
			
			//2019-07-12 no load from web
			/*
			var loadertemp: URLLoader = new URLLoader();
			loadertemp.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandlerLIST);
			loadertemp.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			var streamtemp = $streamurl + MainConstants.XMLCONCEPTS_LIST;
			loadertemp.load(new URLRequest(streamtemp));
			*/

		}

		private function httpStatusHandlerLIST(event: HTTPStatusEvent): void {
			//trace("httpStatusHandler: -----------------------------------" + event);
			//trace("status: " + event.status);
			if (event.status == 0) {
				var GETLISTXML = Object(parent).getDpath();
				loadConceptList(GETLISTXML + "_VE50DATA/" + MainConstants.XMLPATH + MainConstants.XMLCONCEPTS_LIST);
			} else {
				loadConceptList($streamurl + MainConstants.XMLCONCEPTS_LIST);
			}
			//loadLibraryFinal(MainConstants.XMLPATH + dwpasspub);
		}

		/*******************************************************************
		 * FUNCTION:  TO LOAD XML FILE
		 *******************************************************************/
		private function ioErrorHandler(event: IOErrorEvent): void {
			//trace("ioErrorHandler: " + event);
		}

		private function loadConceptList(stream: String): void {
			//trace("Stream list                        "+stream);

			var loader: URLLoader = new URLLoader();

			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

			loader.load(new URLRequest(stream));
			
			loader.addEventListener(Event.COMPLETE, loadData);

			function loadData(evt: Event) {
				conceptsXML = new XML(loader.data);
				//trace("conceptsXML                        "+conceptsXML);
				//populate();
				createConceptsArrayByCategory();
			}
		}

		/*******************************************************************
		 * FUNCTION:  MANIPULATION & RETREIVAL OF CONCEPT DATA
		 *******************************************************************/
		private function createConceptsArrayByCategory(): void {
			var numconcepts: int = conceptsXML.elements("concept").length();
			for (var i: uint = 0; i < numconcepts; i++) {
				//CREATE CONCEPTITEM OBJECT & INSERT IT IN CORRESPONDING CATEGORY
				var conItem: ConceptItem = new ConceptItem(conceptsXML.concept[i]);
				var num: int = conItem.categoryID;
				catList[num].insertConceptToArray(conItem);
				conceptsArray.push(conItem);
			}
			populate();
		}

		/*******************************************************************
		 * FUNCTION: RETURN TO MAIN ALL CONCEPT ARRAY
		 *******************************************************************/
		public function getConceptArray(): Array {
			return conceptsArray;
		}

		/*******************************************************************
		 * FUNCTION:  POPULATE
		 *******************************************************************/
		private function populate() {
			var css: StyleSheet = new StyleSheet();

			var gUrl: URLRequest = new URLRequest("data/conceptList.css"); // new URLRequest("data/glossary.css");//CSS_PATH
			var gLoader: URLLoader = new URLLoader();
			gLoader.load(gUrl);
			gLoader.addEventListener(Event.COMPLETE, gLoaded);
			function gLoaded(event: Event): void {
				//css.parseCSS(URLLoader(event.target).data);							
				css.parseCSS(gLoader.data);
			}
			//css.setStyle("a:hover", {textDecoration:"underline"});
			/////////////////////////////////////////////////////////////////////////////////////////////////

			for (var j: uint = 0; j < catList.length; j++) {
				var myArr: Array = new Array();
				myArr = catList[j].conceptArray;

				this["cat" + j + "_mc"].col1_mc.col_txt.htmlText = "";
				this["cat" + j + "_mc"].col2_mc.col_txt.htmlText = "";
				hideConcepts(j);
				this["cat" + j + "_mc"].conceptCount_txt.text = "" + myArr.length + " concepts";

				for (var i = 0; i < myArr.length; i++) {
					var theField;
					if (i < Math.ceil(myArr.length / 2)) {
						theField = this["cat" + j + "_mc"].col1_mc.col_txt;
					} else {
						theField = this["cat" + j + "_mc"].col2_mc.col_txt;
					}
					//TEXTEVENT CANNOT BE APPLIED TO A TEXT FIELD WITH STYLESHEET????
					theField.htmlText += "<a href='event:" + myArr[i].conceptID + "'><u>" + myArr[i].conceptName + "</u></a>\n";
					//theField.styleSheet = css;
					theField.addEventListener(TextEvent.LINK, showConceptView);
				}
				//Object(stage).addEventListener(TextEvent.LINK, showConceptView); 
				this["cat" + j + "_mc"].listHeight = Math.ceil(myArr.length / 2);
				this["cat" + j + "_mc"].catmc.addEventListener(MouseEvent.CLICK, selectCategory);
				this["cat" + j + "_mc"].buttonMode = true;
				this["cat" + j + "_mc"].useHandCursor = true;
			}
			loadingmenu.visible = false;
		}
		/* //JUST EXAMPLE
		private var exampleText:String = "<h1>This is a headline</h1>"
                   + "<p>This is a line of text. <span class='bluetext'>" 
                    + "This line of text is colored blue.</span></p>";
					*/

		/*************************************************************************************
		 * FUNCTIONS: SHOW THE CURRENT  SELECTED CONCEPT IN LESSON PAGE
		 ************************************************************************************/
		public function showConceptView(event: TextEvent): void {
			var id: int = new int(event.text);
			//trace("---SHOW CONCEPT VIEW = " + id);			
			gotoConceptLessons(retrieveConceptItem(id));
		}

		/*************************************************************************************
		 * FUNCTION: RETRIEVE CONCEPT ITEM
		 ************************************************************************************/
		private function retrieveConceptItem(_theid: int): ConceptItem {
			var myobj: ConceptItem;
			var myArr: Array = catList[activeCat].conceptArray;
			for (var i = 0; i < myArr.length; i++) {
				if (myArr[i].conceptID == _theid) {
					myobj = myArr[i];
				}
			}
			return myobj;
		}

		/*************************************************************************************
		 * FUNCTIONS: GO TO CONCEPT VIEW
		 ************************************************************************************/
		public function gotoConceptView(id: Number) {
			if (id) {
				currentConcept = id;
			}
			var item: ConceptItem = catList[this.activeCat];
			//conceptCopy_txt.htmlText = "<contentCopy>" + addQuestionMarks(item.definition) + "</contentCopy>";
			/*
			checkForScroll();
			//CREATE THE CONCEPTiTEM OBJECT
			// apply the style for the "Related Concepts" text area
			cRelated_txt.styleSheet = appRoot.cssLib.conceptRelated_css;
			cRelated_txt.htmlText  = "";
			for (var i=0; i<c.related.length; i++) {
				cRelated_txt.htmlText += "<a href=\"asfunction:showConcept," + c.related[i] + "\">" + getConceptNameByID(c.related[i]) + "</a>\n<br><br>\n";
			}
			initGlossary();
			initFullGlossary();
			*/
		}



		/*************************************************************************************
		 * FUNCTIONS: SELECT A CATEGORY HANDLER
		 ************************************************************************************/
		public function selectCategory(evt: MouseEvent): void {
			var clicked: MovieClip = MovieClip(evt.target.parent); //RETURN cat0_mc								
			//trace("evt.target.parent = " + evt.target.parent.name);				
			var currentCat: Number = Number(clicked.name.charAt(3)); //cat0_mc---- return 0 														
			//trace("currentCat = " + currentCat);				
			var numRow: Number = this["cat" + currentCat + "_mc"].listHeight

			// NO MORE THAN 9 ROWS HEIGHT
			if (numRow > 9) {
				numRow = 9;
			}
			if (numRow < 4) {
				numRow = 4;
			} //to set a better looking display

			//SET ANIMATION PROPS
			var heightList: Number = numRow * 26; //previous static listheight = 200	
			//trace("heightList"+heightList);
			this.cat0_mc.props = {
				ly: 158,
				ry: (123.35 + heightList),
				ind: 1
			}; //this.cat0_mc.props = {ly:158, ry:323.35, ind:1};						
			this.cat1_mc.props = {
				ly: 190,
				ry: (155.35 + heightList),
				ind: 2
			}; //this.cat1_mc.props = {ly:190, ry:355.35, ind:2};						
			this.cat2_mc.props = {
				ly: 222,
				ry: (187.35 + heightList),
				ind: 3
			}; //this.cat2_mc.props = {ly:222, ry:387.35, ind:3};						
			this.cat3_mc.props = {
				ly: 254,
				ry: (219.35 + heightList),
				ind: 4
			}; //this.cat3_mc.props = {ly:254, ry:419.35, ind:4};						
			this.cat4_mc.props = {
				ly: 286,
				ry: (251.35 + heightList),
				ind: 5
			}; //this.cat4_mc.props = {ly:286, ry:451.35, ind:5};			

			//START ACCORDION ANIMATION
			for (var i: int = 0; i < 5; i++) {
				var mc: MovieClip = MovieClip(this["cat" + i + "_mc"]); // this["cat" + i + "_mc"]; // ALSO WORK										
				// tween all MCs to their proper position
				//trace(mc.props.ly);
				//trace(mc.props.lr);
				if (mc.props.ind == clicked.props.ind) {
					mc.arrow_mc.rotation = 90;
					var tween: TweenLite = TweenLite.to(mc, animSpeed, {
						alpha: 1,
						y: mc.props.ly,
						ease: Linear.easeOut,
						delay: 0,
						onComplete: onFinishTween,
						onCompleteParams: [1, mc]
					});
					function onFinishTween(arg1: Number, arg2: MovieClip): void { //trace("tween finished! arg1="+arg1+",  arg2 = " + arg2); //return 1, cat0_mc														
						showConcepts(currentCat);
					}
					activeCat = i;
				} else if (mc.props.ind < clicked.props.ind) {
					hideConcepts(i);
					mc.arrow_mc.rotation = 0;
					TweenLite.to(mc, animSpeed, {
						y: mc.props.ly,
						ease: Linear.easeOut
					}); //.7 sec animation
				} else {
					hideConcepts(i);
					mc.arrow_mc.rotation = 0;
					TweenLite.to(mc, animSpeed, {
						y: mc.props.ry,
						ease: Linear.easeOut
					});
				}
			}
		}


		/*************************************************************************************
		 * FUNCTIONS:  HIDE & SHOW THE CURRENT CATEGORY CONCEPTS
		 ************************************************************************************/
		public function hideConcepts(catnum: Number) {
			this["cat" + catnum + "_mc"].col1_mc.visible = false; //this.conceptList_mc
			this["cat" + catnum + "_mc"].col2_mc.visible = false;
		}

		public function showConcepts(catNum: Number) {
			this["cat" + catNum + "_mc"].col1_mc.visible = true;
			this["cat" + catNum + "_mc"].col2_mc.visible = true;
		}

		public function showActive() {
			//this["cat" + this.activeCat + "_mc"].showConcepts();
			showConcepts(this.activeCat);
		}


		/*******************************************************************
		 * FUNCTION:  SET UP ALL BUTTONS AND TEXTFIELDS
		 *******************************************************************/
		private function setupPageButtons(): void {
			this.close_button.buttonMode = true;
			this.close_button.mouseChildren = false;
			this.close_button.useHandCursor = true;
			/*
			this.about_button.buttonMode = true;
			this.about_button.mouseChildren = false; 
			this.about_button.useHandCursor = true; 			
			*/
			this.close_button.addEventListener(MouseEvent.MOUSE_OVER, onCloseOver);
			this.close_button.addEventListener(MouseEvent.MOUSE_OUT, onCloseOut);
			this.close_button.addEventListener(MouseEvent.CLICK, onCloseClick);

			//this.about_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoAboutFrm);
			this.about2_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoAboutFrm);

			this.nav_back_button.addEventListener(MouseEvent.MOUSE_OVER, onNavBackOver);
			this.nav_back_button.addEventListener(MouseEvent.MOUSE_OUT, onNavBackOut);
			this.nav_back_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoLastFrm);

			this.nav_home_button.addEventListener(MouseEvent.MOUSE_OVER, onNavHomeOver);
			this.nav_home_button.addEventListener(MouseEvent.MOUSE_OUT, onNavHomeOut);
			this.nav_home_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoHomeFrm);

			this.nav_concepts_button.addEventListener(MouseEvent.MOUSE_OVER, onNavConceptsOver);
			this.nav_concepts_button.addEventListener(MouseEvent.MOUSE_OUT, onNavConceptsOut);
			//nav_concepts_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoConceptsFrm); 

			this.nav_lessons_button.addEventListener(MouseEvent.MOUSE_OVER, onNavLessonsOver);
			this.nav_lessons_button.addEventListener(MouseEvent.MOUSE_OUT, onNavLessonsOut);
			this.nav_lessons_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoSearchFrm);
		}


		/*******************************************************************
		 * FUNCTIONS:  BUTTONS ROLL OVER & OUT
		 *******************************************************************/
		private function onCloseOver(evt: MouseEvent): void {
			close_button.gotoAndStop(2);
		}
		private function onCloseOut(evt: MouseEvent): void {
			close_button.gotoAndStop(1);
		}
		private function onNavBackOver(evt: MouseEvent): void {
			back_icon_roll.gotoAndStop(2);
		}
		private function onNavBackOut(evt: MouseEvent): void {
			back_icon_roll.gotoAndStop(1);
		}
		private function onNavHomeOver(evt: MouseEvent): void {
			house_mc.gotoAndStop(2);
		}
		private function onNavHomeOut(evt: MouseEvent): void {
			house_mc.gotoAndStop(1);
		}
		private function onNavConceptsOver(evt: MouseEvent): void {
			nav_concepts_roll.gotoAndStop(2);
		}
		private function onNavConceptsOut(evt: MouseEvent): void {
			nav_concepts_roll.gotoAndStop(1);
		}
		private function onNavLessonsOver(evt: MouseEvent): void {
			nav_lessons_roll.gotoAndStop(2);
		}
		private function onNavLessonsOut(evt: MouseEvent): void {
			nav_lessons_roll.gotoAndStop(1);
		}

	} // Main class

} //end package


/*
private function getConceptsByCategory(cat:Number):Array{									
		var _arr:Array = new Array();												
		var numconcepts:int = conceptsXML.elements("concept").length();														
		for( var i:uint=0; i < numconcepts; i++ ){													
			if( conceptsXML.concept[i].category == cat)	{						
				_arr.push( conceptsXML.concept[i].name.toString() );											
				//_arr.push( conceptsXML.concept[i].name,  conceptsXML.concept[i].id );					
			}				
		}																		
		//_arr.sortOn("name", 1); // case insensitive
		return _arr;		
}				
*/