﻿var truncate = function(str, limit)
{
    var bits, i;
    if ("string" !== typeof str)
    {
        return '';
    }
    bits = str.split('');
    if (bits.length > limit)
    {
        for (i = bits.length - 1; i > -1; --i)
        {
            if (i > limit)
            {
                bits.length = i;
            }
            else if (' ' === bits[i])
            {
                bits.length = i;
                break;
            }
        }
        bits.push('...');
    }
    return bits.join('');
};

var breakSentence = function(str, limit)
{
    var bits, i;
    if ("string" !== typeof str)
    {
        return '';
    }
    bits = str.split('');
    if (bits.length > limit)
    {
        var tempLimit = limit;
        for (i = 0; i < bits.length; i++)
        {
            if (i > tempLimit)
            {
                if (' ' === bits[i])
                {
                    bits[i] = '<br />';
                    break;
                }
            }

        }
    }
    return bits.join('');
};

///////////////////////////////////////

function sortBy()
{
    var callbacks = arguments;

    return function(a, b)
    {
        for (var i = 0; i < callbacks.length; i++)
        {
            var value = callbacks[i](a, b);
            if (value != 0)
            {
                return value;
            }
        }
        return 0;
    };
}

function compareMonthNumber(a, b)
{
    //desc
    return b.MonthNumber - a.MonthNumber;
}
function compareTitle(a, b)
{
    var articleA = a.Title.toLowerCase();
    var articleB = b.Title.toLowerCase();
    if (articleA < articleB) //sort ascending
    {
        return -1;
    }
    if (articleA > articleB)
    {
        return 1;
    }
    return 0; //default (no sorting)
}

function sortByNumberAsc(a, b)
{
    return a - b;
}
function sortByNumberDsc(a, b)
{
    return b - a;
}




////////////////////////////////////////
function IsNumeric(input)
{
    return (input - 0) == input && input.length > 0;
}

function GetFilename(url)
{
    if (url)
    {
        var m = url.toString().match(/([^\/\\]+\.\w+)$/);
        if (m && m.length > 1)
        {
            return m[1];
        }
    }
    return "";
}

function secondsToTime(secs)
{
    var hours = Math.floor(secs / (60 * 60));

    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);

    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);
    if (seconds < 10)
    {
        seconds = '0' + seconds;
    }

    var obj = {
        "h": hours,
        "m": minutes,
        "s": seconds
    };
    return obj;
}


$.urlParam = function(name)
{
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}

$.userLocation = function(itemType)
{
    var userLocale = $.urlParam('locale');

    //if locale is in querystring then use the value and save it in a cookie
    if (userLocale)
    {
        $.cookie('cookie_userLocation_'+ itemType, userLocale);
    }
    else //else see if value is in the cookie
    {
        userLocale = $.cookie('cookie_userLocation_' + itemType);
    }

    //if locale is not in the querystring or cookie then set default and save in cookie
    if (!userLocale)
    {
        userLocale = 'all';
        $.cookie('cookie_userLocation_' + itemType, userLocale);
    }

    return userLocale;
}

$.filterByLocation = function(itemArray, clickedLocale, itemType)
{
    //set the cookie for the clickedLocale
    if (clickedLocale)
    {
        $.cookie('cookie_userLocation_' + itemType, clickedLocale);
    }
    var userLocation = $.userLocation(itemType); //$.userLocation(itemType) in helpers.js

    if (clickedLocale && clickedLocale !== 'all')
    {
        itemArray = $.grep(itemArray,
                function(item, index)
                {
                    // return clickedLocale if present
                    return item.Locale == clickedLocale;
                });
    }
    else if (userLocation !== 'all')
    {
        itemArray = $.grep(itemArray,
                function(item, index)
                {
                    // return userLocation
                    return item.Locale == userLocation;
                });
    }
return itemArray;
}

function parseXml(xml)
{
    if (jQuery.browser.msie)
    {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(xml);
        xml = xmlDoc;
    }
    return xml;
}
