$(document).ready(function(){

	$("ul.top-navi li a").hover(function() { //When trigger is clicked...

		$(this).addClass("subhover activeLink"); //On hover over, add class "subhover"

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
			$("a", this).removeClass("activeLink"); //On hover out, remove class "subhover"
		});

		//Following events are applied to the trigger (Hover events for the trigger)
	},function() { //When trigger is clicked...

		$(this).removeClass("subhover");

		//Following events are applied to the trigger (Hover events for the trigger)
	});

	// activates the lightbox page, if you are using a dark color scheme use another theme parameter
	punchbox("a[rel^='prettyPhoto'], a[rel^='lightbox'], a[rel^='punchbox']", true);

	function punchbox($elements, autolink)
	{

		// TODO - &embedded=true?iframe=true&width=900&height=600

		if(autolink)
		{
			jQuery('a[href*=pdf], a[href$=jpg], a[href$=png], a[href$=gif], a[href$=jpeg], a[href$=.mov] , a[href$=.swf] , a[href*=vimeo.com] , a[href*=youtube.com]').contents("img").parent().each(function()
			{
				if(!jQuery(this).attr('rel') != undefined && !jQuery(this).attr('rel') != '' && !jQuery(this).hasClass('noLightbox'))
				{
					jQuery(this).attr('rel','lightbox[auto_group]')
				}
			});
		}

		jQuery($elements).prettyPhoto({
				autoplay: false,
				wmode: 'transparent',
				theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
				show_title: false
				});

		jQuery($elements).each(function()
		{
			var $image = jQuery(this).contents("img");
			$newclass = 'lightbox-video';

			if(jQuery(this).attr('href').match(/(jpg|gif|jpeg|png|tif|pdf)/)) $newclass = 'lightbox-image';

			if ($image.length > 0)
			{
				if(jQuery.browser.msie &&  jQuery.browser.version < 7) jQuery(this).addClass('ie6-lightbox');

				var $bg = jQuery("<span class='"+$newclass+" '></span>").appendTo(jQuery(this));

				jQuery(this).bind('mouseenter', function()
				{
					$height = $image.height();
					$width = $image.width();
					$pos =  $image.position();
					$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
				});
			}
		});

		jQuery($elements).contents("img").hover(function()
		{
			jQuery(this).stop().animate({opacity:0.5},400);
		},
		function()
		{
			jQuery(this).stop().animate({opacity:1},400);
		});
	}

	/* form filtering */
	$("input:[name='po']").change(function() {

		var lettingsMin = ' \
			<option value="" > Min: &pound;</option> \
			<option value="400">Min: &pound;400 pcm</option> \
			<option value="600">Min: &pound;600 pcm</option> \
			<option value="750">Min: &pound;750 pcm</option> \
			<option value="900">Min: &pound;900 pcm</option> \
			<option value="1000">Min: &pound;1000 pcm</option> \
			<option value="1250">Min: &pound;1250 pcm</option> \
			<option value="1500">Min: &pound;1500 pcm</option> \
			<option value="1750">Min: &pound;1750 pcm</option> \
			<option value="2000">Min: &pound;2000 pcm</option> \
			<option value="2500">Min: &pound;2500 pcm</option> \
		';
		var lettingsMax = ' \
			<option value="" > Max: &pound;</option> \
			<option value="400">Max: &pound;400</option> \
			<option value="600">Max: &pound;600</option> \
			<option value="750">Max: &pound;750</option> \
			<option value="900">Max: &pound;900</option> \
			<option value="1000">Max: &pound;1000</option> \
			<option value="1250">Max: &pound;1250</option> \
			<option value="1500">Max: &pound;1500</option> \
			<option value="1750">Max: &pound;1750</option> \
			<option value="2000">Max: &pound;2000</option> \
			<option value="2500">Max: &pound;2500</option> \
		';
		var salesMin = ' \
			<option value="0" selected="selected">Min: &pound;</option> \
			<option value="75000">Min: &pound;75,000</option> \
			<option value="100000">Min: &pound;100,000</option> \
			<option value="125000">Min: &pound;125,000</option> \
			<option value="150000">Min: &pound;150,000</option> \
			<option value="175000">Min: &pound;175,000</option>\
			<option value="200000">Min: &pound;200,000</option> \
			<option value="300000">Min: &pound;300,000</option> \
			<option value="400000">Min: &pound;400,000</option> \
			<option value="500000">Min: &pound;500,000</option> \
			<option value="600000">Min: &pound;600,000</option> \
			<option value="700000">Min: &pound;700,000</option> \
			<option value="800000">Min: &pound;800,000</option> \
			<option value="900000">Min: &pound;900,000</option> \
			<option value="1000000">Min: &pound;1,000,000</option> \
			<option value="2000000">Min: &pound;2,000,000</option> \
			<option value="5000000">Min: &pound;5,000,000</option> \
		';
		var salesMax = ' \
			<option value="" selected="selected">Max: &pound;</option> \
			<option value="100000">Max: &pound;100,000</option> \
			<option value="125000">Max: &pound;125,000</option> \
			<option value="150000">Max: &pound;150,000</option> \
			<option value="175000">Max: &pound;175,000</option> \
			<option value="200000">Max: &pound;200,000</option> \
			<option value="300000">Max: &pound;300,000</option> \
			<option value="400000">Max: &pound;400,000</option> \
			<option value="500000">Max: &pound;500,000</option> \
			<option value="600000">Max: &pound;600,000</option> \
			<option value="700000">Max: &pound;700,000</option> \
			<option value="800000">Max: &pound;800,000</option> \
			<option value="900000">Max: &pound;900,000</option> \
			<option value="1000000">Max: &pound;1,000,000</option> \
			<option value="2000000">Max: &pound;2,000,000</option> \
			<option value="5000000">Max: &pound;5,000,000</option> \
		';

		if ($("input:[name='po']:checked").val() == 'R')
		{
			$('select[name=pbr]').html(lettingBranches);
			$('#property-min-price').html(lettingsMin);
			$('#property-max-price').html(lettingsMax);
		}
		else
		{
			$('select[name=pbr]').html(saleBranches);
			$('#property-min-price').html(salesMin);
			$('#property-max-price').html(salesMax);
		}
	});

	if ($('form#feedback').length != 0)
	{
		$('#step2, #step3, #step4, #step5').css('display', 'none');

		$('#step1 input[name=next]').click(function() {

			if (
				($('#step1 select[name=office]').val() == '' || $('#step1 select[name=office]').val() == 'Please choose')
				|| $('#step1 input[name=Responsiveness]:checked').val() === undefined
				|| $('#step1 input[name=Professionalism]:checked').val() === undefined
				|| $('#step1 input[name=OfficeEnvironment]:checked').val() === undefined
				|| $('#step1 input[name=Understanding]:checked').val() === undefined
				|| ($('#step1 textarea[name=other]').val() == '' || $('#step1 textarea[name=other]').val() == 'Please tell us why you feel that way...')
			   )
			{
				alert('Please fill in all fields');
				return false;
			}

			$('#step1').hide();
			$('#step2').show();
			$('#progress-bar #step').html('2/5');
			$('#progress-bar #progress').html('25%');
			$('#progress-inner div').css('width', '25%');
		});

		$('#step2 input[name=next]').click(function() {

			if (
				$('#step2 input[name=Literature]:checked').val() === undefined
				|| $('#step2 input[name=JPBWebsite]:checked').val() === undefined
				|| $('#step2 input[name=Newspaper]:checked').val() === undefined
				|| $('#step2 input[name=Rightmove]:checked').val() === undefined
				|| $('#step2 input[name=HandDelivered]:checked').val() === undefined
				|| ($('#step2 textarea[name=AdditionalComments]').val() == '' || $('#step2 textarea[name=AdditionalComments]').val() == 'If you have any additional comments about how we can improve our products & service, please fill them in here...')
				|| ($('#step2 select[name=whycontact]').val() == '' || $('#step2 select[name=whycontact]').val() == 'Please choose')
			   )
			{
				alert('Please fill in all fields');
				return false;
			}

			$('#step2').hide();
			$('#step3').show();
			$('#progress-bar #step').html('3/5');
			$('#progress-bar #progress').html('50%');
			$('#progress-inner div').css('width', '50%');
		});

		$('#step3 input[name=next]').click(function() {

			if (
				$('#step3 input[name=Valuation]:checked').val() === undefined
				|| $('#step3 input[name=PriorContact]:checked').val() === undefined
				|| $('#step3 input[name=ViewingFeedback]:checked').val() === undefined
				|| $('#step3 input[name=Negotiation]:checked').val() === undefined
				|| $('#step3 input[name=Handover]:checked').val() === undefined
				|| $('#step3 input[name=Advice]:checked').val() === undefined
				|| ($('#step3 textarea[name=Other2]').val() == '' || $('#step3 textarea[name=Other2]').val() == 'Please tell us why you feel that way....')
				|| $('#step3 input[name=OverallSatisfaction]:checked').val() === undefined
				|| $('#step3 input[name=Recommend]:checked').val() === undefined
			   )
			{
				alert('Please fill in all fields');
				return false;
			}

			$('#step3').hide();
			$('#step4').show();
			$('#progress-bar #step').html('4/5');
			$('#progress-bar #progress').html('75%');
			$('#progress-inner div').css('width', '75%');
		});

		$('#step4 input[name=next]').click(function() {

			if (
				$('#step4 input[name=Name]').val() == ''
				|| $('#step4 input[name=From]').val() == ''
				|| $('#step4 input[name=To]').val() == ''
			   )
			{
				alert('Please fill in all fields');
				return false;
			}

			$.ajax({
				type: "POST",
				url: "processfeedback.php",
				data: $('form#feedback').serialize(),
				success: function() {
					$('#step4').hide();
					$('#step5').show();
					$('#progress-bar #step').html('5/5');
					$('#progress-bar #progress').html('100%');
					$('#progress-inner div').css('width', '100%');
				}
			});

		});
	}

});
