// Common Functions

	// Confirm Cancel on forms
	function cancel(target) {
		if ( confirm("You sure you want to cancel?") ) {
			window.location=target;
		}
	}

	function toggleOptions() {
		jQuery(this).toggleClass("expanded");
		jQuery(this).next('.collapsable').toggleClass('expanded');
		jQuery(this).next('.collapsable').slideToggle("fast");
	}
	
	function get_geocode() {
		jQuery.ajax({
			type: "GET",
			url: '/api/geocode/' + mls_num,
			dataType: "html",
			success: function(html) {
			}
		});
	}

	function newImage(img) {
		img.src = "{$base_url}assets/images/nophoto.gif";
	}
	
// jQuery equivalent of onLoad event
// put anything in here that you would like to load on all pages.
jQuery(document).ready(function() {

	// search box focus & blur & submit events
	jQuery('.searchfield-textbox').focus(function() { if (jQuery(this).val() == "City, Zip Code, or Street Address") { jQuery(this).val(''); } });
	jQuery('.searchfield-textbox').blur(function() { if (jQuery(this).val() == "") { jQuery(this).val('City, Zip Code, or Street Address'); } });
	jQuery('#search-form-simple form').submit(function() { if (jQuery('.searchfield-textbox').val() == "City, Zip Code, or Street Address") { jQuery('.searchfield-textbox').val(''); } });

	// load js toggle collapsable widgets
	jQuery('.collapsable').each(function() {
		var title = jQuery(this).attr("title");
		if (title.length <= 0) { title = 'More Info.' }
		jQuery(this).before('<div class="toggle ui-widget-header-alt ui-corner-all">' + title + '<span class="toggle-indicator"></span></div>');
		jQuery('.toggle').click(toggleOptions);
		if (!jQuery(this).hasClass('expanded')) {
			jQuery(this).hide();
		}
		else {
			jQuery(this).prev('.toggle').addClass('expanded');
		}
	});
	

	// jcl - joined checkbox list
	jQuery(".jcl .option-all").each( function() {
		var el = jQuery(this).attr('id').replace('-all', '') + '[]';
		var chkd = false;
		jQuery('input[@name="' + el + '"]').each( function() { if ( jQuery(this).attr('checked') ) { chkd = true; } });
		if (chkd) { jQuery(this).attr('checked', false); }
		else { jQuery(this).attr('checked', true); }
	});
	
	jQuery(".jcl input[@type='checkbox']:checked").parent().addClass('highlight');
	
	jQuery(".jcl input[@type='checkbox']").click( function() {
		jQuery(this).parent().toggleClass('highlight');
		
		var chked = false;
		jQuery('input[@name="' + jQuery(this).attr('name') + '"]').each( function() {
			if ( jQuery(this).attr('checked') ) { chked = true; }
		});
		
		var chk_all_el = jQuery(this).attr('name').replace('[]','');
		if ( chked ) {
			jQuery('#' + chk_all_el + '-all').attr('checked', false);
			jQuery('#' + chk_all_el + '-all').parent().removeClass('highlight');
		}
		else {
			jQuery('#' + chk_all_el + '-all').attr('checked', true);
			jQuery('#' + chk_all_el + '-all').parent().addClass('highlight');
		}
	});
	
	jQuery('.jcl .option-all').click( function() {
		var el = jQuery(this).attr('id').replace('-all', '') + '[]';
		if ( jQuery(this).attr('checked') ) {
			jQuery('input[@name="' + el + '"]').each( function() {
				jQuery(this).attr('checked', false);
				jQuery(this).parent().removeClass('highlight');
			});
		}
	});

	// alternating strips on checkbox list
	jQuery('.option-list-box label:even').addClass('alt');

	// fade away ok messages after a few seconds.
	jQuery('.okmsg').animate( {opacity: "1.0"}, 3000 ).fadeOut(1000);

	// flash error messages
	jQuery(".errormsg").animate({ top: "0px" }, 100 ).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);

	// popup windows
	jQuery('a[rel*=popup]').click(function() {
		var win;
		var top = 0;
		var left = 0;
		var width = 600;
		var height = 400;
		if (screen.width > width) left = (screen.width - width) / 2;
		if (screen.height > height) top = (screen.height - height) / 2;
		win = window.open(jQuery(this).attr('href'), 'popup', 'left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top + ',width=' + width + ',height=' + height + ',scrollbars=1,location=0');
		if (navigator.appName != 'Microsoft Internet Explorer')  { if ( window.focus )  win.focus()};
		return false;
	});

	// handle missing listing photos on search results
	jQuery('.lp-handle').error(function() {
		jQuery(this).attr("src", "/assets/images/nophoto.gif");
		jQuery.ajax({
			type: "POST",
			url: '/mls/refresh_images',
			data: {'sysid': jQuery(this).attr("title")},
			dataType: "html",
			success: function(html) {
			}
		});
	});
		
	// UI tabs FIXME: Conflicts with google maps (map MUST be loaded before tabs)
	//jQuery('.ui-tabs > ul').tabs({ fx: { opacity: 'toggle' } });

	// Tools accordion TODO: Replace this functionality
	//jQuery('#tools > div:lt(3)').hide();
	jQuery('#tools > h3').click(function() { jQuery(this).next("div.accord").slideToggle('fast'); });
	jQuery('#tools .print').click(function(){ window.print(); });

});