/**
 * @author Kirk B Wagoner
 */

//<![CDATA[
jQuery.fn.clrfrm = function () {
  $(this).each (function() { this.reset(); });
}

//Icons for Map

  var image = new google.maps.MarkerImage('img/mm_20_blue.png',
      new google.maps.Size(12, 20),
      new google.maps.Point(0,0),
      new google.maps.Point(6, 20));
  var shadow = new google.maps.MarkerImage('img/mm_20_shadow.png',
      new google.maps.Size(22, 20),
      new google.maps.Point(0,0),
      new google.maps.Point(6, 20));
		
var url = "mapservice.php";		
var minprice= 0;
var maxprice = 100000000;
var beds = "";
var city = "";
var om = null; // for Overlay message

// Overlay message functions
function Setup()
    {
    om = new acme.overlaymessage.OverlayMessage( document.getElementById( 'map' ) );
    On();
    }

function On()
    {
    om.Set( 'Please wait while we load the available homes into the map' );
    }

function Off()
    {
    om.Clear();
    }

// This function takes selection from form and appends to url submited for map data
function test(){
		//Set variables
		var url = "mapservice.php" + "?";
		var beds = document.sform.beds.value;
		var minprice = document.sform.minprice.value;
		var maxprice = document.sform.maxprice.value;
		var minacres = document.sform.minacres.value;
		var maxacres = document.sform.maxacres.value;
		var city = document.sform.city.value;
		var typechoice = document.sform.typechoice.value;
		
		
// Add price to	url
	url = url + "minprice=" + minprice + "&maxprice=" + maxprice;
		if (beds == "ALL"){
			url = url;
			}
		else{
			url = url + "&beds="+beds; 
			}
		if (minacres != "" || maxacres != "" ){
			url = url + "&minacres=" + minacres + "&maxacres" + maxacres;
		}
		if (city !=""){
			url = url + "&city=" + city;
		}
		if (typechoice !=""){
			url = url + "&typechoice=" + typechoice;
		}
		
		load(url);		
// end function		
} 
function test1(){
var url = "mapservice.php" + "?";
var street1 = document.streetform.street1.value;
var mls = document.streetform.MLS.value;

if (street1 !=""){
url = url + "street1=" + street1;
}

else {
	if(mls !=""){
	url = url + "MLS=" + mls;
	}
}
load(url);
}

// load data for map
function load(url) {
			var map = new google.maps.Map(document.getElementById("map"), {
				center: new google.maps.LatLng(46.595806, -112.027031),
				zoom: 10,
				mapTypeId: 'roadmap'
			});
			
			var infoWindow = new google.maps.InfoWindow({maxWidth:500});
			// Change this depending on the name of your PHP file
	Setup(); //turn on overlay "Loading"			
			downloadUrl(url, function(data) {
				var xml = data.responseXML;
				var markers = xml.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < markers.length; i++) {
					var price = markers[i].getAttribute("price");
					var name = markers[i].getAttribute("name");
					var address = markers[i].getAttribute("address");
					var city = markers[i].getAttribute("city");
					var remarks = markers[i].getAttribute("remarks");
					var type = markers[i].getAttribute("type");
					var beds = markers[i].getAttribute("beds");
					var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
					// Display the below in google popout
					var html = "<b>$" + price + "<\/b> <br /> <img class='difcursor' src='images/listings/HELENA" + name + ".JPG' width='230px' height='150px' onError=\"this.src=\'img/nophoto.gif\';\" onClick=\"propdet(" + name + ")\"" + "> <img src='img/logo-small.png' width='230px' height='150px' border='1'><br />" + address + "<br />" + city + "<br />" + remarks;
					//var icon = customIcons[type] || {};
					var marker = new google.maps.Marker({
						map: map,
						position: point,
						icon: image,
						shadow: shadow
					});
					bindInfoWindow(marker, map, infoWindow, html);
					
			} 
		Off(); // Turn off overlay	
		});
		 
		}

		function bindInfoWindow(marker, map, infoWindow, html) {
			google.maps.event.addListener(marker, 'click', function() {
				infoWindow.setContent(html);
				infoWindow.open(map, marker);			
			});
		}

		function downloadUrl(url, callback) {
			$(".bottom").ajaxStart(function(){
    			$("#wait").css("display","block");
  				});
  		
  			$(".bottom").ajaxComplete(function(){
    			$("#wait").css("display","none");
  				});
			
			var request = window.ActiveXObject ?
			new ActiveXObject('Microsoft.XMLHTTP') :
			new XMLHttpRequest;

			request.onreadystatechange = function() {
				if (request.readyState == 4) {
					request.onreadystatechange = doNothing;
					callback(request, request.status);
				}
			};
			request.open('GET', url, true);
			request.send(null);
			}

		function doNothing() {  
		}
 
//-->

// Slide out panels
$(document).ready(function(){
	$(".trigger").click(function(){
		$(".panel").toggle("fast");
		$(this).toggleClass("active");
		return false;
	});
});


$(document).ready(function(){
	$(".trigger1").click(function(){
		$(".panel1").toggle("fast");
		$(this).toggleClass("active");
		return false;
	});
});

$(document).ready(function(){
	$(".trigger2").click(function(){
		$(".panel2").toggle("fast");
		$(this).toggleClass("active");
		return false;
	});
});

$(document).ready(function(){
	$(".trigger3").click(function(){
		$(".panel3").toggle("fast");
		$(this).toggleClass("active");
		return false;
	});
});

$(document).ready(function(){
	$(".trigger4").click(function(){
		$(".panel4").toggle("fast");
		$(this).toggleClass("active");
		return false;
	});
});

$(document).ready(function(){
	$(".trigger5").click(function(){
		window.location.reload()
		return false;
	});
});

$(document).ready(function(){
	$(".trigger6").click(function(){
		$(".panel6").toggle("fast");
		$(".panel7").hide("fast");
		$("#streetform").clrfrm();
		$(this).toggleClass("active");
		return false;
	});
});

$(document).ready(function(){
	$(".trigger7").click(function(){
		$(".panel7").toggle("fast");
		$(".panel6").hide("fast");
		$("#streetform").clrfrm();
		$(this).toggleClass("active");
		return false;
	});
});

$(document).ready(function(){
	$(".triggerleft").click(function(){
		$('.bottom').load('seller.php');
		return false;
	});
});

$(document).ready(function(){
	$(".triggerleft1").click(function(){
		$('.bottom').load('buyer.php');
		return false;
	});
});

$(document).ready(function(){
	$(".triggerleft2").click(function(){
		$('.bottom').load('mort.php');
		return false;
	});
});

$(document).ready(function(){
	$(".triggerleft3").click(function(){
		$('.bottom').load('featured.php?ajax=1');
		 return false;
	});
});

$(document).ready(function(){
	$(".triggerleft4").click(function(){
		window.location.reload()
		return false;
	});
});

//Cycle slideshow
$(document).ready(function() {
    $('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
}); 

//Cycle slideshow #2 and
//send mls # to property description php
function propdet(view){
		$(".bottom").ajaxStart(function(){
    		$("#wait").css("display","block");
  		});
  		
  		$(".bottom").ajaxComplete(function(){
    		$("#wait").css("display","none");
  		});
		
		$('.bottom').load('propdet.php?view=' + view, function(){ 
		$('#pause').click(function() {$('.slides').cycle('pause'); return false; });
    	$('#play').click(function() { $('.slides').cycle('resume'); return false; });
    	
    	$('.slideshow1').hover(
        function() { $('.controls').fadeIn(); },
        function() { $('.controls').fadeOut(); }
    );

    $('.slides').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 3000,
        next:   '#next',
        prev:   '#prev'
    });
	});
	};

// Serialize form data and send	to mort.php
function calcit(){
		$(".bottom").ajaxStart(function(){
    	$("#wait").css("display","block");
  		});
  		
  		$(".bottom").ajaxComplete(function(){
    		$("#wait").css("display","none");
  		});
		
		var str = $('#calcform').serialize();	
		$('.bottom').load('mort.php', str);		
} 

	
$(document).ready(function(){
	$(".bottom").ajaxStart(function(){
    	$("#wait").css("display","block");
  		});
  		
  		$(".bottom").ajaxComplete(function(){
    		$("#wait").css("display","none");
  		});
  		
  		$(".street").click(function(){
		test1();
		
		var street1 = $('#streetform').serialize();
		$('.bottom').load('search.php', street1);
		return false;
	});
});

// Overlay message functions
function namespace( name )
    {
    var parts = name.split( '.' );
    var o = window;

    for ( var i = 0; i < parts.length; ++i)
	{
	if ( o[parts[i]] === undefined )
	    o[parts[i]] = {};
	o = o[parts[i]];
	}
    }

namespace('acme.overlaymessage');
acme.overlaymessage.OverlayMessage=function(container)
{var parent=container.parentNode;var wrapper=document.createElement('div');
wrapper.style.cssText=container.style.cssText;parent.insertBefore(wrapper,container);
parent.removeChild(container);wrapper.appendChild(container);
container.style.cssText='position: relative; width: 100%; height: 100%;';
this.overlay=document.createElement('div');
wrapper.appendChild(this.overlay);
this.backgroundColor='#9999cc';
this.borderColor='#666699';
this.overlay.style.position='relative';
this.overlay.style.top='-55%';
this.overlay.style.backgroundColor=this.backgroundColor;this.overlay.style.width='40%';
this.overlay.style.textAlign='center';
this.overlay.style.marginLeft='auto';
this.overlay.style.marginRight='auto';
this.overlay.style.padding='2em';
this.overlay.style.borderWidth='0.08in';
this.overlay.style.borderStyle='ridge';
this.overlay.style.borderColor=this.borderColor;
this.overlay.style.zIndex='100';
this.overlay.style.opacity='.75';
this.overlay.style.filter='alpha(opacity=75)';
this.overlay.style.display='none';
};
acme.overlaymessage.OverlayMessage.prototype.Set=function(message)
{this.overlay.innerHTML=message;this.overlay.style.display='';
};
acme.overlaymessage.OverlayMessage.prototype.Clear=function()
{this.overlay.style.display='none';
};
acme.overlaymessage.OverlayMessage.prototype.SetBackgroundColor=function(color)
{this.backgroundColor=this.overlay.style.backgroundColor=color;
};
acme.overlaymessage.OverlayMessage.prototype.SetBorderColor=function(color)
{this.borderColor=this.overlay.style.borderColor=color;
};

function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
    var type = this.type;
    var tag = this.tagName.toLowerCase(); // normalize case
    // it's ok to reset the value attr of text inputs,
    // password inputs, and textareas
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = "";
    // checkboxes and radios need to have their checked state cleared
    // but should *not* have their 'value' changed
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    // select elements need to have their 'selectedIndex' property set to -1
    // (this works for both single and multiple select elements)
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};
