package com.digitec.cee {
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.*;
	import flash.text.StyleSheet;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.errors.IllegalOperationError;


	public class HTMLContentTables {
		private var htmlXmlList: XMLList;
		private var XML_PATH: String = MainConstants.PUBLICATION_HTML; //private var XML_PATH:String = "data/publicationsHTML.xml" //MainConstants.PUBLICATION_HTML; //  //private var CSS_PATH:String ="data/glossary.css";// MainConstants.CSS_GLOSSARY_FULLPATH;		// 					  //private var pubHTMLarr:Array;  //ARRAY THAT CONTAINS ALL THE TABLE OF COTENT HTML IFOR EACH PUBLICATION ISBN //private var htmlTableArr:Array = new Array(); //Array of HTMLtable elements
		private var dwpassstate: String; //private var tableContent:String;		
		private var $nc: NetConnection;
		private var $ns: NetStream;
		private var $streampath: String;
		private var $streamurl: String = MainConstants.CEE_SERVER;
		private var dwpasspub: String;
		private var dwpasspubpath: String;
		
		public function HTMLContentTables(filePath: String, passpath) {
			dwpasspubpath = passpath;
			loadHTML(filePath);
		}
		
		/***********************************************************************************		 
		* FUNCTION: LOAD HTML 		 
		***********************************************************************************/
		private function loadHTML(filepath: String): void {
			trace("filepath[HTMLContentTable.as:29]: " + filepath);
			dwpassstate = filepath;
			var dwpasstemp = MainConstants.XMLPATH + dwpassstate; //trace("dwpassstate "+dwpassstate);
			var streamtemp = $streamurl + dwpassstate;
			
			trace("local");
		    trace(dwpasspubpath + MainConstants.XMLPATH + dwpassstate); //loadHTML2(vidpicpath+ "_VE4DATA/"+MainConstants.XMLPATH + dwpassstate);
			loadHTML2(dwpasspubpath + MainConstants.XMLPATH + dwpassstate);
				
		    //2019-07-12 NOLOAD
		    /*
			var htmlLoadertemp: URLLoader = new URLLoader();
			htmlLoadertemp.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandlerHTML);
			htmlLoadertemp.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			var htmlUrltemp: URLRequest = new URLRequest(streamtemp);
			htmlLoadertemp.load(htmlUrltemp);
			*/
		}
		
		private function ioErrorHandler(event: IOErrorEvent): void {
			trace("ioErrorHandler: " + event);
		}
		
		private function httpStatusHandlerHTML(event: HTTPStatusEvent): void {
		
			trace("[HTMLContentTable.as:56] httpStatusHandler: -----------------------------------" + event);
			trace("status: " + event.status);
			
			if (event.status == 0) { //var vidpicpath = Object(parent).getDpath();
				trace("local");
				trace(dwpasspubpath + MainConstants.XMLPATH + dwpassstate); //loadHTML2(vidpicpath+ "_VE4DATA/"+MainConstants.XMLPATH + dwpassstate);
				loadHTML2(dwpasspubpath + MainConstants.XMLPATH + dwpassstate);
			} else {
				trace("internet");

				loadHTML2(MainConstants.CEE_SERVER + dwpassstate);


			} //loadLibraryFinal(MainConstants.XMLPATH + dwpasspub);
		}
		
		private function httpStatusHandlerHTML2(event: HTTPStatusEvent): void {
			trace("[HTMLContentTable.as:73]httpStatusHandler: -----------------------------------" + event);
			trace("status: " + event.status);
		}
		
		private function loadHTML2(filepath: String): void {
			trace("[HTMLContentTable.as:78]filepath pb        " + filepath);

			var htmlUrl: URLRequest = new URLRequest(filepath);
			var htmlLoader: URLLoader = new URLLoader();

			htmlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			htmlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandlerHTML2);

			htmlLoader.load(htmlUrl);
			htmlLoader.addEventListener(Event.COMPLETE, htmlLoaded);

			function htmlLoaded(e: Event): void {
				var _xml: XML = new XML(htmlLoader.data);
				htmlXmlList = new XMLList(_xml.tablecontent);
			}
		}
		
		/***********************************************************************************		 
		* FUNCTION: USE PUB ISBN TO EXTRACT TABLE CONTENT FROM HTML 		 
		***********************************************************************************/
		private function $securityErrorHandler(_e: NetStatusEvent): void {
			trace('$securityErrorHandler');
		}
		
		private function $asyncErrorHandler(_e: AsyncErrorEvent): void {
			trace('$asyncErrorEvent');
		}
		
		public function getTableContentFromHTML(_isbn: String): String {
			var item_str: String; //trace("_isbn"+_isbn);
			if (_isbn != null) {
				var item: XML;
				for each(item in htmlXmlList) { //trace("item.isbn"+item.isbn);
					if (item.isbn == _isbn) {
						var tablecontent: String = item.ptable; //trace(item.ptable);
						item_str = "<p class='ptable'>" + tablecontent + "</p>";
					}
				}
			}
			return item_str;
		}
	} // Main class
}