$(document).ready(function() {

    // show we have javascript
    $('body').addClass('js');

    $('#homeSpotlights img, .standardListing img').bind("contextmenu", function(e) {
        return false;
    });

    $('#home-news a').hover(function() {
        $('#home-news a').removeClass('on');
        $(this).addClass('on');
    });

    $(".banners h2 a").click(function() {
        $(this).blur();
        if ($(this).hasClass("open")) {
            // close
            clicked = $(this);
            $(this).parent().parent().animate({
                height: "50px"
            }, 500, function() {
                if (clicked.parent().parent().hasClass("neighbourhood")) {
                    $(".under-tabs").removeClass("under-tabs-on");
                }
            });
            $(this).removeClass("open");
            $(this).parent().parent().removeClass("open");
        } else {
            // open
            if ($(this).parent().parent().hasClass("neighbourhood")) {
                $(".under-tabs").addClass("under-tabs-on");
            }
            $(this).parent().parent().animate({
                height: "370px"
            }, 500);
            $(this).addClass("open");
            $(this).parent().parent().addClass("open");
        }
        return false;
    });

	$('#accordianListing h2:eq(0)').addClass('on');
	$('#accordianListing ul:eq(0)').show();
	$('#accordianListing h2').click(function(){
		if( $(this).hasClass('on') ) { } else {
			$('#accordianListing h2').removeClass('on');
			$('#accordianListing ul').slideUp();
			$(this).addClass('on');
			$(this).next().slideDown();
		};
	});

	$('blockquote').wrapInner('<span class="quote" />');

    /*-------------------
    ---form validation---
    -------------------*/
    $('.genForm li.required').each(function() {
        $(this).find('input, textarea').blur(function() {
            if ($(this).attr('value') == '') {
                $(this).parent().find('strong.error').remove();
                $(this).parent().removeClass('success');
                var fieldName = $(this).parent().find('label').text();
                fieldName = fieldName.replace(/\* required/, '');
                $(this).parent().append('<strong class="error">' + fieldName + ' is a required field</strong>');
                $(this).parent().addClass('error');
            } else {
                $(this).parent().find('strong.error').remove();
                $(this).parent().addClass('success');
                $(this).parent().removeClass('error');
            };
        });
    });
	$('.genForm li.email').each(function() {
		$(this).find('input').blur(function() {
			var validEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			var enteredEmail = $(this).attr('value');
			if (validEmail.test(enteredEmail)) {
                $(this).parent().find('strong.error').remove();
                $(this).parent().addClass('success');
                $(this).parent().removeClass('error');
            } else {
                $(this).parent().find('strong.error').remove();
                $(this).parent().removeClass('success');
                $(this).parent().append('<strong class="error">Please enter a valid email address</strong>');
                $(this).parent().addClass('error');
            };
        });
    });

    if (jQuery.browser.msie && jQuery.browser.version == 6) { } else {
		if($('#mapHome').length) {
			var arr = ['0 0', '0 -300px', '0 -600px', '0 -900px', '0 -1200px', '0 -1500px', '0 -1800px', '-370px 0', '-370px -300px', '-370px -600px', '-370px -900px', '-370px -1200px', '-370px -1500px', '-370px -1800px', '-740px 0', '-740px -300px', '-740px -600px', '-740px -900px', '-740px -1200px'];
		} else if ($('#mapCheshire').length) {
            var arr = ['0 0', '0 -328px', '0 -656px', '0 -984px', '0 -1312px', '0 -1640px', '-461px 0', '-461px -328px', '-461px -656px', '-461px -984px', '-461px -1312px', '-461px -1640px', '-922px 0', '-922px -328px', '-922px -656px', '-922px -984px', '-922px -1312px', '-922px -1640px', '-1383px 0'];
        } else if ($('#mapNPUCongleton').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUCrewe').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUNantwich').length) {
            var arr = ['0 0', '0 -328px'];
        } else if ($('#mapNPUMacclesfield').length) {
            var arr = ['0 0', '0 -328px', '0 -656px', '0 -984px', '0 -1312px', '0 -1640px', '-461px 0'];
        } else if ($('#mapNPUKnutsford').length) {
            var arr = ['0 0', '0 -328px'];
        } else if ($('#mapNPUWilmslow').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUWarringtonCentral').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUWarringtonWest').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUWarringtonEast').length) {
            var arr = ['0 0', '0 -328px', '0 -656px', '0 -984px'];
        } else if ($('#mapNPUWarringtonSouth').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPURuncorn').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUWidnes').length) {
            var arr = ['0 0', '0 -328px', '0 -656px', '0 -984px'];
        } else if ($('#mapNPUChesterOuter').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUWesternRural').length) {
            var arr = ['0 0', '0 -328px', '0 -656px'];
        } else if ($('#mapNPUEllesmerePortAndNeston').length) {
            var arr = ['0 0', '0 -328px'];
        } else {
			var arr = ['0 0'];
		}
        for (var i = 0; i < arr.length; i++) {
            (function(i) {
                $('#areaMap' + (i + 1)).mouseover(function() {
                    $('.map img').css('background-position', arr[i]);
                }).mouseout(function() {
                    $('.map img').css('background-position', '0 -9999px');
                });
            })(i);
        }
    }

    $('.map area').tooltip({
        showURL: false
    });

});
