﻿function get_rss_feed(url, num, rssdiv, cycle, output) {
    if (cycle==undefined) cycle = false;
    if (output==undefined) output = "normal"; //nolinks will output feed without links
    //clear the content in the div for the next feed.
    jQuery('#'+rssdiv).empty();
 
    //use the JQuery get to grab the URL from the selected item, put the results in to an argument for parsing in the inline function called when the feed retrieval is complete
    try {
        jQuery.get(url, function(d) {
            
            //find each 'item' in the file and parse it
            jQuery(d).find('item').each(function() {
     
                //name the current found item this for this particular loop run
                var jQueryitem = jQuery(this);
                // grab the post title
                var title = jQueryitem.find('title').text();
                // grab the post's URL
                var link = jQueryitem.find('link').text();
                // next, the description
                var description = jQueryitem.find('description').text();
                //don't forget the pubdate
                var pubDate = dateFormat(jQueryitem.find('pubDate').text(), "longDate");
     
                // now create a var 'html' to store the markup we're using to output the feed to the browser window
                if (output == "nolinks") {
                    var html = "<div class=\"rssItem\"><h4 class=\"rssTitle\">" + title + "<\/h4>";
                    html += "<div class=\"rssDate\">" + pubDate + "</div>";
                    html += "<p class=\"rssDesc\">" + description + "</p>";
                    html += "</div>";
                } else if (output == "events") {
                    var m = dateFormat(pubDate, "mmm");
                    var d = dateFormat(pubDate, "d");
                    var y = dateFormat(pubDate, "yyyy");
                    var html = "<div class=\"rssItem clearfix\"><div class=\"rssDate rounded\"><span class=\"month\">" + m + "</span><span class=\"day\">" + d + "</span><span class=\"year\">" + y + "</span></div>";
                    html += "<h4 class=\"rssTitle\">" + title + "<\/h4>";
                    html += "<p class=\"rssDesc\">" + description + "</p>";
                    html += "</div>";
                } else if (output == "list") {
                    var html = "<li class=\"rssItem\"><div class=\"rssTitle\"><a href='" + link + "'>" + title + "</a><\/div>";
                    html += "<div class=\"rssDesc\">" + description + "</div>";
                    html += "</li>";
                } else {
                    var html = "<div class=\"rssItem\"><h4 class=\"rssTitle\"><a href=\"" + link + "\">" + title + "</a><\/h4>";
                    html += "<div class=\"rssDate\">" + pubDate + "</div>";
                    html += "<p class=\"rssDesc\">" + description + " <br /><a href=\"" + link + "\">Read More</a></p>";
                    html += "</div>";
                }
     
                //put that feed content on the screen!
                jQuery('#'+rssdiv).append(html);
                //jQuery('#'+rssdiv).append(jQueryitem.find('title').text());
                
            });
            if (cycle) {
                jQuery('#'+rssdiv).cycle({
                    fx: 'fade',
                    timeout: 5000,
                    speed:100,
                    next:   '#rssNext', 
                    prev:   '#rssPrev',
                    cleartype: !jQuery.support.opacity,
                    cleartypeNoBg: true
                });
            }
        });
    }
    catch (err){
        jQuery('#'+rssdiv).append("<div>Cannot read the RSS Feed at specified URL. Error description: "+err.description+"</div>");
    }
 
}
function BirdGuideSearch(div) {
    var name = $("#"+div).val();
    goToBird(name);
} 
function replaceAll(str, oldstr, newstr) {
    var i =0;
    while (i != -1) {
        str = str.replace(oldstr, newstr);
        i = str.indexOf( oldstr );
    }
    return str;
}

function goToBird(name) {
    //alert(name+"1");
    if (name=="" || name == null) {
        //alert(name+"2");
        return false;
    } else { 
        name = replaceAll(name,"'","");
        name = replaceAll(name," ","_");                
        //alert(name+"3");
        location.href = "http://allaboutbirds.org/guide/"+name+"/id";
        return false;
    }
    alert(name+"4");
    return false;
}
function sortByName(a, b) {
    var x = a.name.toLowerCase();
    var y = b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function goToUrl(url) {
    location.href = url;
}
/*function loadCaption(isNext, idx, slide, div) {
    var cap = jQuery(div+' div.slide:eq('+idx+') img:eq(0)').attr("alt");
    jQuery('#'+div+'-controls div.captions').html(cap);
}function loadCaptionAuto(curr, next, op, div, timeout) {
    var idx = op.currSlide;
    var cap = jQuery(div+' div.slide:eq('+idx+') img:eq(0)').attr("alt");
    jQuery(div+'-controls div.captions').html(cap);
    return timeout;
}*/
function loadCaption(curr, next, op, div) {
    var idx = op.currSlide;
    var cap = jQuery(div+' div.slide:eq('+idx+') img:eq(0)').attr("alt");
    jQuery(div+'-controls div.captions').html(cap);
}
function donothing() {}
