
 
$(document).ready(function() {

	init(); 
	
 
});


function init() {

	var fadeout_content = false;

	$("img#tile_new").click(function () {
		var $checkbox = $("#search_form1 input#new")
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		do_search(null, fadeout_content);
		fadeout_content = true;
		return false;
	}); 
 
	$("img#tile_current").click(function () {
		var $checkbox = $("#search_form1 input#current")
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		do_search(null, fadeout_content);
		fadeout_content = true;
		return false;
	}); 
 
  
	$("img#tile_modified").click(function () {
		var $checkbox = $("#search_form1 input#modified")
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		do_search(null, fadeout_content);
		fadeout_content = true;
		return false;
	}); 
 
   
	$("img#tile_canceled").click(function () {
		var $checkbox = $("#search_form1 input#canceled")
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		do_search(null, fadeout_content);
		fadeout_content = true;
		return false;
	}); 
 
 
	$("#search_form1 input[type='checkbox']").click(function () {
		do_search(null, fadeout_content);
		fadeout_content = true; 
		// jQuery.log('search_form1 input');
 	});  
 	
 
	$("#search_form1 input[type='text']").blur(function () {
		do_search(null, fadeout_content);
		fadeout_content = true; 
		// jQuery.log('search_form1 input');
 	});  
 	
	$("#search_form1 input[type='radio']").click(function () {
		do_search(null, fadeout_content);
		fadeout_content = true; 
		// jQuery.log('search_form1 input');
 	});  
 	
 	
	$("#county_select").change(function () {
		do_search(null, fadeout_content);
		fadeout_content = true;
 	});

	$("#auctioneer_select").change(function () {
		do_search(null, fadeout_content);
		fadeout_content = true;
 	});

	$("#state_select").change(function () {
		do_search(null, fadeout_content);
		fadeout_content = true;
 	});


	$(".advanced_search span.title").click(function () {
		
		// jQuery.log('span.title click');
		
		$('div#ss1').animate({		
				height: '215px'		
			}, 
			500, 
			'easeInOutCubic'
		);					
						
		$('div.advanced_search').animate({		
				width: '764px'
			}, 
			500, 
			'easeInOutCubic'
		);					
		
		$('div.advanced_search div.controls').animate({		
				opacity: '1.00',
				height: '115px'
			}, 
			500, 
			'easeInOutCubic'
		);			
		
		$("#advanced_date_from").datepicker();	
		$("#advanced_date_to").datepicker();
		
		$("img#advanced_date_to_x").click(function() {
			$("#advanced_date_to").attr('value', '');
			do_search(null, true);
		
		});

		$("img#advanced_date_from_x").click(function() {
			$("#advanced_date_from").attr('value', '');
			do_search(null, true);		
		});  
		
	});
	
	


	
	/* Catch return key and send to submit button. */
	$('div.advanced_search div.controls').keypress(function(ev){
		if (ev.keyCode == 13) {
			do_search(null, fadeout_content);
			fadeout_content = true;
			// jQuery.log("ev.keyCode " + ev.keyCode);
		}
	});	

	
	

	var q = $.parseQuery();
 	
	var t = $(location).attr('href').split('/');
 	// jQuery.log('q.status ' + q.status)

	if ( q.status == undefined && (t[3] == 'search-foreclosures' || t[3] == 'search-auctions')) { 
		q.status = 'new'; 	
		// jQuery.log( 'Set q.status = new ');
	}
	if (q.status)
	{
		var $checkbox = $("#search_form1 input#"+q.status);
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		fadeout_content = false;
		do_search(null, fadeout_content);
	}
	
	
	var do_search_now = false; 
	/*
	if (q.new)
	{
		var $checkbox = $("#search_form1 input#new");
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		var do_search_now = true; 
	}
	*/
	if (q.modified)
	{
		var $checkbox = $("#search_form1 input#modified");
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		var do_search_now = true; 
	}
	if (q.canceled)
	{
		var $checkbox = $("#search_form1 input#canceled");
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		var do_search_now = true; 
	}
	if (q.current)
	{
		var $checkbox = $("#search_form1 input#current");
       	$checkbox.attr('checked', !$checkbox.attr('checked'));
		var do_search_now = true; 
	}	 
	 
	if (q.county)
	{
		var cs = $("#search_form1 select#county_select");
		cs.val(q.county).attr('selected', 'selected');
		var do_search_now = true; 
	}
	
	if (q.days_until)
	{
		var du = $("#search_form1 input[name=days_until]");
		// jQuery.log(du);
		// jQuery.log('here at du');
		du.val(q.days_until);
		var do_search_now = true; 
	}
	if (do_search_now) do_search(null, fadeout_content);
	
	// jQuery.log(q);
	

	
	//** JS fixes for sign up page **//
	//** includes incoming link from email **//
		
	if (q.subscription_id > 0  )
	{
		var $sidebar_subid = $("form#signupsidebar input[name=subscription_id][value=" + q.subscription_id + "]");
		$sidebar_subid.attr('checked', 'checked');
	}
	

	
}

function init_lowerSearchControls()
{
	init_tooltip();
	init_sortbind();
}


function init_tooltip() {

	$('div.tooltip').tooltip({ 
		track: true, 
		extraClass: "tooltip-fancy", 		
		opacity: 0.90, 
		fixPNG: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - ", 
		fade: 250,
		top: -15, 
    	left: 5 
	});
}



function init_sortbind() {


	/* sorting search results */
	
	$('a#sort_status').click(function() {
		$('input#sort_field').attr('value', 'status');
		do_search(null, true);
		return false;
	});  
	
	$('a#sort_date').click(function() {
		$('input#sort_field').attr('value', 'sale_date');
		do_search(null, true);
		return false;
	});  
	
	$('a#sort_deposit').click(function() {
		$('input#sort_field').attr('value', 'price');
		do_search(null, true);
		return false;
	}); 
	
	$('a#sort_address').click(function() {
		$('input#sort_field').attr('value', 'address');
		do_search(null, true);
		return false;
	}); 
	
	$('a#sort_county').click(function() {
		$('input#sort_field').attr('value', 'county_name');
		do_search(null, true);
		return false;
	});  
	
}


function do_search(page, fadeout_content ) {
	
	if (fadeout_content) {
		fadeout_time = 500;
	}
	else
	{
		fadeout_time = 0;	
	}
	
	// jQuery.log("fadeout_time " + fadeout_time);


 	$('div#content').animate({		
			opacity: '0.00'
			}, 
			fadeout_time, 
			'easeInOutCubic',
			
			// callback
			function() {
							
				$.ajax({
				  url: '/md_search.php?' + $("#search_form1").serialize(),
				  context: document.body,
				  success: function(html){
				//		$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							500, 
							'easeInOutCubic'
						),
						init_lowerSearchControls();
					}
				}); 
			
			}			
	);	 
 
 }
 
 
  
 function do_signup(fadeout_content) {
	
	if (fadeout_content) {
		fadeout_time = 500;
	}
	else
	{
		fadeout_time = 0;	
	}
	
	// jQuery.log("do_signup fadeout_time " + fadeout_time);
	// jQuery.log(window.location.search);
	
	
	
 	$('div#content').animate({		
			opacity: '0.00'
			}, 
			fadeout_time, 
			'easeInOutCubic',			
			// callback
			function() {
	
				// email href signup form
				// pass values to sub request
				var t = $(location).attr('href').split('?');
 						
				$.ajax({
				  url: '/md_signup.php?' + $("#signup_form1").serialize() + '&' + t[1],
				  context: document.body,
				  success: function(html){
				//		$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							500, 
							'easeInOutCubic'
						),
						init_signup()
					}
				}); 
			
			}			
	);	 
 
 }
 


 
  
 function do_login(fadeout_content) {
	
	if (fadeout_content) {
		fadeout_time = 500;
	}
	else
	{
		fadeout_time = 0;	
	}
	
	// jQuery.log("do_login fadeout_time " + fadeout_time);


 	$('div#content').animate({		
			opacity: '0.00'
			}, 
			fadeout_time, 
			'easeInOutCubic',			
			// callback
			function() {
							
				$.ajax({
				  url: '/md_login.php?' + $("#login_form1").serialize(),
				  context: document.body,
				  success: function(html){
				//		$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							500, 
							'easeInOutCubic'
						),
						init_login()
					}
				}); 
			
			}			
	);	 
 
 }
 
 
 function do_account(fadeout_content) {
	
	if (fadeout_content) {
		fadeout_time = 500;
	}
	else
	{
		fadeout_time = 0;	
	}
	
	// jQuery.log("do_account fadeout_time " + fadeout_time);


 	$('div#content').animate({		
			opacity: '0.00'
			}, 
			fadeout_time, 
			'easeInOutCubic',			
			// callback
			function() {
							
				$.ajax({
				  url: '/md_account.php?' + $("#account_form1").serialize(),
				  context: document.body,
				  success: function(html){
				//		$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							500, 
							'easeInOutCubic'
						),
						init_account()
					}
				}); 
			
			}			
	);	 
 
 }
 
 

 function init_signup() {

	// jQuery.log("function init_signup ");

	/* Bind event handler to signup button click. */
	$('#signup_submit').click(function(){

		// jQuery.log("function init_signup  signup_submit");

		$('div#content').animate({		
			opacity: '0.00'
			}, 
			250, 
			'easeInOutCubic',			
			// callback
			function() { 	
				$.ajax({
				  url: '/md_signup.php?' + $("#signup_form1").serialize(),
				  context: document.body,
				  success: function(html){
					//	$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							250, 
							'easeInOutCubic'
						),
						init_signup()
					}
				});			
			}			
		);	 	
	});
	

	/* Catch return key and send to submit button. */
	$('#signup_form1').keypress(function(ev){
		if (ev.keyCode == 13) $('#signup_submit').click();
		// jQuery.log("ev.keyCode " + ev.keyCode);
	});	
	
	
	
	
}
 
  
 function init_login() {

	// LOGIN

	/* Bind event handler to login button click. */
	$('#login_submit').click(function(){

		// jQuery.log("function init_login  login_submit");

		$('div#content').animate({		
			opacity: '0.00'
			}, 
			250, 
			'easeInOutCubic',			
			// callback
			function() { 	
				$.ajax({
				  url: '/md_login.php?' + $("#login_form1").serialize(),
				  context: document.body,
				  success: function(html){
					//	$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							250, 
							'easeInOutCubic'
						),
						init_login()
					}
				});			
			}			
		);	 	
	});
	
	
	/* Catch return key and send to submit button. */
	$('#login_form1').keypress(function(ev){
		if (ev.keyCode == 13) {
			$('#login_submit').click();
			// jQuery.log("ev.keyCode login_form1 " + ev.keyCode);
			return false;	
		}			
	});	
	
	
	
	
	// NEW PASSWORD
	
	/* Bind event handler to reset button click. */
	$('#newpassword_submit').click(function(){

		// jQuery.log("function init_login  login_submit");

		$('div#content').animate({		
			opacity: '0.00'
			}, 
			250, 
			'easeInOutCubic',			
			// callback
			function() { 	
				$.ajax({
				  url: '/md_login.php?' + $("#newpassword_form1").serialize(),
				  context: document.body,
				  success: function(html){
					//	$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							250, 
							'easeInOutCubic'
						),
						init_login()
					}
				});			
			}			
		);	 	
	});
	

	
	/* Catch return key and send to submit button. */
	$('#newpassword_form1 #email').keypress(function(ev){
		if (ev.keyCode == 13) {
			$('#newpassword_submit').click();
		  	return false;
		}  	
	});	
	
	
}
 
  
function init_account() {

	// jQuery.log("function init_account ");

	/* Bind event handler to save button click. */
	$('#account_form1 .account_submit').click(function(){

		// jQuery.log("function init_account account_form1.account_submit");

		$('div#content').animate({		
			opacity: '0.00'
			}, 
			250, 
			'easeInOutCubic',			
			// callback
			function() { 	
				$.ajax({
				  url: '/md_account.php?' + $("#account_form1").serialize(),
				  context: document.body,
				  success: function(html){
					//	$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							250, 
							'easeInOutCubic'
						),
						init_account()
					}
				});			
			}			
		);	 	
	});
		
		
	/* Bind event handler to logout button click. */
	$('#account_form2 .account_submit').click(function(){

		// jQuery.log("function init_account account_form2.account_submit");

		$('div#content').animate({		
			opacity: '0.00'
			}, 
			250, 
			'easeInOutCubic',			
			// callback
			function() { 	
				$.ajax({
				  url: '/md_account.php?' + $("#account_form2").serialize(),
				  context: document.body,
				  success: function(html){
					//	$('div#content').append(html);
						$('div#content')
						.html(html)
						.animate({		
							opacity: '1.00'
							}, 
							250, 
							'easeInOutCubic'
						),
						init_account()
					}
				});			
			}			
		);	 	
	});

	
	/* Bind event handler to radio button click. */
	$('#account_form1 input[name=subscription_id]').click(function(){

		var subscription_id = $('#account_form1 input[name=subscription_id]:checked').val(); 
		if (subscription_id == -1)
		{
			alert('Your account will be canceled tomorrow.');
		}
		else
		{
			alert('Your new subscription will begin tomorrow, and the credit card on file will be charged tomorrow.');
		}  
		
	});	
	
	
	/* Catch return key and send to submit button. */
	$('#account_form1').keypress(function(ev){
		if (ev.keyCode == 13) {
			
				$('div#content').animate({		
				opacity: '0.00'
				}, 
				250, 
				'easeInOutCubic',			
				// callback
				function() { 	
					$.ajax({
					  url: '/md_account.php?' + $("#account_form1").serialize(),
					  context: document.body,
					  success: function(html){
						//	$('div#content').append(html);
							$('div#content')
							.html(html)
							.animate({		
								opacity: '1.00'
								}, 
								250, 
								'easeInOutCubic'
							),
							init_account()
						}
					});			
				}			
			);	 	
		}			
	});	



}
 
  
 
 
 
 
 

/**
 * Copyright (C) 2009 Jonathan Azoff <jon@azoffdesign.com>
 *
 * This script 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, 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.
 *
 *
 * // jQuery.log v1.0.0 - A jQuery plugin that unifies native console logging across browsers
 * 
 * @usage		call // jQuery.log([args...]) to write to attempt to write to the console of any browser. 
 *				**See http: *azoffdesign.com/plugins/js/log for an example.
 * @param 		args...	one or more javascript objects to be written to the console
 * @returns 	true if a console was detected and successfully used, false if the plug-in had to resort to alert boxes
 * @note 		if a plug-in cannot be located then an alert is called with the arguments you wish to log. Multiple 
 *              arguments are separated with a space.
 * @depends 	just make sure you have jQuery and some code you want to debug.
 */
(function($){
	$.extend({"log":function(){ 
		if(arguments.length > 0) {
			
			// join for graceful degregation
			var args = (arguments.length > 1) ? Array.prototype.join.call(arguments, " ") : arguments[0];
			
			// this is the standard; firebug and newer webkit browsers support this
			try { 
				console.log(args);
				return true;
			} catch(e) {		
				// newer opera browsers support posting erros to their consoles
				try { 
					opera.postError(args); 
					return true;
				} catch(e) { }
			}
			
			// catch all; a good old alert box
			alert(args);
			return false;
		}
	}});
})(jQuery);



/**
 * A simple querystring parser.
 * Example usage: var q = $.parseQuery(); q.fooreturns  "bar" if query contains "?foo=bar"; multiple values are added to an array. 
 * Values are unescaped by default and plus signs replaced with spaces, or an alternate processing function can be passed in the params object .
 * http://actingthemaggot.com/jquery
 *
 * Copyright (c) 2008 Michael Manning (http://actingthemaggot.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 **/
jQuery.parseQuery = function(qs,options) {
	var q = (typeof qs === 'string'?qs:window.location.search), o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}}, options = (typeof qs === 'object' && typeof options === 'undefined')?qs:options, o = jQuery.extend({}, o, options), params = {};
	jQuery.each(q.match(/^\??(.*)$/)[1].split('&'),function(i,p){
		p = p.split('=');
		p[1] = o.f(p[1]);
		params[p[0]] = params[p[0]]?((params[p[0]] instanceof Array)?(params[p[0]].push(p[1]),params[p[0]]):[params[p[0]],p[1]]):p[1];
	});
	return params;
}


