﻿/// <reference path="yui/utilities/utilities.js" />
/// <reference path="rsbase.js" />

RS.util.ShowAddressGeo = function(address,action,callback){
	var map = null;
	var geocoder = null;
	try{
	geocoder = new GClientGeocoder();
	}catch(Error){}
	if (geocoder) {
		if(action=='addlistingform'){
			geocoder.getLatLng(address,
				function(point) {
					if (!point) {
						alert("Your location, "+address+", could not found.\nTry entering only the city, state, zip.\nIn the address box.");
					} else {
						try{
							document.getElementById('ctl00_ctl00_center_maincontent_ListingView1_lat').value=point.lat();
							document.getElementById('ctl00_ctl00_center_maincontent_ListingView1_lng').value=point.lng();
							document.getElementById('ctl00_ctl00_center_maincontent_ListingView1_Button2').value='Address Verified';
						}catch(Error){}
						//alert(point.lat()+' '+point.lng());
						//location.href='http://<%=request("domain")%>/testing/geolocator/frame.asp?sub=1&point='+point
					}
			});
		}
		else{
			geocoder.getLatLng(address,
				function(point) {
					if (!point) {
						alert("Your location, "+address+", could not found.\nTry entering only your city, state, zip.\nIn the address box.");
					} else {
						callback(point);
					}
			});
		}
	}
};

RS.util.TilesViewInit = function(){
	var tilesView = YAHOO.util.Dom.getElementsByClassName('tiles-view','ul');
	for(i=0;tilesView[i];i++){
		var anchors = tilesView[i].getElementsByTagName('a');
		for(ii=0;anchors[ii];ii++){
			anchors[ii].onclick = function(){
				window.location = this.getAttribute('href');
			}
			anchors[ii].style.cursor = 'pointer';
		}
	}
};

RS.util.ListViewInit = function(){
	var gridView = YAHOO.util.Dom.getElementsByClassName('grid-view-front','table')[0];
	if(gridView){
		//alert('this');
		var listings = YAHOO.util.Dom.getElementsByClassName('listing','div',gridView);
		for(i=0;listings.length>i;i++){
			var url = YAHOO.util.Dom.getElementsByClassName('details','div',listings[i])[0].getElementsByTagName('a')[0].getAttribute('href');
			listings[i].setAttribute('url',url);
			listings[i].style.cssText = 'cursor: pointer;';
			listings[i].onclick = function(){
				window.location = this.getAttribute('url');
			}
			listings[i].onmouseover = function(){
				this.style.cssText = 'cursor: pointer; background-color: #dfdef0;';
			}
			listings[i].onmouseout = function(){
				this.style.cssText = 'cursor: pointer;';
			}
		}
	}
};

(function(){
	var Event = YAHOO.util.Event,
		Dom = YAHOO.util.Dom;
	Event.onContentReady('search-box',function(){
		RS.draw.DropButtons();
		var searchBox = Dom.get('ctl00_ctl00_center_searchbox');
		if(searchBox){
			YAHOO.util.Event.addListener('aspnetForm','submit',function(){
				var callback = function(point){
					window.location = 'search.aspx?beds='+Dom.get('ctl00_ctl00_center_beds').options[Dom.get('ctl00_ctl00_center_beds').selectedIndex].value+'&sleeps='+Dom.get('ctl00_ctl00_center_Sleeps').options[Dom.get('ctl00_ctl00_center_Sleeps').selectedIndex].value+'&baths='+Dom.get('ctl00_ctl00_center_baths').options[Dom.get('ctl00_ctl00_center_baths').selectedIndex].value+'&radius='+Dom.get('ctl00_ctl00_center_radius').options[Dom.get('ctl00_ctl00_center_radius').selectedIndex].value+'&dlat='+point.lat()+'&dlng='+point.lng()+'&search='+Dom.get('ctl00_ctl00_center_searchbox').value;
				};
				if(Dom.get('ctl00_ctl00_center_searchbox').value.length > 4){
					RS.util.ShowAddressGeo(Dom.get('ctl00_ctl00_center_searchbox').value,null,callback);
				}
				else{
					alert("Your location, "+Dom.get('ctl00_ctl00_center_searchbox').value+", could not found.\nTry entering only the city, state, zip.\nIn the address box.")
				}
				return false;
			});
			YAHOO.util.KeyListener('ctl00_ctl00_center_searchbox',{ keys:13 },function(){
				//alert('enter');
				return false;
			});
			if(searchBox.value.length<1){
				searchBox.className = 'prompt';
			}
			searchBox.onblur = function(){
				if(this.value.length<1){
					this.className = 'prompt';
				}
			}
			searchBox.onfocus = function(){
				this.className = '';
			}
		}
		if(Dom.get('ctl00_ctl00_center_Button1')){
			Dom.get('ctl00_ctl00_center_Button1').onclick = function(){
				var callback = function(point){
					window.location = 'search.aspx?beds='+Dom.get('ctl00_ctl00_center_beds').options[Dom.get('ctl00_ctl00_center_beds').selectedIndex].value+'&sleeps='+Dom.get('ctl00_ctl00_center_Sleeps').options[Dom.get('ctl00_ctl00_center_Sleeps').selectedIndex].value+'&baths='+Dom.get('ctl00_ctl00_center_baths').options[Dom.get('ctl00_ctl00_center_baths').selectedIndex].value+'&radius='+Dom.get('ctl00_ctl00_center_radius').options[Dom.get('ctl00_ctl00_center_radius').selectedIndex].value+'&dlat='+point.lat()+'&dlng='+point.lng()+'&search='+Dom.get('ctl00_ctl00_center_searchbox').value;
				};
				if(Dom.get('ctl00_ctl00_center_searchbox').value.length > 4){
					RS.util.ShowAddressGeo(Dom.get('ctl00_ctl00_center_searchbox').value,null,callback);
				}
				else{
					alert("Your location, "+Dom.get('ctl00_ctl00_center_searchbox').value+", could not found.\nTry entering only the city, state, zip.\nIn the address box.")
				}
				return false;
			}
		}
	});
	Event.onDOMReady(function(){
		if(Dom.get('ctl00_ctl00_center_maincontent_ListingView1_Button2')){
			Dom.get('ctl00_ctl00_center_maincontent_ListingView1_Button2').onclick = function(){
				RS.util.ShowAddressGeo(Dom.get('ctl00_ctl00_center_maincontent_ListingView1_address').value+', '+Dom.get('ctl00_ctl00_center_maincontent_ListingView1_city').value+', '+Dom.get('ctl00_ctl00_center_maincontent_ListingView1_DropDownList1').options[Dom.get('ctl00_ctl00_center_maincontent_ListingView1_DropDownList1').selectedIndex].value+' '+Dom.get('ctl00_ctl00_center_maincontent_ListingView1_zip').value,'addlistingform');
				return false;
			}
		}
		RS.util.ListViewInit();
		RS.util.TitleToToolTip();
		RS.util.TilesViewInit();
	});
})();