﻿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.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) {
		if (!existsFlag || !listingID) { 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); }
	return { alterCSS: function(cssClass, elt, val) { return alterCSS(cssClass, elt, val); }, createDelegate: function(instance, method) { return function() { return method.apply(instance, arguments); } }, cloneObject: function(what) { return cloneObject(what); }, isSame: function(obj1, obj2) { return isSame(obj1, obj2); }, isArray: function(o) { return (o instanceof Array); }, toJson: function(obj) { return toJson(obj); }, typeOf: function(value) { return typeOf(value); }, readCookie: function(name) { return readCookie(name); }, writeCookie: function(cookieName, value) { return writeCookie(cookieName, value); }, deleteCookie: function(cookieName) { return deleteCookie(cookieName); }, trim: function(string) { string = string || ""; return string.replace(/^\s*/, '').replace(/\s*$/, ''); }, setBaseMediaURL: function(url) { BASE_URL = url; }, getImage: function(listingID, size, existsFlag) { return getImage(listingID, size, existsFlag); }, 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'); }, 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 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; } }
} (); (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);
