var podcasts = new Array()
var dates = new Array()
var years = new Array()
var monthYears = new Array()
var hosts = new Array()
var topics = new Array()
var temp = new Array()
var viewAllDates = 'View all'
var viewAllHosts = 'View all'
var viewAllTopics = 'View all'
var dateFilter = viewAllDates
var hostFilter = viewAllHosts
var topicFilter = viewAllTopics
var m_names = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var withinPodcastsBlock = false

function sortPodcasts() {
   //podcasts.sort(compareByDateAsc)
   podcasts.sort(compareByDateDesc)
}
function compareByDateAsc(a,b) {
   return (b.byDate<a.byDate)-(a.byDate<b.byDate)
}
function compareByDateDesc(a,b) {
   return (a.byDate<b.byDate)-(b.byDate<a.byDate)
}
function addPodcast(title,url,pubDate,host,topic) {
   podcasts[podcasts.length] = new podcast(title,url,pubDate,host,topic,false)
}
function podcast(title,url,pubDate,host,topic,hidden) {
   this.title=title
   this.url=url
   this.pubDate=new Date(Date.parse(pubDate))
   this.host=host
   this.topic=topic
   this.pubYear=this.pubDate.getFullYear()
   this.pubMonthYear=m_names[this.pubDate.getMonth()] + ' ' + this.pubDate.getFullYear()
   this.byDate=this.pubDate.getFullYear()+''+padout(this.pubDate.getMonth())+''+padout(this.pubDate.getDate())+this.title
   this.byTitle=this.title+this.pubDate.getFullYear()+''+padout(this.pubDate.getMonth())+''+padout(this.pubDate.getDate())
   this.hidden=hidden
}
function filter(filterName) {
   var allTags = document.getElementsByTagName("div");
   for (i=0; i<allTags.length; i++)
      {
      if (allTags[i].className==filterName) {
         allTags[i].style.display=showhide(document.getElementById('box'+filterName).checked);
      }
      }
}
/* function collects all div tags from the page
   loops through all tags
   if value passed is to view all, sets all items in hosts array to visible
   if tag class name matches passed value, sets div visible ('block')
   if passed value (host's name) is in array (one of the names) but not the selected name,
      sets div invisible ('none') */
function filterByHost(filterName) {
   hostFilter = filterName
   var allTags = document.getElementsByTagName("div");
   if (filterName==viewAllHosts) {
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }
         if (withinPodcastsBlock && allTags[i].className!="startP") {

            if (hosts.toString().indexOf(allTags[i].className)>-1) {
               allTags[i].style.display='block';
            }

         }
      }
   } else {
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }
         if (withinPodcastsBlock && allTags[i].className!="startP") {

            if (allTags[i].className==filterName) {
               allTags[i].style.display='block';
            } else if (hosts.toString().indexOf(allTags[i].className)>-1) {
               allTags[i].style.display='none';
            }

         }
      }
   }
   showHideNoPodcastsText()
}
function filterByTopic(filterName) {
   topicFilter = filterName
   var allTags = document.getElementsByTagName("div");
   if (filterName==viewAllTopics) {
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }
         if (withinPodcastsBlock && allTags[i].className!="startP") {

            if (topics.toString().indexOf(allTags[i].className)>-1) {
               allTags[i].style.display='block';
            }

         }
      }
   } else {
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }
         if (withinPodcastsBlock && allTags[i].className!="startP") {

            if (allTags[i].className==filterName) {
               allTags[i].style.display='block';
            } else if ((topics.toString().indexOf(allTags[i].className)>-1) && (allTags[i].className.length!=0)) {
               allTags[i].style.display='none';
            }

         }
      }
   }
   showHideNoPodcastsText()
}
function filterByDate(filterName) {
   dateFilter = filterName
   var allTags = document.getElementsByTagName("div");
   if (filterName==viewAllDates) {
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }
         if (withinPodcastsBlock && allTags[i].className!="startP") {

            if (dates.toString().indexOf(allTags[i].className)>-1) {
               allTags[i].style.display='block';
            }

         }
      }
   } else {
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }
         if (withinPodcastsBlock && allTags[i].className!="startP") {

            // 1. does filter match the div name?
            // 2. is filter part of div name? (2007 is part of March 2007)
            // 3. is div name part of filter? (March 2007 contains 2007)
            if (allTags[i].className==filterName || allTags[i].className.indexOf(filterName)>-1 || filterName.indexOf(allTags[i].className)>-1) {
               allTags[i].style.display='block';
            } else if (dates.toString().indexOf(allTags[i].className)>-1) {
               allTags[i].style.display='none';
            }

         }
      }
   }
   showHideNoPodcastsText()
}
function podcastTopics () {
   for (i=0;i<podcasts.length;i++)
      {
      topics[i] = podcasts[i].topic
      }
   topics=removeDuplicates(topics)
   return topics
}
function podcastHosts () {
   for (i=0;i<podcasts.length;i++)
      {
      hosts[i] = podcasts[i].host
      }
   hosts=removeDuplicates(hosts)
   return hosts
}
function podcastDates () {
   year=""
   newYear=""
   monthYear=""
   newMonthYear=""
   var j=0
   for (i=0;i<podcasts.length;i++) {
      year = podcasts[i].pubYear
      if (year!=newYear) {
         dates[j] = year
         newYear = year
         j++
      }
      monthYear = podcasts[i].pubMonthYear
      if (monthYear!=newMonthYear) {
         dates[j] = monthYear
         newMonthYear = monthYear
         j++
      }
   }
   return dates
}
function podcastYears () {
   for (i=0;i<podcasts.length;i++)
      {
      years[i] = podcasts[i].pubDate.getFullYear()
      }
   years=removeDuplicates(years)
   years=years.reverse()
   return years
}
function podcastMonthYears () {
   monthYear = ""
   newMonthYear = ""
   var j=0
   for (i=0;i<podcasts.length;i++) {
      monthYear = podcasts[i].pubMonthYear
      if (monthYear!=newMonthYear) {
         monthYears[j] = monthYear
         newMonthYear = monthYear
         j++
      }
   }
   return monthYears
}
/* function returns value based on
   wheather a checkbox is checked or not */
function showhide (checkboxStatus) {
   if(checkboxStatus){return 'block'}
   else{return 'none'}
}
/* function sorts the array alphabetically
   then it loops through array
   compares current value with the previous value
   removes rows with repeating value */
function removeDuplicates(array) {
   var temp=new Array();
   array.sort();
   for(i=0;i<array.length;i++) {
      if(array[i]==array[i+1]) {continue}
      temp[temp.length]=array[i];
   }
   return temp;
}
function padout(number) {
   return (number < 10) ? '0' + number : number;
}

function showHideNoPodcastsText () {

   var thereArePodcasts = false

   // reset the hidden values
   for (d=0;d<podcasts.length;d++)
      {
      podcasts[d].hidden=false
      }

   // viewAll will show at least one
   if (hostFilter==viewAllHosts || dateFilter==viewAllDates) {
      thereArePodcasts = true

   } else {

      var cPodcast = 0
      var pDivCount=0
      var hidden = false

      var allTags = document.getElementsByTagName("div");
      for (i=0; i<allTags.length; i++) {

         // only process divs within start and end tags
         if (allTags[i].className=="startP") {
            withinPodcastsBlock=true
            // reset the div count to only count podcast divs
            pDivCount=0
         }
         if (allTags[i].className=="endP") {
            withinPodcastsBlock=false
         }

         if (withinPodcastsBlock && allTags[i].className!="startP") {
            hidden=false

            // by topic
            if (topics.toString().indexOf(allTags[i].className)>-1) {
               if (allTags[i].style.display=='none') {
                  hidden = true
               }

            // by date
            } else if (dates.toString().indexOf(allTags[i].className)>-1) {
               if (allTags[i].style.display=='none') {
                  hidden = true
               }

            // by host
            } else if (hosts.toString().indexOf(allTags[i].className)>-1) {
               if (allTags[i].style.display=='none') {
                  hidden = true
               }
            }

            if (hidden) {
               podcasts[cPodcast].hidden=hidden
            }

            pDivCount++
            // the number bellow (+ 1, because count starts with 0)
            // represents the number of divs wrapping each podcast entry
            if (pDivCount>2) {
               pDivCount=0
               cPodcast++
            }
         }

      }

      for (d=0;d<podcasts.length;d++)
         {
         if (podcasts[d].hidden==false) {
            thereArePodcasts=true
         }
         }

   }

   tBlock = document.getElementById('npText')
   if (thereArePodcasts) {
      tBlock.style.display='none';
   } else {
      tBlock.style.display='block';
   }

}
