$(document).ready(function(){
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3.feed-header');
	Cufon.replace('div#get-excuse span');
	Cufon.replace('label');
	Cufon.replace('div#get-excuse a', {hover:true});
	Cufon.replace('p.sub');
	
	$('#embed-code').hide();
	$('.excuses-list').css({'height': '219px'});
	$('.banter-list').css({'height': '252px'});
	$('.static-excuses').css({'height': '290px'});
	$('.excuses-page').css({'height': '300px'});
	
	// if (("#excuse").length) {
		// $("#excuse").focus();
	// }
	
	var test = false;
	$('textarea#excuse').val('Please type your excuse here').css({'color': '#999'});
		$('textarea#excuse').focus(function() {
		if (test == false) {
			$(this).val('').css({'color': '#000'});
		}
	});
	
	$('textarea#excuse').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Please type your excuse here').css({'color': '#999'});
			test = false;
		} else {
			test = true;
		}
	});
	
	if (location.hash == '#submit-list') {
		$('#submit-list').animate({ backgroundColor: "#d7ff8e" },600).animate({ backgroundColor: "#fff" },2000);
	} 
	
	//$("ul.static-excuses li a").fancybox();
	
	$(function() {
		$('.scroll-pane').jScrollPane({
			scrollbarWidth: 5
		});
	});
	
	setInterval(function() {
		$("#banter .scroll-pane").load('test.php'+'?ms=' + new Date().getTime()+'&s=2&l=20&c=banter-list');
		$("#excuses .scroll-pane").load('test.php'+'?ms=' + new Date().getTime()+'&s=1&l=20&c=excuse-list');
	}, 3000);

	
	// $('#submit-excuse').submit(function (event) {
		// event.preventDefault();
		// $('p#response').text('Thank you for your submission.');
	// });
	
	$('#submit-excuse').submit(function (event) {
		event.preventDefault();
		ws_url = 'http://www.footy-feed.co.uk/content/inc/excuse-submit.php?excuse='+$('textarea#excuse').val();
		$.ajax({
			type: 'GET',
			url: ws_url,
			dataType: "xml",
			beforeSend: function() {
				$('form#submit-excuse fieldset').append('<img id="loader" src="/content/img/ajax-loader.gif" alt="Loading" />');
			},
			success: function(xmlIn) {
				results = xmlIn.getElementsByTagName("result");
				$('img#loader').remove();
				$('p#response').html("Thank you for your submission! <scri"+"pt> _gaq.push(['_trackPageview', '/virtual/excuse-submission/']); </script>");
				if ($(results).text() == 1) {
					// $('ul.excuses-page').prepend('<li class="first"><p>'+$('textarea#excuse').val()+'</p><p><a href="#">Jac Test</a></p></li>');
					// $('li.first').animate({ backgroundColor: "#76B308" },800).animate({ backgroundColor: "#fff" },800).removeClass('first');
					$('textarea#excuse').val('');
				}
			},
			error: function() {
				$('p#response').text('Error.');
			}
		});
	});
	
	$('#char-limit').html('140');
	
	function limitChars(textid, limit, infodiv) {
		var text = $('#'+textid).val(); 
		var textlength = text.length;
		if(textlength > limit) {
			//$('#' + infodiv).html('No more than '+limit+' characters!');
			$('#submit').attr('disabled', true).css({'background': '#dbdbdb', 'color': '#9d9d9d', 'cursor': 'auto'});
			$('#' + infodiv).html((limit - textlength)).css({'color': '#f00'});
		} else {
			$('#submit').removeAttr('disabled').css({'background': '#76b308', 'color': '#fff', 'cursor': 'pointer'});
			$('#' + infodiv).html((limit - textlength)).css({'color': '#fff'});
		}
	}
	
	$('#excuse').keyup(function(){
		limitChars('excuse', 140, 'char-limit');
	});
	
	if ($('a.widget')) {
		$('a.widget').each(function(){
			$(this).fancybox({
				'content' : $("#embed-code").html(),
				'scrolling':'no',
				'titleShow':false,
				'autoScale':false
			});
			$.fancybox.resize;
		});
	}
		
});
