function get(idstring) {
	return $(idstring);
}

document.preloaded = new Array();
function preload_images() {
	var imgs = preload_images.arguments;
	var srcbase = imgs[0];
	var p = document.preloaded.length;
	for(var i=1; i<imgs.length; i++) {
		document.preloaded[p] = new Image();
		document.preloaded[p].src = srcbase+imgs[i];
		p++;
	}
}

var clickTxtFields = Array();
function focus_txt_field(box) {
	if(!clickTxtFields[box.name])
		clickTxtFields[box.name] = box.value;
	if(!clickTxtFields[box.name] || box.value == clickTxtFields[box.name]) {
		box.value = '';
		box.style.color = "#000";
	}
}
function blur_txt_field(box) {
	if(clickTxtFields[box.name] && box.value == '') {
		box.style.color = "#aaa";
		box.value = clickTxtFields[box.name];
	}
}

function submitSearchForm(f) {
	var submit_url = "";

	for(var i=0; i<f.elements.length; i++) {
		if(f.elements[i].name.substring(f.elements[i].name.length-5) == "_fake" && f.elements[i].multiple == true) {
			var listvalues = Array();
			var v = 0;

			for(var e=0; e<f.elements[i].options.length; e++) {
				try {
					if(f.elements[i].options[e].selected == true && f.elements[i].options[e].value != "")
						listvalues[v++] = f.elements[i].options[e].value;
				} catch(ex) {  }
			}

			if(listvalues.length > 0) {
				if(f.elements[i].name.indexOf("_wild_fake") !== -1) {
					for(var v=0; v<listvalues.length; v++)
						submit_url += "&"+f.elements[i].name.substring(0, f.elements[i].name.length-10) +"_wild"+ v +"="+ ('%'+listvalues[v]+'%').urlencode();
				}
				else
					submit_url += "&"+f.elements[i].name.substring(0, f.elements[i].name.length-5) +"="+ listvalues.join();
			}
		}
		else if(f.elements[i].name == "streetaddress") {
			// starts with a number?   that should be STREETNUMDISPLAY+'%'

			// comma?  split it and call the first part STREETNAME_wild

			// trim out any (road, rd, street, st, ave, avenue, av, blvd, boulevard, etc...) from STREETNAME_wild

			// 5-digit number at the end?  call it a ZIPCODE

			// any parts left?  alert() the user that all listings are in the state of new york and they should use the menu select's for choosing a county and/or town
		}
		else if(f.elements[i].value != "" && f.elements[i].name != "" && f.elements[i].type != "submit" && f.elements[i].type != "reset" && f.elements[i].type != "image" && f.elements[i].name.indexOf('Month') < 0 && f.elements[i].name.indexOf('Day') < 0 && f.elements[i].name.indexOf('Year') < 0) {
			if(f.elements[i].name.substring(f.elements[i].name.length-5) == "_wild")
				submit_url += "&"+f.elements[i].name.substring(0, f.elements[i].name.length-5) +"="+ ('%'+f.elements[i].value+'%').urlencode();
			else
				submit_url += "&"+f.elements[i].name +"="+ f.elements[i].value;
		}
	}

	window.location.href = "/listings/search/results.htm?"+ submit_url.substring(1);
	setTimeout('modal_wait()', 550);
	return false;
}

function modal_wait() {
	Modalbox.show('<div class="ac"><img src="/site_images/property-search.jpg" alt="Searching..."/></div>', {title:'Searching...', width:440, height:340});
}

window.onresize  = function() {
	var floatdiv = $('floatdiv');

	if(!floatdiv) return;

	if(getCookie('recentactivity') == 'off') {
		if(floatdiv.readAttribute('floatingWhere') != 'close')
			toggle_floatdiv(floatdiv);

		return close_floatdiv(floatdiv);
	}

	var coreWidth = $('core').scrollWidth;
	var floatingWidth = floatdiv.getWidth() * 2;

	if(document.body.clientWidth >= (coreWidth+floatingWidth)) {
		floatingMenu.targetX = -1 * floatingWidth/2 - 6;
		floatdiv.firstChild.firstChild.src = floatdiv.firstChild.firstChild.src.replace('_w', '_e');
		Element.writeAttribute(floatdiv, 'floatingWhere', 'open');
	}
	else {
		floatingMenu.targetX = Math.min(-21, Math.min(-21, -1*(document.body.clientWidth-coreWidth)/2+6));
		floatdiv.firstChild.firstChild.src = floatdiv.firstChild.firstChild.src.replace('_e', '_w');
		Element.writeAttribute(floatdiv, 'floatingWhere', 'close');
	}
}

function toggle_floatdiv(floatdiv) {
	if(!floatdiv) return;

	if(getCookie('recentactivity') == 'off') {
		floatdiv.setStyle({height: 'auto'});
		$(floatdiv.lastChild.firstChild).show();
		floatingMenu.targetY = 'center';
		setCookie('recentactivity', 'on');
	}

	if(floatdiv.readAttribute('floatingWhere') != 'close') {
		floatingMenu.targetX = -21;
		floatdiv.firstChild.firstChild.src = floatdiv.firstChild.firstChild.src.replace('_e', '_w');
		Element.writeAttribute(floatdiv, 'floatingWhere', 'close');
	}
	else {
		floatingMenu.targetX = -1 * floatdiv.getWidth() - 6;
		floatdiv.firstChild.firstChild.src = floatdiv.firstChild.firstChild.src.replace('_w', '_e');
		Element.writeAttribute(floatdiv, 'floatingWhere', 'open');
	}
}
function close_floatdiv(floatdiv) {
	if(!floatdiv) return;

	if(floatdiv.readAttribute('floatingWhere') != 'close')
		toggle_floatdiv(floatdiv);

	$(floatdiv.lastChild.firstChild).hide();
	floatingMenu.targetY = 15;
	floatdiv.setStyle({height: '16px'});

	setCookie('recentactivity', 'off');
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) +
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) +
		";path=/";
}

function getCookie(c_name) {
	if(document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if(c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if(c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/pad [v1.0]
String.prototype.pad = function(l, s, t){
	return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
		+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
		+ this + s.substr(0, l - t) : this;
};

String.prototype.urlencode = function () {
	var SAFECHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < this.length; i++ ) {
		var ch = this.charAt(i);
	    if(ch == " ") {
		    encoded += "+";
		} else if(SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}

	return encoded;
};
