function mainmenu(){
$("ul#nav ul ").css({display: "none"}); // Opera Fix
$("ul#nav li").hover(function(){
$(this).addClass('active');
$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(400);
},function(){
$(this).removeClass('active');
$(this).find('ul:first').css({visibility: "hidden"});
});
}


var x = 1;
var autoslide = 1;
var stopautoslide = 0;

function slide() {
if(stopautoslide == 0) {
	slideright();
}
}

function slideleft() {
	x--;
		if(x == 0 || x == 5) {
			x = 4;
			$('#featured .slide').fadeOut(1000);
			$('li.text').hide();
			$('#featured .slide p').animate({"right":"-500px"});
			slideclass = "#featured .slide"+x;
			textclass = "#featured .text"+x;
			pclass = "#featured .slide"+x+" p";
			$(slideclass).fadeIn(1000);
			$(textclass).fadeIn(1000);
			$(pclass).animate({"right":"0px"},500);
		} else {
			$('li.text').hide();
			$('#featured .slide p').animate({"right":"-500px"});
			$('#featured .slide').fadeOut(1000);
			slideclass = "#featured .slide"+x;
			textclass = "#featured .text"+x;
			pclass = "#featured .slide"+x+" p";
			$(slideclass).fadeIn(1000);
			$(textclass).fadeIn(1000);
			$(pclass).animate({"right":"0px"},500);
		}
}

function slideright() {
	x++;
		if(x == 0 || x == 5) {
			x = 1;
			$('#featured .slide').fadeOut(1000);
			$('li.text').hide();
			$('#featured .slide p').animate({"right":"-500px"});
			slideclass = "#featured .slide"+x;
			textclass = "#featured .text"+x;
			pclass = "#featured .slide"+x+" p";
			$(slideclass).fadeIn(1000);
			$(textclass).fadeIn(1000);
			$(pclass).animate({"right":"0px"},500);
		} else {
			$('li.text').hide();
			$('#featured .slide p').animate({"right":"-500px"});
			$('#featured .slide').fadeOut(1000);
			slideclass = "#featured .slide"+x;
			textclass = "#featured .text"+x;
			pclass = "#featured .slide"+x+" p";
			$(slideclass).fadeIn(1000);
			$(textclass).fadeIn(1000);
			$(pclass).animate({"right":"0px"},500);
		}
}


$(function() {
	mainmenu();
	
	setInterval("slide()",5000);
	
	$('ul.socialicons li a').hover(function() {
		var thetext = $(this).find('img').attr('alt');
		$('p.socialtooltip').html(thetext);
	}, function() {
		$('p.socialtooltip').html("&nbsp;");
	});
	
	$('.arrowright').click(function() {
		slideright();
		stopautoslide = 1;
		return false;
	});
	
	$('.arrowleft').click(function() {
		slideleft();
		stopautoslide = 1;
		return false;
	});
	
	// SIDEBAR TABS
	$('#tabs .tabs ul li').click(function() {
		$('#tabs .panel').hide();
		$('#tabs .tabs ul li').removeClass('active');
	});
	$('#tabs .tabs ul li:eq(0)').click(function() {
		$('#tabs .panel1').fadeIn();
		$(this).addClass('active');	
	});
	$('#tabs .tabs ul li:eq(1)').click(function() {
		$('#tabs .panel2').fadeIn();
		$(this).addClass('active');
	});
	$('#tabs .tabs ul li:eq(2)').click(function() {
		$('#tabs .panel3').fadeIn();
		$(this).addClass('active');
	});
	
	$('.widgetcontacthide form').hide();
	
	$('.widgetcontacthide h4, .contactformopen').click(function() {
		$('.widgetcontacthide form').slideDown();
		return false;
	});
		$('ul.pagelist li:first').addClass('heading');
	$('.widgetblog ul.pagelist li:first').removeClass('heading');
	$('.widgetblog h4').addClass('heading');


	$('#name').focus(function() {
		if($(this).attr("value") == "Just tell us your name") { $(this).attr("value",""); }
	});
	$('#name').blur(function() {
		if($(this).attr("value") == "") { $(this).attr("value","Just tell us your name"); }
	});
	
	$('#email').focus(function() {
		if($(this).attr("value") == "your email address") { $(this).attr("value",""); }
	});
	$('#email').blur(function() {
		if($(this).attr("value") == "") { $(this).attr("value","your email address"); }
	});
	
	$('#phone').focus(function() {
		if($(this).attr("value") == "your phone No.") { $(this).attr("value",""); }
	});
	$('#phone').blur(function() {
		if($(this).attr("value") == "") { $(this).attr("value","your phone No."); }
	});
	
	$('#address').focus(function() {
		if($(this).attr("value") == "your address and postcode") { $(this).attr("value",""); }
	});
	$('#address').blur(function() {
		if($(this).attr("value") == "") { $(this).attr("value","your address and postcode"); }
	});
	
	$('#postcode').focus(function() {
		if($(this).attr("value") == "& your postcode") { $(this).attr("value",""); }
	});
	$('#postcode').blur(function() {
		if($(this).attr("value") == "") { $(this).attr("value","& your postcode"); }
	});
	
	$('#subfooter #s').focus(function() {
		if($(this).attr("value") == "Search our website") { $(this).attr("value",""); }
	});
	$('#subfooter #s').blur(function() {
		if($(this).attr("value") == "") { $(this).attr("value","Search our website"); }
	});
	
	$('.contactform #submit').remove();
    $('.contactform').append('<input type="button" name="submit" id="submit" value="Send" />');
    	
    $('.contactform #submit').click(function() {
        var name = $('.contactform input#name').val();
        var email = $('.contactform input#email').val();
        var phone = $('.contactform input#phone').val();
        var address = $('.contactform textarea#address').val();
        var loft = $('#loft').attr('checked');
        var garage = $('#garage').attr('checked');
        $.ajax({
            type: 'post',
        	url: 'http://www.restyleloft.com/wp-content/themes/restyle/scripts/sendMessage.php',
        	data: 'name=' + name + '&email=' + email + '&phone=' + phone + '&address=' + address + '&loft=' + loft + '&garage=' + garage,

            success: function(results) {
            $('ul.response').show();
            $('ul.response').html(results);
            }
        }); 
    });

	
	
});
