$(document).ready(function() {

	$(".clicklove").click(function () {
		var dataString = 'id=' + $(this).attr("id");
		var parent = $(this);
		$(this).fadeOut(300);
		$.ajax({ type:"POST", url: WWW+"ajax/love/", data:dataString, cache:false,
			success: function (html) {
				parent.html(html);
				parent.fadeIn(300);
			}
		});
	return false;
	});

	// <div id="shoutbox"></div>
	$("#shoutbox").html('\
	<div id="top">Mini-tchat : <div id="close_btn">&nbsp;</div></div>\
	<div id="toggle_chat">\
		<div id="messagebox"></div>\
		<div id="type_box"><input name="shout_message" id="shout_message" type="text" placeholder="Message [Entrer]" maxlength="150" /></div>\
	</div>\
	').show();


	load_data = {'fetch':1};
	window.setInterval(function(){
	 $.post(WWW+"ajax/shoutbox/", load_data, function(data) {
		$('#messagebox').html(data);
		$('#messagebox').scrollTop( $('#messagebox')[0].scrollHeight );
	 });
	}, 2200); // ms
	
	//method to trigger when user hits enter key
	$("#shout_message").keypress(function(evt) {
		if(evt.which == 13) {
				var imessage = $('#shout_message').val();
				post_data = {'message':imessage};
			 	
				//send data to "shout.php" using jQuery $.post()
				$.post(WWW+'ajax/shoutbox/', post_data, function(data) {
					
					//append data into messagebox with jQuery fade effect!
					$(data).hide().appendTo('#messagebox').fadeIn();
	
					//keep scrolled to bottom of chat!
					$('#messagebox').scrollTop($('#messagebox')[0].scrollHeight);
					
					//reset value of message box
					$('#shout_message').val('');
					
				}).fail(function(err) {
					//alert HTTP server error
					//alert(err.statusText);
				});
			}
	});
	
	//toggle hide/show shout box
	$("#shoutbox #top").click(function () {
		//get CSS display state of .toggle_chat element
		var toggleState = $('#toggle_chat').css('display');
		
		//toggle show/hide chat box
		$('#toggle_chat').slideToggle();
		
		//use toggleState var to change close/open icon image
		if(toggleState == 'block') {
			$("#shoutbox #top div").attr('id', 'open_btn');
		} else {
			$("#shoutbox #top div").attr('id', 'close_btn');
		}
		 
		 
	});


});
