var frst_div = '';
var scnd_div = '';

var i;


$(document).ready(function() {
	
	$.getJSON('/phones/getPhones', {}, function(json){
		
			if(frst_div == '' || frst_div == '#phones_odd') {
				frst_div = '#phones_even';
				scnd_div = '#phones_odd';
			} else {
				frst_div = '#phones_odd';
				scnd_div = '#phones_even';
			}
		
			$(scnd_div).hide('slide', {direction: 'up'}, 500, function() {				
				$(frst_div).html('');
				
				json.shuffle();
								
				for(i = 0; i < 3; ++i) {            
           			$(frst_div).append('<span>+7 ' + json[i].phone + '</span>');
      			}
			
            	$(frst_div).show('slide', {direction: 'down'}, 500);
			});
		
		
		setInterval(function() {
				
			if(frst_div == '' || frst_div == '#phones_odd') {
				frst_div = '#phones_even';
				scnd_div = '#phones_odd';
			} else {
				frst_div = '#phones_odd';
				scnd_div = '#phones_even';
			}
		
			$(scnd_div).hide('slide', {direction: 'up'}, 500, function() {				
				$(frst_div).html('');
				
				json.shuffle();
								
				for(i = 0; i < 3; ++i) {            
           			$(frst_div).append('<span>+7 ' + json[i].phone + '</span>');
      			}
			
            	$(frst_div).show('slide', {direction: 'down'}, 500);
			})
		}, 5000);
		
    });
});

Array.prototype.shuffle = function(b) {
	var i = this.length, j, t;
	while(i) {
 		j = Math.floor(( i-- ) * Math.random());
  		t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  		this[i] = this[j];
  		this[j] = t;
 	}

 	return this;
};