﻿
// utility function - precaches images for quick loading time of initially hidden elements
function preCacheImage(imageUrl) {

	var img = new Image();
	img.src = imageUrl;            
	
	return img;
}

//preCacheImage('/wp-content/themes/tomevans/images/backgrounds/bg_greenDark.gif');



var stdMethodsHash = {
	
	enable: function(elm) {
		elm = $(elm);

		if (elm.hasClassName('disabled')) {
			elm.removeClassName('disabled');
		}
		elm.disabled = false;
		
		return elm;
	},
	
	disable: function(elm) {
		elm = $(elm);

		if (!elm.hasClassName('disabled')) {
			elm.addClassName('disabled');
		}
		elm.disabled = true;
		
		return elm;
	},
	
	highlight: function(elm) {
		elm = $(elm);

		if (!elm.hasClassName('on')) {
			elm.addClassName('on');
		}
		
		return elm;
	},
	
	unhighlight: function(elm) {
		elm = $(elm);

		if (elm.hasClassName('on')) {
			elm.removeClassName('on');
		}
		
		return elm;
	}
	
};

Element.addMethods(stdMethodsHash);



function showPopop(page, width, height) {

	if (width > 0) {
		// nothing
	} else {
		width = 345;
	}
	if (height > 0) {
		// nothing
	} else {
		height = 445;
	}
	
	var left;
	var top;
	left = Math.round((screen.width - width) / 2);
	top = Math.round((screen.height - height) / 2);
	
	var winPopup = window.open(page, 'winPopup', 'width='+ width +',height='+ height +',top='+ top +',left='+ left +',status=0,resizable=0,location=0,scrollbars=0,titlebar=0,toolbar=0');
	winPopup.focus();

}

function showPrintPopop(page, width, height) {
	
	if (width > 0) {
		// nothing
	} else {
		width = 787;
	}
	if (height > 0) {
		// nothing
	} else {
		height = 787;
	}
	
	var left;
	var top;
	left = Math.round((screen.width - width) / 2);
	top = Math.round((screen.height - height) / 2);
	
	var winPrintPopup = window.open(page, 'winPrintPopup', 'width='+ width +',height='+ height +',top='+ top +',left='+ left +',status=0,resizable=0,location=0,scrollbars=1,titlebar=0,toolbar=0');
	winPrintPopup.focus();

}




function showPhotoGalleryItem(num) {
	
	var photoItemId = 'photoGalleryItem_'+ num;
	var photoThumbId = 'photoGalleryThumb_'+ num;
	
	if ($('photoGallery') && $(photoItemId) && $(photoThumbId)) {
	
		$('photoGallery').select('.photoGalleryItem').each(function (item) { item.hide(); });
		$('photoGallery').select('.photoGalleryThumb').each(function (item) { item.removeClassName('on'); });
		
		$(photoItemId).show();
		$(photoThumbId).addClassName('on');
		
	}
	
}





var propertySearchPriceRangeValues = $H({
	'0': '0',
	'6': '125K',
	'11': '250K',
	'17': '375K',
	'22': '500K',
	'28': '625K',
	'33': '750K',
	'39': '875K',
	'44': '1M',
	'50': '1.5M',
	'55': '2M',
	'61': '3M',
	'66': '4M',
	'72': '5M',
	'77': '6M',
	'83': '7M',
	'88': '8M',
	'94': '9M',
	'99': '10M',
	//'105': '250K',
	'110': '10M+'
});

var propertySearchPriceRangeValuesInverse = $H();

propertySearchPriceRangeValues.each(function(pair) {
	propertySearchPriceRangeValuesInverse.set(pair.value, parseInt(pair.key));
});

var propertySearchPriceRangeSliderObj = null;

propertySearchGetCheckboxGroupVals = function(searchDiv, groupKey) {

	var rtnStr = '';
	
	if (!searchDiv.down('a#'+ groupKey +'_All') || !searchDiv.down('a#'+ groupKey +'_All').hasClassName('on')) {
		
		var tmpArr = [];
		
		searchDiv.select('a.'+ groupKey +'Button').each(function(btn) {
			
			if (btn.hasClassName('on') && btn.identify() !=  groupKey +'_All') {
				tmpArr.push(encodeURIComponent(btn.identify().substring(groupKey.length + 1)));
				
			}
			
		});
		
		if (tmpArr.size() > 0) {
			rtnStr = '&'+ groupKey +'[]='+ tmpArr.join('&'+ groupKey +'[]=');
		}
		
	}
	
	return rtnStr;

};

propertySearchGetRadioButtonGroupVals = function(searchDiv, groupKey) {

	var rtnStr = '';
	
	if (!searchDiv.down('a#'+ groupKey +'_All') || !searchDiv.down('a#'+ groupKey +'_All').hasClassName('on')) {
		
		searchDiv.select('a.'+ groupKey +'Button').each(function(btn) {
			
			if (btn.hasClassName('on') && btn.identify() !=  groupKey +'_All') {
				rtnStr = '&'+ groupKey +'='+ encodeURIComponent(btn.identify().substring(groupKey.length + 1));
				
			}
			
		});
	
	}
	
	return rtnStr;

};

propertySearchSetupCheckboxGroup = function(searchDiv, groupKey) {

	searchDiv.select('a.'+ groupKey +'Button').each(function(btn) {
		
		btn.observe('click', (function() {
			
			if (this.identify() ==  groupKey +'_All') {
				
				if (!this.hasClassName('on')) {
					this.addClassName('on');
					
					// turn off all others
					searchDiv.select('a.'+ groupKey +'Button').each(function(tmp) {
						if (tmp.identify() != groupKey +'_All') {
							tmp.removeClassName('on');
						}
					});
					
				}
				
			} else {
				
				if (this.hasClassName('on')) {
					this.removeClassName('on');
					
					var allUnchecked = true;
					searchDiv.select('a.'+ groupKey +'Button').each(function(tmp) {
						if (tmp.hasClassName('on')) {
							allUnchecked = false;
						}
					});
					
					if (allUnchecked) {
						$(groupKey +'_All').addClassName('on');
					}
					
				} else {
					this.addClassName('on');
					
					$(groupKey +'_All').removeClassName('on');
				}
				
			}
			
		}).bind(btn));
		
	});

};

propertySearchSetupRadioButtonGroup = function(searchDiv, groupKey) {

	searchDiv.select('a.'+ groupKey +'Button').each(function(btn) {
		
		btn.observe('click', (function() {
			
			if (!this.hasClassName('on')) {
				searchDiv.select('a.'+ groupKey +'Button').each(function(tmp) {
					tmp.removeClassName('on');
				});
				
				this.addClassName('on');
			}
			
		}).bind(btn));
		
	});

};

propertySearchResetGroup = function(searchDiv, groupKey) {

	searchDiv.select('a.'+ groupKey +'Button').each(function(btn) {
		
		if (btn.identify() ==  groupKey +'_All') {
			btn.addClassName('on');
		
		} else {
			btn.removeClassName('on');
			
		}
		
	});

};

propertySearchSetupPriceRangeSliderControl = function(searchDiv) {
	
	if (searchDiv.down('#sliderControl') 
			&& searchDiv.down('#sliderHandle1') && searchDiv.down('#sliderHandle2')
			&& searchDiv.down('#sliderMinValue') && searchDiv.down('#sliderMaxValue')) {
		
		var valuesArr = [];
		
		propertySearchPriceRangeValues.keys().each(function(val) {
			valuesArr.push(parseInt(val));
		});
		
		propertySearchPriceRangeSliderObj = new Control.Slider(['sliderHandle1', 'sliderHandle2'], 'sliderControl', {
		
			minimum: 0,
			maximum: 110,
			range: $R(0, 100),
			values: valuesArr,
			
			sliderValue: [
				propertySearchPriceRangeValuesInverse.get(searchDiv.down('#sliderMinValue').innerHTML), 
				propertySearchPriceRangeValuesInverse.get(searchDiv.down('#sliderMaxValue').innerHTML)
			],
			
			onSlide: function(value) {
				
				if (value[0] < value[1]) {
					searchDiv.down('#sliderMinValue').update(propertySearchPriceRangeValues.get(value[0].toString()));
					searchDiv.down('#sliderMaxValue').update(propertySearchPriceRangeValues.get(value[1].toString()));
				} else {
					searchDiv.down('#sliderMinValue').update(propertySearchPriceRangeValues.get(value[1].toString()));
					searchDiv.down('#sliderMaxValue').update(propertySearchPriceRangeValues.get(value[0].toString()));
				}
				
			},
			
			onChange: function(value) {
				
				if (value[0] < value[1]) {
					searchDiv.down('#sliderMinValue').update(propertySearchPriceRangeValues.get(value[0].toString()));
					searchDiv.down('#sliderMaxValue').update(propertySearchPriceRangeValues.get(value[1].toString()));
				} else {
					searchDiv.down('#sliderMinValue').update(propertySearchPriceRangeValues.get(value[1].toString()));
					searchDiv.down('#sliderMaxValue').update(propertySearchPriceRangeValues.get(value[0].toString()));
				}
				
			}
			
		});
		
	}

};

propertySearchResetPriceRangeSliderControl = function() {
	
	if (propertySearchPriceRangeSliderObj) {
		propertySearchPriceRangeSliderObj.setValue(0, 0);
		propertySearchPriceRangeSliderObj.setValue(110, 1);
	}
	
};

propertySearchGetPriceRangeVals = function() {
	
	var rtnStr = '';
	
	if (propertySearchPriceRangeSliderObj) {
		
		var value = propertySearchPriceRangeSliderObj.values;
		
		if (value[0] < value[1]) {
			rtnStr += '&min_price='+ encodeURIComponent(propertySearchPriceRangeValues.get(value[0].toString()));
			rtnStr += '&max_price='+ encodeURIComponent(propertySearchPriceRangeValues.get(value[1].toString()));
		} else {
			rtnStr += '&min_price='+ encodeURIComponent(propertySearchPriceRangeValues.get(value[1].toString()));
			rtnStr += '&max_price='+ encodeURIComponent(propertySearchPriceRangeValues.get(value[0].toString()));
		}
	
	}
	
	return rtnStr;
};

propertySearchSetupClearButton = function(searchDiv) {

	if (searchDiv.down('a.clearButton')) {
		
		var btn = searchDiv.down('a.clearButton');
		
		btn.observe('click', (function() {
			
			propertySearchResetGroup(searchDiv, 'section');
			propertySearchResetGroup(searchDiv, 'type');
			propertySearchResetGroup(searchDiv, 'area');
			propertySearchResetGroup(searchDiv, 'bedrooms');
			
			propertySearchResetPriceRangeSliderControl();
			
		}).bind(btn));
		
	}

};

propertySearchSetup = function(searchDiv, homeUrl) {
	
	// hookup search group button clicks
	propertySearchSetupRadioButtonGroup(searchDiv, 'section');
	
	propertySearchSetupCheckboxGroup(searchDiv, 'type');
	
	propertySearchSetupCheckboxGroup(searchDiv, 'area');
	
	propertySearchSetupCheckboxGroup(searchDiv, 'bedrooms');
	
	
	// hookup price range slider
	propertySearchSetupPriceRangeSliderControl(searchDiv);
	
	
	// hookup clear button
	propertySearchSetupClearButton(searchDiv);
	
	
	// hookup search button
	if (searchDiv.down('a.searchButton')) {
		
		var btn = searchDiv.down('a.searchButton');
		
		btn.observe('click', (function() {
			
			// build search url
			var url = homeUrl;
			if (searchDiv.down('a#section_Our') && searchDiv.down('a#section_Our').hasClassName('on')) {
				url += '/property-listings/our-listings/?section=Our';
			} else {
				url += '/property-listings/all-sothebys-listings/?section=All';
			}
			
			url += propertySearchGetCheckboxGroupVals(searchDiv, 'type');
			url += propertySearchGetCheckboxGroupVals(searchDiv, 'area');
			url += propertySearchGetCheckboxGroupVals(searchDiv, 'bedrooms');
			
			url += propertySearchGetPriceRangeVals();
			
			//alert(url);
			
			location.href = url;
			
		}).bind(btn));
		
	}
	
};




propertyPhotoGallerySetup = function(elm) {
	
	var navElm = elm.down('.scrollerInner');
	
	var rightArrowElm = elm.down('.rightArrow');
	var leftArrowElm = elm.down('.leftArrow');
	
	var imgsArr = elm.select('.photoGalleryItem');
	var thumbsArr = elm.select('.photoGalleryThumb');
	
	if (navElm && rightArrowElm && leftArrowElm && imgsArr.size() > 0 && thumbsArr.size() > 0) {
		
		var slideDuration = 0.40; // in second
		var eventQueueId = 'propGalleryNav';
		
		var curCount = 0;
		var maxCount = imgsArr.size() - 1;
		
		var curThumb = thumbsArr[curCount];
		var curImage = imgsArr[curCount];
		
		if (thumbsArr.size() > 0) {
			
			// set up clicking on nav item boxe
			for (i=0; i < thumbsArr.size(); i++) {
				
				thumbsArr[i].imageKey = i;
				
				thumbsArr[i].observe('click', (function() {
					if (!this.hasClassName('on')) {
						navElm.changeImage(this.imageKey);
					}
				}).bind(thumbsArr[i]));
				
			};
			
			navElm.changeImage = function(nextCount) {
				
				var nextThumb = thumbsArr[nextCount];
				var nextImage = imgsArr[nextCount];
				
				curCount = nextCount;
				
				nextThumb.highlight();
				curThumb.unhighlight();
				
				curThumb = nextThumb;
				
				nextImage.show();
				curImage.hide();
				
				curImage = nextImage;
				
			};
			
			
			
			var visibleCount = 6;
			var itemMargin = 10;
			
			var maxPosition = Math.ceil(thumbsArr.size() / visibleCount) - 1;
			var curPosition = 0;
			
			// width plus margin
			var itemWidth = thumbsArr[0].getWidth() + itemMargin;
			var stepWidth = visibleCount * itemWidth;
			
			// set inner scroll width based on width and number of items
			navElm.setStyle({ width: (thumbsArr.size() * itemWidth) +'px' });
			
			
			leftArrowElm.disabled = true;
			rightArrowElm.disabled = true;
			
			
			var resetArrows = function() {
				
				if (leftArrowElm && rightArrowElm) {
					
					if (curPosition == 0) {
						leftArrowElm.disable();
					} else {
						leftArrowElm.enable();
					}
					
					if (curPosition == maxPosition) {
						rightArrowElm.disable();
					} else {
						rightArrowElm.enable();
					}
					
				}
				
			};
			
			resetArrows();
			
			// listen to nav clicks
			leftArrowElm.observe('click', function() {
			
				if (navElm && !leftArrowElm.disabled) {
				
					new Effect.Move(navElm, { 
						duration: slideDuration,
						x: stepWidth, 
						mode: 'relative',
				
						afterFinish: function() {
							curPosition--;
							resetArrows();
						}, 
						
						queue: { position: 'end', scope: eventQueueId, limit: 1 }
					});
					
				}
				
				return false;
				
			});
			
			rightArrowElm.observe('click', function() {
				
				if (navElm && !rightArrowElm.disabled) {
					
					new Effect.Move(navElm, { 
						duration: slideDuration,
						x: -stepWidth, 
						mode: 'relative',
						
						afterFinish: function() {
							curPosition++;
							resetArrows();
						}, 
						
						queue: { position: 'end', scope: eventQueueId, limit: 1 }
					});
					
				}
				
				return false;
				
			});
			
		}
		
	}

};





homePageFeaturedPropertiesSlideshowSetup = function(elm, elmImages) {
	
	var navElm = elm.down('.navigation');
	var contentElm = elm.down('.content');
	var imgsArr = elmImages.select('div');
	
	if (navElm && contentElm && imgsArr.size() > 0) {
		
		var navItemBoxesArr = navElm.select('div');
		var contentBoxesArr = contentElm.select('div');
		
		var firstFadeInDuration = 0.5; // in seconds
		
		var curCount = 0;
		var maxCount = imgsArr.size() - 1;
		
		var curImage = imgsArr[curCount];
		var curContent = contentBoxesArr[curCount];
		var curNavItemBox = navItemBoxesArr[curCount];
		
		
		// only if more than one image
		if (maxCount > 0) {
			
			// set up clicking on nav item boxe
			for (i=0; i < navItemBoxesArr.size(); i++) {
				
				navItemBoxesArr[i].imageKey = i;
				
				navItemBoxesArr[i].observe('click', (function() {
					elm.fadeToNext(this.imageKey);
				}).bind(navItemBoxesArr[i]));
				
			};
			
			// now start the slideshow
			var fadeDuration = 2.0; // in seconds
			var fadeDelay = 4000; // in milliseconds
			var eventQueueId = 'homePageSlideshow';
			
			elm.timeoutObj = null;
			elm.fadeEffectObj = null;
		
			elm.fadeToNext = (function(nextCount) {
				
				// only if another fade is not executing
				if (!this.fadeEffectObj) {
					
					if (this.timeoutObj) {
						clearTimeout(this.timeoutObj);
					}
					
					var nextImage = imgsArr[nextCount];
					var nextContent = contentBoxesArr[nextCount];
					var nextNavItemBox = navItemBoxesArr[nextCount];
					
					this.fadeEffectObj = new Effect.Fade(curImage, { 
						duration: fadeDuration,  
						
						beforeStart: (function() {
							
							curContent.hide();
							nextContent.show();
							
							curCount = nextCount;
							
							nextImage.show();
							
							curNavItemBox.unhighlight();
							nextNavItemBox.highlight();
							
						}).bind(this), 
						
						afterFinish: (function() {
							
							curNavItemBox = nextNavItemBox;
							
							
							curImage.unhighlight();
							curImage = nextImage;
							curImage.highlight();
							
							curContent = nextContent;
							
							this.pauseBeforeNext();
							
							this.fadeEffectObj = null;
							
						}).bind(this), 
						
						queue: { position: 'end', scope: eventQueueId, limit: 1 } 
					});
					
				}
				
			}).bind(elm);
			
			elm.pauseBeforeNext = (function() {
				
				this.timeoutObj = setTimeout( (function() {
					
					var nextCount;
					if (curCount < maxCount) {
						nextCount = curCount + 1;
					} else {
						nextCount = 0;
					}
				
					this.fadeToNext(nextCount);
					
				}).bind(this), fadeDelay);
			
			}).bind(elm);
			
			
			elm.pauseBeforeNext();
			
		}
		
		
		
	}

};






// usage : writing out link
// <script>linkMail('hostdomain','username');</script>
// usage : link only
// <a href="javascript:popMail('hostdomain','username')">email us</a>
// usage : showing an email address
// <script>showMail('hostdomain','username');</script>

function popMail(host,user) {
	var acct = user +'@'+ host;
	self.location.href = 'mailto:'+ acct;
}

function linkMail(host,user,cls) {
	document.write('<a href="javascript:popMail(\''+ host +'\',\''+ user +'\');"');
	if (cls) {
		document.write(' class="'+ cls +'"');
	}
	document.write('>');
	showMail(host,user);
	document.write('</a>');
}

function showMail(host,user) {
	var acct = user +'@'+ host;
	document.write(acct);
}




// on window load functions
Event.observe(window, 'load', function() {
	
	// set up default input text
	$$('input[type="text"],textarea').each(function(item) { 
		
		var val = item.getValue();
		if (item.hasClassName(val.replace(/ /g,'-'))) {
			item.addClassName('default');
			item.writeAttribute({ 'defaultval' : val });
		}

		item.observe('focus', function(event) {
			
			var item = event.findElement();
			
			if (item.readAttribute('defaultval') && item.getValue() == item.readAttribute('defaultval')) {
				item.clear();
				item.removeClassName('default');
			}
			
		});
							
		item.observe('blur', function(event) {
			
			var item = event.findElement();
			
			if (item.readAttribute('defaultval') && item.getValue() == '') {
				item.value = item.readAttribute('defaultval');
				item.addClassName('default');
			}
			
		});

	});

});


