function show_promo() {
     $(".promo").slideDown("slow");
}

function hide_promo() {
    $(".promo").slideUp("slow");
}

function toggle_awesome_bar_text() {
    var defaultText = 'Search contacts, leads, properties ...'
    jQuery('#awesome_bar').focus(function(){
        if (this.value == defaultText) {
            jQuery(this).val('');
            jQuery(this).removeClass("faded");
        }
        
    });
    jQuery('#awesome_bar').blur(function() {
        if(jQuery.trim(this.value) == '') {
            this.value = defaultText;
            jQuery(this).addClass("faded");
        }
    });
}

function toggle_add_listing_text() {
    var defaultText = 'Enter an apartment listing URL from one of the supported sites...';
    var defaultText2 = 'Enter an apartment listing URL...';
    var text_index = 1;
    $('#id_text').focus(function(){
        if (this.value == defaultText) 
            $(this).val('');
        else if (this.value == defaultText2) {
            $(this).val('');
            text_index = 2;
        }
        
    });
    $('#id_text').blur(function() {
        if($.trim(this.value) == '') {
            if(text_index == 1)
                this.value = defaultText;
            else
                this.value = defaultText2;

        }
    });
}
function toggle_sort_text() {
    var defaultText = 'Enter a street address to view distance to listings'
    $('#sort').focus(function(){
        if (this.value == defaultText) 
            $(this).val('');
        
    });
    $('#sort').blur(function() {
        if($.trim(this.value) == '')
            this.value = defaultText;
    });    
    
}

function toggle_address_text() {
    var defaultText = 'street address or neighborhood...'
    $('#id_address').focus(function(){
        if (this.value == defaultText) 
            $(this).val('');
        
    });
    $('#id_address').blur(function() {
        if($.trim(this.value) == '')
            this.value = defaultText;
    });    
}
function toggle_rent_text() {
    var defaultText = 'monthly'
    rent_el = $("#id_rent");
    if (rent_el.length < 1)
        rent_el = $("#id_max_rent");
    $(rent_el).focus(function(){
        if (this.value == defaultText) 
            $(this).val('');
        
    });
    $(rent_el).blur(function() {
        if($.trim(this.value) == '')
            this.value = defaultText;
    });
}
function toggle_rooms_text() {
    var defaultText = '1, 2, studio, loft, room...'
    $('#id_bedrooms').focus(function(){
        if (this.value == defaultText) 
            $(this).val('');
        
    });
    $('#id_bedrooms').blur(function() {
        if($.trim(this.value) == '')
            this.value = defaultText;
    });
}
function stripHTML(text){
// remove on ul and li tags
//var re= /<\S[^><]*>/g
var re = /<(ul|li|\/ul|\/li)[^><]*>/g;
return text.replace(re, "")
}


function showDropDown() {
    $(".toplink").click(function() {
        $(this).parent().find("ul").slideDown('fast').show();
        $(this).parent().hover(function(){ 
        }, function() {
            $(this).parent().find("ul").slideUp('fast');
        });
        return false;
        });
}


// Find the difference between two arrays, return whats new and 
// and whats removed and those unchanged
function arr_diff(new_arr, old_arr) {
    var added=[], removed=[], unchanged_indices=[], unchanged=[]
    var a = new_arr.slice(0);
    var b = old_arr.slice(0);
    for (var i=0;i<new_arr.length;i++) {
        var exists=false;
        var old_index = -1;
        var new_index = -1;
        for (var j=0;j<old_arr.length;j++) {
            if(new_arr[i].id == old_arr[j].id) {
                old_index = j;
                new_index = i;
                exists=true;
                break;
                //remove from a and b
                //a.splice(i, 1);
                //b.splice(j, 1)
                //unchanged_indices.push([j]);

            }
        }
        if(exists) {
            //Is not a new marker, remove marker from a and b
            for (var ai = 0; ai<a.length;ai++) {
                if(a[ai].id == new_arr[new_index].id) {
                    a.splice(ai,1);
                }
            }
            for (var bi=0;bi<b.length;bi++) {
                if(b[bi].id == old_arr[old_index].id) {
                    b.splice(bi, 1);
                }
            }
            // Add to unchanged array
            unchanged.push(new_arr[new_index]);
            unchanged_indices.push(old_index);
            //a = a.splice(a.indexOf(new_arr[i]), 1);
            //b = b.splice(b.indexOf(old_arr[old_index]), 1);
        }
    } 
    for(var k=0;k<a.length;k++)
        added.push(a[k]);
    for (var l=0;l<b.length;l++)
        removed.push(b[l]);
    var objDictionary = new Object;
    objDictionary.added = added;
    objDictionary.removed = removed;
    objDictionary.unchanged = unchanged;
    objDictionary.unchanged_indices = unchanged_indices;
    return objDictionary;
}   

String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
    }

function getQueryString() {
    var assoc = new Array();
    var queryString = unescape(location.search.substring(1));
    var keyValues = queryString.split('&');
    for (var i in keyValues) {
        var key = keyValues[i].split('=');
        assoc[key[0]] = key[1];
    }
    return assoc;
}


Date.prototype.getDayName = function ()
{
    switch(this.getDay())
    {
        case 0: return 'Sunday';
        case 1: return 'Monday';
        case 2: return 'Tuesday';
        case 3: return 'Wednesday';
        case 4: return 'Thursday';
        case 5: return 'Friday';
        case 6: return 'Saturday';
    }
};

String.prototype.padLeft = function (value, size) 
{
    var x = this;
    while (x.length < size) {x = value + x;}
    return x;
};

Date.prototype.getMonthName = function ()
{
    //return this.toLocaleString().replace(/[^a-z]/gi,'');
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 
        'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    return months[this.getMonth()];
};

Date.prototype.toFormattedString = function (f)
{
    var nm = this.getMonthName();
    var nd = this.getDayName();
    f = f.replace(/yyyy/g, this.getFullYear());
    f = f.replace(/yy/g, String(this.getFullYear()).substr(2,2));
    f = f.replace(/MMM/g, nm.substr(0,4).toUpperCase());
    f = f.replace(/Mmm/g, nm.substr(0,4));
    f = f.replace(/MM\*/g, nm.toUpperCase());
    f = f.replace(/Mm\*/g, nm);
    f = f.replace(/mm/g, String(this.getMonth()+1).padLeft('0',2));
    f = f.replace(/DDD/g, nd.substr(0,3).toUpperCase());
    f = f.replace(/Ddd/g, nd.substr(0,3));
    f = f.replace(/DD\*/g, nd.toUpperCase());
    f = f.replace(/Dd\*/g, nd);
    f = f.replace(/dd/g, String(this.getDate()).padLeft('0',2));
    f = f.replace(/d\*/g, this.getDate());
    return f;
};

//Get value of querystring from URL by name
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

function get_hashtag() {
    var re = /#\w+$/gi;
    var url = document.location.toString();
    var match = re.exec(url);
    var anchor = null;
    if (match != null) 
        anchor = match[0].substr(1);
    return anchor;

}

function changeView(tabname) { 
  var tabs = ['#table', '#map'],
      index = (tabname == '#table') ? 1 : 0,
      current_tab = tabs[index],
      tab_to_go = tabname || "#map";
  if ($(tab_to_go + "_view").is(':visible'))
    return false;

  $(current_tab+"_view").hide(10,function(){
      $(tab_to_go + "_view").show(400);
      mpmetrics.track('action', {'Change View':tab_to_go});
    });
  $('#button_'+current_tab.substring(1)+"_view").addClass('main_button_light');
  $('#button_'+tab_to_go.substring(1) + "_view").removeClass('main_button_light');
  
  if (tabname == '#map') {
    //$("#enlarge_link").text("Shrink Map");
    //toggleMapSize();
    //GMapsObject.setMapView();
  }
  return false;
}

