var maxScrollBox = 0;
var allowScrollBox = true;
var _timerShoutBox = null;
var refreshShoutBox = 15000;
var shoutboxOpening = false;

var totalShoutBox = 0;


function initShoutBox(){
	document.getElementById('contentBox').scrollTop = 100000;
	checkMaxScroll();
	totalShoutBox = $('.shoutBoxBlock').size();
	pushShoutBox();
	_timerShoutBox = setTimeout(function(){loadShoutBoxMessages()},refreshShoutBox);
	
	document.getElementById('contentBox').onscroll = function(){

		var curScroll = document.getElementById('contentBox').scrollTop;
		closePushShoutBox();
		if(curScroll<maxScrollBox){allowScrollBox = false;} else {allowScrollBox = true;}
		//$('#debug_1').html('allow : ' + allowScrollBox+' - '+uniqid());
		
		
	}
	
}


function pushShoutBox(){
		
		var new_size = $('.shoutBoxBlock').size();
		var diff =  new_size - totalShoutBox;
		totalShoutBox = new_size;
			
		if(allowScrollBox == true && shoutboxOpening == true){ return false;}
		var _currentPush = 0;
		if($('#PUSH_SHOUTBOX').get(0)){ _currentPush = parseInt($('#PUSH_SHOUTBOX').html()); }		
		
		_currentPush = diff + _currentPush;
		
		
		$('#PUSH_SHOUTBOX').remove();
		if(_currentPush<1){ return false;}
		$('#btn_shoutbox').before('<div class="NF_PUSH" id="PUSH_SHOUTBOX">'+_currentPush+'</div>');
		$('#PUSH_SHOUTBOX').css({opacity:0,marginTop:40});
		$('#PUSH_SHOUTBOX').animate({opacity:1,marginTop:0},300);
}

function closePushShoutBox(){
		$('#PUSH_SHOUTBOX').remove();
}

function openShoutBox(){
	$('#shoutBox').show();	
	document.getElementById('contentBox').scrollTop = 100000;
	checkMaxScroll();
	refreshShoutBox = 5000;
	shoutboxOpening = true;
	$('#btn_shoutbox').addClass('actif');
	$('#bottom').find('.NF_PUSH').remove();
	Set_Cookie( 'SHOUTBOX_OPEN', 1);
	document.getElementById('shoutBoxTxt').focus();
}

function closeShoutBox(){
	$('#shoutBox').hide();
	refreshShoutBox = 15000;	
	shoutboxOpening = false;
	$('#btn_shoutbox').removeClass('actif');
	Set_Cookie( 'SHOUTBOX_OPEN', 0);
}


function openCloseShoutBox(){
	if(shoutboxOpening == true){closeShoutBox();} else {openShoutBox();}
}

function checkMaxScroll(){
	maxScrollBox = Math.round(document.getElementById('contentBox').scrollHeight - $('#contentBox').height()-5);
}



function loadShoutBoxMessages(){
	clearTimeout(_timerShoutBox);
	$('#shoutBoxCheck').load('shoutbox_check_maj.php?uid='+encodeURIComponent(uniqid()),function(){		
		clearTimeout(_timerShoutBox);
		_timerShoutBox = setTimeout(function(){loadShoutBoxMessages()},refreshShoutBox);
	});	
}

function refreshShoutBoxMessages(){
	$('#contentBoxMessage').load('shoutbox_messages.php?uid='+encodeURIComponent(uniqid()),function(){
		pushShoutBox();
		checkMaxScroll();
		clearTimeout(_timerShoutBox);
		_timerShoutBox = setTimeout(function(){loadShoutBoxMessages()},refreshShoutBox);
		if(allowScrollBox){
			document.getElementById('contentBox').scrollTop = maxScrollBox+100;
		}
	});
}


function submitShoutBox(){
	var _newMessage = trim($('#shoutBoxTxt').val());
	
	if(strlen(_newMessage)>0){
		clearTimeout(_timerShoutBox);
		allowScrollBox = true;
		document.getElementById('shoutBoxTxt').blur();
		openNotify('Your Message is Submitting...',true);
		$('#shoutBoxTxt').val('');
		$('#shoutBoxActions').load('post_shoutbox.php?message='+encodeURIComponent(_newMessage),function(){
			document.getElementById('shoutBoxTxt').focus();
			deleteNotify();			
			refreshShoutBoxMessages();
		});
		
		
		
	}
}



function resizeShoutBox(_class){
	Set_Cookie( 'SHOUTBOX_CLASS', _class, 30);
	$('#shoutBox').removeAttr('class');	
	$('#shoutBox').addClass(_class);
	$('#shoutBoxSizes').find('a').removeClass('actif');
	$('#shoutBoxSizes').find('.'+_class+'_btn').addClass('actif');	
	document.getElementById('contentBox').scrollTop = 100000;
	checkMaxScroll();
	shoutboxOpening = true;
}
