$j(document).ready(function()
{
	// ================
	// = Sharing page =
	// ================
	jQuery.fn.delay = function(time,func){
		return this.each(function(){
			setTimeout(func,time);
		});
	};

	$j('#sharing-houseshare_points').jCarouselLite({
		vertical: true,
		visible: 1,
		auto: 4000,
		speed: 1000,
		pauseOnHover: true
	});

	$j('#sharing-flatshare_points').delay(2000, function(){
		$j('#sharing-flatshare_points').jCarouselLite({
			vertical: true,
			visible: 1,
			auto: 4000,
			speed: 1000
		});

	});

	$j("#share_benefits").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		circular: true,
		visible: 1,
		scroll: 1,
		start: 0,
		auto: 8000,
		speed: 0,
		btnGo:
		["#benefit1", "#benefit2", "#benefit3", "#benefit4", "#benefit5", "#benefit6", "#benefit7"],
		beforeStart: function(a) {
			$j(a).parent().fadeTo(1000, 0);
			var bg = $j(a).find('img').attr('src');
		},
		afterEnd: function(a) {
			$j(a).parent().fadeTo(1000, 1);

			$j('.sb_button').removeClass('on')
			$j('#'+$j(a).attr('rel')).addClass('on')

		}
	});


	//This adds the click event to your object and tells it where to go
	$j('.clickable').click(function(e) {
		window.location=$j(this).find("a").attr("href");
	});

	//call the jQuery Hover Over and Out
	$j('.hoverable').hover(over, out);
	//Tell the browser to change the background when hovered over
	function over(event) {
		$j(this).css("cursor", "pointer");
		$j(this).addClass('hover_over')
	}
	//tell the browser to change the background to nothing 
	//when going outside the object area
	function out(event) {
		$j(this).css("background", "");
		$j(this).removeClass('hover_over')
	}

	$j('#login_email_input').focus(function() {
		if ($j('#login_email_input').val() == 'E-Mail') {
			$j('#login_email_input').val('');
		};
	})


	$j('#login_password_input').focus(function() {
		if ($j('#login_password_input').val() == 'Password') {
			$j('#login_password_input').val('');
		};
	})

	$j('#email_access').change(function() {
		$j.ajax({
			type: "POST",
			url: "/ajaxCalls/setUserShareEmail.php",
			data: "email_access="+$j('#email_access').val(),
			success: function(msg){
				//alert( "Data Saved: " + msg );
			}
		});


	})

	// auto log out if leave
	// Change this URL to be that of your own website  

	var base_url_length = base_url.length;  

	$j("a").click(function() {  
		var link_location = $j(this).attr('href');  
		// if the link clicked is external  
		if (link_location.indexOf("http://")!=-1 && link_location.substr(0, base_url_length)!=base_url) {  
			$j.ajax({
				type: "POST",
				url: "/ajaxCalls/logout.php",
				data: '',
				success: function(msg){
					//alert( "Data Saved: " + msg );
				}
			});
		}  
	});  


	$j("#value_form").submit(function(e) {  
		e.preventDefault();

		$j.ajax({
			type: "POST",
			url: "/ajaxCalls/getRentValuation.php",
			data: ({
				your_name : $j(this).find('#your_name').val(),
				email : $j(this).find('#email').val(),
				property : $j(this).find('#property').val(),
				bedrooms : $j(this).find('#bedrooms').val(),
				othertext : $j(this).find('#othertext').val(),
				telephone : $j(this).find('#telephone').val()
				}),
				success: function(return_data){
					var return_data = $j.parseJSON(return_data)
					if (return_data.status == 'done') {
						if ($j('#othertext').val() != '') {
								show_valuation_thanks(return_data)
						} else {
							show_valuation(return_data)
						}
						
					} else {
						valuation_error(return_data)
					}
				}
			}); 
			//return false;
		});

		function show_valuation(data) {
			$j('#valuation_answer p.price').html(data.rate);
			$j('#valuation_request').hide()
			$j('#valuation_answer').show()
			$j('#valuation_thanks').hide()
		}
		
		function show_valuation_thanks(data) {
			$j('#valuation_request').hide()
			$j('#valuation_answer').hide()
			$j('#valuation_thanks').show()
		}
		

		function valuation_error (return_data) {
			if (return_data.your_name == 0) {
				$j("#value_form label#your_name-label").addClass('required')
			} else {
				$j("#value_form label#your_name-label").removeClass('required')
			};
			if (return_data.email == 0) {
				$j("#value_form label#email-label").addClass('required')
			} else {
				$j("#value_form label#email-label").removeClass('required')
			}
			if (return_data.property == 0) {
				$j("#value_form #property-fieldset").addClass('required')
			} else if (return_data.property == '99999') {
				$j("#value_form #othertext-fieldset").addClass('required')
			} else {
				$j("#value_form #property-fieldset").removeClass('required')
			}
			
			if (return_data.othertext == 0) {
				$j("#value_form #othertext-fieldset").addClass('required')
			} else {
				$j("#value_form #othertext-fieldset").removeClass('required')
			}
			
			if (return_data.telephone == 0) {
				$j("#value_form label#telephone-label").addClass('required')
			} else {
				$j("#value_form label#telephone-label").removeClass('required')
			}
		}

		$j("#property").change(function() {
			if ($j(this).val() == '99999') {
				$j('#othertext-fieldset').show();
				$j('#property-fieldset').hide()
			} else {
				$j('#othertext-fieldset').hide()
				$j('#property-fieldset').show()
			}
		})

	})
	
	
