jQuery(document).ready(function($){
	
	$.ajaxSetup({
		"type": "post",
		"dataType": "json",
		"error": function() {
			alert("There was a problem processing your request");
		}	
	});
	
	$(".checky label")
		.click(function(){
			$(this).siblings("input[type='radio']").click();
			$(this).siblings("input[type='checkbox']").click();
		})
		.css("cursor", "pointer");

	$("form[name=main-search]").submit(function() {
		var form 	= $(this);
		var url 	= $(this).attr('action') + '?';
		
		var query 	= $("input[type=text][name=query]", form).val();
		var type 	= $("input[type=radio][name=type]:checked", form).val();
		
		if (query === 'Enter Location Here' || query === 'Entrer la ville, la Province ou l\'Etat ici') {
			query = '';
		}
		
		url += 'city=' + query + '&radius_city=25';
		
		if (type === 'investment') {
			url += '&plex_type[]=duplex&plex_type[]=triplex&plex_type[]=quadruplex&plex_type[]=quintuplex&plex_type[]=sixplex';
		} else {
			url += '&plex_type[]=commercial';
		}
                
                url += '&order=mrb_low';
		
		location.href = url;
		return false;
	});
	
	$("a#header-login").click(function() {
		var popup = $("#header-login-popup");
		if (popup.is(":visible")) {
			popup.slideUp(100);
		} else {
			$("form input", popup).first().focus();
			popup.slideDown(100);
		}
		return false;
	});
	
	$("#header-login-popup form").submit(function(){
				
		$.ajax({
			"url": "/login_ajax",
			"data": {
				'email': $("#header-login-popup form input[name=email]").val(),
				'password': $("#header-login-popup form input[name=password]").val()
			},
			"success": function(resp) {
				if (resp && resp.status === 'good') {
					location.href = location.href;
				} else {
					alert("Incorrect login/password");
				}
			}
		});
		
		return false;
		
	});
	
	$("a#header-create-account").click(function(){
		
	});
	
	
	
	if (bsn && bsn.AutoSuggest) {
		if ($("#main-search")) {
			var as_json = new bsn.AutoSuggest(
				'main-search', 
				{
					script: "/autosuggest_v2.1.3/toto.php?json=true&limit=6&",
					varname: "input",
					timeout: 3500,
					json: true,
					shownoresults: false,
					maxresults: 6,
					callback: function (obj) { 
						//document.getElementById('testid').value = obj.id; 
					}
				}
			);
		}
		
		if ($("#location-search")) {
			var as_json = new bsn.AutoSuggest(
				'location-search', 
				{
					script: "/autosuggest_v2.1.3/toto.php?json=true&limit=6&",
					varname: "input",
					timeout: 3500,
					json: true,
					shownoresults: false,
					maxresults: 6,
					callback: function (obj) { 
						//document.getElementById('testid').value = obj.id; 
					}
				}
			);
		}
		
		if ($("#location-search2")) {
			var as_json = new bsn.AutoSuggest(
				'location-search2', 
				{
					script: "/autosuggest_v2.1.3/toto.php?json=true&limit=6&",
					varname: "input",
					timeout: 3500,
					json: true,
					shownoresults: false,
					maxresults: 6,
					callback: function (obj) { 
						//document.getElementById('testid').value = obj.id; 
					}
				}
			);
		}
	}
	
	
	
 	
	
	/*
	$("input[type='text'][name='search']")
		.each(function(){
		
			var changed = false;
			$(this)
			  	.addClass('quiet')
			  	.val('Search...')
			  	.bind('change', function() { 
			  		if (! changed) { 
			  			changed = true; 
			  		} 
			  	})
			  	.bind('focus', function() { 
			  		$(this).removeClass('quiet'); 
			  		if (! changed) { 
			  			$(this).val(''); 
			  		}  
			  	})
			  	.bind('blur', function() { 
			  		if (! changed) { 
			  			$(this).val('Search...'); 
			  			$(this).addClass('quiet'); 
			  		} 
			  	} 
			);
		
		});
	*/
	
	
	popup = new (function(){
		var popup = this,
			popped,
			dim,
			body = $("body");
		
		this.signup = function() {
	 		popped = $("<div id='popped' />")
	 			.css({
	 				"left": (body.width() / 2) - 250,
	 				"top": "200px",
	 				"width": "500px",
	 				"height": "360px"
	 			})
	 			.html("Loading...")
	 			.appendTo(body)
	 			.fadeIn('fast');
	 		
	 		dim = $("<div id='dim' />")
	 			.appendTo(body)
	 			.fadeIn()
	 			.click(function(){
	 				popup.hide();
	 			});
	 		
	 		$.ajax({
				"url": SITE_LANG === 'fr_FR'? "/popup_login_fr" : "/popup_login",
				"dataType": "html",
				"success": function(resp) {
					popped.html(resp);
				}
			});
	 	};
		
		this.resize = function(width, height) {
			opts = {};
			
			if (width > 0) {
				opts["width"] = width + "px";
			}
			if (height > 0) {
				opts["height"] = height + "px";
			}
			
			opts["left"] =  (body.width() / 2) - 250;
		
			popped.animate(opts, 200);
		};
		
		this.hide = function() {
			if (popped) {
				popped.fadeOut(function() {
					$(this).remove();
				});
			}
			
			if (dim) {
				dim.fadeOut(function() {
					$(this).remove();
				});
			}
		};
	
	
	});
	
});
























/*
* jQuery Form Example Plugin 1.5.1
* Populate form inputs with example text that disappears on focus.
*
* e.g.
* $('input#name').example('Bob Smith');
* $('input[@title]').example(function() {
* return $(this).attr('title');
* });
* $('textarea#message').example('Type your message here', {
* className: 'example_text'
* });
*
* Copyright (c) Paul Mucur (http://mudge.name), 2007-2011.
* Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt)
* licenses.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
(function(a){"use strict",a.fn.example=function(b,c){var d=a.isFunction(b),e=a.extend({},c,{example:b});return this.each(function(){var b,c,f=a(this);a.metadata?b=a.extend({},a.fn.example.defaults,f.metadata(),e):b=a.extend({},a.fn.example.defaults,e),c=function(){a(this).find("."+b.className).val("")},a.fn.example.boundClassNames[b.className]||(a(window).bind("unload.example",function(){a("."+b.className).val("")}),a.fn.on?a("body").on("submit.example","form",c):a.fn.delegate?a("body").delegate("form","submit.example",c):a.fn.live?a("form").live("submit.example",c):a("form").bind("submit.example",c),a.fn.example.boundClassNames[b.className]=!0),f.val()!==this.defaultValue&&(d||f.val()===b.example)&&f.val(this.defaultValue),f.val()===""&&this!==document.activeElement&&f.addClass(b.className).val(d?b.example.call(this):b.example),f.bind("focus.example",function(){a(this).is("."+b.className)&&a(this).val("").removeClass(b.className)}).bind("change.example",function(){a(this).is("."+b.className)&&a(this).removeClass(b.className)}).bind("blur.example",function(){a(this).val()===""&&a(this).addClass(b.className).val(d?b.example.call(this):b.example)})})},a.fn.example.defaults={className:"example"},a.fn.example.boundClassNames=[]})(jQuery);


