/*

ZepFrog - General Javascript convenience functions and includes, externalized from theme. NOT to be confused with zep_functions.php.

ZepFrog Corp. © Copyright 2012

*/

//
// Parsing Functions
//

// Query string parsing function, specific to alpha/numeric values of any length
function getQueryStringValueByKey(href, key) {
    var match = RegExp('[?&]' + key + '=([^&]*)').exec(href);
    return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : null;
}

// Query string parsing function, specific to numeric values in the range 0-99999
function getQueryStringNumericValueByKey(href, key) {
    var match = RegExp('[?&]' + key + '=([0-99999]*)').exec(href);
    return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : null;
}


//
// AJAX RSS
//

// Render container via wp-ajax callback with argument
function renderAjaxContainer(container, callback, argument, async) {
	
    jQuery.ajax({
		async: async, // Asynchronous if undefined or true, synchronous if false
        type: 'POST',
        url: ajaxurl,
        beforeSend: function( xhr ) { $(container).html('<img src="./wp-content/images/loading.gif" alt="Wait" />'); },
        data: {"action":callback,"argument":argument},
        success: function(data){ $(container).html(data); }
    });

	return argument;
}

// Render content at container via wp-ajax articles callback with feed_url
function renderContentRSS(container, feed_url) {
	return renderAjaxContainer(container, 'zep_rssimport_articles', feed_url);
}

// Render publications at container via wp-ajax publications callback with feed_url
function renderPublicationsRSS(container, feed_url) {
	return renderAjaxContainer(container, 'zep_rssimport_publications', feed_url);
}

// Synchronously render subnav at container via wp-ajax subnav callback with optional argument
function renderAjaxSubnav(container, argument) {
	return renderAjaxContainer(container, 'subnav_callback', (!argument) ? '' : argument, false);
}

// Render content at container via wp-ajax articles widget callback with feed_url
function renderContentRSSWidget(container, feed_url) {
	return renderAjaxContainer(container, 'zep_rssimport_articles_widget', feed_url);
}
