 $(document).ready(function(){

	var options = {
		dataType:       'json',
		beforeSubmit:   validate,
		success:        success
	};
	$('#chat-form').ajaxForm(options);


	/* $("#doIt").click( function() {

		var tmpResponse = { time: 9000, username: 'hihi', user_id: 3 };

		$('#listDiv').append(prepare( tmpResponse ));
		$('#list-' + count).fadeIn('slow');
		
	} )
*/


	// $("#doIt2").click( doRefresh );


 }); // jQuery ready


function validate(formData, jqForm, options) {
	for (var i=0; i < formData.length; i++) {
		if (!formData[i].value) {
			alert('Please fill in all the fields');
			$('input[@name='+formData[i].name+']').css('background', 'red');
			return false;
		}
	}

	$('#chat-response').html('<img src="'+files+'images/loader.gif" />');
	// $('#nickname-box').attr('disabled', 'true');
	$('#nickname-box').addClass('disabledText');
	
	clearTimeout(timeoutID);
}

function success(response, status)  {
	if(status == 'success') {
		lastTime = response.time;
		$('#chat-response').html('<img src="'+files+'images/accept.png" />');
		$('#listDiv').append(prepare(response));
		$('input[@name=message]').attr('value', '').focus();
		$('#list-'+count).fadeIn('slow');
		e.scrollTop = e.scrollHeight;
		timeoutID = setTimeout(refresh, 3000);
	}
}



	function doRefresh() {
		$.getJSON(files+"daddy-shoutbox2.php?action=view&quantity=20&time="+lastTime+"&lastid="+ lastid , function(json) {

			if(json.length) {
				for(i=0; i < json.length; i++) {
					$('#listDiv').append(prepare( json[i] ));
					$('#list-' + count).fadeIn('slow');
	
					if ( json[i].postid > lastid ) {
						lastid = json[i].postid;
					}
				}
				scrollToBottom( 'scrollDiv' );
			}

			timeoutId = setTimeout( "doRefresh()", 4000 );

		} )

	}



	var count = 0;
	var files = '/jquery-shoutbox/demos/jquery-shoutbox/';
	var lastTime = 0;
	var lastid = 0;
	var timeoutID = 0;

	function prepare(response) {


		var d = new Date();
		count++;
		d.setTime(response.time*1000);
		var hour=d.getHours();
		var min=d.getMinutes();
		hour = hour + "";
		min = min + "";
		if (hour.match(/^\d$/)) {
			hour = "0" + hour;
		}
		if (min.match(/^\d$/)) {
			min = "0" + min;
		}
		var ampm = 'AM';
		if ( hour > 12 ) {
			hour = hour - 12;
			ampm = 'PM';
		}
		var mytime = hour+':'+min + ampm;
		var minim='';
		var nickname = response.nickname;
		var admins = ',epheph,Crackhead,MJ2k,$$BIGMONEY$$,futtbuck,';
		var mods = ',VC Punker,';
		var string='';
		if ( response.nickname == '{SMB_USERNAME}' ) {
			string = '<div class="chat-item-owner" ' + minim + ' id="list-'+count+'">';
		}
		else if ( response.userid == 0 )
		{
			string = '<div class="chat-item-guest" ' + minim + ' id="list-'+count+'">';
		}
		else if ( admins.indexOf( ',' + nickname + ',' ) != -1 )
		{
			string = '<div class="chat-item-admin" ' + minim + ' id="list-'+count+'">';
		}
		else if ( mods.indexOf( ',' + nickname + ',' ) != -1 )
		{
			string = '<div class="chat-item-mod" ' + minim + ' id="list-'+count+'">';
		}
		else
		{
			string = '<div class="chat-item" ' + minim + ' id="list-'+count+'">';
		}

		var userid_link = "";
		if ( response.userid > 0 ) {
			userid_link = '<a href="http://gta4play.com/forums/profile.php?mode=viewprofile&u=' + response.userid + '">';
		}

		string = string
			+ '<span class="shoutbox-list-time">'+mytime+'</span> '               + '<span class="shoutbox-list-nick">' + userid_link + response.nickname+'</a>:</span> '              + '<span class="shoutbox-list-message">'+response.message+'</span>'
			+'</div>';
		return string;
	}




	function scrollToBottom( scrollDiv ) {
		e = document.getElementById( scrollDiv );
		e.scrollTop = e.scrollHeight;
	}

	function startChat () {
		timeoutId = setTimeout( "doRefresh()", 1000 );
	}

