$(document).ready(function(){
    $("[name='rpp']").change(function(){
        var data = "";
		data = $(".filter").serializeArray();
		data.push({name: "rpp",value: $(this).val()});
		data.push({name: "p",value: 1});
		data.push({name: "action2",value: "paging"});
		$.bbq.pushState("#"+$.param(data));
    });
    $(".paging .button").click(function(){
        var data = "";
        var page = 1;
        if($(this).attr("value")=="next")
        {
            page = parseInt($(".paging .selected").attr("value"))+1;
        }
        else if($(this).attr("value")=="prev")
        {
            page = parseInt($(".paging .selected").attr("value"))-1;
        }
        else
        {
            page = $(this).attr("value");
        }
		data = $(".filter").serializeArray();
		data.push({name: "rpp",value:  $("[name='rpp']").val()});
		data.push({name: "p",value: page});
		data.push({name: "action2",value: "paging"});
		$.bbq.pushState("#"+$.param(data));
    });
    $(".paging .pageSelector").click(function(){
		$(".popup").remove();
        var lastPageNumber = parseInt($(this).attr("lastPageNumber"));
        var currentPageNumber = parseInt($(this).attr("currentPageNumber"));
        var html = "";
        html += "<span class='popup'>";
        html += "<span class='close'>X</span>";
        html += "   <select class='chooser'>";
        for(var i=0; i<lastPageNumber; i++)
        {
            html += "<option value='"+(i+1)+"'";
            if(i+1==currentPageNumber)
            html += " selected='selected'";
            html += ">"+(i+1)+"</option>";
        }
        html += "   </select>";
        html += "</span>";
        $(this).after(html);
        $(".paging .close").click(function(){
            $(".popup").remove();
        });
        $(".paging .chooser").change(function(){
            var data = "";
			data = $(".filter").serializeArray();
			data.push({name: "rpp",value: $("[name='rpp']").val()});
			data.push({name: "p",value: $(this).val()});
			data.push({name: "action2",value: "paging"});
			$.bbq.pushState("#"+$.param(data));
        });
    });
    
});

