﻿
var defaultContainer = "#ajaxListContent";
var pMoreListId = "p#MoreList";

/*Insert the loading panel into the target container*/
function ShowMoreListLoading()
{
    
    jQuery("span#MoreListButton").fadeOut("fast");
    jQuery("span#MoreListLoading").fadeIn("fast");
}

/*Hide loading panel*/
function HideMoreListLoading()
{
    jQuery("span#MoreListButton").fadeIn("fast");
    jQuery("span#MoreListLoading").fadeOut("fast");    
}

/*Show trends item*/
function ShowAjaxTrendsItem(selectedTag)
{
    ShowAjaxItem("/trends_ajax.aspx?" + "tag=" + selectedTag,"div.newsHolder",defaultContainer)
}

/*Show news item*/
function ShowAjaxNewsItem(selectedTag)
{
    ShowAjaxItem("/news_ajax.aspx?" + "tag=" + selectedTag,"div.newsHolder",defaultContainer)
}

/*Show Search results*/
function ShowAjaxSearchItem(selectedTag,selectedKeyword,selectedSection)
{
    ShowAjaxItem("/search_ajax.aspx?" + "tag=" + selectedTag + "&keyword=" + selectedKeyword + "&section=" + selectedSection,"h2.searchItem",defaultContainer)
}

/*Show tech news ajax*/
function ShowTechAjax(holderId)
{
    ShowAjaxItem("/technews_ajax.aspx?","none",holderId)
}

/*Show Ajax Item*/
function ShowAjaxItem(ajaxUrl,recordHolderId, holderId)
{    
    ShowMoreListLoading();
    
    //count how many record to skip
    var skipRecord = ($(recordHolderId).size());

    // now if I join our inputs using '&' we'll have a query string
    jQuery.ajax
    ({
        type: "POST",
        data: "JQUERY=1",
        url: ajaxUrl + "&skip="+ skipRecord,
        cache: false,
        timeout: 90000,
        
        error: function(xhr, ajaxOptions, thrownError)
        {
            alert("Sorry, your details could not be retrieved. Please try again later.");
            HideMoreListLoading();
            
        },
        success: function(result) 
        {
            
            
            if (result.match("{{NO_MORE_PAGE}}"))
            {   //remove more list button
                jQuery(pMoreListId).remove();
                //alert("No more records");
            }
            else if (result.match("{{ERROR}}"))
            {
                //alert(result);
                jQuery(holderId).append(result);
                jQuery(pMoreListId).remove();
            }
            else
            {
                //load
                jQuery(holderId).append(result);
             
                
                if (result.match("{{LAST_PAGE}}"))
                {
                    //remove more list button
                    jQuery(pMoreListId).remove();
                }
            }
            HideMoreListLoading();
            
        }
     });    
}
