/******************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/14/2010
 *
 * This is the MainCEE class for the CEE application
 * CS5 version of com.digitec.cee.MainCEE
 *
 *******************************************/
package com.digitec.cee {
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	//import flash.system.LoaderContext;		
	import flash.errors.IllegalOperationError;
	import flash.net.SharedObject;

	//TO LOAD PDFS		
	import flash.html.HTMLLoader;
	import flash.html.HTMLPDFCapability;

	import flash.desktop.NativeApplication;

	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowDisplayState;
	import flash.events.NativeWindowDisplayStateEvent;
	//this.stage.scaleMode = StageScaleMode.NO_SCALE;
	import flash.filesystem.File;
	import flash.system.Capabilities;

	public class MainCEE extends MovieClip {
		//SET A LOADER FOR EACH MOVIE
		private var homeSwf: String = MainConstants.HOME_SWF;
		private var aboutSwf: String = MainConstants.ABOUT_SWF;
		private var conceptsSwf: String = MainConstants.CONCEPTS_SWF;
		private var searchSwf: String = MainConstants.SEARCH_SWF;
		private var lessonsSwf: String = MainConstants.LESSONS_SWF;
		private var pubsSwf: String = MainConstants.PUBS_SWF;
		private var eulaSwf: String = MainConstants.EULA_SWF;

		//MOVIES
		private var homeMC: MovieClip;
		private var aboutMC: MovieClip;
		private var conceptsMC: MovieClip;
		private var searchMC: MovieClip;
		private var lessonsMC: MovieClip;
		private var pubsMC: MovieClip;
		private var eulaMC: MovieClip;
		private var dbacktrue = false;
		private var holder: MovieClip;
		private var window: NativeWindow = stage.nativeWindow;

		//OBJECTS OF MOVIES
		//private var homeObj:Object;
		//private var aboutObj:Object;
		private var conceptsObj: Object;
		private var searchObj: Object;
		private var lessonsObj: Object;
		public var pubsObj: Object;
		//private var eulaObj:Object;		

		private static const HOMEPAGE: String = MainConstants.HOMEPAGE;
		private static const ABOUT: String = MainConstants.ABOUT;
		private static const CONCEPTS: String = MainConstants.CONCEPTS;
		private static const SEARCH: String = MainConstants.SEARCH;
		private static const LESSONS: String = MainConstants.LESSONS;
		private static const PUBS: String = MainConstants.PUBS;
		private static const EULA: String = MainConstants.EULA;

		private var counterMoviesLoaded: int = 0;
		private var backfrompubs: Boolean = true;
		private var backfromsearch: Boolean = true;
		private var mcLoader_0: Loader;
		private var mcLoader_1: Loader;
		private var mcLoader_2: Loader;
		private var mcLoader_3: Loader;
		private var mcLoader_4: Loader;
		private var mcLoader_5: Loader;
		private var mcLoader_6: Loader;

		//FOR PUBLICATION MOVIE
		private var htmlContentTable: HTMLContentTables;
		private var htmlPath: String = MainConstants.PUBLICATION_HTML;

		//PDF				
		private var pdfPath: String = MainConstants.PDF_PATH;
		private var pdfFile: String;
		private var mypdffinal: String;
		private var currentDirectories: Array;

		//private var pdfExt:String= ".pdf";
		private var hwidth: Number = 550;
		private var hheight: Number = 440; //412
		private var position: Array = [232, 84];

		//ARRAY OF ALL  CONCEPTS ITEMS
		private var conceptsListArray: Array = new Array();

		//use it with the back button to remember which pdf was
		private var currentPDFlessonId: String = "";
		private var currentPubId: String = "";
		private var currentPubType: String = ""; //"pub" or "lesson"

		//private var lastPageVisited:String;
		private var currentPageVisited: String;
		private var lastPageVisited: Array = [HOMEPAGE]; //= [];		

		private var currentConceptIdVisited: String = "";
		private var lastConceptIdVisited: Array = new Array();

		//private var lastPDFlessonId:String = "";
		//private var lastPubId:String= "";
		private var lastPDFlessonId: Array = new Array();
		private var lastPubId: Array = new Array();
		//private var lastPubType:String = ""; //"pub" or "lesson"
		private var lastPubType: Array = new Array();
		private var lastSearchScreen: Array = new Array();
		private var lastStateSearchScreen: Array = new Array();
		private var clikingBack: Boolean = false;

		private var isBrowseLesson: Boolean = false; //set default browse is not Browse Lessons, is Browse Concepts
		private var isfullscreen: Boolean = false;


		public function MainCEE() {
			getDrives();
			setup();
		}

		/*******************************************************************
		 * FUNCTION:  SET UP STARTING APP LISTENER
		 *******************************************************************/
		private function setup(): void {
			addEventListener(Event.ADDED_TO_STAGE, init);
			window.addEventListener(Event.RESIZE, checkSize);

		}
		// *************************** get drives code
		private function getDrives(): void {
			/*
			var os: String = Capabilities.os.substr(0, 3).toLowerCase()

			var currentDrives: Array = (os == "mac") ? new File('/Volumes/').getDirectoryListing() : File.getRootDirectories();
			
			var file = File.applicationDirectory; 

			for each(var file: File in currentDrives) {
				var dtemp = file.name;

				if (dtemp != ".DS_Store") {
					var currentDirectories: Array = (os == "mac") ? new File('/Volumes/' + dtemp + '/').getDirectoryListing() : new File(dtemp + '/').getDirectoryListing();

					for each(var file2: File in currentDirectories) {


						if (file2.name == "_VE50DATA") {

							if (os == "mac") {
								mypdffinal = "file:///Volumes/" + dtemp + "/";
							} else {

								mypdffinal = "file:///" + dtemp + "/";

							}

						}

					};
				}
			}
				
			*/
				mypdffinal = "/"; //File.applicationDirectory.name + "/";

		}
		////////////////Scale function
		public function getDpath() {
			return mypdffinal;
		}
		private function checkSize(e: Event) {


			var tempwidth = ((window.width - 800) / 2) + 232;
			var tempheight = (((window.height - 600) / 2) + 84) - 12;

			position = [tempwidth, tempheight];

		}
		public function setfullscreen(isfullscreen: Boolean): void {

			stage.scaleMode = StageScaleMode.SHOW_ALL;
			window.maximize();

		}



		/*******************************************************************
		 * FUNCTION: INITIALIZE APP LOADING ABOUT, CONCEPTS, SEARCH, LESSONS, HOME: MOVIES
		 * AND ADDED TO DISPLAY STACK
		 *******************************************************************/
		private function init(e: Event): void {
			//loadMovies();
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//if (loaderInfo.parameters.suspendInfo) {	} else if (loaderInfo.parameters.learnerLanguage) {	}																		
			loadMovies();
		}

		//FUNCTION LOAD MOVIES TO MAIN MOVIE (CONTAINER)
		private function loadMovies(): void {
			var moviesArray: Array = new Array();
			//ADD ALL MOVIE PATHS TO BE LOADED TO ARRAY
			moviesArray[0] = homeSwf;
			moviesArray[1] = aboutSwf;
			moviesArray[2] = conceptsSwf;
			moviesArray[3] = searchSwf;
			moviesArray[4] = lessonsSwf;
			moviesArray[5] = pubsSwf;
			moviesArray[6] = eulaSwf;

			//LOAD ALL MOVIES IN THE ARRAY
			for (var arrindex: int = 0; arrindex < moviesArray.length; arrindex++) {
				this["mcLoader_" + arrindex] = new Loader();
				try {
					this["mcLoader_" + arrindex].load(new URLRequest(moviesArray[arrindex]));
					this["mcLoader_" + arrindex].contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded);
				} catch (error: IllegalOperationError) {
					trace("<Error> " + error.message);
				}
			}

		}


		//FUNCTION AFTER EACH MOVIE IS LOADED
		public function movieLoaded(myevent: Event): void {
			this.addChild(MovieClip(myevent.target.content));
			/*
			for (var ind: int = 0; ind < this.numChildren; ind++) {
				var str: String = this.getChildAt(ind).toString(); //return "[object AboutCEE]"				
				trace("str = " +str);
			}
			*/
			trace("movie loaded = " +myevent.target.content.name);

			//CHECK FOR ALL MOVIES BE LOADED
			counterMoviesLoaded++;
			if (counterMoviesLoaded == 7) {
				allMoviesLoaded();
			}
		}


		/* IF ALL MOVIES ARE LOADED PUT HOME MOVIE AT TOP
		 * AND ASSIGN EVERY MOVIE A NAME */
		private function allMoviesLoaded() {


			for (var ind: int = 0; ind < this.numChildren; ind++) {
				var str: String = this.getChildAt(ind).toString(); //return "[object AboutCEE]"				
				var movieName: String = str.substring(8, str.length - 1);
trace("name: " +movieName);
				switch (movieName) {
					case "HomeCEE":
						homeMC = MovieClip(this.getChildAt(ind));
						//homeObj = this.getChildAt(ind);												
						break;
					case "AboutCEE":
						aboutMC = MovieClip(this.getChildAt(ind));
						//aboutObj = this.getChildAt(ind);												
						break;
					case "ConceptsCEE":
						conceptsMC = MovieClip(this.getChildAt(ind));
						conceptsObj = this.getChildAt(ind);
						break;
					case "SearchCEE":
						searchMC = MovieClip(this.getChildAt(ind));
						searchObj = this.getChildAt(ind);
						break;
					case "LessonsCEE":
						lessonsMC = MovieClip(this.getChildAt(ind)); //ACCESS DISPLAY OBJ IN LESSON MC
						lessonsObj = this.getChildAt(ind); //ACCESS CODE IN LESSON MC
						break;
					case "PubsCEE":
					case "PublicationsCEE":
						pubsMC = MovieClip(this.getChildAt(ind)); //ACCESS DISPLAY OBJ IN LESSON MC						
						pubsObj = this.getChildAt(ind);
						break;
					case "EulaCEE":
						eulaMC = MovieClip(this.getChildAt(ind));
						//eulaObj = this.getChildAt(ind);												
						break;
					default:
						trace("default:" + movieName);
				}
			}


			//ADD HOME TO THE TOP 
			this.addChild(homeMC);
			//lastPageVisited =  HOMEPAGE;
			//lastPageVisited.push(HOMEPAGE);
			//trace("LasPageVisited.push("+HOMEPAGE);
			currentPageVisited = HOMEPAGE;
			//checkForShareObject();
			//this.addChild(eulaMC);			

			//CREATE AN OBJECT THAT CONTAINS AN ARRAY
			//OF CONTENT TABLES IN HTML FORMAT
			//USED FOR PUBLICATIONS MOVIE
			var passpath2 = mypdffinal + "_VE50DATA/";
			trace("htmlPath" + htmlPath + "           passpath2" + passpath2);
			htmlContentTable = new HTMLContentTables(htmlPath, passpath2);
		}
		/*
		private function checkForShareObject()
		{
			//SET A SHAREOBJECT IN CLIENT
			var shareObj2:SharedObject = SharedObject.getLocal("ceeCookie", "/");
			if( shareObj2.data.returnClient)
			{
				//shareObj2.clear(); ///////////////////////JUST FOR TESTING PURPOSE  /////////////DELETE AFTER TESTING
			}
			else
			{
				this.addChild(eulaMC);				
			}
		}
		
		*/
		/*******************************************************************
		 * FUNCTION:  DISPLAY PAGE (MOVIE) HOME, ABOUT, CONCEPTS, SEARCH & LESSONS
		 * USE RELATIVE PATH TO GET MAIN MOVIE = Ex. MainCEE
		 * FIND CHILD INDEX OF MOVIE  Ex."HomeCEE" & ADD IT ON TOP OF DISPLAY STACK
		 *******************************************************************/
		public function gotoPage(movieName: String): void {
			/*
			if(backfrompubs){
				trace("currentPageVisited"+currentPageVisited);
				lastPageVisited.push(currentPageVisited);
				trace("lastPageVisited go to page"+lastPageVisited);
				backfrompubs = true;
				//trace("LasPageVisited.push(current="+ currentPageVisited);
				trace("page path  "+lastPageVisited);
			}*/
			trace("-------------------------------" + movieName + "-------------------------------");
			switch (movieName) {
				case HOMEPAGE:
					lastPageVisited.push(HOMEPAGE);
					lastPubType = [];
					lastPubId = [];
					lastPDFlessonId = [];
					lastConceptIdVisited = [];
					lastSearchScreen = [];
					lastStateSearchScreen = [];
					currentConceptIdVisited = "";
					lastPageVisited = [HOMEPAGE];
					this.addChild(homeMC);

					break;
				case ABOUT:
					lastPageVisited.push(ABOUT);
					this.addChild(aboutMC);
					break;
				case CONCEPTS:
					lastPageVisited.push(CONCEPTS);
					this.addChild(conceptsMC);
					break;
				case SEARCH:
					trace("search trip");
					//lastPageVisited.push(searchMC);
					this.addChild(searchMC);
					var backS2: int = lastSearchScreen.length;
					if (backS2 < 1) {
						searchObj.byKeywordSearch3();
					}
					break;
				case LESSONS:
					lastPageVisited.push(LESSONS);
					this.addChild(lessonsMC);
					break;
				case PUBS:
					//lastPageVisited.push(PUBS);
					this.addChild(pubsMC);
					break;
				default:
					trace("some other movie");
			}
			clikingBack = false;
			currentPageVisited = movieName;
			trace(currentPageVisited);
		}

		/*******************************************************************
		 * FUNCTION:  DISPLAY PAGE by back (MOVIE) HOME, ABOUT, CONCEPTS, SEARCH & LESSONS
		 * USE RELATIVE PATH TO GET MAIN MOVIE = Ex. MainCEE
		 * FIND CHILD INDEX OF MOVIE  Ex."HomeCEE" & ADD IT ON TOP OF DISPLAY STACK
		 *******************************************************************/
		public function gotoPage2(movieName: String): void {
			trace("page 2 jump     " + lastPageVisited);

			/*if (movieName=="home")
			{
				trace("did this jump");
				lastPageVisited=["home"];
				trace("page home child         "+lastPageVisited);
			}*/
			trace("movie name      " + movieName);
			switch (movieName) {
				case HOMEPAGE:
					trace("this is the home child");

					currentPageVisited = HOMEPAGE;
					lastPubType = [];
					lastPubId = [];
					lastPDFlessonId = [];
					lastConceptIdVisited = [];
					lastSearchScreen = [];
					lastStateSearchScreen = [];
					currentConceptIdVisited = "";
					lastPageVisited = [HOMEPAGE];
					this.addChild(homeMC);

					break;
				case ABOUT:
					currentPageVisited = ABOUT;
					this.addChild(aboutMC);
					//lastPageVisited.pop();
					break;
				case CONCEPTS:
					currentPageVisited = CONCEPTS;
					this.addChild(conceptsMC);
					//lastPageVisited.pop();
					break;
				case SEARCH:
					/*
						
						this.addChild(searchMC);												
						break; */
					currentPageVisited = SEARCH;
					var backS: int = lastSearchScreen.length;
					if (backS > 0) {
						var search: String = lastSearchScreen[backS - 1];
						trace("search jump" + search);
						//trace("ID+++++++++++" + id);
						//lastSearchScreen.pop();							
						//setConceptLesson(concept);						
						//clikingBack = false;				
						this.addChild(searchMC);
						switch (search) {
							case "sstate":
								trace("sstate length  " + lastStateSearchScreen.length)
								trace(lastStateSearchScreen[lastStateSearchScreen.length - 1]);
								var dstemp = lastStateSearchScreen[lastStateSearchScreen.length - 1];
								//trace(dstemp.charAt(0));
								if (lastStateSearchScreen.length > 0 && dstemp.charAt(0) == "s") {
									this.searchObj.loadStateStandard(lastStateSearchScreen[lastStateSearchScreen.length - 1]);
								} else if (lastStateSearchScreen.length > 0 && dstemp.charAt(0) != "s" && dstemp.charAt(0) != "m") {
									this.searchObj.findLessonByStandard(lastStateSearchScreen[lastStateSearchScreen.length - 1]);
								} else {

									searchObj.standardSearch2();
								}
								break;
							case "skeyword":
								searchObj.byKeywordSearch2();
								break;
							case "spublication":
								searchObj.byPubSearch2();
								break;
							default:

								trace("function not found");
						}

					}
					//currentPageVisited = movieName;
					break;
				case LESSONS:
					//this.addChild(lessonsMC);	
					trace("back click                              " + lastConceptIdVisited);
					var back: int = lastConceptIdVisited.length;
					trace("back+++++++++++" + back);
					if (back > 0) {
						var id: String = lastConceptIdVisited[back - 1];
						//trace("ID+++++++++++" + id);
						//lastConceptIdVisited.pop();					
						var concept: ConceptItem = getConceptItemPerId2(id)
						//setConceptLesson(concept);						
						clikingBack = false;
						this.addChild(lessonsMC);
						lessonsObj.setConceptText(concept);

						//	
					}

					break;
				case PUBS:
					//this.addChild(pubsMC);																		
					if (lastPubType[lastPubType.length - 1] == "lesson") {
						setPDFLesson2(lastPDFlessonId[lastPDFlessonId.length - 1], isBrowseLesson);
						//dbacktrue=true;
						//trace("pre lastPubType pdf "+lastPubType);
						//trace("pre lastPDFlessonId pdf "+lastPDFlessonId);
						//lastPubType.pop();
						//lastPDFlessonId.pop();
						//trace("lastPubType pdf "+lastPubType);
						//trace("lastPDFlessonId pdf "+lastPDFlessonId);

					} else if (lastPubType[lastPubType.length - 1] == "pub") {
						//trace("lastPubType     "+lastPubType);
						//trace("lastPubId     "+lastPubId);
						setPublicationLesson2(lastPubId[lastPubId.length - 1], isBrowseLesson);
						//trace("pre last pub type pub " +lastPubType );
						//trace("pre last pub id pub " +lastPubId );
						//lastPubType.pop();
						//lastPubId.pop();
						//dbacktrue=true;
						//trace("last pub type pub " +lastPubType );
						//trace("last pub id pub " +lastPubId );
					}

					this.addChild(pubsMC);
					//lastPageVisited.pop();
					break;
				default:
					trace("some other movie");
			}

			//currentPageVisited = movieName;				

		}

		public function setLastPDFlessonId(str: String, isBrowsigLesson: Boolean): void {
			isBrowseLesson = isBrowsigLesson; //if came from Bowse Lessons = true
			//lastPageVisited.push(PUBS);
			lastPageVisited.push(PUBS);
			currentPDFlessonId = str;
			currentPubType = "lesson";
		}
		public function setLastPDFlessonId2(str: String, isBrowsigLesson: Boolean): void {
			isBrowseLesson = isBrowsigLesson; //if came from Bowse Lessons = true
			trace("lastPDFlessonId" + lastPDFlessonId);
			lastPageVisited.push(PUBS);
			lastPDFlessonId.push(str);
			currentPubType = "lesson";
			lastPubType.push(currentPubType);
		}
		public function setLastPubId(str: String, isBrowsigLesson: Boolean): void {
			isBrowseLesson = isBrowsigLesson;

			//lastPubType.push(currentPubType);
			lastPageVisited.push(PUBS);
			currentPubType = "pub";
			lastPubType.push(currentPubType);
			lastPubId.push(str);
			currentPubId = str;

		}
		public function setLastPubId2(str: String, isBrowsigLesson: Boolean): void {
			lastPageVisited.push(PUBS);
			isBrowseLesson = isBrowsigLesson; //if came from Bowse Lessons = true
			currentPubType = "pub";
			lastPubType.push(currentPubType);
			lastPubId.push(str);
			trace("++++++++++++++++++++++++++++++++++++++++lastPageVisited " + lastPageVisited);
			trace("last pub hit " + lastPubId);
			trace("lastPageVisited   22    " + lastPageVisited);
		}

		/******************
		
		**********************/
		public function setLastSearch(str: String, isBrowsigSearch: Boolean): void {
			lastPageVisited.push(SEARCH);
			isBrowsigSearch = isBrowsigSearch; //if came from Bowse Lessons = true
			//currentPubType = "pub";
			trace("Search track previous   " + str);
			lastSearchScreen.push(str);
			trace("lastPageVisited   Search    " + lastPageVisited);
			trace("lastSearchVisited   Search    " + lastSearchScreen);
		}

		public function setLastStateSearch(str: String, str2: String, isBrowsigSearch: Boolean): void {
			lastPageVisited.push(SEARCH);
			isBrowsigSearch = isBrowsigSearch; //if came from Bowse Lessons = true
			//currentPubType = "pub";
			trace("Search track previous   " + str);
			lastSearchScreen.push(str);
			lastStateSearchScreen.push(str2);
			trace("lastPageVisited   Last State    " + lastPageVisited);
			trace("lastSearchVisited   Last State    " + lastSearchScreen);
		}
		/*******************************************************************
		 * FUNCTION: GET CONCEPT ITEM PER ID
		 *******************************************************************/
		public function getConceptItemPerId(id: String): ConceptItem {
			//Get the array from Concepts for first time
			//trace("id                 "+id);
			if (conceptsListArray.length == 0) {
				conceptsListArray = conceptsObj.getConceptArray();
			}

			var conceptitem: ConceptItem;
			for (var ind: int = 0; ind < conceptsListArray.length; ind++) {
				//var item:ConceptItem = conceptsListArray[ind];
				var theid: String = "" + conceptsListArray[ind].conceptID;
				if (theid == id) {
					conceptitem = conceptsListArray[ind];
				}
			}
			//trace("conceptitem              "+conceptitem);
			//setConceptLesson2(conceptitem);
			return conceptitem;
		}
		public function getConceptItemPerId2(id: String): ConceptItem {
			//Get the array from Concepts for first time
			if (conceptsListArray.length == 0) {
				conceptsListArray = conceptsObj.getConceptArray();
			}
			currentPageVisited = PUBS;
			var conceptitem: ConceptItem;
			for (var ind: int = 0; ind < conceptsListArray.length; ind++) {
				//var item:ConceptItem = conceptsListArray[ind];
				var theid: String = "" + conceptsListArray[ind].conceptID;
				if (theid == id) {
					conceptitem = conceptsListArray[ind];
				}
			}
			//trace("conceptitem              "+conceptitem);
			return conceptitem;
		}
		public function setlessonpage(_concept: ConceptItem): void {
			currentConceptIdVisited = "" + _concept.conceptID;
			//}else{
			trace("currentConceptIdVisited" + currentConceptIdVisited);
			lastConceptIdVisited.push(currentConceptIdVisited);

			trace("lastConceptIdVisited                                           " + lastConceptIdVisited);
			//lastConceptIdVisited.push(currentConceptIdVisited);
			lastPageVisited.push(LESSONS);
			trace("lastPageVisited                            " + lastPageVisited);
		}
		public function setConceptLesson2(args) {

			trace("when did this go boom");
			//currentConceptIdVisited = ""+ _concept.conceptID;


			trace("currentConceptIdVisited                                           " + currentConceptIdVisited);
			//lastConceptIdVisited.push(currentConceptIdVisited);


			//gotoPage(LESSONS);			
			//to access the class code of lessonMC
			//lessonsObj.setConceptText(_concept);					

		}

		/*******************************************************************
		 * FUNCTION: GET LESSON PER ID
		 *******************************************************************/
		public function getLessonPerId(id: String): LessonItem {
			var publicationObj: Publication = pubsObj.getPublicationObject();
			var lessonitem: LessonItem = publicationObj.getLessonItem(id);
			return lessonitem;
		}

		/*******************************************************************
		 * FUNCTION:  SET CONCEPT IN LESSON PAGE
		 *******************************************************************/
		public function setConceptLesson(_concept: ConceptItem): void {
			trace("this function 1");
			//If user is clicking back button don't record page in history
			//else clean history and start new history
			//if(clikingBack == true){	
			//clikingBack = false;				
			//}else{					
			//if(currentConceptIdVisited == "" )	{
			currentConceptIdVisited = "" + _concept.conceptID;
			//}else{
			//trace("currentConceptIdVisited"+currentConceptIdVisited);
			lastConceptIdVisited.push(currentConceptIdVisited);
			//currentConceptIdVisited = ""+_concept.conceptID;
			//}
			//lastConceptIdVisited.push( _concept.conceptID);
			//trace("Lesson id =====" +  _concept.conceptID);
			//trace(lastConceptIdVisited);				
			//}

			gotoPage(LESSONS);
			//to access the class code of lessonMC
			lessonsObj.setConceptText(_concept);

		}

		/*******************************************************************
		 * FUNCTION:  SET FIRST PUBLICATION  PAGE
		 *******************************************************************/
		public function setPublicationLesson(id: String, isLessonBrowse: Boolean): void {
			trace("this function 2"); //If user is clicking back button don't record page in history
			//else clean history and start new history
			//trace("1   id         "+id+"            isLessonBrowse        "+isLessonBrowse);
			//lastPubId.push(id);
			//lastPubType.push("pub");
			//trace("lastPDFlessonId on open pub "+lastPubId);
			if (clikingBack == true) {
				clikingBack = false;
			}
			currentPageVisited = PUBS;
			gotoPage(PUBS);
			pubsObj.setPublicationPage(htmlContentTable, id, isLessonBrowse);

			//If user is clicking back button don't record page in history
			//else clean history and start new history
			//if(clikingBack == true){	clikingBack = false;}
			//else{	
			//lastPubIdVisited = new Array();
			//lastPubIdVisited.push(_concept.conceptID);
			//}
		}
		public function setPublicationLesson2(id: String, isLessonBrowse: Boolean): void {
			//If user is clicking back button don't record page in history
			//else clean history and start new history
			//trace("2    id         "+id+"            isLessonBrowse        "+isLessonBrowse)
			if (clikingBack == true) {
				clikingBack = false;
			}

			//trace("lastPubType       "+lastPubType+"    lastPubId          "+lastPubId);
			backfrompubs = false;
			//gotoPage(PUBS);
			currentPageVisited = PUBS;
			pubsObj.setPublicationPage(htmlContentTable, id, isLessonBrowse);

			//If user is clicking back button don't record page in history
			//else clean history and start new history
			//if(clikingBack == true){	clikingBack = false;}
			//else{	
			//lastPubIdVisited = new Array();
			//lastPubIdVisited.push(_concept.conceptID);
			//}
		}
		/*******************************************************************
		 * FUNCTION:  GET HTMLCONTENTTABLE
		 *******************************************************************/
		public function getContentTable(): HTMLContentTables {
			return htmlContentTable;
		}

		/*******************************************************************
		 * FUNCTION:  SET LESSON PDF
		 *******************************************************************/

		public function setPDFLesson(id: String, isLessonBrowse: Boolean): void {
			if (clikingBack == true) {
				clikingBack = false;
			}
			//window.restore();
			//checkSize2();
			var lessonitem: LessonItem = getLessonPerId(id);
			pdfFile = lessonitem.pdf_url;
			trace("lessonitem.pdf_url -----------" + pdfFile); //1-56183-614-1
			lastPDFlessonId.push(id);
			lastPubType.push("lesson");
			//currentPageVisited = PUBS;
			//CHECK IF pdfFile IS A WEB LINK INSTEAD OF A PDF FILE NAME
			//MEANS IF pdfFile CONTAINS "http:" THEN IS A WEB LINK AND LAUNCH IT
			var myPattern: RegExp = /http:/i;
			if (pdfFile.search(myPattern) > -1) {
				try {
					var urlreq: URLRequest = new URLRequest(pdfFile);
					navigateToURL(urlreq, "_blank");
				} catch (e: Error) {
					trace(e.message);
				}
				//open a web page
				//http://ve.councilforeconed.org
			} else {

				//trace("setPDF -----------" + id);//15924
				if (HTMLLoader.pdfCapability == HTMLPDFCapability.STATUS_OK) {
					trace("PDF content can be displayed");
				} else {
					trace("PDF cannot be displayed. Error code:", HTMLLoader.pdfCapability);
				}

				var htmlLoader: HTMLLoader = new HTMLLoader();
				//trace("is this tripping");
				//var lessonitem:LessonItem = getLessonPerId( id );
				//pdfFile = lessonitem.pdf_url;

				//trace("lessonitem.pdf_url -----------" + pdfFile);//1-56183-614-1

				//pdfFile = "1-56183-051-8_07";

				var pdfFullPath: String = mypdffinal + pdfPath + pdfFile;
				trace("pdfFullPath     " + pdfFullPath);
				var url: URLRequest = new URLRequest(pdfFullPath);

				htmlLoader.width = hwidth;
				htmlLoader.height = hheight;

				//holder = new MovieClip();						

				//holder.x=position[0];
				//holder.y=position[1];								

				// function pdfLoaded(myevent:Event):void{	

				//trace("this function 3");
				gotoPage(PUBS);
				//holder.addChild(htmlLoader);	
				//holder.visible=false;
				pubsObj.setPDFPage(pdfFullPath, id, isLessonBrowse);
				//}									
				//try	{															
				//htmlLoader.load(url);			
				//htmlLoader.addEventListener(Event.COMPLETE, pdfLoaded);					
				//}catch(error:IllegalOperationError){ trace("<Error> " + error.message);	}															
			}
		}
		public function holdertrue() {
			//holder.visible=true;
		}
		public function setPDFLesson2(id: String, isLessonBrowse: Boolean): void {
			if (clikingBack == true) {
				clikingBack = false;
			}
			window.restore();
			var lessonitem: LessonItem = getLessonPerId(id);
			pdfFile = lessonitem.pdf_url;
			//trace("lessonitem.pdf_url -----------" + pdfFile);//1-56183-614-1
			trace("===================================================================1");

			//CHECK IF pdfFile IS A WEB LINK INSTEAD OF A PDF FILE NAME
			//MEANS IF pdfFile CONTAINS "http:" THEN IS A WEB LINK AND LAUNCH IT
			var myPattern: RegExp = /http:/i;
			if (pdfFile.search(myPattern) > -1) {
				try {
					var urlreq: URLRequest = new URLRequest(pdfFile);
					navigateToURL(urlreq, "_blank");
				} catch (e: Error) {
					trace(e.message);
				}
				//open a web page
				//http://ve.councilforeconed.org
			} else {

				//trace("setPDF -----------" + id);//15924
				if (HTMLLoader.pdfCapability == HTMLPDFCapability.STATUS_OK) {
					trace("PDF content can be displayed");
				} else {
					trace("PDF cannot be displayed. Error code:", HTMLLoader.pdfCapability);
				}

				var htmlLoader: HTMLLoader = new HTMLLoader();
				//trace("is this tripping");
				//var lessonitem:LessonItem = getLessonPerId( id );
				//pdfFile = lessonitem.pdf_url;

				//trace("lessonitem.pdf_url -----------" + pdfFile);//1-56183-614-1

				//pdfFile = "1-56183-051-8_07";
				var pdfFullPath: String = mypdffinal + pdfPath + pdfFile;
				trace("pdfFullPath     " + pdfFullPath);

				//var url:URLRequest = new URLRequest(pdfFullPath);

				htmlLoader.width = hwidth;
				htmlLoader.height = hheight;

				//holder = new MovieClip();						

				//holder.x=position[0];
				//holder.y=position[1];								

				// function pdfLoaded(myevent:Event):void{		
				// trace("this function 4");
				gotoPage(PUBS);
				//holder.addChild(htmlLoader);	
				//holder.visible=false;
				pubsObj.setPDFPage(pdfFullPath, id, isLessonBrowse);
				//}									
				//try	{															
				//htmlLoader.load(url);			
				//htmlLoader.addEventListener(Event.COMPLETE, pdfLoaded);					
				//}catch(error:IllegalOperationError){ trace("<Error> " + error.message);	}															
			}
		}

		/*******************************************************************
		 * FUNCTION:  GO BACK TO LAST VISITED PAGE
		 *******************************************************************/
		/*
		public function gotoLastPage() :void
		{			
			gotoPage(lastPageVisited);						
		}	
		*/
		public function gotoLastPage(): void {
			/*if( currentPageVisited == LESSONS && lastConceptIdVisited[lastConceptIdVisited.length-1] == currentConceptIdVisited ){
				lastConceptIdVisited.pop(); 
			}
			//currentConceptIdVisited
			//lastConceptIdVisited.pop();
			
			if (currentPageVisited == LESSONS && lastConceptIdVisited.length==0 && lastPageVisited[lastPageVisited.length-1]!=LESSONS){	
				lastConceptIdVisited.push(currentConceptIdVisited);
				//lastConceptIdVisited.pop();
			}
			*/
			//if (lastPageVisited.length == 2){

			if (lastPageVisited[lastPageVisited.length - 1] == LESSONS && lastConceptIdVisited.length > 1) {
				trace("pop last concept id");
				lastConceptIdVisited.pop();
			}

			//}
			/*
			if (lastPageVisited[lastPageVisited.length-1]==LESSONS && lastConceptIdVisited.length>1){	
				trace("pop last concept id");
				lastConceptIdVisited.pop();
			}
					
			*/

			if (lastSearchScreen[lastSearchScreen.length - 1] == "sstate" && lastPageVisited[lastPageVisited.length - 1] == SEARCH) {
				lastStateSearchScreen.pop();
			}
			if (lastPageVisited[lastPageVisited.length - 1] == SEARCH) {
				//lastStateSearchScreen.pop();
				lastSearchScreen.pop();
			}
			//if(currentPageVisited == SEARCH && lastSearchScreen.length == 0 && lastStateSearchScreen.length == 0 && lastPageVisited[lastPageVisited.length-1]==SEARCH){
			//lastPageVisited.pop();
			//}
			trace("pre lastPubType clear+++++++++++++++++++++++++" + lastPubType);
			trace("pre lastPDFlessonId clear+++++++++++++++++++++++++" + lastPDFlessonId);
			trace("pre lastPubId clear+++++++++++++++++++++++++" + lastPubId);
			trace("pre lastPageVisited clear+++++++++++++++++++++++++" + lastPageVisited);
			trace("pre currentPageVisited clear+++++++++++++++++++++++++" + currentPageVisited);
			trace("pre isBrowseLesson clear+++++++++++++++++++++++++ " + isBrowseLesson);

			/*if (lastPageVisited[lastPageVisited.length-1]==PUBS){																	
				if(lastPubType[lastPubType.length-1] == "lesson" ){	
					trace("lastPubType back 2       "+lastPubType);
						//setPDFLesson(lastPDFlessonId[lastPDFlessonId.length-1], isBrowseLesson);
					lastPubType.pop();
					lastPDFlessonId.pop();
					//if (lastPageVisited[lastPageVisited.length-1]==PUBS){
						//lastPageVisited.pop();
					//}
					
				} else if(lastPubType[lastPubType.length-1] == "pub" ){	
					trace("lastPubId back 2       "+lastPubId);
					lastPubType.pop();
					lastPubId.pop();
				}
			}*/


			if (lastPageVisited[lastPageVisited.length - 1] == PUBS) {
				if (lastPubType[lastPubType.length - 1] == "lesson") {
					trace("lastPubType back 2       " + lastPubType);
					//setPDFLesson(lastPDFlessonId[lastPDFlessonId.length-1], isBrowseLesson);
					lastPubType.pop();
					lastPDFlessonId.pop();

				} else if (lastPubType[lastPubType.length - 1] == "pub") {
					trace("lastPubId back 2       " + lastPubId);
					lastPubType.pop();
					lastPubId.pop();
				}
			}
			lastPageVisited.pop();
			trace("lastPubType clear+++++++++++++++++++++++++" + lastPubType);
			trace("lastPDFlessonId clear+++++++++++++++++++++++++" + lastPDFlessonId);
			trace("lastPubId clear+++++++++++++++++++++++++" + lastPubId);
			trace("lastPageVisited clear+++++++++++++++++++++++++" + lastPageVisited);
			trace("currentPageVisited clear+++++++++++++++++++++++++" + currentPageVisited);
			trace("isBrowseLesson clear+++++++++++++++++++++++++ " + isBrowseLesson);
			/*
			if (currentPageVisited == "pubs" && lastPubType.length > 0 && lastPageVisited[lastPageVisited.length-1]!=PUBS){	
				trace("-------------------------------trip here---------------------------------");
				lastPageVisited.push(PUBS);
			} /*else if (currentPageVisited == "pubs" && lastPubType.length == 0 && lastPageVisited[lastPageVisited.length-1]==PUBS){	
				trace("-------------------------------trip here2---------------------------------");
				lastPageVisited.pop();
			}*/
			/*if (lastPageVisited[lastPageVisited.length-1]==PUBS){																	
				if(lastPubType[lastPubType.length-1] == "lesson" ){	
					trace("lastPubType back 2       "+lastPubType);
						//setPDFLesson(lastPDFlessonId[lastPDFlessonId.length-1], isBrowseLesson);
					lastPubType.pop();
					lastPDFlessonId.pop();
					//if (lastPageVisited[lastPageVisited.length-1]==PUBS){
						//lastPageVisited.pop();
					//}
					
				} else if(lastPubType[lastPubType.length-1] == "pub" ){	
					trace("lastPubId back 2       "+lastPubId);
					lastPubType.pop();
					lastPubId.pop();
				}
			}*/

			clikingBack = true;
			var dwback: int = (lastPageVisited.length);
			//trace("dwback  "+dwback);
			var dwjump: String = lastPageVisited[dwback - 1];
			trace("dwjump  " + dwjump);
			trace(" lastPageVisited[dwback-1]=" + lastPageVisited[dwback - 1]);
			//trace(lastPageVisited);
			//trace("dwback"+dwback);
			//trace("dwjump"+dwjump);
			//if(dwjump != "home"){

			//var popStr:String = lastPageVisited.pop();
			trace("check it here            " + lastPageVisited);

			//trace("popStr  "+popStr);
			//}
			if (currentPageVisited == "home") {
				trace("Home");
				lastPubType = [];
				lastPubId = [];
				lastPDFlessonId = [];
				lastConceptIdVisited = [];
				lastSearchScreen = [];
				lastStateSearchScreen = [];
				currentConceptIdVisited = "";
			}
			//trace("lastPageVisited.pop()="+popStr);
			trace("end         " + lastPageVisited);
			gotoPage2(dwjump);
		}


		public function closeApplication() {
			NativeApplication.nativeApplication.exit();
			// this.nativeApplication.exit();
			/* //DISPLAY AN ALERT DIALOG BEFORE CLOSE
			public function cerrarApp(e:Event):void{
			  e.preventDefault();
			  Alert.yesLabel = "Si";
			  Alert.show("¿Realmente quiere salir?","Salir aplicación", Alert.YES | Alert.NO, null, close, null, 2);
			}
			private function cerrar(e:CloseEvent):void{
			  if(e.detail == Alert.YES) {this.nativeApplication.exit(); }
			} 
			*/
		}


	} // Main class

} //end package