/*
	MXNAWebService 2 Class
	Created by Mike Chambers
	
	You can find information on the MXNA WebService API at:
	http://www.markme.com/mxna/tools/cfm
	http://www.markme.com/mxna/webservices/docs.html
	
	Note : This requires that the Flash MX 2004 Web Service Class be included
	via the following steps:
	
	1. Window > Other Panels > Common Libraries > Classes
	2. Drag the WebServicesClasses Symbol from the library onto the stage.
	3. Delete the WebServicesClasses Symbol (steps 2 and 3 puts it in your library)
*/


import mx.services.WebService;
import mx.services.PendingCall;

class com.macromedia.mxna2.MXNAWebService
{
        private var dispatchEvent:Function;
        public var addEventListener:Function;
        public var removeEventListener:Function;

        private var wsdlURL:String = "http://weblogs.macromedia.com/mxna/webservices/mxna2.cfc?wsdl";
        private var ws:WebService;

        function MXNAWebService(wsdl:String)
        {
            mx.events.EventDispatcher.initialize(this);
        
            if(wsdl != undefined)
            {
                wsdlURL = wsdl;
            }
            
            ws = new WebService(wsdlURL);
        }
        
        /* APIs and Callbacks */
        
        /* getCategories
               		
        		Returns a query with all MXNA 2.0 categories.
        		 
        		Columns include: categoryId, categoryName.
        */
        public function getCategories():Void
        {
            //this needs to set its type info
            var pc:PendingCall = ws.getCategories();
                //HACK!!!!
                pc["controller"] = this;
                
                pc.onResult = getCategories_Result;
                pc.onFault = onFault;
        }
              
        private function getCategories_Result(result:Array)
        {
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetCategories", 
	    						categories:result});
        }             
              
              
        /*	getFeeds
        
        		Returns a query of all feeds aggregated by MXNA 2.0.
        		
        		Columns include: feedId, feedName, feedDescription, feedCategories, 
        			feedUrl, languageId, lastUpdated, siteName, siteUrl, and 
        			siteDescription. 
        			
        			The column feedCategories is a comma-delimited list of all the 
        			categories the feed is in.
        */      
        	public function getFeeds(Void):Void
		{
            var pc:PendingCall = ws.getFeeds();
                //HACK!!!!
                pc["controller"] = this;
                
                pc.onResult = getFeeds_Result;
                pc.onFault = onFault;   			
		}
		
		private function getFeeds_Result(result:Array):Void
		{
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetFeeds", 
	    						feeds:result});			
		}
		
		
		/*	getFeedsByCategory
		
			Returns a query of all feeds aggregated by MXNA 2.0 in the specified 
				category. 
				
			Columns include: feedId, feedName, feedDescription, feedCategories, 
				feedUrl, languageId, lastUpdated, siteName, siteUrl, and 
				siteDescription. 
				
			The column feedCategories is a comma-delimited list of all the categories 
				the feed is in. The category that was passed in will always be one 
				of the categories included in the list.

			Parameters:
   				categoryId: numeric, required, categoryId - The ID of the category 
   				for which you want feeds. 
		*/
        public function getFeedsByCategory(categoryId:Number):Void
        {
            var pc:PendingCall = ws.getFeedsByCategory(categoryId);
                //HACK!!!!
                pc["controller"] = this;
                pc["categoryId"] = categoryId;
                
                pc.onResult = getFeedsByCategory_Result;
                pc.onFault = onFault;        
        }
  
        private function getFeedsByCategory_Result(result:Array)
        {
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetFeedsByCategory", 
	    						feeds:result, 
	    						categoryId:this["categoryId"]});
        }  
  
  		
  		/* 	getAggregateLanguageData  
  		
 				Returns a query with aggregated language data, including the total
 					 number of feeds MXNA is aggregating in each language, 
 					 and the total number of posts, as well. 
 					 
 				Columns include: languageId, languageName, feedCount, postCount.
  		*/
  		public function getAggregateLanguageData(Void):Void
  		{
  			
            var pc:PendingCall = ws.getAggregateLanguageData();
                //HACK!!!!
                pc["controller"] = this;
                
                pc.onResult = getAggregateLanguageData_Result;
                pc.onFault = onFault;        
  		}
  
        private function getAggregateLanguageData_Result(result:Array)
        {      	
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetAggregateLanguageData", 
	    						data:result});
        }  
  
  
  		/* getFeedsByLanguage
  			
  			Returns a query of all feeds aggregated by MXNA 2.0 in the specified 
  				language. 
  				
  			Columns include: feedId, feedName, feedDescription, feedCategories, 
  				feedUrl, languageId, lastUpdated, siteName, siteUrl, and 
  				siteDescription. 
  				
  			The column feedCategories is a comma-delimited list of all the 
  				categories the feed is in.

			Parameters:
   				languageId: numeric, required, languageId - The ID of the language 
   					for which you want feeds. 
  		*/
    		public function getFeedsByLanguage(languageId:Number):Void
		{
            var pc:PendingCall = ws.getFeedsByLanguage(languageId);
                //HACK!!!!
                pc["controller"] = this;
                pc["languageId"] = languageId;
                
                pc.onResult = getFeedsByLanguage_Result;
                pc.onFault = onFault;   			
		}
  
  		private function getFeedsByLanguage_Result(result:Array):Void
        {	
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetFeedsByLanguage", 
	    						feeds:result, 
	    						languageId:this["languageId"]});
        } 
  
  		/* getLanguages
  		
  			 Returns a query with all MXNA 2.0 languages. 
  			 
  			 MXNA 2.0 supports just about all languages. The languages this function
  			 	returns are those for which MXNA 2.0 has feeds. 
  			 	
  			Columns include: languageId, languageName.
  		*/
  		public function getLanguages(Void):Void
		{
            var pc:PendingCall = ws.getLanguages();
                //HACK!!!!
                pc["controller"] = this;
                
                pc.onResult = getLanguages_Result;
                pc.onFault = onFault;   			
		}
  
        private function getLanguages_Result(result:Array)
        {
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetLanguages", 
	    						languages:result});
        } 
  
  		/* getMostPopularPosts
  		
 			Returns the specified number of posts with the most clicks in the last 
 			30 days. The limit cannot exceed 50. 
 			
 			Columns include: postId, clicks, dateTimeAggregated, feed, feedName, 
 				postTitle, postExcerpt, postLink.

			Parameters:
   				limit: numeric, required, limit - The total number of rows you 
   					want returned.  		
  		*/
		public function getMostPopularPosts(numberOfDays:Number, numberOfPosts:Number):Void
		{
			var pc:PendingCall = ws.getMostPopularPosts(numberOfDays, numberOfPosts);
			//HACK!!!!
			pc["controller"] = this;
			pc["numberOfPosts"] = numberOfPosts;
			pc["numberOfDays"] = numberOfDays;
			            
			pc.onResult = getMostPopularPosts_Result;
			pc.onFault = onFault;   		
		}
		
		private function getMostPopularPosts_Result(result:Array):Void
		{
		    //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetMostPopularPosts", 
	    						posts:result, 
	    						numberOfPosts:this["numberOfPosts"],
	    						numberOfDays:this["numberOfDays"]
	    						});
		}
    
  
  		/*	getMostProlificFeeds 
  		
  				Returns the specified number of feeds with the highest number of 
  					posts in the last 30 days. The limit cannot exceed 50. 
  					
  				Columns include: feedId, feedName, feedDescription, clickAverage, 
  					totalClicks, postCount.
				
				Parameters:
   					limit: numeric, required, limit - The total number of rows 
   						you want returned. 
  		*/
  		public function getMostProlificFeeds(numberOfDays:Number, numberOfPosts:Number):Void
		{	
			var pc:PendingCall = ws.getMostProlificFeeds(numberOfDays, numberOfPosts);
			//HACK!!!!
			pc["controller"] = this;
			pc["numberOfPosts"] = numberOfPosts;
			pc["numberOfDays"] = numberOfDays;
			            
			pc.onResult = getMostProlificFeeds_Result;
			pc.onFault = onFault;   		
		}
  
 		private function getMostProlificFeeds_Result(result:Array):Void
		{
		    //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetMostProlificFeeds", 
	    						feeds:result, 
	    						numberOfPosts:this["numberOfPosts"],
	    						numberOfDays:this["numberOfDays"]});
		}
  
  		/* getMostPopularFeeds
  		
  				Returns the specified number of feeds with the highest click average
  					in the last 30 days. The limit cannot exceed 50. 
  					
  				Columns include: feedId, feedName, feedDescription, clickAverage, 
  					totalClicks, postCount.

				Parameters:
   					limit: numeric, required, limit - The total number of rows you 
   						want returned.  		
  		*/
    		public function getMostPopularFeeds(numberOfDays:Number, numberOfPosts:Number):Void
		{
			var pc:PendingCall = ws.getMostPopularFeeds(numberOfDays, numberOfPosts);
			//HACK!!!!
			pc["controller"] = this;
			pc["numberOfPosts"] = numberOfPosts;
			pc["numberOfDays"] = numberOfDays;
			            
			pc.onResult = getMostPopularFeeds_Result;
			pc.onFault = onFault;   		
		}
  
   		private function getMostPopularFeeds_Result(result:Array):Void
		{
		    //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetMostPopularFeeds", 
	    						feeds:result, 
	    						numberOfPosts:this["numberOfPosts"],
	    						numberOfDays:this["numberOfDays"]});
		}
  
  		/* getMostRecentPosts 
  		
			Returns a query containing most recently aggregated posts. 
			
			Columns include: postTitle, postExcerpt, postLink, clicks, 
				ranking (1-11), uniqueId, dateTimeAggregated, and feedId.

			Parameters:
				limit: numeric, required, limit - The total number of rows you 
					want returned.
				offset: numeric, required, offset - Which row to start the 
				   	search from (the first row is 0).
				languageIds: string, required, languageIds - A comma-delimited 
				   	list of language IDs. 
		*/	
		public function getMostRecentPosts(limit:Number, offset:Number, 
											languageIds:String):Void
		{		
						
            var pc:PendingCall = ws.getMostRecentPosts(limit, offset, languageIds);
                //HACK!!!!
                pc["controller"] = this;
				pc["limit"] = limit;
				pc["offset"] = offset;
				pc["languageIds"] = languageIds;
                
                pc.onResult = getMostRecentPosts_Result;
                pc.onFault = onFault;   			
		}
  
  		private function getMostRecentPosts_Result(result:Array):Void
		{
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetMostRecentPosts", 
	    						posts:result,
	    						languageIds:this["languageIds"],
	    						offset:this["offset"],
	    						limit:this["limit"]});			
		}
  
  		/* getPostsByCategory 
  		
			Returns a query containing most recently aggregated posts in the 
			specified category. Columns include: postTitle, postExcerpt, postLink, 
			clicks, ranking (1-11), uniqueId, dateTimeAggregated, and feedId.

			Parameters:
   				limit: numeric, required, limit - The total number of rows
   				 	you want returned.
   				offset: numeric, required, offset - Which row to start the search 
   					from (the first row is 0).
   				categoryId: numeric, required, categoryId - The category from which 
   					you want the most recent posts.
   				languageIds: string, required, languageIds - A comma-delimited list 
   					of language IDs. 
  		*/
        public function getPostsByCategory(limit:Number, offset:Number, 
        		categoryId:Number, languageIds:String):Void
        {
            var pc:PendingCall = ws.getPostsByCategory(limit, offset, categoryId,
            												languageIds);
                //HACK!!!!
                pc["controller"] = this;
                pc["categoryId"] = categoryId;
                pc["languageIds"] = languageIds;
                pc["limit"] = limit;
                pc["offset"] = offset;
                
                pc.onResult = getPostsByCategory_Result;
                pc.onFault = onFault;            
        }
  
        private function getPostsByCategory_Result(result:Array)
        {	
            //HACK!!!!!!
            this["controller"].dispatchEvent(
            						{type:"onGetPostsByCategory", 
	    							posts:result, 
		    						categoryId:this["categoryId"],
		    						languageIds:this["languageIds"],
		    						limit:this["limit"],
		    						offset:this["offset"]
	    							});
        }   
  
		/* getPostsByFeed
		
			Returns a query containing most recently aggregated posts in the 
				specified feed. Columns include: postTitle, postExcerpt, postLink, 
				clicks, ranking (1-11), uniqueId, dateTimeAggregated, and feedId.

			Parameters:
   				limit: numeric, required, limit - The total number of rows you want
   					 returned.
   				offset: numeric, required, offset - Which row to start the search
   					 from (the first row is 0).
   				feedId: numeric, required, feedId - The feed from which you want the
   					 most recent posts. 		
		*/
		public function getPostsByFeed(limit:Number, offset:Number, feedId:Number):Void
		{		
						
            var pc:PendingCall = ws.getPostsByFeed(limit, offset, feedId);
                //HACK!!!!
                pc["controller"] = this;
				pc["feedId"] = feedId;
				pc["offset"] = offset;
				pc["limit"] = limit;
                
                pc.onResult = getPostsByFeed_Result;
                pc.onFault = onFault;   			
		}
			
		private function getPostsByFeed_Result(result:Array):Void
		{
			
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetPostsByFeed", 
	    							posts:result, 
								feedId:this["feedId"],
								offset:this["offset"],
								limit:this["limit"]});				
		} 
        
		/* getPostsBySmartCategory
			Returns a query containing most recently aggregated posts in the specified smart category. Columns include: postTitle, postExcerpt, postLink, clicks, ranking (1-11), uniqueId, dateTimeAggregated, and feedId.

			Parameters:
				limit: numeric, required, limit - The total number of rows you want
			   		returned.
			   	offset: numeric, required, offset - Which row to start the search 
			   		from (the first row is 0).
			   	smartCategoryId: numeric, required, smartCategoryId - The smart 
			   		category from which you want the most recent posts.
			   	smartCategoryKey: string, required, smartCategoryKey - The smart
			   		 category key.
			   	languageIds: string, required, languageIds - A comma-delimited 
			   		list of language IDs. 
		*/
		public function getPostsBySmartCategory(limit:Number, offset:Number,
												smartCategoryId:Number, smartCategoryKey:String,
												languageIds:String):Void
		{
			var pc:PendingCall = ws.getPostsBySmartCategory(limit, offset, smartCategoryId,
														smartCategoryKey, languageIds);
			//HACK!!!!
			pc["controller"] = this;
			pc["limit"] = limit;
			pc["offset"] = offset;
			pc["smartCategoryId"] = smartCategoryId;
			pc["smartCategoryKey"] = smartCategoryKey;
			pc["languageIds"] = languageIds;
                
			pc.onResult = getPostsBySmartCategory_Result;
			pc.onFault = onFault;   	
		}
		
		 private function getPostsBySmartCategory_Result(result:Array):Void
		{
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetPostsBySmartCategory", 
	    						posts:result,
	    						languageIds:this["languageIds"],
	    						limit:this["limit"],
	    						offset:this["offset"],
	    						smartCategoryId:this["smartCategoryId"],
	    						smartCategoryKey:this["smartCategoryKey"]
	    						});			
		} 
		
		/*	getSearchHistory
		
			 Returns a query containing the last unique searches conducted with 
			 	MXNA 2.0. 
			 	
			 Columns include: searchTerms.
		*/
		public function getSearchHistory(Void):Void
		{
			var pc:PendingCall = ws.getSearchHistory();
			
			//HACK!!
			pc["controller"] = this;
			
			pc.onResult = getSearchHistory_Result;
			pc.onFault = onFault; 			
		}		
		
		private function getSearchHistory_Result(result:Array):Void
		{
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetSearchHistory", 
	    						history:result});			
		}  
		
		
		/*	getSmartCategories
		
			Returns a query with all MXNA 2.0 smart categories.
			
			Columns include: smartCategoryId, smartCategoryName, smartCategoryKey.
		*/
		public function getSmartCategories(Void):Void
		{
			var pc:PendingCall = ws.getSmartCategories();
			
			//HACK!!
			pc["controller"] = this;
			
			pc.onResult = getSmartCategories_Result;
			pc.onFault = onFault; 			
		}		
		
		 private function getSmartCategories_Result(result:Array):Void
		{
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onGetSmartCategories", 
	    						categories:result});			
		}
		
		/* 	search
		
			Returns a query containing posts resulting from a text search. 
			
			Columns include: postTitle, postExcerpt, postLink, clicks, uniqueId, 
				dateTimeAggregated, and feedId.

			Parameters:
   				limit: numeric, required, limit - The total number of rows you want
   					returned.
   				offset: numeric, required, offset - Which row to start the search
   					from (the first row is 0).
   				searchTerms: string, required, searchTerms - The terms being
   					searched for.
   				sortBy: string, required, sortBy - How you want the results ordered.
   					Either 'date' or 'relevance'.
   				languageIds: string, required, languageIds - A comma-delimited list
   					of language IDs. 		
		*/
		public function search(limit:Number, offset:Number, searchTerm:String,
							sortBy:String, languageIds:String):Void
		{
			var pc:PendingCall = ws.search(limit, offset, searchTerm,
														sortBy, languageIds);
			//HACK!!!!
			pc["controller"] = this;
			pc["limit"] = limit;
			pc["offset"] = offset;
			pc["searchTerm"] = searchTerm;
			pc["sortBy"] = sortBy;
			pc["languageIds"] = languageIds;
                
			pc.onResult = search_Result;
			pc.onFault = onFault;   			
		}

		private function search_Result(result:Array):Void
		{
            //HACK!!!!!!
            this["controller"].dispatchEvent({type:"onSearch", 
	    						posts:result,
	    						languageIds:this["languageIds"],
	    						limit:this["limit"],
	    						offset:this["offset"],
	    						searchTerm:this["searchTerm"],
	    						sortBy:this["sortBy"]
	    						});					
		}  


        public function onFault(fault:Object)
        {
			this["controller"].dispatchEvent({type:"onFault", fault:fault});
        }
        
        
}