var FeedCollection = [];
var FeedCount;

function loadSettings()
        {
        
         //cboRefreshInterval.value = System.Gadget.Settings.read("cboRefreshInterval");
         //Standard Feeds
         chkTopRated.checked = System.Gadget.Settings.read("chkTopRated");
         chkTopFavorites.checked = System.Gadget.Settings.read("chkTopFavorites");
         chkMostViewed.checked = System.Gadget.Settings.read("chkMostViewed");
         chkMostDiscussed.checked = System.Gadget.Settings.read("chkMostDiscussed");
         chkMostLinked.checked = System.Gadget.Settings.read("chkMostLinked");
         chkRecentlyFeatured.checked = System.Gadget.Settings.read("chkRecentlyFeatured");
         chkSearchVideos.checked = System.Gadget.Settings.read("chkSearchVideos");
         txtSearch.value = System.Gadget.Settings.read("txtSearch");
         //Categories
         chkCatAutosVehicles.checked = System.Gadget.Settings.read("chkCatAutosVehicles");
         chkCatComedy.checked = System.Gadget.Settings.read("chkCatComedy");
         chkCatEntertainment.checked = System.Gadget.Settings.read("chkCatEntertainment");
         chkCatFilmAnimation.checked = System.Gadget.Settings.read("chkCatFilmAnimation");
         chkCatHowtoStyle.checked = System.Gadget.Settings.read("chkCatHowtoStyle");
         chkCatMusic.checked = System.Gadget.Settings.read("chkCatMusic");
         chkCatNewsPolitics.checked = System.Gadget.Settings.read("chkCatNewsPolitics");
         chkCatPeopleBlogs.checked = System.Gadget.Settings.read("chkCatPeopleBlogs");
         chkCatPetsAnimals.checked = System.Gadget.Settings.read("chkCatPetsAnimals");
         chkCatSports.checked = System.Gadget.Settings.read("chkCatSports");
         chkCatTravelEvents.checked = System.Gadget.Settings.read("chkCatTravelEvents");
        

           
        }
    	
        function settingsClosing(event)
        {
            if (event.closeAction == event.Action.commit)
            {
            //System.Gadget.Settings.write("cboRefreshInterval", cboRefreshInterval.value);
            System.Gadget.Settings.write("chkTopRated", chkTopRated.checked);
            System.Gadget.Settings.write("chkTopFavorites", chkTopFavorites.checked);
            System.Gadget.Settings.write("chkMostViewed", chkMostViewed.checked);
            System.Gadget.Settings.write("chkMostDiscussed", chkMostDiscussed.checked);
            System.Gadget.Settings.write("chkMostLinked", chkMostLinked.checked);
            System.Gadget.Settings.write("chkRecentlyFeatured", chkRecentlyFeatured.checked);
            System.Gadget.Settings.write("chkSearchVideos", chkSearchVideos.checked);
            System.Gadget.Settings.write("txtSearch", txtSearch.value);
            //Categories
            System.Gadget.Settings.write("chkCatAutosVehicles", chkCatAutosVehicles.checked);
            System.Gadget.Settings.write("chkCatComedy", chkCatComedy.checked);
            System.Gadget.Settings.write("chkCatEntertainment", chkCatEntertainment.checked);
            System.Gadget.Settings.write("chkCatFilmAnimation", chkCatFilmAnimation.checked);
            System.Gadget.Settings.write("chkCatHowtoStyle", chkCatHowtoStyle.checked);
            System.Gadget.Settings.write("chkCatMusic", chkCatMusic.checked);
            System.Gadget.Settings.write("chkCatNewsPolitics", chkCatNewsPolitics.checked);
            System.Gadget.Settings.write("chkCatPeopleBlogs", chkCatPeopleBlogs.checked);
            System.Gadget.Settings.write("chkCatPetsAnimals", chkCatPetsAnimals.checked);
            System.Gadget.Settings.write("chkCatSports", chkCatSports.checked);
            System.Gadget.Settings.write("chkCatTravelEvents", chkCatTravelEvents.checked);  
             
         
            
            
//                setCountID1 = CountIDSetting1.value;
//                System.Gadget.Settings.write("CountIDSetting1", setCountID1);
//                
//                setCountID2 = CountIDSetting2.value;
//                System.Gadget.Settings.write("CountIDSetting2", setCountID2);
//                
//                setCountID3 = CountIDSetting3.value;
//                System.Gadget.Settings.write("CountIDSetting3", setCountID3);
                
                               
            }
        }
        
        
        
//        var setCountID1 = System.Gadget.Settings.read("CountIDSetting1");
//         var setCountID2 = System.Gadget.Settings.read("CountIDSetting2");
//          var setCountID3 = System.Gadget.Settings.read("CountIDSetting3");
        
        
        function settingsClosed(event)
        {
            if (event.closeAction == event.Action.commit)
                {
                    
                    UpdateVideos();
//                    setCountID1 = System.Gadget.Settings.read("CountIDSetting1");
//                    txtItem1.value = setCountID1;
//                    setCountID2 = System.Gadget.Settings.read("CountIDSetting2");
//                    txtItem2.value = setCountID2;
//                    setCountID3 = System.Gadget.Settings.read("CountIDSetting3");
//                    txtItem3.value = setCountID3;
//                    
//                    UpdateStats();
                  
                }
        }
        
        
        function GetFeeds(StartIndex, Orderby){
           // var chkTopRatedsetting = System.Gadget.Settings.read("chkTopRated");
            
            var FeedArgs = "start-index=" + StartIndex + "&max-results=25&orderby=" + Orderby;

            var Searchtext = "";
            FeedCount = 0;
            FeedCollection = []; //reset array      
            
            //Build FeedCollection based on settings
            if (System.Gadget.Settings.read("chkTopRated") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/standardfeeds/top_rated?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Top Rated";
             }
            if (System.Gadget.Settings.read("chkTopFavorites") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/standardfeeds/top_favorites?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Top Favorites";
             }
            if (System.Gadget.Settings.read("chkMostViewed") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/standardfeeds/most_viewed?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Most Viewed";
             }
            if (System.Gadget.Settings.read("chkMostDiscussed") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/standardfeeds/most_discussed?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Most Discussed";
            } 
            if (System.Gadget.Settings.read("chkMostLinked") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/standardfeeds/most_linked?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Most Linked";
            }
            if (System.Gadget.Settings.read("chkRecentlyFeatured") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/standardfeeds/recently_featured?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Recently Featured";
            }
            if (System.Gadget.Settings.read("chkSearchVideos") == true){
                    Searchtext = System.Gadget.Settings.read("txtSearch");
                    Searchtext = Searchtext.replace(" ", "%20");
                    FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos?vq=%22" + Searchtext + "%22&" + FeedArgs;

                    FeedCount += 1;
                    FeedType = "Custom";
            }
            //Categories
            if (System.Gadget.Settings.read("chkCatAutosVehicles") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Autos?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Autos";
            }
            if (System.Gadget.Settings.read("chkCatComedy") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Comedy?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Comedy";
            }
            if (System.Gadget.Settings.read("chkCatEntertainment") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Entertainment?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Entertainment";
            }
            if (System.Gadget.Settings.read("chkCatFilmAnimation") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Film?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Film";
            }
            if (System.Gadget.Settings.read("chkCatHowtoStyle") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Howto?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Howto";
            }
            if (System.Gadget.Settings.read("chkCatMusic") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Music?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Music";
            }
            if (System.Gadget.Settings.read("chkCatNewsPolitics") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/News?" + FeedArgs;
             FeedCount += 1;
             FeedType = "News";
            }
            if (System.Gadget.Settings.read("chkCatPeopleBlogs") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/People?" + FeedArgs;
             FeedCount += 1;
             FeedType = "People";
            }
            if (System.Gadget.Settings.read("chkCatPetsAnimals") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Animals?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Animals";
            }
            if (System.Gadget.Settings.read("chkCatSports") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Sports?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Sports";
            }
            if (System.Gadget.Settings.read("chkCatTravelEvents") == true){
             FeedCollection[FeedCount] = "http://gdata.youtube.com/feeds/videos/-/Travel?" + FeedArgs;
             FeedCount += 1;
             FeedType = "Travel";
            }
            
        
        }
        
