/******************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/14/2010
 * 
 * This is the xml reader for the CEE application
 * CS5 version of com.digitec.cee.XMLReader
 *  
*******************************************/ 
package com.digitec.cee
{
	import flash.errors.IllegalOperationError;
	
    public class XMLReader
    {				
		private var itemArray:Array;
		private var timeleft:String;
		private var tabName:String = "";
		private var moreThan6min:Boolean = true;
				
		/******************************************************************************************
		 *CONSTRUCTOR - parami
		 ******************************************************************************************/  
		public function XMLReader(){ }		
		
		/******************************************************************************************
		 *READ A XML FILE
		 ******************************************************************************************/  
		 public function readXML(itemXML:XML, nholders:int){
			itemArray = new Array();
			itemXML.ignoreWhitespace = true; 					
			var currentItemNode:XMLList = itemXML.children();	
			var i:Number = 0; 
			var childNode:XML;
		
			for each (childNode in currentItemNode) {		
				if(childNode.localName() == "Item"){		
					itemArray[i] = new Item();			
					var node:XML;		
					for each (node in childNode.children()) {			
						if (node.localName() == "ItemID") {										
								try{itemArray[i].setID(node.text());									
								}catch(error:IllegalOperationError){ trace("<Error> " + error.message);}	
						}
						else if (node.localName() == "Title") {								
								try{itemArray[i].setItemTitle(node.text());		
								}catch(error:IllegalOperationError){ trace("<Error> " + error.message);}	
						}
						else if (node.localName() == "ConvertedBuyItNowPrice") {										
								try{
									itemArray[i].setNowPrice(""+node.text());									
									//trace("Price: "+node.text() );
									//var pricenum:String = ""+node.valueOf();
									//pricenum = SliderFunctions.formatPrice(pricenum);
									//itemArray[i].setNowPrice(""+pricenum);
								}catch(error:IllegalOperationError){ trace("<Error> " + error.message);}	
						}			
						else if (node.localName() == "GalleryURL") {												
								try{itemArray[i].setImgURL(node.text());	
								}catch(error:IllegalOperationError){ trace("<Error> " + error.message);}	
																									
						}	
						else if (node.localName() == "ViewItemURLForNaturalSearch") {											
								try{itemArray[i].setItemURL(node.text());	
								}catch(error:IllegalOperationError){ trace("<Error> " + error.message);}	
						}	
						else if (node.localName() == "TimeLeft") {										
								try{
									//trace("Time: "+node.text() );
									itemArray[i].formatTimeLeft(node.text());		
								}catch(error:IllegalOperationError){ trace("<Error> " + error.message);}	
						}				
					}
					//Check for more than 4
					//trace("i before= "+i );
					var remainhours:int = int( itemArray[i].getHoursLeft() );
					//trace("remainhours: "+remainhours );
					var remainmin:int = int( itemArray[i].getMinutesLeft() );
					//trace("remainmin: "+remainmin );
					var theprice:int = int(itemArray[i].getNowPrice() );
					//trace("theprice: "+theprice );
					//if( (remainhours == 0 && remainmin < 5) || theprice==0){
							//skip the index increase
					//}else{ 
						i++; 
					//}	
					//trace("i after= "+i );
				} 
				if(i>nholders){return itemArray; }
			}				
			return itemArray;			
		}					
						
		
		
		/******************************************************************************************
		 *GET TAB NAME FROM XML
		 ******************************************************************************************/  
		public function getTabNameFromXML(){	
			return tabName;
		}
		
		/******************************************************************************************
		 *READ AN ITEM INFO: XML FILE
		 ******************************************************************************************/  
		public function read1ItemXML(oneItemXML:XML){				
			oneItemXML.ignoreWhitespace = true; 
			//trace(oneItemXML);	
			var currentItemNode:XMLList = oneItemXML.children();			
			var childNode:XML;	
			for each (childNode in currentItemNode) {		
				if(childNode.localName() == "Item"){
					//trace("Item "+childNode.localName());						
					var node:XML;		
					for each (node in childNode.children()) {						
						if (node.localName() == "TimeLeft") {	
							timeleft = node.text();
							//trace("timeleft = "+node.text());		
						}				
					}					
				}
			}				
			return timeleft;
		}
		
    } //class
} //package

