// IE 6 and less display errors w/ jQuery, this prevents that
//onerror=handleErr;
//function handleErr(msg,url,l) {return false;}


var onflag = false;
var shoutboxlessflag = false;
var cid = -1;
var lines = 10;
var shoutboxmoreval = 5;

var placeholders = [ '', 'name...', 'shout...' ];

$(document).ready(function() {

//shoutbox 

//shoutbox hide toggle
	$("#shoutboxtoggle").toggle(
		function(e) {
			e.preventDefault(); 
			$(".shoutboxhide").slideDown("fast");
			$("#shoutboxtoggle span").removeClass("last");
//			$("#shoutboxloading").show();
			updateShoutBox();
			$.doTimeout( 'shoutboxpoll', 5000, updateShoutBox );
		},
		function(e) {
			e.preventDefault(); 
			$(".shoutboxhide").slideUp("fast",function() {
				$.doTimeout( 'shoutboxpoll' );
				$("#shoutboxtoggle span").addClass("last");
//				$("#shoutboxloading").hide();
				$("span#shouterror").html('');
			});
		});

//shoutbox placeholder data	
	var cookie_name = readCookie('slapsme_shoutbox_name');
   if ( cookie_name == null )
		$("input#shoutboxname").attr('value','name...').css('color','#999');
	else
		$("input#shoutboxname").attr('value',cookie_name).css('color','#000');

	$("input#shoutboxname").focus(function() {
		if ($.inArray($(this).attr('value'), placeholders) > -1 )
			$(this).attr('value','').css('color','#000');
	});
	$("input#shoutboxname").blur(function() {
		if ($(this).attr('value') == '')
			$(this).attr('value', placeholders[1]).css('color','#999');
	});

	$("input#shoutboxshout").attr('value','shout...').css('color','#999');
	$("input#shoutboxshout").focus(function() {
		if ($.inArray($(this).attr('value'), placeholders) > -1 )
			$(this).attr('value','').css('color','#000');
	});
	$("input#shoutboxshout").blur(function() {
		if ($(this).attr('value') == '')
			$(this).attr('value', placeholders[2]).css('color','#999');
	});

//show more/less
	$('#shoutboxmore').click(function(e){
		e.preventDefault();
		$('li.shoutboxless').show();
		var start = cid-lines-shoutboxmoreval+1;
		var end = cid-lines;
		if ( lines + shoutboxmoreval > cid ) {
			return false;
		}
		lines += shoutboxmoreval;

		$("#shoutboxloading").show();
      $.ajax({
         type: "GET",
         url: "/shoutbox.php",
         data: {newload: 1, start: start, end: end},
         success: function(data) {
//				$('#shoutboxloading').hide();
				data = $.parseJSON(data);
				var out;
				for(var i=0;i<data.length;i++) {
					out = constructShout(data[i]['ts'], data[i]['name'], data[i]['shout']);
					$("#shoutboxcont").append(out);
//					$("#shoutboxcont div:eq("+(lines-shoutboxmoreval+i)+")").delay(i*100).fadeIn(200);
					$("#shoutboxcont div:last-child").delay(i*100).fadeIn(200);
				}
         },
         error: function(o, status, error) {
            $("#shoutboxloading").hide();
				lines -= shoutboxmoreval;
         },
         complete: function() {
//            $("#shoutboxloading").hide();
         },
         cache: false,
         timeout: 5000
      });

		return false;
	});

	$('#shoutboxless').click(function(e){
		e.preventDefault();
		if ( shoutboxlessflag ) return false;
		shoutboxlessflag = true;

		lines -= shoutboxmoreval;
		if ( lines < shoutboxmoreval ) {
			lines = shoutboxmoreval;
			shoutboxlessflag = false;
//			$('li.shoutboxless').fadeOut();
			$('li.shoutboxless').hide();
		} else {
			for (var i=shoutboxmoreval-1;i>=0;i--) {
				if ( i == 0 ) {
					if ( lines <= shoutboxmoreval ) 
						$('li.shoutboxless').hide();
//						$('li.shoutboxless').fadeOut();
					shoutboxlessflag = false;
				}
				$("#shoutboxcont div:eq("+(i+lines)+")").fadeOut((shoutboxmoreval-i)*100, function() { $(this).remove(); });
			}
		}
		return false;
	});

	
});

function check_shout() {
	var valid = true;

	var name  = $.trim($("input#shoutboxname").attr('value'));
	var shout = $.trim($("input#shoutboxshout").attr('value'));

	if ($.inArray(shout, placeholders) > -1) valid = false;
	if ($.inArray(name, placeholders) > -1) valid = false;
	if (!valid) $("span#shouterror").html("Please enter a valid name and shout!");
	else $("span#shouterror").html('');
	if (valid) {
		$("#shoutboxloading").show();
      $.ajax({
         type: "POST",
         url: "/shoutbox.php",
         data: {s: 1, name: name, shout: shout},
         success: function(data) {
				$("input#shoutboxshout").attr('value',placeholders[2]).css('color','#999');
            $("#shoutboxloading").hide();
				updateShoutBox();
         },
         error: function(o, status, error) {
            $("#shoutboxloading").hide();
         },
         complete: function() {
            $("#shoutboxloading").hide();
         },
         cache: false,
         timeout: 5000
      });

	}
	return false;
}





function updateShoutBox() {
	if (!onflag ) {
		onflag = true;
		$("#shoutboxloading").show();
		$.ajax({
			type: "GET",
			url: "/shoutbox.php",
			data: {since: 1, cid: cid, lines: lines},
			success: function(data) {
				addToShoutBox(data);
				$("#shoutboxloading").hide();
				onflag = false;
			},
			error: function(o, status, error) {
				$("#shoutboxloading").hide();
				onflag = false;
			},
			complete: function() {
				$("#shoutboxloading").hide();
				onflag = false;
			},
			cache: false,
			timeout: 5000
		});
		return true;
   } else
		return false;
}


function addToShoutBox(data, prepend) {
	if ( data != "0" ) {
		data = $.parseJSON(data);
		var out;
		for(var i=data.length-1;i>=0;i--) {
			out = constructShout(data[i]['ts'], data[i]['name'], data[i]['shout']);
			$("#shoutboxcont").prepend(out);
			$("#shoutboxcont div:gt("+(lines-1)+")").slideUp(100, function(data){ $(this).remove();   });
//			$("#shoutboxcont div:eq(0)").delay(i*100).slideDown(100);
			$("#shoutboxcont div:eq(0)").delay(i*100).fadeIn(200);
		}
		cid = data[0]['id'];
	}
}

function constructShout(ts, name, shout) {
	var out;
	out = "";
	out += "<div class='shoutboxshoutitem' style='display:none'><span class=\"shoutboxtime\">" + ts + "</span>";
	out += " <span class=\"shoutboxname\">" + name +":</span>";
	out += " <span class=\"shoutboxtext\">" + shout + "</span></div>";
	return out;
}














function readCookie(name) {
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}


