// default product settings
	var map;
	var pano;
	var baseIcon;
	var graphicIcon;
	var pointLatLon;
	var houseMarker;
	var count = 0;
	var pos = 0;
	var lastPos = -1;

	$(function(){
		
		$('#map').each(function(){
			if (GBrowserIsCompatible()) {
				pano = new GStreetviewClient();
				map = new GMap2(document.getElementById("map"));
	
				// Create a base icon for all of our markers that specifies the
				// shadow, icon dimensions, etc.
				baseIcon = new GIcon(G_DEFAULT_ICON);
				baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
				baseIcon.iconSize = new GSize(20, 34);
				baseIcon.shadowSize = new GSize(37, 34);
				baseIcon.iconAnchor = new GPoint(9, 34);
				baseIcon.infoWindowAnchor = new GPoint(9, 2);
			}
	
			loadMap();
		});
	
		$('.previewListing').click(function(){		 	
			$(this).parents('.listingInfo').children('.listingExtraInfo').slideToggle();
			$(this).toggleClass('arrowDown');
		});
		
		$('.viewListing').click(function(){		 	
			window.location = ($(this).parents('.listingInfo').children('a').attr('href'));
		});
		
		$('.listingExtraInfo').hide();
		
		$('.box>.middle:not(.altMiddle)').each(function(){
			if($(this).height() <= 200){
				$(this).css({backgroundImage:'url(./images/layout/middleShortBG.jpg)'});
			}
		});
		
		$('.pages').clone().insertAfter($('.listingInfo:last'));
		
		if($('.photoDisplay>.photo')){ swapPhoto(0); }
		
		$('#calc').hide();
		   
	});

	function createOverlay(kmlPath){								 
		geoXml = new GGeoXml("http://www.gurneyrealestate.com/maps/" + kmlPath);
		map.addOverlay(geoXml);	
	}
	
	function showPhotos(){
		$('#map').hide();
		$('#pano').hide(); 
		$('.photo').show();
		$('.photoThumbnails').slideDown();
		$('#calc').hide();
	}
	
	function showPano(){
		$('#map').hide();
		$('#pano').show(); 
		$('.photo').hide();
		$('.photoThumbnails').slideUp();
		$('#calc').hide();
	}
	
	function showMap(){
		$('#map').show();
		$('#pano').hide(); 
		$('.photo').hide();
		$('.photoThumbnails').slideUp();
		$('#calc').hide();
	}
	
	function showCalc(){
		$('#calc').show();
		$('#map').hide();
		$('#pano').hide(); 
		$('.photo').hide();
		$('.photoThumbnails').slideUp();
	}
	
	function showSchools(zipCode){
		$.getScript("http://www.gurneyrealestate.com/ajax/showSchools.php?zipCode=" + zipCode, function(data){						 
		});
	}
	
	function removeListing(mlsNumber, objID, hide){
		$.getScript("http://www.gurneyrealestate.com/ajax/removeListing.php?mlsNumber=" + mlsNumber + "&objID=" + objID + "&hide=" + hide, function(data){						 
		});
	}
	
	function addListing(mlsNumber, objID){
		$.getScript("http://www.gurneyrealestate.com/ajax/addListing.php?mlsNumber=" + mlsNumber + "&objID=" + objID, function(data){						 
		});
	}
	
	function addAlerts(mlsNumber, objID, price){
		$.getScript("http://www.gurneyrealestate.com/ajax/addPriceAlert.php?mlsNumber=" + mlsNumber + "&objID=" + objID + "&price=" + price, function(data){						 
		});
	}
	
	function removeAlerts(mlsNumber, objID, price){
		$.getScript("http://www.gurneyrealestate.com/ajax/removePriceAlert.php?mlsNumber=" + mlsNumber + "&objID=" + objID + "&price=" + price, function(data){						 
		});
	}
	
	function sendFriend(mlsNumber){
		$('.sendFriendContent').load("http://www.gurneyrealestate.com/ajax/getSendFriend.php?sendMLS=" + mlsNumber);
		$('#sendFriendOverlayID').show();	
	}
	
	function sendFriendData(){
		$.post("http://www.gurneyrealestate.com/ajax/sendFriend.php", { 'yourName' : $('#yourName').val(), 'yourEmail' : $('#yourEmail').val(), 'friendsName' : $('#friendsName').val(), 'friendsEmail' : $('#friendsEmail').val(), 'sendMLS' : $('#sendMLS').val(), 'security_code' : $('#security_code').val() }, function(data){ $('.sendFriendContent').html(data); });
	}
	
	function swapPhoto(photoIndex){
		
		$('.propertyInfo>.photoThumbnails>.sel').removeClass('sel');
		
		if(lastPos > -photoIndex){
			pos = lastPos - 1;
		} else if(lastPos < -photoIndex){
			pos = lastPos + 1;
		} else {
			pos = -photoIndex;
			$('.propertyInfo>.photoThumbnails>.thumbnail').eq(-pos).addClass('sel');
			return;
		}
		
		lastPos = pos;
		
		$('.propertyInfo>.photoThumbnails>.thumbnail').eq(-pos).addClass('sel');
		
		//pos = -photoIndex;
		
		count = 0;
		
		$('.photoDisplay>.photo').each(function(){
			
			if(pos >= 0){
				var z		= $('.photo').length - pos;
				var opac	= 1 - (pos * .30);
				var dif 	= z / $('.photo').length;
				var w		= 500 * dif;
				var h		= 365 * dif;
				
				var t		= (pos * (10 * (dif+1))) + 5;
				var l		= (632 - w) + (pos * (40 * (dif+1)));
				
				//if(lastPos < photoIndex){$(this).css({'z-index': z});}
				
			} else {
				var z		= $('.photo').length + pos;
				var opac	= 1 - (-pos * .15);
				var dif 	= z / $('.photo').length;
				var w		= 500 * dif;
				var h		= 365 * dif;
				
				var t		= (-pos * (10 * (dif+1))) + 5;
				var l		= 132 - (-pos * (40 * (dif+1)));
				
				//if(lastPos > photoIndex){$(this).css({'z-index': z});}
			}
			
			$(this).animate({
				'top': t,
				'left':l,
				'width': w,
				'height': h,
				'opacity': opac,
				'z-index': z
			}, 300, function(){ 
				$(this).css({'z-index': z});
				
				if($('.photo').length == count && lastPos != photoIndex){
					swapPhoto(photoIndex);
				}
			});
			
			pos++;
			count++;
			
		});	
		
	}
	
	function moveListing(id){
		offsetTop = $('.listingInfo:first').offset();
		offsetThis = $('#listing'+id).offset();
		
		$('#listing'+id).css('borderColor','#003300');
		$('#listing'+id).css('backgroundColor','#e6f3dd');
		
		if(offsetThis.top != offsetTop.top){
			
			$('#listing'+id).css('position','absolute');
			$('#listing'+id).css('z-index','999999');
			$('#listing'+id).css('top',offsetThis.top);
			$('#listing'+id).fadeTo(500, 0.75);
			
			$('#listing'+id).next('.listingInfo').css('marginTop', $('#listing'+id).height() + 12);
			
			$('#listing'+id).next('.listingInfo').animate({
				'marginTop': 5
			}, 2000);
			
			$('.listingInfo:first').css('borderColor','#cccccc');
			$('.listingInfo:first').css('backgroundColor','#FFFFFF');
			$('.listingInfo:first').animate({
				'marginTop':$('#listing'+id).height() + 12
			}, 2000);
			
			$('#listing'+id).animate({
				'top': offsetTop.top
			}, 2000, function(){
				$('#listing'+id).css('position','relative');
				$('#listing'+id).css('z-index','0');
				$('#listing'+id).css('top',0);
				$('.listingInfo:first').css('marginTop', 5);
				$('#listing'+id).insertBefore($('.listingInfo:first'));
				$('#listing'+id).fadeTo(500, 1);
			});
		}
	}
	
	// Create Markers
	function createMarker(point, index, content, icon) {
		
		// Set Icon
		graphicIcon = new GIcon(baseIcon);
		graphicIcon.image = "http://www.gurneyrealestate.com/images/map/"+icon+".png";
			
		// Set up our GMarkerOptions object
		markerOptions = { icon:graphicIcon };
		var marker = new GMarker(point, markerOptions);
	
		GEvent.addListener(marker, "click", function() {
			houseMarker = this;
			myPano = new GStreetviewPanorama(document.getElementById("pano"));
			pano.getNearestPanorama(point, showPanoData);
			//marker.openInfoWindowHtml(content, contentOptions);
			moveListing(index);
		});
				
		return marker;
	}
	
	function createMarkerProperty(point, index, content, icon) {
		
		// Set Icon
		graphicIcon = new GIcon(baseIcon);
		graphicIcon.image = "http://www.gurneyrealestate.com/images/map/"+icon+".png";
			
		// Set up our GMarkerOptions object
		markerOptions = { icon:graphicIcon };
		var marker = new GMarker(point, markerOptions);
	
		houseMarker = marker;
		myPano = new GStreetviewPanorama(document.getElementById("pano"));
		pano.getNearestPanorama(point, showPanoData);
				
		return marker;
	}
			
	function createMiniMap(id, point, icon){
		
		var miniMap = new GMap2(document.getElementById(id));
		miniMap.setCenter(point, 15);
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
				
		// Set Icon
		graphicIcon = new GIcon(baseIcon);
		graphicIcon.image = "http://www.gurneyrealestate.com/images/map/"+icon+".png";
		
		markerOptions = { icon:graphicIcon };
				
		miniMap.addOverlay(new GMarker(point, markerOptions));
	}
			
	function showPanoData(panoData) {
		if (panoData.code != 200) {
			//GLog.write('showPanoData: Server rejected with code: ' + panoData.code);
			return;
		}
				
		var angle = computeAngle(houseMarker.getLatLng(), panoData.location.latlng);
		myPano.setLocationAndPOV(panoData.location.latlng, {yaw: angle});
	}

	function computeAngle(endLatLng, startLatLng) {
		var DEGREE_PER_RADIAN = 57.2957795;
		var RADIAN_PER_DEGREE = 0.017453;
		
		var dlat = endLatLng.lat() - startLatLng.lat();
		var dlng = endLatLng.lng() - startLatLng.lng();
		// We multiply dlng with cos(endLat), since the two points are very closeby,
		// so we assume their cos values are approximately equal.
		var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat) * DEGREE_PER_RADIAN;
		
		return wrapAngle(yaw);
	}
		
	function wrapAngle(angle) {
		if (angle >= 360) {
			angle -= 360;
		} else if (angle < 0) {
			angle += 360;
		}
		
		return angle;
	}
	
	function calcpayment() {
	   var amt = parseFloat($('#amt').val());
	   var down = parseFloat($('#down').val());
	   amt = amt - down;
	   var ir = parseFloat($('#ir').val()) / 1200;
	   var term = parseFloat($('#term').val()) * 12;
	   var total=1;
	
	   for (i=0;i<term;i++) {
		  total = total * (1 + ir);
	   }
	   mp = amt * ir / ( 1 - (1/total));
	
	  // use Math object to chop all numbers after 2 digits
	  $('#payment').val(Math.round(mp*100)/100);
	  $('#total').val(Math.round(mp * term *100)/100);

	}
