
var chatTimer = null;

$(document).ready(function(){
	BrainGnat.html5Shiv();
	submitChat();
	showChatLog();
	clearChat();
	chatButtons();
	loadMap();	
	

});

function submitChat(){
	$('#submitChat').click(function(){
		var author = $('#authorName').attr('value');
		var post = $('#post').attr('value');
		var avatar = $('#userAvatar').attr('value');
		var id = $('#userId').attr('value');
		var dataString = "author=" + author  + "&post=" + post + "&avatar=" + avatar + '&id=' + id; 
		$.ajax({type:"POST", url:"postchat.php",data:dataString});
		$('#post').attr('value', '');
		$(this).blur();
		clearTimeout(chatTime);
		showChatLog();
		return false;
	});		
}

function showChatLog(){
	if ($('.chatwall').length > 0) {
		if($('.chatwall li:first-child').length > 0){
			var splits = $('.chatwall li:first-child').attr('class').split("-");
			var lastId = splits[1] * 1;
		} else {
			lastId = 0;
		}
		var dataString = "id=" + lastId;
		$.ajax({type:"POST", url:"readchat.php",data:dataString,success:function(data){
			$('.chatwall').html(data + $('.chatwall').html());
			if(data != '') {
				$('.chatwall li').attr('style', '');
			}
		}});
		chatTime = setTimeout('showChatLog()', 10000);
	}
}

function clearChat(){
	$('#post').focus(function(){
		if($(this).attr('value') == 'Tell us what your thoughts are...'){
			$(this).attr('value', '');
		}
	});
	$('#post').blur(function(){
		if($(this).attr('value') == ''){
			$(this).attr('value', 'Tell us what your thoughts are...');
		}
	});
}

function chatButtons(){
	$('#chat-down').click(function(){
		var margin = $('.chatwall li:first-child').css('margin-top').substr(0,$('.chatwall li:first-child').css('margin-top').length-2) * 1;
		margin = margin - 544;
		var totalHeight = 0;
		for(i=0; i<$('.chatwall li').length; i++){
			totalHeight = totalHeight + $('.chatwall li:eq('+i+')').outerHeight();
		}
		if(margin < -totalHeight)
		{
			margin = -totalHeight;
		}
		$('#chat-up').unbind('click');
		$('#chat-down').unbind('click');
		$('.chatwall li:first-child').animate({marginTop: margin + 'px'}, 500, function(){$('#chat-down').blur();chatButtons();});
	});
	$('#chat-up').click(function(){
		var margin = $('.chatwall li:first-child').css('margin-top').substr(0,$('.chatwall li:first-child').css('margin-top').length-2) * 1;
		margin = margin + 544;
		if(margin > 0){
			margin = 0;
		}
		$('#chat-up').unbind('click');
		$('#chat-down').unbind('click');
		$('.chatwall li:first-child').animate({marginTop: margin + 'px'}, 500, function(){$('#chat-up').blur();chatButtons();});
	});
}

function loadMap(){
	if($('#map').length > 0){
		BrainGnat.Google.map.loadFromHCard('#where .vcard', '#map', 14, true);
	}
}
