(function($) {

	$( document ).ready( function() {

		$( 'body.full_search > div' ).css( 'visibility', 'hidden' );

	} );

})(jQuery);

var r_height = 0;

/**
 * Full Search
 * 
 * @param	string	datatables table id
 * @param	string	google map id
 * @para	object	center position
 */
function full_search( tbl_id, gmap_id, center )
{
	if( tbl_id == undefined || gmap_id == undefined ) {
	
		return;
		
	}
	
	// attach form data to datatable and submit
		
	$( '.fs_listings_btn' ).click( function() {
		$( '#fs_loading' ).show();
		$( this ).button( 'disable' );
		var f = $( '#tabs div' ).not( ':hidden' ).children( 'form' );
		var d = f.serializeArray();
		//~ d.push( { 'name': 'fid', 'value': f.attr( 'id' ) } );
		$( document ).data( 'fs-form-data', d );
		$.data( document.body, tbl_id ).fnSettings()._iDisplayStart = 0;
		$.data( document.body, tbl_id ).fnDraw( false );
	} );
	
	$( '.fs_show_tabs_btn' ).click( function() {
		$( '.fs_show_tabs:visible' ).hide();
		$( '#tabs:hidden' ).show( 'blind', 'fast', function() { 
			$( 'div.full_search' ).data( 'resize_table' )();
		} );
		$( '#tabs' ).show( 'blind', 'fast', function() { 
			$( 'div.full_search' ).data( 'resize_table' )();
		} );
	} );
	
	// search form tabify
	
	var tabs_activate_fn	= function(event, ui) {
								$( '#tabs div.ui-tabs-panel:has(.ui-tabs-hide) form' ).each( function(i, e){ 
									e.reset(); 
								} );
							};
	var tabs_load_fn		= function(event, ui) {
								var resize_table = $( 'div.full_search' ).data( 'resize_table' );
								if( $.isFunction( resize_table ) ) {
									resize_table();
								}
							};
	var tabs_create_fn		= function(event, ui) {
								$( document ).trigger( 'tabs_loaded' );
							};
	
	// detect jQuery UI breaking changes
	
	if( $.isFunction( window.is_greater_or_equal_to_jquery_ui_version ) && is_greater_or_equal_to_jquery_ui_version( '1.9.0' ) ) {
		
		$( '#tabs' ).tabs( { 
			'activate': tabs_activate_fn,
			'load': tabs_load_fn,
			'create': tabs_create_fn,
			'active': 0
		} );
		
	} else {
	
		$( '#tabs' ).tabs( { 
			'select': tabs_activate_fn,
			'show': tabs_load_fn,
			'create': tabs_create_fn,
			'selected': 0
		} );
	
	}
	
	// map click events
	
	$( '.listing_table' ).on( 'click', '.o', function() { 
		$.scrollTo( '#google_map', { axis:'y', duration: 500, offset: -25 } );
		var map = $(document).data( 'map' + gmap_id );
		if( map != null) { 
			map.setZoom(14);
		}
	} );
												
	$( '.listing_table' ).on( 'click', 'a.p', function(e) {
		e.preventDefault();
		window.open(
			$( this ).attr( 'href' ),
			'_blank',
			'width=1000,height=600,scrollbars=yes,status=yes,resizable=yes,screenx=25,screeny=25'
		);
	} );
	
	// resize tools + map on window resize
										
	var throttleTimeout;
		
	$( 'div.full_search' ).data( 'resize', function() {
		
		var win_width = $( window ).width();
		var map_width = ( ( win_width < 1000 ? 1000 : win_width ) - $( '.fs_ctrl_pnl' ).width()  );
		$( '.fs_map' ).width(  map_width + 'px' );
		if(typeof full_search_height_override === "undefined"){
			full_search_height_override = 0;
		}
		$( 'div.full_search' ).height( ($( window ).height() - full_search_height_override) + 'px' );
		google.maps.event.trigger( $( document ).data( 'map' + gmap_id ), 'resize' );
		
		if( 
			$.isPlainObject( center ) && 
			center.latitude && 
			center.longitude
		) {
		
			// On first load -- reset center
		
			$( document ).data( 'map' + gmap_id ).setCenter( new google.maps.LatLng( center.latitude, center.longitude ) );
			
			center = null;
			
		}
		
		$( 'div.full_search' ).data( 'resize_table' )();
				
		if( win_width > 1200 ) {
			
			$( '.search_info' ).show();
			
		} else {
		
			$( '.search_info' ).hide();
			
		}
				
		if( win_width > 955 ) {
			
			$( '.navigation' ).css( { 'right': 'auto', 'left': '50%' } );
			
		} else {
			
			$( '.navigation' ).css( { 'right': '0', 'left': 'auto' } );
			
			if( win_width > 800 ) {
			
				$( '.powered_by_rs:hidden' ).show();
				
			} else {
				
				$( '.powered_by_rs:visible' ).hide();
				
			}
			
		}
		
		$( 'div.full_search' ).trigger( 'resize_callback' );
		
		$( 'body.full_search > div' ).css( 'visibility', 'visible' );
		$( '.full_search_loading' ).fadeOut();
		
	} );
	
	var dt_diff = $( '.dataTables_scrollBody' ).outerHeight( true ) - $( '.dataTables_scrollBody' ).height();
	
	$( 'div.full_search' ).data( 'resize_table', function() {
		$.data( document.body, tbl_id ).fnAdjustColumnSizing( false );
		
		var win_height = $( window ).height();
	
		var t = $.data( document.body, tbl_id );
		if( t != undefined ) {
			var otbl = t.fnSettings();
			
			var tr_height = r_height + $( '.fs-form-elements' ).outerHeight( true );
						
			
			if(typeof dt_height_subtract === "undefined"){
				var dt_height = ( win_height - tr_height + dt_diff );
			} else {
				var dt_height = ( win_height - dt_height_subtract);
			}

			if( dt_height > 200 ) {
			
				$( '.dataTables_scrollBody' ).height( dt_height  + 'px' );
				$( '.fs_ctrl_pnl > div' ).css( 'overflow-y', 'hidden' );
				
			} else {
			
				$( '.fs_ctrl_pnl > div' ).css( 'overflow-y', 'auto' );
			
			}
			
			otbl.oScroll.sY = dt_height;
		}
	} );
	
	var browser = $.browser;
	if ( ! browser ) {
	   var ua = navigator.userAgent.toLowerCase();
	   var m = /(msie) ([\w.]+)/.exec( ua ) || ! /compatible/.test(ua) && /(mozilla)/.exec( ua ) || [];
	   browser = { version: m[2] };
	   browser[ m[1] ] = true;
	}
	
	$( window ).resize( function() {
		if (browser.msie) {
			if( ! throttleTimeout ) {
				throttleTimeout = setTimeout(
					function() {			
						$( 'div.full_search' ).data( 'resize' )();
						throttleTimeout = null;
					},
					50
				);
			}
		} else {
			$( 'div.full_search' ).data( 'resize' )();
		}
	} ).resize();
}


/**
 * Re-calculate r_height
 * 
 * @note	you may have to set fixed heights in css in order for jQuery
 * 			to properly determine heights.
 */ 
function recalculate_r_height()
{
	fg_toolbars	= $( '.dataTables_wrapper .fg-toolbar' );
	r_height 	= $( '.print_masthead' ).outerHeight( true ) + 
				  ( fg_toolbars.outerHeight( true ) * fg_toolbars.length ) + 
				  $( '.dataTables_scrollHead' ).outerHeight( true );
}
