﻿var global_newsFilterURL = 'http://www.rightnow.com/filtSortAjax.php';
function buildListingExtraInfo( data )
{
    var extraInfo = "";
    var locationOK = ( data['location'] && data['location'] != "" );
    var publicationOK = ( data['publication'] && data['publication'] != "" );
    var releaseDateOK = ( data['releaseDate'] && data['releaseDate'] != "" );
    if( locationOK || publicationOK || releaseDateOK )
    {
        if( releaseDateOK )
        {
            extraInfo += data['releaseDate'];
            if( locationOK || publicationOK )
                extraInfo += "<br />";
        }
        var divider = "";
        if( locationOK && publicationOK )
            divider = " | ";
        if( locationOK ) extraInfo += data['location'];
        extraInfo += divider;
        if( publicationOK ) extraInfo += data['publication'];
        extraInfo = "<p>"+ extraInfo +"</p>";
    }
    return extraInfo;
}
/*---------------------------------------------------------------------------------------
 *
 *  The purpose of this file is to create the front-end AJAX logic for the new
 *   news center.
 *
 *         Usage: populateListing([CONTENT-TYPE],[LISTING-ID],[LIST-SIZE],[LIST-TYPE])
 *  Requirements:
 *         [CONTENT-TYPE]:
 *            "releases"    - Press Releases
 *            "articles"    - External News Articles (Wrappers)
 *            "clients"     - Client Pages
 *            "partners"    - Partner Pages
 *            "events"      - Event Pages
 *            "resources"   - Resources (Wrappers)
 *         [LISTING-ID] (int)
 *            The CMS ID of the listing page (for uniqueness and access)
 *         [LISTING-SIZE] (int)
 *            The number of elements to be returned in this listing.
 *         [LIST-TYPE] must be:
 *            "links"       - Only links are listed (text is the title of the element)
 *            "details"     - HTML list is produced with full detail for each element
 *            "summary"     - Simple line-separated entries are produced with title-link and summary only
 */
function populateListing( contentType, listingID, listSize, listType )
{
    var url = global_newsFilterURL;
    var filter1Val = jQuery("#filter1_"+listingID).val();
    if( filter1Val == null ) filter1Val = "All";
    var filter2Val = jQuery("#filter2_"+listingID).val();
    if( filter2Val == null ) filter2Val = "All";
    var sortVal = jQuery("#sortlist_"+listingID).val();
    if((sortVal == null ) && (contentType !== "releases") && (contentType !== "articles")) { sortVal = "Ascending"; }
    else if(sortVal == null) { sortVal = "Decending"; }
    var contentArea = jQuery("#listingarea_"+listingID);
    var loadingArea = jQuery("#loadingarea_"+listingID);
    contentArea.fadeOut(500, function() {
            // Clear the area so we can append fresh content
            contentArea.html("");
            loadingArea.show();
            // Make the main AJAX request with the specified constraints
            jQuery.getJSON(url+'?filter1='+filter1Val+'&filter2='+filter2Val+'&type='+contentType+'&listSize='+listSize+'&sort='+sortVal+'&callback=?', function(data, textStatus) {
                //if (data.error) {
                if (data.error) {
                    contentArea.append("<em>No items matched your filter selections.</em>").fadeIn(500);
                    return;
                }
                if( listType == "links" ) {
                jQuery.each(data, function() {
                    contentArea.append("<li><a href='"+ this['linkHref'] +"'>"+this['title']+"</a></li>");
                    });
                } else if( listType == "details" ) {
                jQuery.each(data, function() {
                    contentArea.append("<li><div><h2><a href='"+ this['linkHref'] +"'><span class='tag'>"+ this['title'] +"</span></a></h2>"+ buildListingExtraInfo( this ) +"<p>"+ this['description'] +" <a href='"+ this['linkHref'] +"'>[continue]</a></p></div></li>");
                    });
                } else if( listType == "summary" ) {
                jQuery.each(data, function() {
                    contentArea.append("<a style='text-decoration: none;' href='"+ this['linkHref'] +"'><h2>"+ this['title'] +"</h2></a>"+ buildListingExtraInfo( this ) +"<p>"+ this['description'] +"</p><hr />");
                    });
                } else {
                alert( "populateListing: Unknown listType '"+ listType +"' on listingID '"+ listingID +"'. The only type of listings supported are 'links', 'details' and 'summary'" );
                }
            });
    });
    // Fade the area in after all the data is loaded to present it in a "pretty" way
    contentArea.fadeIn(1500, function() { loadingArea.hide(); });
}


// Filters should depend on the contentType (i.e. news/pr, asset, client, partner, etc.)
function initFilters( contentType, listingID )
{
    var url = global_newsFilterURL;
    var filter1 = jQuery("#filter1_"+listingID);
    var filter2 = jQuery("#filter2_"+listingID);
    var sortList = jQuery("#sortlist_"+listingID);
    jQuery.getJSON(url+'?type='+contentType+'&function=getCats&level=first&callback=?', function(data) {
            filter1.append("<option value='All'>All</option>");
            jQuery.each(data, function() { filter1.append("<option>"+this+"</option>"); });
            filter1.show();    // All filters should start out hidden
            });
    jQuery.getJSON(url+'?type='+contentType+'&function=getCats&level=second&callback=?', function(data) {
            filter2.append("<option value='All'>All</option>");
            jQuery.each(data, function() {
                if( this.toLowerCase() != "[category]" )    // In case the [category] keyword is not filtered out at the filter app level.
                filter2.append("<option>"+this+"</option>");
                });
            filter2.show();    // All filters should start out hidden
            });
    jQuery.getJSON(url+'?type='+contentType+'&function=getSort&callback=?', function(data) {
            jQuery.each(data, function() { sortList.append("<option>"+this+"</option>"); });
            sortList.show();    // All filters should start out hidden
            });
}
