/** * @package IDX Listings Template * @author John Paul Pineda * @email john.pineda@thedesignpeople.com * @copyright 2015 John Paul Pineda * @version 1.0 August 7, 2015 **/ jQuery.noConflict(); // Global variables should be avoided in order to lower the possibility of variable naming collisions. One way to minimize the number of globals is by namespacing our variables and functions. ( function( $ ) { var idx_provider_id = 0; // ID of the current selected IDX Provider. Available IDX Providers are as follows: IDX Broker Original = 1, IDX Broker Platinum = 2 and IDX iHomeFinder = 3. var idx_lt_site_url = 'http://www.comfortlivingohiollc.com'; if ( idx_provider_id == 1 ) { var idx_broker_original = { available_fields: { address: 'idx_listing_address', description: 'idx_listing_description', price: 'idx_listing_price', number_of_bedrooms: 'idx_listing_number_of_bedrooms', number_of_full_bathrooms: 'idx_listing_number_of_full_bathrooms', number_of_partial_bathrooms: 'idx_listing_number_of_partial_bathrooms', number_of_bathrooms: 'idx_listing_number_of_bathrooms', size_in_square_feet: 'idx_listing_size_in_square_feet', size_in_acres: 'idx_listing_size_in_acres', url: 'idx_listing_url', photo_url: 'idx_listing_photo_url', photo_gallery_url: 'idx_listing_photo_gallery_url', mls_number: 'idx_listing_mls_number', details_mls_courtesy_text: 'idx_listing_details_mls_courtesy_text' }, initializeListingsTemplate: function( v_listings_template ) { var this_object = this; var listings_template = v_listings_template; // Assign by reference only if ( $( "#IDX-main .IDX-resultsCell" ).length > 0 ) { var listings = []; var listing = {}; var listings_template_content = '', listings_template_inline_css = ''; var listing_address = {}; var listing_complete_address; var listing_description; var listing_price; var listing_number_of_bedrooms; var listing_number_of_full_bathrooms; var listing_number_of_partial_bathrooms; var listing_number_of_bathrooms; var listing_size_in_square_feet; var listing_size_in_acres; var listing_url; var listing_photo_url; var listing_photo_gallery_url; var listing_mls_number; var listing_details_mls_courtesy_text; $( "#IDX-main .IDX-resultsCell" ).each( function( v_key, v_html ) { listing_address.number = castToString( $( ".IDX-resultsAddressNumber", v_html ).html() ); listing_address.direction = castToString( $( ".IDX-resultsAddressDirection", v_html ).html() ); listing_address.name = castToString( $( ".IDX-resultsAddressName", v_html ).html() ); listing_address.city = castToString( $( ".IDX-resultsAddressCity", v_html ).html() ); listing_address.state = castToString( $( ".IDX-resultsAddressState", v_html ).html() ); listing_address.state_abbreviation = castToString( $( ".IDX-resultsAddressStateAbrv", v_html ).html() ); listing_address.zip = castToString( $( ".IDX-resultsAddressZip", v_html ).html() ); listing_complete_address = formatIDXBrokerListingAddress( listing_address ); listing_description = castToString( $( ".IDX-resultsDescription", v_html ).html() ); listing_price = castToString( $( "#IDX-priceNumber", v_html ).html() ); listing_number_of_bedrooms = castToInt( $( ".IDX-resultsSideBedrooms .IDX-resultsSideValue", v_html ).html() ); listing_number_of_full_bathrooms = castToInt( $( ".IDX-resultsSideBathrooms .IDX-resultsSideValue", v_html ).text() ); listing_number_of_partial_bathrooms = castToInt( $( ".IDX-resultsSideHalfBaths .IDX-resultsSideValue", v_html ).text() ); listing_number_of_bathrooms = ( listing_number_of_full_bathrooms + ( ( listing_number_of_partial_bathrooms > 0 ) ? ( '.' + listing_number_of_partial_bathrooms ) : '' ) ); listing_size_in_square_feet = castToString( $( ".IDX-resultsSideSqFt .IDX-resultsSideValue", v_html ).text(), 'N/A' ); listing_size_in_acres = castToString( $( ".IDX-resultsSideAcres .IDX-resultsSideValue", v_html ).text(), 'N/A' ); listing_url = castToString( $( ".IDX-resultsPhoto a", v_html ).attr( 'href' ) ); listing_photo_url = castToString( $( ".IDX-resultsPhoto a img", v_html ).attr( 'src' ) ); listing_photo_gallery_url = castToString( $( ".IDX-resultsLinks .IDX-resultsGalleryLink.IDX-linkItem a" ).attr( 'href' ) ); listing_mls_number = castToString( $( ".IDX-resultsSideListingID .IDX-resultsSideValue", v_html ).html(), 'Unknown' ); listing_details_mls_courtesy_text = castToString( $( ".IDX-detailsMLSCourtesy", v_html ).text() ); listing = { listing_number: ( v_key + 1 ), title: '', address: listing_complete_address, description: listing_description, price: listing_price, number_of_bedrooms: listing_number_of_bedrooms, number_of_full_bathrooms: listing_number_of_full_bathrooms, number_of_partial_bathrooms: listing_number_of_partial_bathrooms, number_of_bathrooms: listing_number_of_bathrooms, size_in_square_feet: listing_size_in_square_feet, size_in_acres: listing_size_in_acres, url: listing_url, photo_url: listing_photo_url, photo_gallery_url: listing_photo_gallery_url, mls_number: listing_mls_number, details_mls_courtesy_text: listing_details_mls_courtesy_text }; listings_template_content += strReplaceByObject( this_object.available_fields, listing, listings_template.html ); listings.push( listing ); } ); listings_template_inline_css = ( "\n\n" ); $( "hr.IDX-resultsSeperator" ).after( ( listings_template_inline_css + listings_template_content + '
' ) ); } } }; } else if ( idx_provider_id == 2 ) { var idx_broker_platinum = { available_fields: { type: 'idx_listing_type', status: 'idx_listing_status', address: 'idx_listing_address', description: 'idx_listing_description', price: 'idx_listing_price', number_of_bedrooms: 'idx_listing_number_of_bedrooms', number_of_full_bathrooms: 'idx_listing_number_of_full_bathrooms', number_of_partial_bathrooms: 'idx_listing_number_of_partial_bathrooms', number_of_bathrooms: 'idx_listing_number_of_bathrooms', size_in_square_feet: 'idx_listing_size_in_square_feet', size_in_acres: 'idx_listing_size_in_acres', url: 'idx_listing_url', photo_url: 'idx_listing_photo_url', photo_gallery_url: 'idx_listing_photo_gallery_url', mls_number: 'idx_listing_mls_number', details_mls_courtesy_text: 'idx_listing_details_mls_courtesy_text' }, listing_types: { 1: 'Residential', 2: '2-4 Family', 3: 'Condo/Coop/Townhome', 4: 'Rentals', 5: 'Commercial Sale/Lease', 6: 'Business', 7: 'Land', 8: '5+ Mixed Use' }, initializeListingsTemplate: function( v_listings_template ) { var this_object = this; var listings_template = v_listings_template; // Assign by reference only if ( $( "#IDX-main .IDX-resultsCell" ).length > 0 ) { var listings = []; var listing = {}; var listings_template_content = '', listings_template_inline_css = ''; var listing_type; var listing_status; var listing_address = {}; var listing_complete_address; var listing_description; var listing_price; var listing_number_of_bedrooms; var listing_number_of_full_bathrooms; var listing_number_of_partial_bathrooms; var listing_number_of_bathrooms; var listing_size_in_square_feet; var listing_size_in_acres; var listing_url; var listing_photo_url; var listing_photo_gallery_url; var listing_mls_number; var listing_details_mls_courtesy_text; $( "#IDX-main .IDX-resultsCell" ).each( function( v_key, v_html ) { listing_type = this_object.listing_types[ castToInt( $( this ).data( 'mlsptid' ), 'N/A' ) ]; listing_status = castToString( $( ".IDX-resultsField-propStatus .IDX-resultsText", v_html ).html(), 'N/A' ); listing_address.number = castToString( $( ".IDX-resultsAddressNumber", v_html ).html() ); listing_address.direction = castToString( $( ".IDX-resultsAddressDirection", v_html ).html() ); listing_address.name = castToString( $( ".IDX-resultsAddressName", v_html ).html() ); listing_address.city = castToString( $( ".IDX-resultsAddressCity", v_html ).html() ); listing_address.state = castToString( $( ".IDX-resultsAddressState", v_html ).html() ); listing_address.state_abbreviation = castToString( $( ".IDX-resultsAddressStateAbrv", v_html ).html() ); listing_address.zip = castToString( $( ".IDX-resultsAddressZip", v_html ).html() ); listing_complete_address = formatIDXBrokerListingAddress( listing_address ); listing_description = castToString( $( ".IDX-resultsDescription", v_html ).html() ); listing_price = castToString( $( "#IDX-priceNumber", v_html ).html() ); listing_number_of_bedrooms = castToInt( $( ".IDX-resultsField-bedrooms .IDX-resultsText", v_html ).html() ); listing_number_of_full_bathrooms = castToInt( $( ".IDX-resultsField-fullBaths .IDX-resultsText", v_html ).text() ); listing_number_of_partial_bathrooms = castToInt( $( ".IDX-resultsField-partialBaths .IDX-resultsText", v_html ).text() ); listing_number_of_bathrooms = ( listing_number_of_full_bathrooms + ( ( listing_number_of_partial_bathrooms > 0 ) ? ( '.' + listing_number_of_partial_bathrooms ) : '' ) ); listing_size_in_square_feet = castToString( $( ".IDX-resultsField-sqFt .IDX-resultsText", v_html ).text(), 'N/A' ); listing_size_in_acres = castToString( $( ".IDX-resultsField-acres .IDX-resultsText", v_html ).text(), 'N/A' ); listing_url = castToString( $( ".IDX-resultsPhoto a", v_html ).attr( 'href' ) ); listing_photo_url = castToString( $( ".IDX-resultsPhoto noscript ", v_html ).attr( 'data-src' ) ); listing_photo_gallery_url = castToString( $( ".IDX-resultsPhotogallery .IDX-resultsPhotoGalleryLink" ).attr( 'href' ) ); listing_mls_number = castToString( $( ".IDX-resultsField-listingID .IDX-resultsText", v_html ).html(), 'Unknown' ); listing_details_mls_courtesy_text = castToString( $( ".IDX-mlsContainer .IDX-detailsMLSCourtesy", v_html ).html() ); listing = { listing_number: ( v_key + 1 ), title: '', type: listing_type, status: listing_status, address: listing_complete_address, description: listing_description, price: listing_price, number_of_bedrooms: listing_number_of_bedrooms, number_of_full_bathrooms: listing_number_of_full_bathrooms, number_of_partial_bathrooms: listing_number_of_partial_bathrooms, number_of_bathrooms: listing_number_of_bathrooms, size_in_square_feet: listing_size_in_square_feet, size_in_acres: listing_size_in_acres, url: listing_url, photo_url: listing_photo_url, photo_gallery_url: listing_photo_gallery_url, mls_number: listing_mls_number, details_mls_courtesy_text: listing_details_mls_courtesy_text }; listings_template_content += strReplaceByObject( this_object.available_fields, listing, listings_template.html ); listings.push( listing ); } ); listings_template_inline_css = ( "\n\n" ); $( ".IDX-resultCellContainer" ).after( ( listings_template_inline_css + listings_template_content + '
' ) ); } } }; } else if ( idx_provider_id == 3 ) { var idx_ihomefinder = { available_fields: { address: 'idx_listing_address', price: 'idx_listing_price', number_of_bedrooms: 'idx_listing_number_of_bedrooms', number_of_full_bathrooms: 'idx_listing_number_of_full_bathrooms', number_of_partial_bathrooms: 'idx_listing_number_of_partial_bathrooms', number_of_bathrooms: 'idx_listing_number_of_bathrooms', lot_size_and_unit_of_area: 'idx_listing_lot_size', url: 'idx_listing_url', photo_url: 'idx_listing_photo_url', mls_number: 'idx_listing_mls_number' }, getNewElementTextByTag: function( v_html, v_tag ) { var new_element; new_element = $( ( '
' + v_html + '
' ) ); return $( v_tag, new_element ).text(); }, initializeListingsTemplateMapDisplay: function() { $( "#ihf-map-canvas" ).parent().parent().addClass( 'ihf-map-canvas-container' ); }, initializeListingsTemplate: function( v_listings_template ) { var this_object = this; var listings_template = v_listings_template; // Assign by reference only if ( $( "#ihf-main-container.ihf-container .ihf-results-address" ).length > 0 ) { var listings = []; var listing = {}; var map_canvas_row_number = 0; $( "#ihf-main-container.ihf-container div.row" ).each( function( v_key, v_html ) { if ( $( this ).find( '.ihf-refine-search-container, #ihf-mini-search-form, #ihf-sort-values, #ihf-map-canvas, .pagination, #ihf-board-disclaimers' ).length > 0 ) { $( this ).addClass( "idx-ihf-visible-row" ); if ( $( this ).find( '#ihf-map-canvas' ).length > 0 ) map_canvas_row_number = v_key; } } ); var listings_template_content = '', listings_template_inline_css = ''; var listing_address; var listing_price; var results_listing_info; var results_listing_infos; var listing_number_of_bedrooms; var listing_number_of_full_bathrooms; var listing_number_of_partial_bathrooms; var listing_number_of_bathrooms; var listing_lot_size; var listing_unit_of_area; var listing_lot_size_and_unit_of_area; var container_tag; var new_element; var listing_url; var listing_photo_url; var listing_mls_number; $( "#ihf-main-container.ihf-container .ihf-results-address" ).each( function( v_key, v_html ) { listing_address = $.trim( castToString( $( this ).find( 'a' ).html() ) ); //list price listing_price = $.trim( ( ( $( ".ihf-results-property-info .ihf-results-price .ihf-for-sale-price" ).eq( v_key ).html() ).replace( 'LIST: ', '' ) ) ); //line added for sold sold_listing_price = $.trim( ( ( $( ".ihf-results-property-info .ihf-results-price .ihf-sold-price" ).eq( v_key ).html() ).replace( 'LIST: ', '' ) ) ); listing_price = ( listing_price == '' ) ? sold_listing_price : listing_price; // Ensure that the following fields: listing_number_of_bedrooms, listing_number_of_full_bathrooms, listing_number_of_partial_bathrooms, listing_number_of_bathrooms and listing_lot_size will have a value as sometimes the iHomeFinder IDX generates different HTML element(s) or markup(s). results_listing_info = $.trim( ( $( ".ihf-results-property-info" ).eq( v_key ).html() ).toLowerCase() ); results_listing_infos = results_listing_info.split( ( ( results_listing_info.indexOf( '
' ) > -1 ) ? '
' : '
' ) ); listing_number_of_bedrooms = 0; listing_number_of_full_bathrooms = 0; listing_number_of_partial_bathrooms = 0; listing_number_of_bathrooms = '0'; listing_lot_size = 0.00; listing_unit_of_area = 'Sq. Ft.'; // If listing type is "Lots/Land" if ( ( results_listing_info.indexOf( 'lot acres' ) > -1 ) ) { container_tag = ( ( results_listing_infos[ 0 ].indexOf( 'strong' ) > -1 ) ? 'strong' : 'b' ); listing_lot_size = $.trim( this_object.getNewElementTextByTag( results_listing_infos[ 0 ], container_tag ) ); listing_unit_of_area = 'Acres'; } /* If listing type is "Multi-Unit Residential" */ else if ( ( results_listing_info.indexOf( 'number of units' ) > -1 ) ) { listing_lot_size = 'N/A'; listing_unit_of_area = ''; } else { // For listing number of bedrooms container_tag = ( ( results_listing_infos[ 0 ].indexOf( 'strong' ) > -1 ) ? 'strong' : 'b' ); listing_number_of_bedrooms = castToInt( this_object.getNewElementTextByTag( results_listing_infos[ 0 ], container_tag ) ); // For listing number of bathrooms container_tag = ( ( results_listing_infos[ 1 ].indexOf( 'strong' ) > -1 ) ? 'strong' : 'b' ); new_element = $( ( '
' + results_listing_infos[ 1 ] + '
' ) ); $( container_tag, new_element ).each( function( v_inner_key, v_inner_html ) { if ( v_inner_key == 0 ) listing_number_of_full_bathrooms = castToInt( $( this ).text() ); else listing_number_of_partial_bathrooms = castToInt( $( this ).text() ); } ); listing_number_of_bathrooms = ( listing_number_of_full_bathrooms + ( ( listing_number_of_partial_bathrooms > 0 ) ? ( '.' + listing_number_of_partial_bathrooms ) : '' ) ); // For listing lot size container_tag = ( ( results_listing_infos[ 2 ].indexOf( 'strong' ) > -1 ) ? 'strong' : 'b' ); listing_lot_size = $.trim( castToString( this_object.getNewElementTextByTag( results_listing_infos[ 2 ], container_tag ), 'N/A' ) ); //added for sold listing_infos = []; if( listing_price.indexOf('SOLD:') > -1 ) { container_tag = ( ( results_listing_infos[ 0 ].indexOf( 'strong' ) > -1 ) ? 'strong' : 'b' ); $.each( results_listing_infos, function(i,v){ listing_infos.push( $.trim( $( results_listing_infos[ i ] ).text() ) ); }); listing_number_of_bedrooms = listing_infos[1].replace('beds:', ''); //bath listing_number_of_bathrooms = listing_infos[2]; //bed listing_lot_size = listing_infos[3]; //sq.ft } } listing_lot_size_and_unit_of_area = ( listing_lot_size.toUpperCase() + ' ' + listing_unit_of_area ); listing_url = castToString( $( this ).find( 'a' ).attr( 'href' ) ); listing_photo_url = castToString( $( ".ihf-results-photo a img" ).eq( v_key ).attr( 'src' ) ); listing_mls_number = $.trim( castToString( ( $( ".ihf-results-extra-info .ihf-results-listingnum" ).eq( v_key ).html() ).replace( 'Listing #', '' ) ) ); listing = { listing_number: ( v_key + 1 ), title: '', address: listing_address, price: listing_price, number_of_bedrooms: listing_number_of_bedrooms, number_of_full_bathrooms: listing_number_of_full_bathrooms, number_of_partial_bathrooms: listing_number_of_partial_bathrooms, number_of_bathrooms: listing_number_of_bathrooms, lot_size_and_unit_of_area: listing_lot_size_and_unit_of_area, url: listing_url, photo_url: listing_photo_url, mls_number: listing_mls_number }; listings_template_content += strReplaceByObject( this_object.available_fields, listing, listings_template.html ); listings.push( listing ); } ); listings_template_inline_css = ( "\n\n" ); $( "#ihf-main-container.ihf-container div.row" ).eq( map_canvas_row_number ).after( ( '
' + listings_template_inline_css + listings_template_content ) ); } } }; } function setCookie( v_cookie_name, v_cookie_value, v_number_of_days_to_expire ) { var current_date = new Date(); current_date.setTime( current_date.getTime() + ( v_number_of_days_to_expire * 24 * 60 * 60 * 1000 ) ); var cookie_expiration = "expires=" + current_date.toGMTString(); document.cookie = v_cookie_name + "=" + v_cookie_value + "; " + cookie_expiration; } function getCookie( v_cookie_name ) { var cookie_name = v_cookie_name + "="; var cookie_array = document.cookie.split( ';' ); for ( var x = 0; x < cookie_array.length; x++ ) { var cookie_key_and_value = cookie_array[ x ]; while ( cookie_key_and_value.charAt( 0 ) == ' ' ) cookie_key_and_value = cookie_key_and_value.substring( 1 ); if ( cookie_key_and_value.indexOf( cookie_name ) != -1 ) return cookie_key_and_value.substring( cookie_name.length, cookie_key_and_value.length ); } return ''; } function castToInt( v_string, v_default_value_when_empty ) { if ( typeof v_default_value_when_empty == 'undefined' ) v_default_value_when_empty = 0; if ( typeof v_string == 'undefined' || v_string == null || isNaN( v_string ) || v_string == '' ) v_string = v_default_value_when_empty; else v_string = parseInt( v_string ); return v_string; } function castToString( v_string, v_default_value_when_empty ) { if ( typeof v_default_value_when_empty == 'undefined' ) v_default_value_when_empty = ''; if ( typeof v_string == 'undefined' || v_string == null || v_string == '' ) v_string = v_default_value_when_empty; return $.trim( v_string ); } if ( idx_provider_id == 1 || idx_provider_id == 2 ) { function formatIDXBrokerListingAddress( v_listing_address ) { var listing_address_parts_array = []; if ( $.trim( ( v_listing_address.number + ' ' + v_listing_address.direction + ' ' + v_listing_address.name ) ) != '' ) listing_address_parts_array.push( ( v_listing_address.number + ' ' + v_listing_address.direction + ' ' + v_listing_address.name ) ); if ( v_listing_address.city != '' ) listing_address_parts_array.push( v_listing_address.city ); listing_address_state = ( ( v_listing_address.state_abbreviation == '' ) ? v_listing_address.state : v_listing_address.state_abbreviation ); if ( $.trim( ( listing_address_state + ' ' + v_listing_address.zip ) ) != '' ) listing_address_parts_array.push( ( listing_address_state + ' ' + v_listing_address.zip ) ); return ( ( listing_address_parts_array.length > 0 ) ? listing_address_parts_array.join( ', ' ) : '' ); } } function strReplaceByObject( v_searches, v_replacements, v_string ) { var escaped_field_shortcode; var regex; for ( var object_key in v_searches ) { escaped_field_shortcode = ( '\\[' + v_searches[ object_key ] + '\\]' ); regex = new RegExp( escaped_field_shortcode, 'g' ); v_string = v_string.replace( regex, v_replacements[ object_key ] ); } return v_string; } function restoreIDXListingsDefaultTemplate() { if ( idx_provider_id == 1 ) $( "#IDX-main" ).addClass( 'idx-broker-original-default-listings-template' ); else if ( idx_provider_id == 2 ) $( ".IDX-resultCellContainer" ).addClass( 'idx-broker-platinum-default-listings-template' ); else if ( idx_provider_id == 3 ) $( "#ihf-main-container.ihf-container" ).addClass( 'idx-ihomefinder-default-listings-template' ); } function initActionIDXListingsTemplate() { var listings_template = {}; var action = 'get_listings_template_html_and_css'; var view_name = getCookie( 'idx_listings_template_view_name' ); var post_data = { 'action': action, 'current_page_url': window.location.href, 'view_name': view_name, 'request_id': Math.random() }; if ( idx_provider_id == 3 ) idx_ihomefinder.initializeListingsTemplateMapDisplay(); $.ajax( { url: ( idx_lt_site_url + '/wp-admin/admin-ajax.php' ), type: 'GET', async: true, cache: false, dataType: 'jsonp' /* JSONP or "JSON with padding" is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy( http://en.wikipedia.org/wiki/Same-origin_policy ). */, data: post_data, success: function( v_response ) { var response = v_response; // Assign by reference only if ( response.status == 'success' && response.is_there_idx_listings_template == 'yes' && $.trim( response.page_listings_html ) != '' ) { listings_template.html = response.page_listings_html; listings_template.css = response.page_listings_css; if ( idx_provider_id == 1 && view_name != 'idx-search-results-map-view' ) idx_broker_original.initializeListingsTemplate( listings_template ); // If the IDX Provider is "IDX Broker Original" else if ( idx_provider_id == 2 && view_name != 'idx-search-results-map-view' ) idx_broker_platinum.initializeListingsTemplate( listings_template ); // If the IDX Provider is "IDX Broker Platinum" else if ( idx_provider_id == 3 && view_name != 'idx-search-results-map-view' ) idx_ihomefinder.initializeListingsTemplate( listings_template ); // If the IDX Provider is "IDX iHomeFinder" } else if ( response.status == 'success' && response.is_there_idx_listings_template == 'no' ) restoreIDXListingsDefaultTemplate(); else if ( response.status == 'failed' && response.is_there_idx_listings_template == 'no' ) alert( response.errmsg ); } } ); } function initActionIDXListingsTemplateChangeView() { if ( $( ".idx-listings-template-view-name" ).length > 0 ) { $( "body" ).on( 'click', '.idx-listings-template-view-name', function() { var view_name = $.trim( $( this ).data( 'idx-listings-template-view-name' ) ); setCookie( 'idx_listings_template_view_name', view_name, 2 ); window.location.href = window.location.href; } ); } } $( document ).ready( function() { initActionIDXListingsTemplate(); initActionIDXListingsTemplateChangeView(); } ); } )( jQuery );