$("#tools").hide();

$(document).ready(function() {
		
	//Hide addthis from msie 6
	/*if ($.browser.msie && $.browser.version < 7) {
		$("#tools").html('<a class="white" href="javascript:popup_win(\'print_listing2.aspx?lid=753811\',750,600,50,50);"><img src="/images/printer.gif" /> Print</a><br />' +
					'<a class="white" target="_blank" href="http://www.fenwick-keats.com/mortgagecalculator.aspx"><img src="/images/calc.png" /> Mortgage Calculator</a><!-- <asp:Label id="Calculator" runat="server"></asp:Label> --><br />' +
					'<a class="white" href="javascript:__doPostBack(\'ctl00$ContentPlaceHolder1$AddToCart\',\'\')"><img class="tool" id="savetool" src="images/cart.png" /> Save to Favorites</a>');
	}*/
	
	$("#tools").show();
	
	//Update print link to point at correct property
	var currurl = window.location.href;
	var currurlbits = currurl.split('?');
	var printurl = "print_listing2.aspx?" + currurlbits[1];
	$("li.printericon a").attr("href", "javascript:popup_win('"+printurl+"',750,600,50,50)");
	
	//Make no fee red and bold
	$("[id*=lblNoFee]").each(function() {
		var currhtml = $(this).html();
		$(this).html('<span class="red strong">' + currhtml + '</span>');
	});
		
	//Fix display of Mortgage Calculator Icon
	/* $("#ctl00_ContentPlaceHolder1_Calculator br").replaceWith("&nbsp;"); */
	$("#Image4").attr("onMouseOut", "");
	$("#Image4").attr("onMouseOver", "");
	$("#Image4").attr("width", "16");
	$("#Image4").attr("height", "16");
	$("#Image4").attr("src", "/images/calc.png");
	
	$("#EmailAgent2 a:nth-child(1)").prepend("<img src=\"/images/email.png\" alt=\"Email Icon\" />");
	$("#EmailAgent2 a:nth-child(3)").prepend("<img src=\"/images/vcard.png\" alt=\"Vcard Icon\" />");
	$("#EmailAgent2 a:nth-child(5)").prepend("<img src=\"/images/house.png\" alt=\"Listings Icon\" />");
	
	
	var glocation = replaceAll($("#ctl00_ContentPlaceHolder1_Address").html(), " ", "+");
	var newloc = glocation.replace(/#.+/, "");
	var newloc = newloc.replace(/\,* *Apt.+/, "");
	
	
	$("#gmap iframe").attr("src", "http://maps.google.com/maps?f=q&source=s_q&hl=en&q=loc:" + newloc + "+NY&output=embed");
	
	
	  /*****************/
	 /* Listings Page */
	/*****************/	
	
	// Change main image when hovering over thumbnails on listings page
	$(".thumb").hover(function() {
		var image = $(this).attr("src");
		$("#ctl00_ContentPlaceHolder1_MainPicture").attr("src", image);		
	});	
	
	// Remove maxw and maxh from parent a
	$(".thumb").parent("a").each(function() {
		var href = $(this).attr('href');
		newhref = href.replace("&maxw=400&maxh=266", "&maxw=990&maxh=745")
		$(this).attr('href', newhref);
	});
	
	// Pop up lightbox when clicking on thumbnail on listings page
	$(".thumb").parent("a").lightBox();
	
	// Remove apartment from listing detail address
/*
	var addr = $("#ctl00_ContentPlaceHolder1_Address").html();
	var newaddr = addr.replace(/#.+/, "");
	var newaddr = newaddr.replace(/\,* *Apt.+/, "");
	$("#ctl00_ContentPlaceHolder1_Address").replaceWith(newaddr);
*/
	
});

function replaceAll(strText, takeout, putin) {
	var strReplaceAll = strText;
	var intIndexOfMatch = strReplaceAll.indexOf( takeout );
	 
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		strReplaceAll = strReplaceAll.replace( takeout, putin )
		 
		// Get the index of any next matching substring.
		intIndexOfMatch = strReplaceAll.indexOf( takeout );
	}
	
	return strReplaceAll;
}
