(function($){
var baseUrl = '',
statusesNum = 0,
current = 0,
lastId = 0;


function changeStatus(status, data, side){
//	console.log(status, data, side)
	var classes = Array(),
	current = status.find('.statusBlock').eq(side);//side == 0 ? status.find('.current') : status.find('.next');
	current.attr('data-id', data.id);

	current.find('img.avatarImg').attr('src', data.profile_img);
	current.find('.username').text(data.username);
	if( data.media != '' ) {
		current.find('.mediaImg').attr('src', data.media);
		classes.push('hasMedia');
		current.find('.message').addClass('col-sm-offset-6').text(data.text);
	} else {
		current.find('.message').text(data.text);
	}

	current.addClass( classes.join(' ') );
}



function getNextStatus(){
	var status = $('#wallWrapper .status').eq(current),
	side = parseInt( status.attr('data-current-side') ),
	nextSide = (side+1)%2;/*,
	id = parseInt( status.find('> .statusBlock').eq(side).attr('data-id') )*/;
//	console.log(current, side, nextSide, id);

	$.ajax({
		method		: "GET",
		url			: baseUrl+'/next/'+lastId,
		contentType	: "application/json",
		success		: function(response) {
			if( response.state == 1 ) {
				lastId = response.id;
			}

			changeStatus(status, response, nextSide);

			status
			.attr('data-current-side', nextSide)
			.toggleClass('switchSlide');

			window.setTimeout(getNextStatus, 3000);
		}
	});
//	window.setTimeout(getNextStatus, 3000);

	current = (current < statusesNum-1) ? current+1 : 0;
}



function setWall(data) {
	var dummyStatus = $('.dummyStatus'),
	wrapper = $('#wallWrapper');
	statusesNum = data.length;

	for (var s=0; s<statusesNum; s++) {
		var status = $(dummyStatus).clone();
		status.removeClass("dummyStatus hidden");
		wrapper.append(status);
		changeStatus(status, data[s], 0);
		lastId = data[s]['id'];
	}
//	lastId = data[statusesNum-1].next_id

	window.setTimeout(getNextStatus, 3000);
}



function init() {
	baseUrl = $('body').data('base-url') + '/api';

	$.ajax({
		method		: "GET",
		url			: baseUrl,
		contentType	: "application/json",
		success		: setWall
	});
}


$(document).ready(init);
})(jQuery);
