﻿
var FST_UGC_URL = FSTPATH + 'content/getugc.aspx';
var FST_RSS_URL = FSTPATH + 'rss/getfeedashtml.aspx';
var FST_GET_CATEGORIES_URL = FSTPATH + 'content/getcategories.aspx';


function fst_GetUGCArticles(divID, type, authorID, maxResults, timeFrame, displayOrder, returnFormat, templateID, startPosition)
{
    if($(divID))
    {
        fst_GetUGCContent(divID, "articles", type, maxResults, displayOrder, timeFrame, authorID, returnFormat, templateID, startPosition, 0, '');
    }
}

function fst_GetUGCBlogPosts(divID, authorID, maxResults, timeFrame, displayOrder, returnFormat, templateID, startPosition)
{
    if($(divID))
    {
        fst_GetUGCContent(divID, "blogposts", 0, maxResults, displayOrder, timeFrame, authorID, returnFormat, templateID, startPosition, 0, '');
    }
}

function fst_GetUGCFormThreads(divID, authorID, maxResults, timeFrame, displayOrder, returnFormat, templateID, startPosition)
{
    if($(divID))
    {
        fst_GetUGCContent(divID, "forumthreads",'', maxResults, displayOrder, timeFrame, authorID, returnFormat, templateID, startPosition, 0, '');
    }
}

function fst_GetUGCCompanies(divID, categoryID, nameList, maxResults, timeFrame, displayOrder, returnFormat, templateID, startPosition)
{
    if($(divID))
    {
        fst_GetUGCContent(divID, "companies", 0, maxResults, displayOrder, timeFrame, 0, returnFormat, templateID, startPosition, categoryID, nameList);
    }
}


function fst_GetUGCContent(divID, contentType, type,  maxResults, displayOrder, timeFrame, authorID, returnFormat, templateID, startPosn, categoryID, nameList)
{   
    if($(divID))
    {           
        // grab the "friendly" params and turn them into proper params
        switch(contentType)
        {
            case "articles":
            case "ARTICLES":
                contentType = 1;
                break;
                
            case "blogposts":
            case "BLOGPOSTS":
                contentType = 2;
                break;
                
            case "forumthreads":
            case "FORUMTHREADS":
                contentType = 3;
                break;
                
            case "companies":
            case "COMPANIES":
                contentType = 4;
                break;
                
            default:
                contentType = 0;
                break;
        }
        
        switch(returnFormat)
        {
            case "xml":
            case "XML":
                returnFormat = 1;
                break;
                
            case "json":
            case "JSON":
                returnFormat = 2;
                break;
                
            case "formatted":
            case "FORMATTED":
                returnFormat = 3;
                break;
        }
        
	    new Ajax.Request(FST_UGC_URL,
	    {
	      method: 'post',
	      parameters: { contenttype: contentType, type: type, maxresults: maxResults, order: displayOrder, timeframe: timeFrame, returnformat: returnFormat, author: authorID, template: templateID, startat: startPosn, category: categoryID, namelist: nameList },
	      onSuccess: function(transport)
		    {
		        fst_GetUGCSuccess(divID, transport.responseText);
		    },
	      onFailure: function()
		    {
			    fst_GetUGCError();
		    }
	    });
	}
}

function fst_GetUGCSuccess(divID, retVal)
{   
    if($(divID))
    {
        $(divID).innerHTML = retVal;
    }
    else
    {
        return retVal;
    }
}

function fst_GetUGCError(divID)
{
    if($(divID))
    {
        $(divID).innerHTML = '<p>Unable to retrieve content</p>';
    }
}


function fst_GetRssFeed(destinationDiv, feedID, maxLinks)
{
    new Ajax.Request(FST_RSS_URL,
    {
      method: 'post',
      parameters: { feedid: feedID, maxlinks: maxLinks },
      onSuccess: function(transport)
        {
            fst_GetRssFeedSuccess(transport.responseText, destinationDiv);
        },
      onFailure: function()
        {
	        fst_GetRssFeedSuccess("<p>RSS error</p>", destinationDiv);
        }
    });
}

function fst_GetRssFeedSuccess(retVal, destinationDiv)
{
    if($(destinationDiv))
    {
        if(retVal != '')
        {
            $(destinationDiv).innerHTML = retVal;
        }
        else
        {
            $(destinationDiv).innerHTML = "<p>No stories found</p>";
        }
    }
}

function fst_GetCategoryList(destinationDiv, parentCategoryID)
{
    new Ajax.Request(FST_GET_CATEGORIES_URL,
    {
      method: 'post',
      parameters: { catid: parentCategoryID },
      onSuccess: function(transport)
        {
            fst_GetCategoryListSuccess(transport.responseText, destinationDiv);
        },
      onFailure: function()
        {
	        fst_GetCategoryListSuccess("<p>Category error</p>", destinationDiv);
        }
    });
}

function fst_GetCategoryListSuccess(retVal, destinationDiv)
{
    if($(destinationDiv))
    {
        if(retVal != '')
        {
            $(destinationDiv).innerHTML = retVal;
        }
        else
        {
            $(destinationDiv).innerHTML = "<p>No categories found</p>";
        }
    }
}
