﻿var Sawbuck = window.Sawbuck || {}; var $j = jQuery.noConflict(); Sawbuck.Utilities = function() {
	var BASE_URL = ""; function typeOf(value) { var s = typeof value; if (s === 'object') { if (value) { if (value instanceof Array) { s = 'array'; } } else { s = 'null'; } } return s; }
	Array.prototype.sum = function() { for (var i = 0, sum = 0; i < this.length; sum += this[i++]); return parseFloat(sum); }
	Array.prototype.first = function() { return this[0]; }
	Array.prototype.last = function() { return this[this.length - 1]; }
	Array.prototype.each = function(iterator) {
		for (var i = 0; i < this.length; i++) {
			try { iterator.call(this, this[i], i); }
			catch (error) { } 
		}
		return this;
	}
	Array.prototype.collect = function(iterator) { var results = []; this.each(function(value, index) { results.push(iterator(value, index)); }); return results; }
	Array.prototype.findAll = function(iterator) { var results = []; this.each(function(value, index) { if (iterator(value, index)) results.push(value); }); return results; }
	Array.prototype.detect = function(iterator) { return this.findAll(iterator).first(); }
	Array.prototype.find = Array.prototype.detect; Array.prototype.invoke = function(method) { return this.collect(function(value, index) { return value[method].apply(value, arguments.slice(1)); }); }
	Number.prototype.format = function(decimalPoints, thousandsSep, decimalSep) {
		var val = this + '', re = /^(-?)(\d+)/, x, y; if (decimalPoints != null) val = this.toFixed(decimalPoints); if (thousandsSep && (x = re.exec(val))) { for (var a = x[2].split(''), i = a.length - 3; i > 0; i -= 3) a.splice(i, 0, thousandsSep); val = val.replace(re, x[1] + a.join('')); }
		if (decimalSep) val = val.replace(/\./, decimalSep); return val;
	}
	if (typeof Number.prototype.toFixed != 'function' || (.9).toFixed() == '0' || (.007).toFixed(2) == '0.00') Number.prototype.toFixed = function(f) {
		if (isNaN(f *= 1) || f < 0 || f > 20) f = 0; var s = '', x = this.valueOf(), m = ''; if (this < 0) { s = '-'; x *= -1; }
		if (x >= Math.pow(10, 21)) m = x.toString(); else {
			m = Math.round(Math.pow(10, f) * x).toString(); if (f != 0) {
				var k = m.length; if (k <= f) { var z = '00000000000000000000'.substring(0, f + 1 - k); m = z + m; k = f + 1; }
				var a = m.substring(0, k - f); var b = m.substring(k - f); m = a + '.' + b;
			} 
		}
		if (m == '0') s = ''; return s + m;
	}
	Number.prototype.toPrice = function(decimalPoints) { decimalPoints = decimalPoints || 0; return "$" + this.format(decimalPoints, ","); }
	String.prototype.toPrice = function(decimalPoints) { decimalPoints = decimalPoints || 0; return "$" + parseFloat(this).format(decimalPoints, ","); }
	String.prototype.startsWith = function(character) { if (!character || character.length > this.length) return false; return (this.substr(0, character.length) == character); }
	String.prototype.format = function(decimalPoints, thousandsSep, decimalSep) { return parseFloat(this).format(decimalPoints, thousandsSep, decimalSep); }
	String.prototype.truncate = function(n, trimText) {
		trimText = trimText || "..."
		return this.substr(0, n - 1) + (this.length > n ? trimText : '');
	}; String.prototype.trim = function() { var string = this || ""; return string.replace(/^\s*/, '').replace(/\s*$/, ''); }
	function toJson(obj) { switch (typeof obj) { case 'object': if (obj) { var list = []; if (obj instanceof Array) { for (var i = 0; i < obj.length; i++) { list.push(toJson(obj[i])); } return '[' + list.join(',') + ']'; } else { for (var prop in obj) { list.push('"' + prop + '":' + toJson(obj[prop])); } return '{' + list.join(',') + '}'; } } else { return 'null'; } case 'string': return '"' + obj.replace(/(["'])/g, '\\$1') + '"'; case 'number': case 'boolean': return new String(obj); } }
	function cloneObject(what) { var newObj = (what instanceof Array) ? [] : {}; for (i in what) { if (i == 'clone') continue; if (what[i] && typeof what[i] == "object") { newObj[i] = cloneObject(what[i]); } else newObj[i] = what[i] } return newObj; }
	function deep_clone(obj) { eval("var tmp = " + toJson(obj)); return tmp; }
	function isSame(obj1, obj2) { return toJson(obj1) == toJson(obj2); }
	function parseImagePath(listingID) {
		listingID = listingID.toString(); var pieces = []; var prevStart = listingID.length; var start = prevStart - 3; while (start > 0) { var len = prevStart - start; pieces.push(listingID.substr(start, len)); prevStart = start; start = prevStart - 3; }
		pieces.push(listingID.substr(0, prevStart)); pieces.reverse(); return pieces.join("/");
	}
	function getImage(listingID, size, existsFlag, listingType) {
		if (!existsFlag || !listingID) {
			listingID = listingID || 0; if (listingType && listingType >= 1) {
				var imageID = listingType + "_" + (parseInt(listingID) % 3)
				switch (size) { case "listing": return BASE_URL + "no_image/" + imageID + "_listing.png"; case "mini": return BASE_URL + "no_image/" + imageID + "_mini.png"; case "gallery": return BASE_URL + "no_image/" + imageID + "_gallery.png"; default: return BASE_URL + "no_image/orig.gif"; } 
			}
			switch (size) { case "listing": return BASE_URL + "no_image/listing.gif"; case "mini": return BASE_URL + "no_image/mini.gif"; case "gallery": return BASE_URL + "no_image/gallery.gif"; default: return BASE_URL + "no_image/orig.gif"; } 
		}
		var sizeSuffix = ""; switch (size) { case "listing": sizeSuffix = "-listing.jpg"; break; case "mini": sizeSuffix = "-mini.jpg"; break; case "gallery": sizeSuffix = "-gallery.jpg"; break; default: sizeSuffix = "-orig.jpg"; break; }
		return BASE_URL + parseImagePath(listingID) + sizeSuffix;
	}
	function alterCSS(cssClass, element, value) {
		var cssRules; if (document.all) { cssRules = 'rules'; }
		else if (document.getElementById) { cssRules = 'cssRules'; }
		var added = false; try {
			for (var S = 0; S < document.styleSheets.length; S++) {
				for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) { if (document.styleSheets[S][cssRules][R].selectorText == cssClass) { if (document.styleSheets[S][cssRules][R].style[element]) { document.styleSheets[S][cssRules][R].style[element] = value; added = true; break; } } }
				if (!added) { if (document.styleSheets[S].insertRule) { document.styleSheets[S].insertRule(cssClass + ' { ' + element + ': ' + value + '; }', document.styleSheets[S][cssRules].length); } else if (document.styleSheets[S].addRule) { document.styleSheets[S].addRule(cssClass, element + ': ' + value + ';'); } } 
			} 
		}
		catch (Error) { } 
	}; function readCookie(name) {
		var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); }
		return null;
	}
	function deleteCookie(cookie_name) { var cookie_date = new Date(); cookie_date.setTime(cookie_date.getTime() - (1 * 24 * 60 * 60 * 1000)); document.cookie = cookie_name + "=; expires=" + cookie_date.toGMTString(); Sawbuck.Debugger.report(cookie_name + "=; expires=" + cookie_date.toGMTString()); Sawbuck.Debugger.report(document.cookie); }
	function writeCookie(cookie_name, value) { document.cookie = cookie_name + "=" + value + ";path=/;domain=sawbuck.com"; Sawbuck.Debugger.report("Set cookie: " + cookie_name + "=" + value + ";path=/;domain=sawbuck.com"); Sawbuck.Debugger.report(document.cookie); }
	function daysAgo(t) { var today = new Date().getTime(); var days = Math.floor((today - t) / (1000 * 60 * 60 * 24)); return days; }
	function loadListingMedia(listingID) {
		var webService = "/service/WebService.asmx/ListingMediaByID"; $j.ajax({ type: "POST", url: webService, data: Sawbuck.Utilities.toJson({ listingID: listingID }), contentType: "application/json; charset=utf-8", dataType: "json", processData: false, success: function(response) {
			var images = response.d; $j("#listingPics div:first").html(""); for (var i = 0; i < images.length; i++) {
				var image = $j("<img>").attr({ src: images[i].u, vspace: 2, "class": (i == images.length - 1) ? 0 : i + 1, style: "display: block; cursor: pointer; margin-top: 4px" }); var image_div = $j("<div>").attr({ style: "vertical-align: middle; height: 280px; width: 428px; position: relative;" }); image_div.append(image); if (images[i].c && images[i].c != "null" && images[i].c != "Caption" && images[i].c != "undefined")
					image_div.append("<p class='caption' style='position: absolute; bottom: 6px; left: 0; width: 100%; background: #FFFFFF'>" + images[i].c + "</p>"); $j("#listingPics div:first").append(image_div); image = null; image_div = null;
			}
			var listing; try { listing = Sawbuck.Page.getListingByID(listingID); $j("#listingPics").dialog("option", "title", "Pictures of " + listing.addr); } catch (e) { listing = { addr: "", url: "", stat: 0, el: false }; $j("#listingPics .tour").hide(); $j("#listingPics .listingLink").hide(); }
			$j("#listingPics img").click(function(ui) { $j("#listingPics div:first").scrollTop(this.className * 284) }); $j("#listingPics .tour").attr({ href: "/property" + listing.url + "/tour", target: "_blank" }); $j("#listingPics .listingLink").attr({ href: "/property" + listing.url, target: "_blank" }); try { if (Sawbuck.Page.getURL() == "/sold" || listing.stat != 1 || listing.el) $j("#listingPics .tour").hide(); else $j("#listingPics .tour").show() } catch (e) { }; $j("#listingPics").dialog("open"); Sawbuck.Reporting.trackEvent("Listing Pictures", "From Search Page", "# of Pictures", images.length);
		}, error: function(e, t) { } 
		});
	}
	var _videos_watched = 0; function showListingVideo(listingId) {
		var mediaLookupURL = "http://sawbuckfe.sundayskylb1.com/MediaLookup/?accountId=Sawbuck%26videoletId=ListingShowcase%26key="; var mediaLookupSuffix = ":1.0:default:overlaid_video"; var options = {}; if (_videos_watched < 1 && false)
			options.commercialFile = Sawbuck.SERVER + '/video/ss_intro_2.flv'; options.isPopup = false; options.screenColor = "#ffffff"; options.autoStart = true; sundaySky.create(mediaLookupURL + listingId + mediaLookupSuffix, options); $j(".ssVideo").dialog('open'); try { $j(".ssVideo").dialog('option', 'title', "Video of " + Sawbuck.Page.getListingByID(listingId).addr); } catch (e) { }; Sawbuck.Reporting.trackEvent("Listing Video Played", "From Search Page"); _videos_watched++;
	}
	return { alterCSS: alterCSS, createDelegate: function(instance, method) { return function() { return method.apply(instance, arguments); } }, cloneObject: cloneObject, isSame: isSame, isArray: function(o) { return (o instanceof Array); }, toJson: toJson, typeOf: function(value) { return typeOf(value); }, readCookie: readCookie, writeCookie: writeCookie, deleteCookie: deleteCookie, trim: function(string) { string = string || ""; return string.replace(/^\s*/, '').replace(/\s*$/, ''); }, setBaseMediaURL: function(url) { BASE_URL = url; }, getImage: getImage, loadListingMedia: loadListingMedia, showListingVideo: showListingVideo, daysAgo: daysAgo, modo: function(dialogBoxID) { $j("#" + dialogBoxID).dialog('open'); return false; }, selectAll: function(group) { $j(group).find('input').attr('checked', 'checked'); }, unselectAll: function(group) { $j(group).find('input').removeAttr('checked'); }, refreshAds: function() {
		try {
			$j(".ad iframe")[0].src = $j(".ad iframe")[0].src
			$j(".partnerContent iframe")[0].src = $j(".partnerContent iframe")[0].src
		} catch (e) { };
	}, URLEncode: function(clearString) {
		clearString = clearString || ""; var output = ''; var x = 0; clearString = clearString.toString(); var regex = /(^[a-zA-Z0-9_.]*)/; while (x < clearString.length) {
			var match = regex.exec(clearString.substr(x)); if (match != null && match.length > 1 && match[1] != '') { output += match[1]; x += match[1].length; } else {
				if (clearString[x] == ' ')
					output += '+'; else { var charCode = clearString.charCodeAt(x); var hexVal = charCode.toString(16); output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase(); }
				x++;
			} 
		}
		return output;
	} 
	};
} (); Sawbuck.Debugger = function() {
	var timer = 0; var stopWatch; var console = window.console || false; if (!console) {
		window.console = {}
		window.console.log = function() { return false; };
	}
	function report(args) { if (window.console.log(args) === false) { if (typeof args == "object") args = "<span class='object'>" + Sawbuck.Utilities.toJson(args) + "</span>"; var now = new Date(); $j("#debugger .debugContent ul").append("<li title='" + now + "' >" + args + "</li>"); $j("#debugger").show(); $j("#debugger .debugContent").scrollTop($j("#debugger .debugContent ul").height()); delete now; } }
	return { report: function(args) { return false; }, clear: function() { $j("#debugger .debugContent ul").text(""); return false; }, close: function() { $j("#debugger").hide(); return false; }, startTimer: function() { if (!stopWatch) timer = 0; stopWatch = setInterval(function() { timer++ }, 1); }, pauseTimer: function() { clearInterval(stopWatch); }, stopTimer: function() { clearInterval(stopWatch); stopWatch = null; return timer; }, getTime: function() { return timer; } }
} (); (function($) { $.fn.hoverIntent = function(f, g) { var c = { sensitivity: 7, interval: 100, timeout: 0 }; c = $.extend(c, g ? { over: f, out: g} : f); var d, cY, pX, pY; var h = function(a) { d = a.pageX; cY = a.pageY }; var i = function(a, b) { b.hoverIntent_t = clearTimeout(b.hoverIntent_t); if ((Math.abs(pX - d) + Math.abs(pY - cY)) < c.sensitivity) { $(b).unbind("mousemove", h); b.hoverIntent_s = 1; return c.over.apply(b, [a]) } else { pX = d; pY = cY; b.hoverIntent_t = setTimeout(function() { i(a, b) }, c.interval) } }; var j = function(a, b) { b.hoverIntent_t = clearTimeout(b.hoverIntent_t); b.hoverIntent_s = 0; return c.out.apply(b, [a]) }; var k = function(e) { var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; while (p && p != this) { try { p = p.parentNode } catch (e) { p = this } } if (p == this) { return false } var a = jQuery.extend({}, e); var b = this; if (b.hoverIntent_t) { b.hoverIntent_t = clearTimeout(b.hoverIntent_t) } if (e.type == "mouseover") { pX = a.pageX; pY = a.pageY; $(b).bind("mousemove", h); if (b.hoverIntent_s != 1) { b.hoverIntent_t = setTimeout(function() { i(a, b) }, c.interval) } } else { $(b).unbind("mousemove", h); if (b.hoverIntent_s == 1) { b.hoverIntent_t = setTimeout(function() { j(a, b) }, c.timeout) } } }; return this.mouseover(k).mouseout(k) } })(jQuery);