in your code: instead of i++, change it with the current index of the cn_item with selected class. it should look like this:
function next() {
i = $('.cn_item').index($('.selected')) + 1;
if(i== $('.cn_item.selected').parent().children().length){
x=1;
that should fix everything.