$("#news-container li").hide();

var interval_speed = 6;
var type_speed = 40;

// Using window.onload as we dont want the effects to start untill
// the page is fully displaying
window.onload = function() {
	var index = 0;
	var element_count = $("#news-container li").length;

	displaySnippet(index);
	index = 1;
	var prev = 0;

	setInterval(function(){
		if(index == element_count) {
			index = 0;
			prev = element_count - 1;
		}

		$("#news-container li").eq(prev).fadeOut("fast", function(){
			displaySnippet(index);
			prev = index;
			index += 1;
		});
	}, interval_speed * 1000);
}

function displaySnippet(i) {
	var element = $("#news-container li").eq(i);
	var str = element.html();
	element.html("").show();
	var l = str.length;
	var c = 0;
	var interval = setInterval(function(){
		if(c <= l) {
			var nextCharacter = str.charAt(c);
			var range = 0;
			// a element
			if(nextCharacter == "<") {
				if(str.charAt(c + 1).toLowerCase() == "a") {
					range = c + str.indexOf(">", c);
				} else if(str.charAt(c + 1) == "/") {
					range = 4;
				}
			}
			var display = str.substring(0, c + range);
			if(c < l) display += "_";
			element.html(display);
			c += range + 1;
		} else {
			clearInterval(interval);
		}
	}, type_speed);
}




/*
 * Callback form
 */

if($("select#callback_reason").attr("value") == "repairs") $("div#formsub").html("Please phone 01245 426 163");

$(document).ready(function() {

	$("select#callback_reason").change(function(e) {
		check_callback_reason(this);
	});
	$("select#callback_reason").keyup(function(e) {
		check_callback_reason(this);
	});

});

function check_callback_reason(obj)
{
	if($(obj).attr("value") == "repairs")
	{
		$("div#formsub").html("Please phone 01245 426 163");
		$("div#formsub").parent().find("> p").hide();
		$("div#callback input[type=text]").each(function(i) {
			$(this).hide();
		});
		$("div#callback label").each(function(i) {
			if(i > 0) $(this).hide();
		});
		$("#captcha-code").hide();
	}
	else {
		$("div#formsub").html('<input type="submit" name="callback_sub" id="callback_sub" value="Request callback" />');
		$("div#formsub").parent().find("> p").show();
		$("div#callback input[type=text]").each(function(i) {
			$(this).show();
		});
		$("div#callback label").each(function(i) {
			$(this).show();
		});
		$("#captcha-code").show();
	}
}

if($("select#callback_reason").attr("value") == "repairs") $("div#formsub").html("Please phone 01245 426 163");

$(document).ready(function() {

	$("select#callback_reason").change(function(e) {
		check_callback_reason(this);
	});
	$("select#callback_reason").keyup(function(e) {
		check_callback_reason(this);
	});

});

function check_callback_reason(obj)
{
	if($(obj).attr("value") == "repairs")
	{
		$("div#formsub").html("Please phone 01245 426 163");
		$("div#formsub").parent().find("> p").hide();
		$("div#formsub").parent().css("margin-top","45px");
		$("div#callback input[type=text]").each(function(i) {
			$(this).hide();
		});
		$("div#callback label").each(function(i) {
			if(i > 0) $(this).hide();
		});
		$("#captcha-code").hide();
	}
	else {
		$("div#formsub").html('<input type="submit" name="callback_sub" id="callback_sub" value="Request callback" />');
		$("div#formsub").parent().find("> p").show();
		$("div#formsub").parent().css("margin-top","70px");
		$("div#callback input[type=text]").each(function(i) {
			$(this).show();
		});
		$("div#callback label").each(function(i) {
			$(this).show();
		});
		$("#captcha-code").show();
	}
}