function listingAction(action, listing, child, userAction) {
	var afterCallback = function () {
		switch (action) {
		   case 'spam':
				setTimeout(function() { $(child).innerHTML = 'Marked as spam.'; }, 100);
				break;
			case 'scam':
			case 'fraud':
				setTimeout(function() { $(child).innerHTML = 'Thank you for reporting this item as spam/fraud - we will investigate it. (If you clicked the link by mistake, please don\'t worry. It takes more than one complaint to get the item removed.)'; }, 100);
				break;
			case 'abuse':
				setTimeout(function() { $(child).innerHTML = 'Thank you for reporting this item as abusive - we will investigate it. (If you clicked the link by mistake, please don\'t worry. It takes more than one complaint to get the item removed.)'; }, 100);
				break;	
			case 'inappropriate':
				setTimeout(function() { $(child).innerHTML = 'Thank you for reporting this item as inappropriate - we will investigate it.'; }, 100);
				break;
			case 'unavailable':
				setTimeout(function() { $(child).innerHTML = 'Thank you. We will remove the listing after verifying it is unavailable.'; }, 100);
				break;
			case 'suspicious':
				setTimeout(function() { $(child).innerHTML = 'Marked as suspicious. Refresh this page to see the change.'; }, 100);
				break;
			case 'unmarkspam':
			case 'unmarksusp':
				setTimeout(function() { $(child).innerHTML = 'Listing has been unmarked.'; }, 100);
				break;
			case 'adminremove':
				setTimeout(function() { $(child).innerHTML = 'Listing has been administratively removed.'; }, 100);
				break;
		}
	};
	var ajaxRequest = new XMLHttpRequestObject(null, afterCallback, null);
	ajaxRequest.send('/ajax/actions/?action=' + userAction + '&listing=' + listing);
	$(child).innerHTML = 'Marking as ' + action + '...';
}

// Render a listing using an AJAX callback.
function fillListing(divid, listingid) {
	var expandedListing = document.getElementById(divid);
	var fullbody = getElementsByClass('sbody', expandedListing, 'span');
	var vin = getElementsByClass('vin', expandedListing, 'span');
	var frequency = getElementsByClass('listing-frequency', expandedListing, 'strong');
	var map = getElementsByClass('map', expandedListing, 'div'); 
	var mapMessage = getElementsByClass('map-message', expandedListing, 'div');
	new XMLHttpRequestObject(
		function () {},
		function () {},
		function (xml) {
			fullbody = fullbody[0];
			if (fullbody) {
				if (fullbody.innerHTML == '') {
					body = null;
					tags = xml.getElementsByTagName('body');
					if (tags.length > 0) {
						body = tags[0];
					}
					if (body &&
					    body.childNodes &&
					    body.childNodes.length > 0 &&
					    body.childNodes[0].nodeValue !=  "") {
						fullbody.innerHTML = body.childNodes[0].nodeValue;
					} else {
						fullbody.innerHTML = "No description available";
					}
				}
			}
			
			vin = vin[0] ? vin[0] : null;
			if (vin) {
				if (vin.innerHTML == '') {
					body = null;
					tags = xml.getElementsByTagName('vin');
					if (tags.length > 0) {
						body = tags[0];
					}
					if (body &&
					    body.childNodes &&
					    body.childNodes.length > 0 &&
					    body.childNodes[0].nodeValue !=  "") {
						vin.innerHTML = body.childNodes[0].nodeValue;
					} else {
						vin.innerHTML = "No VIN available";
					}
				}
			}
			
			if (frequency.length > 0) {
				frequency = frequency[0];
				if (frequency.innerHTML == '' || frequency.innerHTML == 'very rarely') {
					similar = null;
					tags = xml.getElementsByTagName('num_similar');
					var headerText = "Listing Frequency";
					if (tags.length > 0) {
						similar = tags[0];
					}
					if (similar &&
					    similar.childNodes &&
					    similar.childNodes.length > 0 &&
					    similar.childNodes[0].nodeValue !=  "0") {
						var days = parseInt(180/similar.childNodes[0].nodeValue);
						if (days == 0) {
							frequency.innerHTML = "every few hours";
						} else if (days == 1) {
							frequency.innerHTML = 'every day';
						} else {
							frequency.innerHTML = "every " + days + " days";
						}
					} else if (similar &&
					           similar.childNodes &&
					           similar.childNodes.length > 0 &&
					           similar.childNodes[0].nodeValue ==  "0") {
						frequency.innerHTML = "very rarely";
					} else {
						frequency.innerHTML = "very rarely";
					}
				}
			}

			var loadMapFunc = function() {
			
				if (!window.GBrowserIsCompatible ||
				    !window.GControl ||
				    !window.GMap2)
				{
					setTimeout(loadMapFunc, 200);
					return;
				}

				if (map.length > 0 && GBrowserIsCompatible()) {
					map = map[0];
					mapMessage = mapMessage[0];
					
					var lat = xml.getElementsByTagName('latitude');
					if (lat) lat = lat[0];
					if (lat && lat.childNodes && lat.childNodes.length > 0) {
						lat = lat.childNodes[0].nodeValue;
					} else {
						lat = null;
					}
					var lng = xml.getElementsByTagName('longitude');
					if (lng) lng = lng[0];
					if (lng && lng.childNodes && lng.childNodes.length > 0) {
						lng = lng.childNodes[0].nodeValue;
					} else {
						lng = null;
					}

					var loc = xml.getElementsByTagName('location_code');
					if (loc) loc = loc[0];
					if (loc && loc.childNodes && loc.childNodes.length > 0)
					{
						loc = loc.childNodes[0].nodeValue;
					}
					else
					{
						loc = null;
					}
					
					if (lat && lng && loc && (loc.substr(0, 5) === 'addr:' || loc.substr(0, 6) === 'saddr:'))
					{
						var rough = (loc.substr(0, 6) === 'saddr:');
					
						map.style.width = "150px";
						map.style.height = "150px";
						// map.style.styleFloat = "right";
						// map.style.floatStyle = "right";
						// map.style.cssFloat = "right";
						map.style.overflow = "hidden";
						map.style.marginLeft = "2em";
	
						var gmap = new GMap2(map);
						gmap.setCenter(new GLatLng(lat, lng), 13);
						GEvent.addListener(gmap, "moveend", function() {
							gmap.panTo(new GLatLng(lat, lng));	
						});					
	
						var pin = new GIcon();
						pin.shadow = "http://i.oodleimg.com/a/maps/shadow.png";
						if (rough) pin.shadow = "http://i.oodleimg.com/a/maps/shadow_rough.png";
						pin.shadowSize = new GSize(22, 24);
						pin.iconSize = new GSize(14, 24);
						pin.iconAnchor = new GPoint(7, 22);
						pin.infoWindowAnchor = new GPoint(7, 5);
						pin.image =  "http://i.oodleimg.com/a/maps/icon.png";
						if (rough) pin.image = "http://i.oodleimg.com/a/maps/icon_rough.png";
						var marker = new GMarker(new GLatLng(lat, lng), pin);
						gmap.addOverlay(marker);
						
						if (rough) mapMessage.innerHTML = 'Indicated location is approximate';
					}
				}
			};
			
			LazyLoad.loadOnce(['http://img.oodle.com/jaml/lazyloadmap/1242837454'], loadMapFunc, null, null, true);
		},
		"/ajax/listing/?listing_id=" + listingid
	);
}

function toggleListing(id) {
	var element = document.getElementById(id);
	if (element) {
		if (hasClass(element, "normal-listing")) {	
			fillListing(id, id);	
			swapToggleLink(id, "show");
			removeClass(element, "normal-listing");
			addClass(element, "expanded-listing");
		} else if (hasClass(element, "expanded-listing")) {
			swapToggleLink( id, "hide" );
			removeClass(element, "expanded-listing");
			addClass(element, "normal-listing");
		}
	}
}

function swapToggleLink(id, event) {
	var toggleBtn = document.getElementById(id + "_toggleBtn");
	if (toggleBtn) {
		var seeDetails = getElementsByClass('see-details-link', toggleBtn)[0];
		var hideDetails = getElementsByClass('hide-details-link', toggleBtn)[0];
		
		if (event == 'hide') {
			seeDetails.style.display = "inline";
			hideDetails.style.display = "none";
		} 
		else if(event == 'show') 
		{
			seeDetails.style.display = "none";
			hideDetails.style.display = "inline";
		}
	}
}

function doToggle(id, event) {
	//var now = new Date().getTime();
	if (typeof _cs != 'undefined' && _cs && _cs != id) {
		// preserve scroll state
		var curElement = document.getElementById(id);
		var oldOffset = getElementTop(curElement);
		toggleListing(_cs); 
		var newOffset = getElementTop(curElement);
		if (oldOffset - newOffset != 0) {
			// scroll by offset
			window.scrollBy(0, newOffset - oldOffset)
		}
	}
	if (typeof _cs == 'undefined' || _cs == null || _cs != id) {
		_cs = id;
	} else {
		_cs = null;
	}
	toggleListing(id);
	//var later = new Date().getTime();

	//alert(later - now);
}
function toggleNarrow(controlObj, toggleObj) {
	toggle(toggleObj);
	if (toggleObj.style.display == 'none') {
		removeClass(controlObj, 'expanded');
	} else {
		addClass(controlObj, 'expanded');
	}
}

// @param CheckBox childObj
// @param UL parentObj
// Check childObj and and uncheck all of
// the CheckBox objects in parentObj.
// Args can be undefined.
function clickAll(childObj, parentObj) {
	//if (childObj.checked)
	//	return;
	if (!childObj || !parentObj)
		return;
	var elems = getElementsByTagNames('input', parentObj);
	for (var i = 1; i < elems.length; i++)
		elems[i].checked = false;
	childObj.checked = true;
}

// @param UL parentObj
// iterate through the CheckBox objects in parentObj
// and check the first object if there are no
// "on" object, otherwise uncheck the first object.
// Args can be undefined.
function fixAll(parentObj) {
	if (!parentObj)
		return;
	var elems = getElementsByTagNames('input', parentObj);
	var on = 0;
	for (var i = 1; i < elems.length; i++) {
		if (elems[i].checked == true) {
			on++;
			break;
		}
	}
	elems[0].checked = !on;
}

// @param CheckBox childObj
// @param UL parentObj
// toggle the state of childObj, then iterate
// through the CheckBox objects in parentObj
// and check the first object if there are no
// "on" object, otherwise uncheck the first object.
// Args can be undefined.
function clickAndFixAll(childObj, parentObj) {
	if (!childObj || !parentObj)
		return;
	childObj.checked = !childObj.checked;
	fixAll(parentObj);
}


/* facet browser related */



function facetTab(attribute) {
	
	var facetForm = $('facet-form');
	var facetOptions = $('facet-options');
	var facetDropPoint = $('facet-drop-point');
	var facetHeaderDropPoint = $('facet-header-drop-point');
	
	// remove any hidden inputs substituting for real form input
	var hiddenAttributeInputs = getElementsByClass('hidden-' + attribute, facetForm);
	for (var i = 0; i < hiddenAttributeInputs.length; i++)
		hiddenAttributeInputs[i].parentNode.removeChild(hiddenAttributeInputs[i]);
	
	// the tab contents
	var facetContainers = getElementsByClass('facet-container', facetDropPoint);
	var existingTab = document.getElementById('facet-' + attribute + '-container');
	
	for (var i = 0; i < facetContainers.length; i++)
		facetContainers[i].style.display = 'none';
	
	// the header
	var facetHeaders = getElementsByClass('facet-header', facetHeaderDropPoint);
	var existingHeader = document.getElementById('facet-' + attribute + '-header');
	
	for (var i = 0; i < facetHeaders.length; i++)
		facetHeaders[i].style.display = 'none';
		
	// the option - change class to selected
	var facetOptions = getElementsByClass('facet-option', facetOptions);
	var currentOption = document.getElementById('facet-' + attribute + '-option');
	
	for (var i = 0; i < facetOptions.length; i++)
		facetOptions[i].className = 'facet-option';
	
	currentOption.className = 'facet-option selected';
	
	// show tab
	if (existingTab && existingHeader)
	{	
		existingTab.style.display = '';
		existingHeader.style.display = '';
	}
	else
	{
		new XMLHttpRequestObject(
			function()
			{
				facetDropPoint.style.background = 'url(http://i.oodleimg.com/a/loading.gif) 50% 50% no-repeat';
			}, 
			null, 
			function(xml, text)
			{
				// to prevent a race condition from happening wherein two facets are
				// shown at once, we need to execute the hiding logic in this closure
				// (which is a synchronous block)
				// the tab contents
				var facetContainers = getElementsByClass('facet-container', facetDropPoint);
				for (var i = 0; i < facetContainers.length; i++)
					facetContainers[i].style.display = 'none';
				
				// the header
				var facetHeaders = getElementsByClass('facet-header', facetHeaderDropPoint);
				for (var i = 0; i < facetHeaders.length; i++)
					facetHeaders[i].style.display = 'none';
			
				facetDropPoint.style.background = 'white';
				
				// we can't just use += innerHTML, since that clears checkboxes
				var dummy = document.createElement('div');
				dummy.innerHTML = text;
						
				facetHeaderDropPoint.appendChild(dummy.getElementsByTagName('div')[0]);
				facetDropPoint.appendChild(dummy.getElementsByTagName('div')[0]);
			} , 
			'/ajax/facets/?u=' + encodeURIComponent(window.location.pathname + window.location.search) + '&c=' + encodeURIComponent(attribute));
	}
}

function facetBrowser(currentAttribute, partner, vertical) 
{
	var facetBrowserBody = $('facet-browser-body');
	var facetButtons = $('facet-buttons');
	facetBrowserBody.style.display = '';
	facetButtons.style.display = '';
	
	var renderAsIframe = false;
	if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 7)
		renderAsIframe = true;

	var facetBrowser = new YAHOO.widget.Dialog(
		$('facet-container'), 
		{
			underlay : 'shadow', 
			iframe: renderAsIframe,
			modal: true,
			width: '690px',
			fixedcenter: true,
			draggable : false,
			zIndex : 1999999,
			effect : {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.50}
		});
		
	var submitEvent = function() { 
		cmCreateManualLinkClickTag('/event-conversion.php&cm_sp=facet_submit-_-' + partner+ '-_-' + vertical); 
		$('facet-form').submit(); 
		return false; 
	};
	
	var cancelEvent = function() { 
		cmCreateManualLinkClickTag('/event-conversion.php&cm_sp=facet_close-_-' + partner+ '-_-' + vertical); 
		facetBrowser.hide(); 
		if ($('partner-sky')) $('partner-sky').style.visibility = 'visible'; 
		return false; 
	};
	
	$('facet-submit-button').onclick = submitEvent;
	$('facet-cancel-button').onclick = cancelEvent;
	
	facetBrowser.submitEvent.fire = submitEvent;
	facetBrowser.cancelEvent.fire = cancelEvent;

	document.body.className = "yui-skin-sam";	 
	var element = document.getElementById('hiddenfacets');
	
	// hide any flash banners
	if ($('partner-sky')) $('partner-sky').style.visibility = 'hidden';
	
	facetBrowser.render(element);
	facetBrowser.show();
	
	// track in coremetrics
	if (vertical == '')
		vertical = 'null';
	cmCreateManualLinkClickTag('/event-conversion.php&cm_sp=facet_open-_-' + partner+ '-_-' + vertical);
	
	
	// remove the fixedcenter property AFTER rendering if the screen is small
	if (getWindowHeight() < 550)
		facetBrowser.cfg.setProperty('fixedcenter', false);
	
	// load the tab
	facetTab(currentAttribute); 
}

function locationModal(partner, vertical)
{
	var facetBrowserBody = $('location-modal-body');
	var facetButtons = $('location-modal-buttons');
	facetBrowserBody.style.display = '';
	facetButtons.style.display = '';
	
	var renderAsIframe = false;
	if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 7)
		renderAsIframe = true;

	var facetBrowser = new YAHOO.widget.Dialog(
		$('location-modal-container'), 
		{
			underlay : 'shadow', 
			iframe: renderAsIframe,
			modal : true,
			width: '510px',
			fixedcenter: true,
			draggable : false,
			zIndex : 999999,
			hideaftersubmit: false,
			effect : {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.50}
		});
		 
	document.body.className = "yui-skin-sam";	 
	var element = document.getElementById('hiddenmodals');
	
	facetBrowser.render(element);
	
	var defaultText = $('location-modal-where').value; 

	var submitEvent = function() { cmCreateManualLinkClickTag('/event-conversion.php&cm_sp=location_modal_submit-_-' + partner+ '-_-' + vertical); $('location-modal-form').submit(); return false; };
	var submitCancel = function() { cmCreateManualLinkClickTag('/event-conversion.php&cm_sp=location_modal_close-_-' + partner+ '-_-' + vertical); facetBrowser.hide(); document.body.className = ''; $('location-modal-where').value = defaultText; return false; };

	$('location-modal-submit-button').onclick = submitEvent;
	$('location-modal-cancel-button').onclick = submitCancel;
	
	facetBrowser.submitEvent.fire = submitEvent;
	facetBrowser.cancelEvent.fire = submitCancel;
	
	$('location-modal-where').onblur();
	
	facetBrowser.show();

	// track in coremetrics
	if (vertical == '')
		vertical = 'null';
	cmCreateManualLinkClickTag('/event-conversion.php&cm_sp=location_modal_open-_-' + partner+ '-_-' + vertical);	
}
function submitAlertPopup()
{
	var email = $('alert-popup-email').value;
	var frequency = $('alert-popup-frequency').value;
	var alertBody = $('alert-popup-body');

	new XMLHttpRequestObject(
		function() { },
		function() { },
		function(response) 
		{
			var tag = response.getElementsByTagName('alert-response');
			alertBody.innerHTML = tag[0].firstChild.nodeValue;
		},
		'/ajax/alert/?email=' + email + '&frequency=' + frequency
	);
	return false;
}

// global counter for listing clicks
var listingClicks = 0;
var triggerPopupCount = 1;

function listingClicked(elementID)
{
//	alert(elementID);
	listingElement = $(elementID);
	
	// increment the global listing clicks counter
	listingClicks++;

	if (listingElement && listingClicks == triggerPopupCount)
	{
		var alertWindow = $('alert-form-popup');
		
		if (alertWindow)
		{
			alertWindow.style.position = 'absolute';
			document.body.appendChild(alertWindow);
			
			var listingPos = getElementPos(listingElement);
			alertWindow.style.left = (listingPos[0] - 150) + 'px';
			alertWindow.style.top = (listingPos[1] - 100) + 'px';
			
			alertWindow.style.display = 'block';

			return true;
		}
	}

	return false;
}
function mouseHL(div)
{
	var childs = div.getElementsByTagName("div");
	if (!childs || !childs[0] || !childs[1])
		return;
	
	childs[0].style.visibility="visible";
	childs[1].style.visibility="hidden";
	
}

function unmouseHL(div)
{
	var childs = div.getElementsByTagName("div");
	if (!childs || !childs[0] || !childs[1])
		return;
	
	childs[0].style.visibility="hidden";
	childs[1].style.visibility="visible";
}

function distInfoBoxHtml(index)
{
	//range
	//last 6 month number
	//today number
}

function makeSubmit(form)
{
	var n = form.model.options.length;
	form.model.options[n] = new Option("","");
	form.model.selectedIndex = n;
	form.submit();
}

function neighborhoodSubmit(form)
{
	if (form.loc)
	{
		form.removeChild(form.loc);
	}
	form.loc_neighborhood.name = "loc";
	form.submit();
}

function citySubmit(form)
{
	if (form.loc)
	{
		form.removeChild(form.loc);
	}
	form.loc_city.name = "loc";
	form.submit();
}

function withLocSubmit(form)
{
	if (form.loc)
	{
		form.removeChild(form.loc);
	}
	if (form.loc_neighborhood && form.loc_neighborhood.value !=  "")
	{
		form.loc_neighborhood.name = "loc";
	}
	else
	{
		form.loc_city.name = "loc";
	}

	form.submit();
}
/*
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.5.2
*/
(function(){YAHOO.widget.TabView=function(K,J){J=J||{};if(arguments.length==1&&!YAHOO.lang.isString(K)&&!K.nodeName){J=K;K=J.element||null;}if(!K&&!J.element){K=I.call(this,J);}YAHOO.widget.TabView.superclass.constructor.call(this,K,J);};YAHOO.extend(YAHOO.widget.TabView,YAHOO.util.Element);var F=YAHOO.widget.TabView.prototype;var E=YAHOO.util.Dom;var H=YAHOO.util.Event;var D=YAHOO.widget.Tab;F.CLASSNAME="yui-navset";F.TAB_PARENT_CLASSNAME="yui-nav";F.CONTENT_PARENT_CLASSNAME="yui-content";F._tabParent=null;F._contentParent=null;F.addTab=function(M,O){var P=this.get("tabs");if(!P){this._queue[this._queue.length]=["addTab",arguments];return false;}O=(O===undefined)?P.length:O;var R=this.getTab(O);var T=this;var L=this.get("element");var S=this._tabParent;var Q=this._contentParent;var J=M.get("element");var K=M.get("contentEl");if(R){S.insertBefore(J,R.get("element"));}else{S.appendChild(J);}if(K&&!E.isAncestor(Q,K)){Q.appendChild(K);}if(!M.get("active")){M.set("contentVisible",false,true);}else{this.set("activeTab",M,true);}var N=function(V){YAHOO.util.Event.preventDefault(V);var U=false;if(this==T.get("activeTab")){U=true;}T.set("activeTab",this,U);};M.addListener(M.get("activationEvent"),N);M.addListener("activationEventChange",function(U){if(U.prevValue!=U.newValue){M.removeListener(U.prevValue,N);M.addListener(U.newValue,N);}});P.splice(O,0,M);};F.DOMEventHandler=function(P){var K=this.get("element");var Q=YAHOO.util.Event.getTarget(P);var S=this._tabParent;if(E.isAncestor(S,Q)){var L;var M=null;var J;var R=this.get("tabs");for(var N=0,O=R.length;N<O;N++){L=R[N].get("element");J=R[N].get("contentEl");if(Q==L||E.isAncestor(L,Q)){M=R[N];break;}}if(M){M.fireEvent(P.type,P);}}};F.getTab=function(J){return this.get("tabs")[J];};F.getTabIndex=function(N){var K=null;var M=this.get("tabs");for(var L=0,J=M.length;L<J;++L){if(N==M[L]){K=L;break;}}return K;};F.removeTab=function(M){var L=this.get("tabs").length;var K=this.getTabIndex(M);var J=K+1;if(M==this.get("activeTab")){if(L>1){if(K+1==L){this.set("activeIndex",K-1);}else{this.set("activeIndex",K+1);}}}this._tabParent.removeChild(M.get("element"));this._contentParent.removeChild(M.get("contentEl"));this._configs.tabs.value.splice(K,1);};F.toString=function(){var J=this.get("id")||this.get("tagName");return"TabView "+J;};F.contentTransition=function(K,J){K.set("contentVisible",true);J.set("contentVisible",false);};F.initAttributes=function(J){YAHOO.widget.TabView.superclass.initAttributes.call(this,J);if(!J.orientation){J.orientation="top";}var L=this.get("element");if(!YAHOO.util.Dom.hasClass(L,this.CLASSNAME)){YAHOO.util.Dom.addClass(L,this.CLASSNAME);}this.setAttributeConfig("tabs",{value:[],readOnly:true});this._tabParent=this.getElementsByClassName(this.TAB_PARENT_CLASSNAME,"ul")[0]||G.call(this);this._contentParent=this.getElementsByClassName(this.CONTENT_PARENT_CLASSNAME,"div")[0]||C.call(this);this.setAttributeConfig("orientation",{value:J.orientation,method:function(M){var N=this.get("orientation");this.addClass("yui-navset-"+M);if(N!=M){this.removeClass("yui-navset-"+N);}switch(M){case"bottom":this.appendChild(this._tabParent);break;}}});this.setAttributeConfig("activeIndex",{value:J.activeIndex,method:function(M){this.set("activeTab",this.getTab(M));},validator:function(M){return !this.getTab(M).get("disabled");}});this.setAttributeConfig("activeTab",{value:J.activeTab,method:function(N){var M=this.get("activeTab");if(N){N.set("active",true);this._configs["activeIndex"].value=this.getTabIndex(N);}if(M&&M!=N){M.set("active",false);}if(M&&N!=M){this.contentTransition(N,M);}else{if(N){N.set("contentVisible",true);}}},validator:function(M){return !M.get("disabled");}});if(this._tabParent){B.call(this);}this.DOM_EVENTS.submit=false;this.DOM_EVENTS.focus=false;this.DOM_EVENTS.blur=false;for(var K in this.DOM_EVENTS){if(YAHOO.lang.hasOwnProperty(this.DOM_EVENTS,K)){this.addListener.call(this,K,this.DOMEventHandler);}}};var B=function(){var Q,L,P;var O=this.get("element");var N=A(this._tabParent);var K=A(this._contentParent);for(var M=0,J=N.length;M<J;++M){L={};if(K[M]){L.contentEl=K[M];}Q=new YAHOO.widget.Tab(N[M],L);this.addTab(Q);if(Q.hasClass(Q.ACTIVE_CLASSNAME)){this._configs.activeTab.value=Q;this._configs.activeIndex.value=this.getTabIndex(Q);}}};var I=function(J){var K=document.createElement("div");if(this.CLASSNAME){K.className=this.CLASSNAME;}return K;};var G=function(J){var K=document.createElement("ul");if(this.TAB_PARENT_CLASSNAME){K.className=this.TAB_PARENT_CLASSNAME;}this.get("element").appendChild(K);return K;};var C=function(J){var K=document.createElement("div");if(this.CONTENT_PARENT_CLASSNAME){K.className=this.CONTENT_PARENT_CLASSNAME;}this.get("element").appendChild(K);return K;};var A=function(M){var K=[];var N=M.childNodes;for(var L=0,J=N.length;L<J;++L){if(N[L].nodeType==1){K[K.length]=N[L];}}return K;};})();(function(){var E=YAHOO.util.Dom,J=YAHOO.util.Event;var B=function(L,K){K=K||{};if(arguments.length==1&&!YAHOO.lang.isString(L)&&!L.nodeName){K=L;L=K.element;}if(!L&&!K.element){L=H.call(this,K);}this.loadHandler={success:function(M){this.set("content",M.responseText);},failure:function(M){}};B.superclass.constructor.call(this,L,K);this.DOM_EVENTS={};};YAHOO.extend(B,YAHOO.util.Element);var F=B.prototype;F.LABEL_TAGNAME="em";F.ACTIVE_CLASSNAME="selected";F.ACTIVE_TITLE="active";F.DISABLED_CLASSNAME="disabled";F.LOADING_CLASSNAME="loading";F.dataConnection=null;F.loadHandler=null;F._loading=false;F.toString=function(){var K=this.get("element");var L=K.id||K.tagName;return"Tab "+L;};F.initAttributes=function(K){K=K||{};B.superclass.initAttributes.call(this,K);var M=this.get("element");this.setAttributeConfig("activationEvent",{value:K.activationEvent||"click"});this.setAttributeConfig("labelEl",{value:K.labelEl||G.call(this),method:function(N){var O=this.get("labelEl");if(O){if(O==N){return false;}this.replaceChild(N,O);}else{if(M.firstChild){this.insertBefore(N,M.firstChild);}else{this.appendChild(N);}}}});this.setAttributeConfig("label",{value:K.label||D.call(this),method:function(O){var N=this.get("labelEl");
if(!N){this.set("labelEl",I.call(this));}C.call(this,O);}});this.setAttributeConfig("contentEl",{value:K.contentEl||document.createElement("div"),method:function(N){var O=this.get("contentEl");if(O){if(O==N){return false;}this.replaceChild(N,O);}}});this.setAttributeConfig("content",{value:K.content,method:function(N){this.get("contentEl").innerHTML=N;}});var L=false;this.setAttributeConfig("dataSrc",{value:K.dataSrc});this.setAttributeConfig("cacheData",{value:K.cacheData||false,validator:YAHOO.lang.isBoolean});this.setAttributeConfig("loadMethod",{value:K.loadMethod||"GET",validator:YAHOO.lang.isString});this.setAttributeConfig("dataLoaded",{value:false,validator:YAHOO.lang.isBoolean,writeOnce:true});this.setAttributeConfig("dataTimeout",{value:K.dataTimeout||null,validator:YAHOO.lang.isNumber});this.setAttributeConfig("active",{value:K.active||this.hasClass(this.ACTIVE_CLASSNAME),method:function(N){if(N===true){this.addClass(this.ACTIVE_CLASSNAME);this.set("title",this.ACTIVE_TITLE);}else{this.removeClass(this.ACTIVE_CLASSNAME);this.set("title","");}},validator:function(N){return YAHOO.lang.isBoolean(N)&&!this.get("disabled");}});this.setAttributeConfig("disabled",{value:K.disabled||this.hasClass(this.DISABLED_CLASSNAME),method:function(N){if(N===true){E.addClass(this.get("element"),this.DISABLED_CLASSNAME);}else{E.removeClass(this.get("element"),this.DISABLED_CLASSNAME);}},validator:YAHOO.lang.isBoolean});this.setAttributeConfig("href",{value:K.href||this.getElementsByTagName("a")[0].getAttribute("href",2)||"#",method:function(N){this.getElementsByTagName("a")[0].href=N;},validator:YAHOO.lang.isString});this.setAttributeConfig("contentVisible",{value:K.contentVisible,method:function(N){if(N){this.get("contentEl").style.display="block";if(this.get("dataSrc")){if(!this._loading&&!(this.get("dataLoaded")&&this.get("cacheData"))){A.call(this);}}}else{this.get("contentEl").style.display="none";}},validator:YAHOO.lang.isBoolean});};var H=function(K){var O=document.createElement("li");var L=document.createElement("a");L.href=K.href||"#";O.appendChild(L);var N=K.label||null;var M=K.labelEl||null;if(M){if(!N){N=D.call(this,M);}}else{M=I.call(this);}L.appendChild(M);return O;};var G=function(){return this.getElementsByTagName(this.LABEL_TAGNAME)[0];};var I=function(){var K=document.createElement(this.LABEL_TAGNAME);return K;};var C=function(K){var L=this.get("labelEl");L.innerHTML=K;};var D=function(){var K,L=this.get("labelEl");if(!L){return undefined;}return L.innerHTML;};var A=function(){if(!YAHOO.util.Connect){return false;}E.addClass(this.get("contentEl").parentNode,this.LOADING_CLASSNAME);this._loading=true;this.dataConnection=YAHOO.util.Connect.asyncRequest(this.get("loadMethod"),this.get("dataSrc"),{success:function(K){this.loadHandler.success.call(this,K);this.set("dataLoaded",true);this.dataConnection=null;E.removeClass(this.get("contentEl").parentNode,this.LOADING_CLASSNAME);this._loading=false;},failure:function(K){this.loadHandler.failure.call(this,K);this.dataConnection=null;E.removeClass(this.get("contentEl").parentNode,this.LOADING_CLASSNAME);this._loading=false;},scope:this,timeout:this.get("dataTimeout")});};YAHOO.widget.Tab=B;})();YAHOO.register("tabview",YAHOO.widget.TabView,{version:"2.5.2",build:"1076"});YAHOO.namespace("Oodle.ListingAction");

YAHOO.Oodle.PopupAction =
{
	mapImpl : 'google',

	setMapImple : function(mapImpl)
	{
		YAHOO.Oodle.PopupAction.mapImpl = mapImpl;
	},

	hideResultMsg:function(divID)
	{
		var msgElemnt = document.getElementById(divID);

		if (msgElemnt)
		{
			msgElemnt.style.display = 'none';
		}
	},

	updateMsg:function(msg, listingID, timeOut)
	{
		var divID = listingID + '-action-result';
		var msgElemnt = document.getElementById(divID);

		if (msgElemnt)
		{
			msgElemnt.style.display = 'block';
			msgElemnt.innerHTML = msg;
			if (timeOut > 0)
			{
				var timeoutID = setTimeout("YAHOO.Oodle.PopupAction.hideResultMsg('" + divID + "')", timeOut);
				//clearTimeout(timeoutID)
			}
		}
	},

	listingAction:function(listingID, action, source, partner)
	{
		var handleSuccess = function (o) {
			try {
	   			var formData = YAHOO.lang.JSON.parse(o.responseText);
	   			if (formData.result)
	   			{
					YAHOO.Oodle.PopupAction.updateMsg(formData.result, listingID, 7000);
	   			}
				else
					YAHOO.Oodle.PopupAction.updateMsg(formData.error['*'], listingID, 7000);
			}
			catch (e)
			{
			    // Unable to parse data
			    YAHOO.Oodle.PopupAction.updateMsg('Unable to parse response.', listingID, 7000);
			}
		}

		var callback =
		{
			success: handleSuccess,
			failure: this.handleFailure,
			timeout: 45000
		}

		var cObj = YAHOO.util.Connect.asyncRequest(
			'POST',
			'/ajax/listingaction/?action=admin&source=' + source +
			'&type=' + action +'&listing_id=' + listingID + '&partner=' + partner,
			callback);
	},

	loadMapDivForMapQuest:function(mapDiv, data)
	{
		var lat = data.lat;
		var lng = data.lng;
		var rough = data.rough;

		if (rough) {
			var mapMessageDiv = document.createElement('div');
			mapMessageDiv.innerHTML = 'Indicated location is approximate';
			mapMessageDiv.className = "map-message";
			mapDiv.appendChild(mapMessageDiv);
		}

		var gMapDiv = document.createElement('div');
		gMapDiv.style.width = data.width;
		gMapDiv.style.height = data.height;
		gMapDiv.className = "map-div";
		gMapDiv.id = "tidy-mq-map";

		var listingPoiCollection = new MQA.ShapeCollection();
		var point = new MQA.LatLng(lat,lng);
		listingMapIcon = new MQA.Icon("http://i.oodleimg.com/a/maps/icon.png",14,24);
		var marker = new MQA.Poi(point);
        marker.setValue('infoTitleHTML',data.desc);
		marker.setValue('key',data.id);
		marker.setValue('icon',listingMapIcon);

		myMapInit = new MQA.MapInit();
		myMapInit.setBestFitRect(listingPoiCollection.getBoundingRect());
		mq_listings_map = new MQA.TileMap(gMapDiv, 9, new MQA.LatLng(lat, lng), "map", myMapInit);
		mq_listings_map.setCenter(point);
		mq_listings_map.addShape(marker);
		mapDiv.appendChild(gMapDiv);

	},

	loadMapDiv:function(mapDiv, data)
	{
		var lat = data.lat;
		var lng = data.lng;
		var rough = data.rough;

		if (rough) {
			var mapMessageDiv = document.createElement('div');
			mapMessageDiv.innerHTML = 'Indicated location is approximate';
			mapMessageDiv.className = "map-message";
			mapDiv.appendChild(mapMessageDiv);
		}

		var gMapDiv = document.createElement('div');
		gMapDiv.style.width = data.width;
		gMapDiv.style.height = data.height;
		gMapDiv.className = "map-div";

		mapDiv.appendChild(gMapDiv);
		var gmap = new GMap2(gMapDiv);

		gmap.setCenter(new GLatLng(lat, lng), 13);
		GEvent.addListener(gmap, "moveend", function()
		{
			gmap.panTo(new GLatLng(lat, lng));
		});

		gmap.addControl(new GSmallZoomControl());

		var pin = new GIcon();

		pin.shadow = "http://i.oodleimg.com/a/maps/shadow.png";
		if (rough)
			pin.shadow = "http://i.oodleimg.com/a/maps/shadow_rough.png";
		pin.shadowSize = new GSize(22, 24);
		pin.iconSize = new GSize(14, 24);
		pin.iconAnchor = new GPoint(7, 22);
		pin.infoWindowAnchor = new GPoint(7, 5);
		pin.image = "http://i.oodleimg.com/a/maps/icon.png";
		if (rough) {
			pin.image = "http://i.oodleimg.com/a/maps/icon_rough.png";
		}

		var marker = new GMarker(new GLatLng(lat, lng), pin);
		gmap.addOverlay(marker);
	},

	handleDataSuccess:function(o)
	{
		var listingID = o.argument[1];
  		var bd = o.argument[0];
  			var html = o.argument[3];

		var data = YAHOO.lang.JSON.parse(o.responseText);
  		bd.style.background = 'white';
  		if (o.argument[3])
  		{
  			bd.innerHTML = data.html + html;
  		}
  		else
  		{
			bd.innerHTML = data.html;
  		}

		var tabView = YAHOO.Oodle.PopupAction.createTabs(listingID);

	    var container = $(listingID + '-popup');
	    tabView.appendTo(container);

	    var handleActiveTabChange = function(e, listingID)
	    {
	    	var tab = e.newValue;
	    	var reCaptcha = listingID + '-' + tab.viewType + '-captcha';
	    	var reCaptchaEl = $(reCaptcha);
	    	if (reCaptchaEl) {
	    		Recaptcha.create(data.captchakey, reCaptcha, {theme: "red"});
	    	}
	    }

	    tabView.addListener('activeTabChange', handleActiveTabChange, listingID);
		tabView.set('activeIndex', o.argument[2]);
		YAHOO.Oodle.PopupAction["tab-" + listingID] = tabView;

		// Set up domain proxy for contact form
		var domainDiv = $(listingID + '-domain');
		if (domainDiv)
		{
			domainDiv.value = window.location.hostname;
		}

		var mapDiv = $('popup-map-' + listingID);
		if (mapDiv)
		{
			if (YAHOO.Oodle.PopupAction.mapImpl == 'mapquest')
			{
				YAHOO.Oodle.PopupAction.loadMapDivForMapQuest(mapDiv, data.map);
			}
			else
			{
				var loadMapFunc = function()
				{
					if (!window.GBrowserIsCompatible ||
				    !window.GControl ||
				    !window.GMap2)
					{
						setTimeout(loadMapFunc, 200);
						return;
					}

					YAHOO.Oodle.PopupAction.loadMapDiv(mapDiv, data.map);
				};

				LazyLoad.loadOnce(['http://img.oodle.com/jaml/lazyloadmap/1242837454'], loadMapFunc, null, null, true);
			}
		}
	},

	init:function(listingID, activeTabIndex, html, page, category, tabName)
	{
		YAHOO.Oodle.Dialog.hide();
		var cmReport = tabName + '-_-' + page + '-_-' + category;
		cmCreateManualLinkClickTag('/event-conversion.php?cm_sp=' + cmReport);

		var divID = 'listing-detail-popup-' + listingID;
		var popupEl = $(divID);
		if (!popupEl)
		{
			popupEl = document.createElement('div');
			popupEl.id = divID;
			popupEl.className = "yui-skin-sam pop-up";

			var bd =  document.createElement('div');
			bd.id = divID + '-bd';
			bd.style.height = '490px'
			bd.className = "bd";
			popupEl.appendChild(bd);
			bd.style.background = 'url(http://i.oodleimg.com/a/lightbox-loading.gif) 50% 50% no-repeat white';

			var callback =
			{
			  success: YAHOO.Oodle.PopupAction.handleDataSuccess,
			  failure: function(o, bd) {
			  		bd = o.argument[0];
			  		bd.style.background = "white";
			  		bd.innerHTML = 'Oops! There was a communication error. Please reload your page and try again.'
			  	},
			  timeout: 5000,
			  argument: [bd, listingID, activeTabIndex, html]
			}
			sUrl = '/ajax/popup/?listing_id=' + listingID + '&page=' + page;
			YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
		}
		else
		{
			YAHOO.Oodle.PopupAction["tab-" + listingID].set('activeIndex', activeTabIndex);
		}

		var popupModal = new YAHOO.widget.Dialog(
				popupEl,
				{	width: '480px',
					fixedcenter: true,
					draggable : true,
					zIndex : 999999999,
					underlay : 'shadow',
					modal : true,
					iframe: true,
					hideaftersubmit: true,
					effect : {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.50}
				});

		document.body.className = "yui-skin-sam"
		var hiddenEl = $('hiddenmodals');

		if (!bd) {
			bd = $(divID + '-bd');
		}

		if (bd)
		{
			bd.style.display = 'block';
			popupModal.cancelEvent.fire = function() {
				bd.style.display = 'none';
			}
		}

		popupModal.render(hiddenEl);
		popupModal.show();

		if (YAHOO.Oodle.PopupAction.currentModal &&
			YAHOO.Oodle.PopupAction.currentModal != popupModal)
		{
			YAHOO.Oodle.PopupAction.currentModal.hide();
		}
		YAHOO.Oodle.PopupAction.currentModal = popupModal;
	},

	createTabs:function(listingID)
	{
		var myTabs = new YAHOO.widget.TabView();

		var tabs = {
			details: 'Details',
			md: 'Market Data',
			contact: 'Contact Seller',
			share: 'Share',
			report: 'Report'};

		var tabIndex = 0;
		for (index in tabs)
		{
			var tabID = listingID + '-' + index;
			var tabEl = $(tabID);

			if (tabEl)
			{
				var tabObj = new YAHOO.widget.Tab({
						        label:  '<span id="' + index +'-label">' + tabs[index] + '</span>',
						        contentEl: tabEl
						    });

				tabObj.viewType = index;
			    myTabs.addTab(tabObj);
			}
		}

		YAHOO.widget.Tab.prototype.ACTIVE_TITLE = '';
		return myTabs;
	},

	resetShareListing:function(listingID, email)
	{
		YAHOO.Oodle.ListingAction.setValue('frm_send_friend-friend_email', '');
		YAHOO.Oodle.ListingAction.setValue('frm_send_friend-subject', '');
		YAHOO.Oodle.ListingAction.setValue('frm_send_friend-message', '');
		YAHOO.Oodle.ListingAction.setValue('frm_send_friend-your_email', email);
		YAHOO.Oodle.ListingAction.setValue('frm_send_friend-listing_id', listingID);
		YAHOO.Oodle.ListingAction.setTitle('email-listing-title', listingID);
	},

	submitShareWithFriend:function(listingID)
	{
		var handleSuccess = function(data)
		{
			if (data.result)
			{
				var listingID = YAHOO.Oodle.PopupAction.listingID;
				var resultEl = $(listingID + '-share-result');
				var formEl = $(listingID + '-share-form');

				resultEl.innerHTML = data.result;
				resultEl.style.display = 'block';
				formEl.style.display = 'none';
			}
			else
			{
				Recaptcha.reload();
			}
		}

		YAHOO.Oodle.PopupAction.listingID = listingID;
		YAHOO.Oodle.AjaxFormObject.startRequest(
				'share-' + listingID,
				'/ajax/listingaction/?action=emailfriend',
				handleSuccess);
	},

	submitContact:function(listingID)
	{
		var handleSuccess = function(data)
		{
			if (data.result)
			{
				var listingID = YAHOO.Oodle.PopupAction.listingID;
				var resultEl = $(listingID + '-contact-result');
				var formEl = $(listingID + '-contact-form');

				if (data.result.redirect)
				{
					YAHOO.Oodle.ListingAction.report.hide();
					var listingID = data.result.redirect;
					if (element)
					{
						var url = element.attributes.getNamedItem("href").value;
						window.location = url;
					}
				}
				else
				{
					resultEl.innerHTML = data.result.msg;
					resultEl.style.display = 'block';
					formEl.style.display = 'none';
				}
			}
			else
			{
				Recaptcha.reload();
			}
		}

		YAHOO.Oodle.PopupAction.listingID = listingID;
		YAHOO.Oodle.AjaxFormObject.startRequest(
				'frm_contact_seller-' + listingID,
				'/ajax/listingaction/?action=contact',
				handleSuccess);
	},

	submitReportListing:function(listingID)
	{
		var handleSuccess = function(data)
		{
			if (data.result)
			{
				var listingID = YAHOO.Oodle.PopupAction.listingID;
				var resultEl = $(listingID + '-report-result');
				var formEl = $(listingID + '-report-form');

				if (data.result.redirect)
				{
					YAHOO.Oodle.ListingAction.report.hide();
					var listingID = data.result.redirect;
					if (element)
					{
						var url = element.attributes.getNamedItem("href").value;
						window.location = url;
					}
				}
				else
				{
					resultEl.innerHTML = data.result.msg;
					resultEl.style.display = 'block';
					formEl.style.display = 'none';
				}
			}
		}

		YAHOO.Oodle.PopupAction.listingID = listingID;
		YAHOO.Oodle.AjaxFormObject.startRequest(
				'listing_report-' + listingID,
				'/ajax/listingaction/?action=report',
				handleSuccess);
	}
}
