// variables
// ----------------------------------------------------------------
var browserHeight;


// functions
// ----------------------------------------------------------------
function showForm(){
	hideForm();
	$(this).next().css('display', 'block').stop().animate({opacity:1},{duration:400, easing:'easeInOutQuint'});
	$(this).addClass('active');
}

function hideForm(){
	$('.form-popup').stop().animate({opacity:0},{duration:400, easing:'easeInOutQuint', complete: function(){ $(this).css('display', 'none') } });
	$('section#newsletter-signup a, section#free-valuation a').removeClass('active');
}

function checkBrowserHeight(){
	if (browserHeight < 770) {
		$('section#free-valuation .form-popup').stop().animate({top: '-120px'},{duration:800, easing: 'easeInOutQuint'});
	} else {
		$('section#free-valuation .form-popup').stop().animate({top: '0px'},{duration:800, easing: 'easeInOutQuint'});	
	}
}

function checkPage(){
	if ( $('body.buy').length ) {
		//$('a#nav_buy').css('color', '#00abd2');
		$('a#nav_buy').parent().css('height', 'auto');		
		$('#buy_filter').css('display', 'block');
		
		if ( $('body.buy-property-details').length ) {
			$('#buy_filter_form').find('select').attr('disabled', 'true');
			$('#buy_filter_form').css('opacity', 0.25);
		}
	}
	
	if ( $('body.rent').length ) {
		//$('a#nav_rent').css('color', '#00abd2');
		$('a#nav_rent').parent().css('height', 'auto');
		$('#rent_filter').css('display', 'block');
		
		if ( $('body.rent-property-details').length ) {
			$('#rent_filter_form').find('select').attr('disabled', 'true');
			$('#rent_filter_form').css('opacity', 0.25);
		}
	}
}

// document ready
// ----------------------------------------------------------------
$(function(){
	
	$('.form-popup label').inFieldLabels({
		fadeOpacity: 0.5,
		fadeDuration: 300
	});
	
	browserHeight = $(window).height();
	checkBrowserHeight();
	
	$('.popupform-init').click(function(e) { e.preventDefault() });
	$('.popupform-init').bind('click', showForm);	
	$('.close-form').bind('click', hideForm);
	
	checkPage();
	
    $('#newsletter-form').submit(function (e) {
        e.preventDefault();
        $.getJSON(
        this.action + "?callback=?",
        $(this).serialize(),
        function (data) {
            if (data.Status === 400) {
                alert("Error: " + data.Message);                
            } else { // 200
                alert("Success: " + data.Message);
                hideForm();
            }
        });
    });
})

// document resize
// ----------------------------------------------------------------
$(window).resize(function(){
	browserHeight = $(window).height();
	checkBrowserHeight();
})

