jQuery(document).ready(function(){
var j=jQuery;
j(".widget_bp_swa_widget div.pagination-links a").live("click",function(){
	var page=get_var_in_url(j(this).attr("href"),"acpage");
	//determine current scope
	var scope='';
	if(j(".widget_bp_swa_widget #activity-filter-links li").get(0))
	{var scope_anchor=j(".widget_bp_swa_widget #activity-filter-links li.selected a");
	if(scope_anchor.get(0))
		scope=get_var_in_url(j(scope_anchor).attr('href'),"afilter");

	}
fetch_and_show_activity(page,scope);
return false;
});

function fetch_and_show_activity(page,scope){
	var per_page=j(".widget_bp_swa_widget #swa_per_page").val();	
	var max_items=j(".widget_bp_swa_widget #swa_max_items").val();	
			
j.post( ajaxurl, {
			'action': 'swa_fetch_content',
			'cookie': encodeURIComponent(document.cookie),
			'page': page,
			'scope': scope,
			'max'  :max_items,
			'per_page':per_page
				},
		function(response){
			j(".widget_bp_swa_widget .swa-wrap").replaceWith(response);	
			j(".widget_bp_swa_widget #activity-filter-links li#afilter-"+scope).addClass("selected");

			});//for pagination

}


//for filters
j(".widget_bp_swa_widget #activity-filter-links li a").live("click",function(){
	var page=1;//when ever someone clicks on a filter link, start by showing the first
	var scope=get_var_in_url(j(this).attr("href"),"afilter");//'get_current_scope';
	fetch_and_show_activity(page,scope);
	//make the current filter selected

	return false;
});


function get_var_in_url(url,name){
    var urla=url.split("?");
    var qvars=urla[1].split("&");//so we have an arry of name=val,name=val
    for(var i=0;i<qvars.length;i++){
        var qv=qvars[i].split("=");
        if(qv[0]==name)
            return qv[1];
      }
}
});