/**
 * Digg-style pagination function.
 *
 * @param   integer total       - The total number of results to page
 * @param   integer limit       - The maximum number of results per page
 * @param   integer links       - The maximum number of page links to display in pagination
 * @param   integer adjacents   - The minimum number of pages in pagination to the left/right of the current page before hiding pages
 * @param   string  url         - An optional URL to use when creating page links
 * @param   string  anchor      - An optional anchor to append to the URL
 *
 * @return  string
 */
buildPagingLinks = function(total, limit, page, links, url, anchor, adjacents)
{

    // Data sanitization
    total       = (total)       ? parseInt(total)                                      : 0;
    limit       = (limit)       ? parseInt(limit)                                      : 25;
    page        = (page)        ? parseInt(page)                                       : 1;
    links       = (links)       ? parseInt(links)                                      : 10;
    adjacents   = (adjacents)   ? parseInt(adjacents)                                  : 1;
	url         = (url)         ? url                                                  : window.location.pathname;
	anchor      = (anchor)      ? (anchor.substr(0, 1)) == '#' ? anchor : '#' + anchor : '';

	//other vars
	var prev       = (page - 1);               //previous page is page - 1
	var next       = (page + 1);               //next page is page + 1
	var lastpage   = Math.ceil(total / limit); //lastpage is = total items / items per page, rounded up.
	var lpm1       = (lastpage - 1);           //last page minus 1

	// Declare our pagination container
    var pagination = '';

    //
	if (lastpage > 1) {

		pagination += '<div class="pagination">';

		// Previous button
		pagination += (page > 1) ? '<a class="pagilink" href="' + url + '&amp;p=' + prev + anchor + '">' : '<span class="disabled">';
		pagination += '&laquo; prev';
		pagination += (page > 1) ? '</a>' : '</span>';

		// The pages
		if (lastpage < (7 + (adjacents * 2)))	{

		    // Not enough pages to break up the links
			for (var counter = 1; counter <= lastpage; counter++) {

			    pagination += (counter == page) ? '<span class="current">' : '<a class="pagilink" href="' + url + '&amp;p=' + counter + anchor + '">';
			    pagination += counter;
			    pagination += (counter == page) ? '</span>' : '</a>';

			}

		} else if (lastpage >= (7 + (adjacents * 2))) {

		    //enough pages to hide some
			//close to beginning; only hide later pages
			if (page < (1 + (adjacents * 3))) {

				for (counter = 1; counter < (4 + (adjacents * 2)); counter++) {

				    pagination += (counter == page) ? '<span class="current">' : '<a class="pagilink" href="' + url + '&amp;p=' + counter + anchor + '">';
				    pagination += counter;
				    pagination += (counter == page) ? '</span>' : '</a>';

				}

				pagination += '...';
				pagination += '<a class="pagilink" href="' + url + '&amp;p=' + lpm1 + anchor + '">' + lpm1 + '</a>';
				pagination += '<a class="pagilink" href="' + url + '&amp;p=' + lastpage + anchor + '">' + lastpage + '</a>';

			} else if ((lastpage - (adjacents * 2)) > page && page > (adjacents * 2)) {

			    // In middle, hide some front and some back
				pagination += '<a class="pagilink" href="' + url + '&amp;p=1' + anchor + '">1</a>';
				pagination += '<a class="pagilink" href="' + url + '&amp;p=2' + anchor + '">2</a>';
				pagination += '...';

				for (counter = (page - adjacents); counter <= (page + adjacents); counter++) {

				    pagination += (counter == page) ? '<span class="current">' : '<a class="pagilink" href="' + url + '&amp;p=' + counter + anchor + '">';
				    pagination += counter;
				    pagination += (counter == page) ? '</span>' : '</a>';

				}

				pagination += '...';
				pagination += '<a class="pagilink" href="' + url + '&amp;p=' + lpm1 + anchor + '">' + lpm1 + '</a>';
				pagination += '<a class="pagilink" href="' + url + '&amp;p=' + lastpage + anchor + '">' + lastpage + '</a>';
			} else {

			    // Close to end; only hide early pages
				pagination += '<a class="pagilink" href="' + url + '&amp;p=1' + anchor + '">1</a>';
				pagination += '<a class="pagilink" href="' + url + '&amp;p=2' + anchor + '">2</a>';
				pagination += "...";

				for (counter = (lastpage - (1 + (adjacents * 3))); counter <= lastpage; counter++) {

				    pagination += (counter == page) ? '<span class="current">' : '<a href="' + url + '&amp;p=' + counter + anchor + '">';
				    pagination += counter;
				    pagination += (counter == page) ? '</span>' : '</a>';

				}

			}

		}

		// Next button
		pagination += (page < (counter - 1)) ? '<a class="pagilink" href="' + url + '&amp;p=' + next + anchor + '">' : '<span class="disabled">';
		pagination += 'next &raquo;';
		pagination += (page < (counter - 1)) ? '</a>' : '</span>';

		pagination += '</div>\n';

	}

	return pagination;

}

/**
 * Some prototype functions
 */

/**
 * trim() - Trims whitespace from both the left and the right of a string
 */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

/**
 * ltrim() - Trims whitespace from the left of a string
 */
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

/**
 * rtrim() - Trims whitespace from the right of a string
 */
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/**
 * Convert a hyphenated set of words into one camel cased word. For example,
 * the hyphenated set of words 'border-left-width' would turn into 'borderLeftWidth'.
 *
 * @return  string
 */
String.prototype.toCamelCase = function()
{

    var self = this;

    if (self.indexOf('-') > 0) {

        var parts = self.split('-');

        // Start the new text with the first word
        self = parts[0];

        // We skip over the first word, and capitalize
        // each word after.
        for (i = 1; i < parts.length; i++) {

            // Uppercase the first letter, and ensure the rest is lowercase.
            self += parts[i].substr(0, 1).toUpperCase() + parts[i].substr(1).toLowerCase();

        };

    };

    return self;

};

/**
 * Returns TRUE if needle exists in haystack, or FALSE if it doesn't.
 *
 * @param   string  needle      - The property to find in haystack
 * @param   array   haystack    - The array to look in for needle
 *
 * @return  boolean
 */
inArray = function(needle, haystack)
{

    var inHaystack = false;

    for (it in haystack) {

        if (needle == haystack[it]) {

            inHaystack = true;

        }

    }

    return inHaystack;

}

/**
 * Removes needle from haystack. The new array will not
 * preserve the original keys.
 *
 * @param   string  needle      - The property to remove from haystack
 * @param   array   haystack    - The array to remove needle from
 *
 * @return  array
 */
kickArray = function (needle, haystack)
{

    var newArray = [];

    for (it in haystack) {

        if (haystack[it] != needle) {

            newArray.push(haystack[it]);

        }

    }

    return newArray;

}
