Skip to content

Instantly share code, notes, and snippets.

@jatkins
Created February 26, 2014 19:34
Show Gist options
  • Save jatkins/03892eeeeaf44d41c769 to your computer and use it in GitHub Desktop.
Save jatkins/03892eeeeaf44d41c769 to your computer and use it in GitHub Desktop.
$(document).ready(initPage);
var url = "http://namai.timesdigitalgroup.com/api/v1/";
var host = "http://namai.timesdigitalgroup.com";
$(window).load(function() {
initImages();
initAnimateLogo();
});
function initNav() {
var effectBox;
effectBox = $("#main");
contentBox = $("#container");
navBox = $("#nav");
footerBox = $("#footer");
$("#btn-nav-open").click(function() {
heightContent = $(document).height();
heightNav = navBox.height();
if (!footerBox.length) heightFooter = 0;
else heightFooter = + footerBox.height() + 4;
summaryHeight = heightContent + heightNav;
effectBox.height(summaryHeight);
effectBox.removeClass('close-nav close-nav_r').addClass('open-nav open-nav_r');
effectBox.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
effectBox.addClass('opener');
effectBox.removeClass('closer');
});
});
$("#btn-nav-close").click(function() {
effectBox.removeClass('open-nav open-nav_r opened').addClass('close-nav close-nav_r');
effectBox.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
effectBox.removeClass('close-nav close-nav_r');
effectBox.addClass('closer');
effectBox.height(heightContent);
});
});
$('.main-nav a').each(function(i){
$(this).click(function(){
$('.trigger-nav').touchSlider('move', i);
$('.trigger-nav li').removeClass('active');
$(this).closest('li').addClass('active');
return false;
});
if (window.location.hash) {
$('.trigger-nav a[data-tab="'+window.location.hash.substr(1)+'"]').click();
}
});
}
function initPage(){
initSearch();
initGallery();
initNav();
initSlide();
initTabs();
initInputSelector();
initCounter();
formSearch();
cookieCrisp();
backButton();
resetSearch();
requestCount();
}
function initAnimateLogo() {
$("#logo").attr('class', 'animate');
}
function initGallery(c) {
var ctx = (typeof(c) == 'undefined') ? document : c;
$('.trigger-nav', jQuery(ctx)).each(function(i, el){
var $this = $(this);
$this.touchSlider({
holder: 'div.wrapp',
box: 'div.frame',
'loader': {
'static': {'item': '.item'}
},
mapper: 'single',
renderer: /android/i.test(navigator.userAgent.toLowerCase()) ? 'jquery' : ['css', 'jquery'],
touch: false,
helpers: [
{
'control': {
link:true,
element:'div.control'
}
},
'nav'
]
});
});
var ctx = (typeof(c) == 'undefined') ? document : c;
$('.detail-gallery', jQuery(ctx)).each(function(i, el){
var $this = $(this);
$this.touchSlider({
lockScroll: 'true',
holder: 'div.wrapp',
box: 'div.frame',
'loader': {
'static': {'item': '.item'}
},
mapper: 'index',
renderer: /android/i.test(navigator.userAgent.toLowerCase()) ? 'jquery' : ['css', 'jquery'],
helpers: [
{
'counter': {
element: 'div.control'
}
},
{
'control': {
link: true,
element: 'div.control'
}
},
]
});
});
}
function initImages() {
$('img.photo').each(function(){
$(this).width($(this).width() / 2);
});
}
function initCounter() {
var counter = $("#counter");
if (counter.length) {
var digit = parseInt($("#num").text());
var f_digit = accounting.formatNumber(digit);
$.each(f_digit, function(index, value) {
if (value==',') $(counter).append("<span class='comma'>,</span>")
else $(counter).append("<span class='counter-item num-"+ value + "'><span>" + value + "</span><i></i></span>")
});
initActionCounter(Math.floor($('#hidden_num').val()).toString());
}
}
function initActionCounter(o_digit) {
var _digit = $("#counter").find('span.counter-item');
$(_digit).each(function(index){
var numCell = $(this);
var num = $(this).find('span');
var bg = $(this).find('i');
var _length = num[0].textContent;
var classNum = o_digit[index];
if (classNum != _length)
{
bg.attr('class', 'digit-' + classNum);
num.attr('class', 'digit-' + classNum);
num[0].textContent = classNum;
numCell.removeClass('item-' + _length).removeClass('num-' + _length).addClass('inc').addClass('item-' + classNum);
}
});
}
function initSlide() {
var _parentSlide = '.slide-block';
var _linkSlide = '.slide-block_link';
var _openClassS = 'active';
$(_linkSlide).click(function(){
$(this).closest(_parentSlide).addClass(_openClassS);
return false;
});
}
function initInputSelector() {
$('#min-price').val('$' + addCommas($('.min-price input[type=range]').val()));
$('#max-price').val('$' + addCommas($('.max-price input[type=range]').val())+'+');
$('#min-ft').val($('.min-ft input[type=range]').val());
$('#max-ft').val($('.max-ft input[type=range]').val());
$('#acr-min').val($('.acr-min input[type=range]').val());
$('#acr-max').val($('.acr-max input[type=range]').val());
$('#min-build').val($('.min-build input[type=range]').val());
$('#max-build').val($('.max-build input[type=range]').val());
requestCount();
}
function initSearch() {
if ($('#search-filter').length && $('#search-results').length) {
$('#search-toggle').click(function(){
toggleSearchState();
return false;
});
if ($('#search-toggle').attr('data-state') == 'result') {
$('#search-filter').hide();
$('#search-results').show();
initSearchGallery();
} else {
$('#search-filter').show();
$('#search-results').hide();
}
}
}
function initSearchGallery() {
$('#search-results').touchSlider({
holder: 'div.wrapp',
box: 'div.frame',
'loader': {
'static': {'item': '.item'}
},
mapper: {
'index': {
center: true
}
},
renderer: ['css', 'jquery'],
});
}
function toggleSearchState() {
if ($('#search-toggle').attr('data-state') == 'result') {
setSearchState('filter');
} else {
setSearchState('result');
}
}
function setSearchState(state) {
// fake items
var items = [
'<div class="item"><a href="detail.html" class="item-info"><figure class="item-photo ico-location"><span><img src="images/img-01.jpg" alt="" class="photo" /></span></figure><div class="item-title"><h3>737 Quail Hollow Drive</h3><address>Kingsport, TN 37663</address></div><ul><li class="price">$224,990</li><li>3 bed</li><li>2 bath</li><li>2300 sqft</li><li>MLS# 456789</li></ul></a></div>',
'<div class="item"><a href="detail.html" class="item-info"><figure class="item-photo"></figure><div class="item-title"><h3>737 Quail Hollow Drive</h3><address>Elizabethton TN 37643</address></div></a></div>'
];
switch(state) {
case 'result':
$('#search-results').touchSlider('destroy');
$('#search-results .frame').html('');
// generates search results items
for (var i = 0; i < 30; i++) {
$('#search-results .frame').append(items[Math.round(Math.random())]);
}
$(document).scrollTop(0);
$('#content').css({'height': screen.height});
$('#search-filter').css({
'display': 'block',
'top': 55,
'position': 'absolute',
'z-index': 5,
'opacity': 1
}).animate({
'top': screen.height
}, 500);
$('#search-results').css({'opacity': 0}).show().animate({'opacity': 1});
initSearchGallery();
setTimeout(function() {
$('#search-results').touchSlider('refresh');
$('#search-filter').hide();
$('#content').css({'height': 'auto'});
$('#search-filter').css({'position': 'static', 'z-index':1});
}, 500);
$('#search-toggle').text("Filter Results").attr('data-state', 'result');
break;
case 'filter':
$(document).scrollTop(0);
$('#content').css({'height': screen.height});
$('#search-filter').css({
'display': 'block',
'top': screen.height,
'position': 'absolute',
'z-index': 5,
'opacity': 1
}).animate({
'top': 55
}, 500);
$('#search-results').animate({'opacity': 0});
setTimeout(function() {
$('#search-results').hide();
$('#content').css({'height': 'auto'});
$('#search-filter').css({'position': 'static', 'z-index': 1});
}, 500);
$('#search-toggle').text("See Results").attr('data-state', 'filter');
break;
}
}
function formSearch() {
$('#omni-bar').bind("input", function(){
requestCount();
});
$('#property-type :checkbox').each(function(idx, elm){
var self = $(this);
var link = $('a', self.parent());
if (self.attr('checked')) {
link.parent().removeClass('off');
count += parseInt(elm.value);
console.log(parseInt(elm.value));
}
link.click(function(){
if (self.attr('checked')) {
$(this).parent().addClass('off');
count -= parseInt(elm.value);
self.removeAttr('checked');
} else {
$(this).parent().removeClass('off');
count += parseInt(elm.value);
self.attr('checked', 'checked');
}
$("#count").text(count);
return false;
});
$("#count").text(count);
});
$('#city_select').bind("change", function(){
requestCount();
});
$('.ico-plus#beds').click(function(){
var bedrooms = Math.floor($('#bedrooms').val());
if(bedrooms < 10){
bedrooms = bedrooms + 1;
$('#bedrooms').val(bedrooms);
$('#bedrooms').text(bedrooms);
requestCount();
}
return false;
});
$('.ico-minus#beds').click(function(){
var bedrooms = Math.floor($('#bedrooms').val());
if(bedrooms != 0){
bedrooms = bedrooms - 1;
$('#bedrooms').val(bedrooms);
$('#bedrooms').text(bedrooms);
requestCount();
}
return false;
});
$('.ico-plus#baths').click(function(){
var bathrooms = Math.floor($('#bathrooms').val());
if(bathrooms < 10){
bathrooms = bathrooms + 1;
$('#bathrooms').val(bathrooms);
$('#bathrooms').text(bathrooms);
requestCount();
}
return false;
});
$('.ico-minus#baths').click(function(){
var bathrooms = Math.floor($('#bathrooms').val());
if(bathrooms != 0){
bathrooms = bathrooms - 1;
$('#bathrooms').val(bathrooms);
$('#bathrooms').text(bathrooms);
requestCount();
}
return false;
});
$('.min-price input[type=range]').bind('change', function(){
$('#min-price').val('$' + addCommas($(this).val()));
requestCount();
})
$('.max-price input[type=range]').bind('change', function(){
$('#max-price').val('$' + addCommas($(this).val())+'+');
requestCount();
})
$('.min-ft input[type=range]').bind('change', function(){
$('#min-ft').val($(this).val());
requestCount();
})
$('.max-ft input[type=range]').bind('change', function(){
$('#max-ft').val($(this).val());
requestCount();
})
$('.acr-min input[type=range]').bind('change', function(){
$('#acr-min').val($(this).val());
requestCount();
})
$('.acr-max input[type=range]').bind('change', function(){
$('#acr-max').val($(this).val());
requestCount();
})
$('.min-build input[type=range]').bind('change', function(){
$('#min-build').val($(this).val());
requestCount();
})
$('.max-build input[type=range]').bind('change', function(){
$('#max-build').val($(this).val());
requestCount();
})
}
function requestCount() {
count = 0
if($("#omni-bar").val() == '') {
if($('#detailed').parent().is('.active')) {
$.ajax({
type: "GET",
dataType: "json",
url: url + "listing_types/search",
data: {
city: $("#city_select").val(),
bedrooms: $("#bedrooms").val(),
baths: $("#bathrooms").val(),
min_price: $("#min-price").val(),
max_price: $("#max-price").val(),
min_sqft: $('#min-ft').val(),
max_sqft: $('#max-ft').val(),
min_acr: $('#acr-min').val(),
max_acr: $('#acr-max').val()
}
}).done(function(msg) {
$('#property-type input[type="checkbox"]').each(function(){
$(this).val(0);
});
$.each(msg, function(key, value){
$('.property-list #'+key).val(value);
});
count = 0;
$('#property-type input[type="checkbox"]').each(function(){
if($(this).attr('checked')) {
count += Math.floor($(this).val());
}
});
$("#count").text(count);
$("#get-results").attr("href", "results.php?city=" + $("#city_select").val() + "&bedrooms=" + $("#bedrooms").val() + "&bathrooms=" + $("#bathrooms").val() + "&min_price=" + $('#min-price').val() + "&max_price=" + $('#max-price').val() + "&min_sqft=" + $('#min-ft').val() +
"&max_sqft=" + $('#max-ft').val() + "&min_acr=" + $('#acr-min').val() + "&max_acr=" + $('#acr-max').val() );
});
} else {
$.ajax({
type: "GET",
dataType: "json",
url: url + "listing_types/search",
data: {
city: $("#city_select").val(),
bedrooms: $("#bedrooms").val(),
baths: $("#bathrooms").val(),
min_price: $("#min-price").val(),
max_price: $("#max-price").val()
}
}).done(function(msg) {
$('#property-type input[type="checkbox"]').each(function(){
$(this).val(0);
});
$.each(msg, function(key, value){
$('.property-list #'+key).val(value);
});
count = 0;
$('#property-type input[type="checkbox"]').each(function(){
if($(this).attr('checked')) {
count += Math.floor($(this).val());
}
});
$("#count").text(count);
$("#get-results").attr("href", "results.php?city=" + $("#city_select").val() + "&bedrooms=" + $("#bedrooms").val() + "&bathrooms=" + $("#bathrooms").val() + "&min_price=" + $('#min-price').val() + "&max_price=" + $('#max-price').val());
});
}
} else {
$.ajax({
type: "GET",
dataType: "json",
url: url + "listing_types/search",
data: {
keyword: $("#omni-bar").val()
}
}).done(function(msg) {
$('#property-type input[type="checkbox"]').each(function(){
$(this).val(0);
});
$.each(msg, function(key, value){
$('.property-list #'+key).val(value);
});
$('#property-type input[type="checkbox"]').each(function(){
{
count += Math.floor($(this).val());
}
});
$("#count").text(count);
$("#get-results").attr("href", "results.php?keyword=" + $("#omni-bar").val() );
});
}
}
function cookieCrisp() {
$('.ico-fav').click(function(){
var mls = $('#mls').text();
$.cookie('fav'+mls, mls);
});
}
function backButton() {
$('#back').click(function(){
window.history.go(-1);
});
}
function resetSearch() {
$('#form-reset').click(function() {
location.reload();
});
}
function initTabs() {
$('.toggle-box').each(function(){
$(this).mtTabs({
item: '> .toggle a.toggle-link'
});
});
}
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
(function($) {
$.fn.mtTabs = function(options){
var options = $.extend({
item: 'a',
activeTabClass: 'active',
onStart: null,
onChange: null
}, options);
var $this = $(this);
this.each(function() {
var $this = $(this);
var items = $(options.item, $this);
if (!items.length) return;
items.bind('click', function () {
showTab($(this));
return false;
});
if ($.isFunction(options.onStart)) options.onStart();
$(this).find('.toggle-link').each(function() {
if($(this).parent().is('.active')) $(this).click();
})
function showTab(link) {
var tab = link.attr('href');
items.each(function() {
if (link.attr('href') != $(this).attr('href')) {
$(this).parent().removeClass(options.activeTabClass);
} else {
$(this).parent().addClass(options.activeTabClass);
}
if ($(this).attr('href') != '#') {
$($(this).attr('href')).hide();
}
});
jQuery(tab).show();
if ($.isFunction(options.onChange)) options.onChange(tab);
}
});
return this;
}
})(jQuery);
// When ready...
window.addEventListener("load",function() {
// Set a timeout...
setTimeout(function(){
// Hide the address bar!
window.scrollTo(0, 1);
}, 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment