/******************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/14/2010
 * 
 * Publication class for lessonsFrame5 movie of the CEE application
 * CS5 version of com.digitec.cee.Publication
 *  
*******************************************/
package com.digitec.cee
{
	import flash.display.MovieClip;	
	import flash.events.*;			
	import flash.text.StyleSheet;
	import flash.net.URLLoader;
	import flash.net.URLRequest;	
	import flash.net.NetConnection;
    import flash.net.NetStream;
	import flash.events.IOErrorEvent;
	public class Publication
	{								
		//private var XML_PATH:String ="data/library_ve50.xml";  //MainConstants.XML_GLOSSARY_FULLPATH; // 
		private var xmlLibrary:String = MainConstants.LIBRARY;
		private var CSS_PATH:String ="data/pubs.css";// MainConstants.CSS_GLOSSARY_FULLPATH;		// 		
		
		//private  var cssPublication:StyleSheet;		
		 private  var cssPublication:StyleSheet= new StyleSheet();												
		private var pubsXmlList:XMLList;				
		private var lessonsXmlList:XMLList;						
		private var $nc:NetConnection;
        private var $ns:NetStream;
        private var $streampath:String;
        private var $streamurl:String = MainConstants.CEE_SERVER;
		private var pubItemsArray:Array = new Array();				
		private var lessonItemsArray:Array = new Array();
		private var dwpasspub:String;
		private var dpubpath:String;
		public function Publication(dpath)
		 {					
			dpubpath=dpath;
		 	trace("dpath"+dpath);
			loadPublication(xmlLibrary, dpath);			
			loadCSS();
		}								
		
		/*******************************************************************
		 * FUNCTION: GET AN ARRAY OF PUBLICATION ITEMS
		 *******************************************************************/
		 public function getPubItemArray():Array
		{						
			return pubItemsArray;
		}
		
		/*******************************************************************
		 * FUNCTION: GET AN ARRAY OF LESSONS ITEMS
		 *******************************************************************/
		 public function getLessonItemArray():Array
		{						
			return lessonItemsArray;
		}
		
		/*******************************************************************
		 * FUNCTION: GET FULL PUBLICATION ITEMS ARRAY
		 *******************************************************************/
		 public function getXMLPublication():XMLList//Array
		{						
			return pubsXmlList;
		}
		
		/*******************************************************************
		 * FUNCTION: GET STYLE SHEET FOR PUBLICATIONS
		 *******************************************************************/
		 public function getStyleSheet():StyleSheet
		{						
			return cssPublication;
		}
		
		/*******************************************************************
		 * FUNCTION: GET AN PUBLICATION
		 *******************************************************************/
		 public function getPublicationItem():String //GlossaryItem
		{						
			return "item";
		}
		 
		
		/*******************************************************************
		 * FUNCTION: SETUP FRAME TEXT_FIELDS  FROM XML
		 *******************************************************************/
		private function loadPublication(stream:String, dpath):void
		{					
		trace("dpath222222222222 "+dpath);
			dwpasspub = stream;
			var streamtemp =$streamurl+ dwpasspub;
			
			loadPublicationf(dpubpath+ "_VE50DATA/"+MainConstants.XMLPATH + dwpasspub);
			trace("MainConstants.XMLPATH + dwpasspub"+dpubpath+ "_VE50DATA/"+MainConstants.XMLPATH + dwpasspub);
				
		    //2019-07-11 NOLOAD
		    /*
			var pubsUrltemp:URLRequest = new URLRequest(streamtemp); 
			var pubsLoadertemp:URLLoader = new URLLoader();
			pubsLoadertemp.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandlerPUB);
			pubsLoadertemp.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			pubsLoadertemp.load(pubsUrltemp);
			*/
			
		}
		private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
		private function httpStatusHandlerPUB(event:HTTPStatusEvent):void {
            trace("[Publication.as:114]httpStatusHandler: -----------------------------------" + event);
            trace("status: " + event.status);
			if (event.status == 0){
				
				loadPublicationf(dpubpath+ "_VE50DATA/"+MainConstants.XMLPATH + dwpasspub);
				trace("MainConstants.XMLPATH + dwpasspub"+dpubpath+ "_VE50DATA/"+MainConstants.XMLPATH + dwpasspub);
			}else {
				loadPublicationf($streamurl+ dwpasspub);
			}
			//loadLibraryFinal(MainConstants.XMLPATH + dwpasspub);
        }
        
		private function loadPublicationf(stream:String):void
		{						
			//LOADGLOSSARY XML
			trace("pubstream 2: "+stream);
			var pubsLoader:URLLoader = new URLLoader();
			pubsLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			var pubsUrl:URLRequest = new URLRequest(stream); 
			
			pubsLoader.load(pubsUrl);
			pubsLoader.addEventListener(Event.COMPLETE,pubsLoaded);
			function pubsLoaded(event:Event):void {
				var _xml:XML = new XML(pubsLoader.data);   				
				//pubsXmlList = new XMLList(_xml.pub);								
				//pubsXmlList = new XMLList(_xml.publications.pub);								
				lessonsXmlList =  new XMLList(_xml.lessons.lesson);								
				pubsXmlList = new XMLList(_xml.publications.pub);								
				createObjectsArray();
			}										
										
		}
		
		
		/*******************************************************************
		 * FUNCTION: CREATE PUBITEMS AND LESSONITEMS ARRAYS
		 *******************************************************************/
		private function createObjectsArray():void
		{						
				 var item:XML;
				for each(item in pubsXmlList)
				{					
					var pubItem:PublicationItem = new PublicationItem(item );																											
					pubItemsArray.push(pubItem);															
				}			
			
				for each(item in lessonsXmlList)
				{					
					var lessonItem:LessonItem = new LessonItem(item );																											
					lessonItemsArray.push(lessonItem);															
				}	
		}
		
		/*******************************************************************
		 * FUNCTION: SETUP FRAME TEXT_FIELDS  FROM XML
		 *******************************************************************/
		private function loadCSS():void
		{								
			//LOAD GLOSSARY CSS AND APPLY TO TERMS TEXT
			var pUrl:URLRequest =  new URLRequest(CSS_PATH);   // new URLRequest("data/pubs.css");//
			var pLoader:URLLoader = new URLLoader();
			pLoader.load(pUrl);
			pLoader.addEventListener(Event.COMPLETE,pLoaded);
			function pLoaded(event:Event):void {				
   				// cssPublication= new StyleSheet();												
				cssPublication.parseCSS(URLLoader(event.target).data);							
			}							
		}																
		
				
		/*************************************************************************************
		 * FUNCTIONS: GET  PUBLICATIONS PER ID
		  ************************************************************************************/						  
		public function getPublication( strid:String ):PublicationItem
		{							
			var returnPubItem:PublicationItem;
			//var item_str:String;						
			for(var index:int = 0; index < pubItemsArray.length; index++ )
			{
				if(strid == pubItemsArray[index].pid ) { returnPubItem = pubItemsArray[index]; }
			}
			/*
			if(strid!=null)
			{				
           		var item:XML;
				for each(item in pubsXmlList)
				{
					//CREATE PUBLICATION ITEM OBJECT & INSERT IT IN CORRESPONDING CATEGORY					
					var pubItem:PublicationItem = new PublicationItem(item );																											
					pubItemsArray.push(pubItem);										
					// GET THE PUB THAT MATCH THE ID
					if( item.@id == strid )	{	returnPubItem = pubItem; }
				}	
			}										
			*/
			return returnPubItem;
		}						
		
		/*************************************************************************************
		 * FUNCTIONS: GET  LESSONS PER ID
		  ************************************************************************************/				
		public function getLessonItem( strid:String ):LessonItem
		{							
			var returnLessonItem:LessonItem;
			for(var index:int = 0; index < lessonItemsArray.length; index++ )
			{
				if(strid == lessonItemsArray[index].id ) { returnLessonItem = lessonItemsArray[index]; }
			}
			return returnLessonItem;
		}						
		
		/*************************************************************************************
		 * FUNCTIONS: GET  LESSONS PER URL
		  ************************************************************************************/				
		public function getLessonItemPerURL(strurl:String):LessonItem
		{							
			var returnLessonItem:LessonItem;
			trace("strurl                               "+strurl);
			
			for(var index:int = 0; index < lessonItemsArray.length; index++ )
			{
				//trace("lessonItemsArray[index].pdf_url:     "+lessonItemsArray[index].pdf_url);
				if(strurl == lessonItemsArray[index].pdf_url ) { 
					returnLessonItem = lessonItemsArray[index];
					
				}
			}
			trace("returnLessonItem                 "+returnLessonItem);
			return returnLessonItem;
		}						
		
		/*************************************************************************************
		 * FUNCTIONS:GET FULL GLOSSARY TERMS
		  ************************************************************************************/				
		public function getFullPublication(  ):String	
		{										
			 var item:XML;
			 var full_str:String = "";			
			for each(item in pubsXmlList)
			{										
					var definition:String =  item.desc;										
					//full_str += "<p class='title'>"+ item.@title+"</p>" + "<p class='def'>"+definition+"</p>" ;											
					full_str += "<p>"+ item.@title+"</p>" + "<p class='desc'>"+definition+"</p>" ;											
			}				
			return full_str;
		}						
		 
		  /*************************************************************************************
		 * FUNCTION: PRINT PUBLICATION
		  ************************************************************************************/
		  private function printPublication(ev:MouseEvent):void
		  {
			 // this.glossary_mc.terms_txt.htmlText= "";
		  }			
		  
		 
	} // Main class
 
} //end package



/*
//////////////////////////BACKUP//////////////////////////////////
public function defineGlossaryTerm( str:String ):String{							
	var item_str:String;			
	if(str!=null)	{				
		 var item:XML;
		for each(item in glossXmlList)	{
			if( item.@name == str ){ 
				//this.glossary_mc.terms_txt.htmlText= item; //WORK
				//this.glossary_mc.terms_txt.htmlText= item.toXMLString(); //ALSO WORK
				var definition:String =  item.def;
				//definition = definition.replace("<def>", ""); //eliminate any <def>
				//definition = definition.replace("</def>", ""); //eliminate any </def>						
				//this.glossary_mc.terms_txt.text=  item.@name + ": "+ definition ; //"Glossary: " + str;						
				item_str= "<p class='term'>"+ item.@name+"</p>" + "<p class='def'>"+definition+"</p>" ;										
			}
		}	
	}	return item_str;
}								
*/







