

        var starter =0;
        var perPage = limit;
        var totalRecs = 0;
        var saveBeby = '';
        var within = 0;
        var sorter = 1;
        var asc = -1;
 

function getCBs(){
 	for(i=0;i<=49;i++){
 		if(document.mapForm.cb_extras[i].checked){
			XCB.push(i+1);
 		}
	}
}


function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function doSearch(city){var beby=ltrim(document.getElementById('beby').value);if(document.mapForm.myGal[0].checked)theFile="list-search.php?doList=1";else if(document.mapForm.myGal[1].checked)theFile="map-search.php?doGrid=0";else if(document.mapForm.myGal[2].checked)theFile="list-search.php?doList=0";else theFile="map-search.php?doGrid=1";getCBs();if(document.getElementById('beby').value.indexOf('ity,')>0){alert('Please enter a city, zip or mls#');document.getElementById('beby').focus();document.getElementById('beby').value='';return false;}else window.location=theFile + '&home=1&beby='+beby+'&within='+document.getElementById('within').value+'&minprice='+document.getElementById('minprice').value+'&maxprice='+document.getElementById('maxprice').value+'&type='+document.getElementById('type').value+'&bed='+document.getElementById('beds').value+'&bath='+document.getElementById('baths').value+'&square='+document.getElementById('square').value+'&year='+document.getElementById('year').value+'&foreclosure='+('foreclosure')+'&waterfront='+document.getElementById('waterfront').value+'&lotsize='+document.getElementById('lotsize').value+'&pool='+document.getElementById('pool').value+'&pDrop='+document.getElementById('pDrop').value+'&keywds='+document.getElementById('keywds').value+'&xcb='+XCB
}



 
 
 

$(document).ready(function(){

	var homeCard;
	var myInt;

	var index = 1;
	var images = $("#gallery img");
	var thumbs = $("#thumbs img");
//	var imgHeight = $(thumbs).attr("height");
	var imgHeight = 57;
	var currentDiv = 1;
	

 	function fixIt(){
		$("#container").css({ opacity: 1.0 });		

	}

 
	function initialize(){
 		if($.browser.msie){
	//		$('#beby').css('line-height',45);
		}
		showGallery();
      	showMiddle(0);
 		
     }
	
	
	function saveFav(mlsnum){
		if(bigUser < 100){
			alert("Please sign up in order save favorites.");
		}else{
			var s = "/jq-save.php?saveSearch=0&mlsnum=" + mlsnum;
			$.post(s, function(data) {
				alert(data);
			});	
		}
	}
	
	function showGallery(){
		var str;
		//$('#loading1').show();
		
		var url = "/jq-home.php";
			$.getJSON(url, function(json) {
				var counter=0;		
					if (json.jrecords.length > 0) {
					$.each(json.jrecords,function() {
						var rec = this;		
						counter++;
						if(rec.hasphoto){
							myImage = "http://www.dreamrealty.com/photos/Photo" + this.sysid  + "-1.jpeg";
						}else{	
							myImage = "images/no_image.gif";
						}
						//console.log(myImage);
						var div1 = document.createElement('div');
						var div2 = document.createElement("div");
						
							var thumb = document.createElement('span');					
							spanId = "thumb-" + counter;
							$(thumb).attr("id",spanId);
							$(thumb).html("<img src=" + myImage + ">");
							$(thumb).html(str);
							
							var span2 = document.createElement('span');					
							spanId = "span2-" + counter;
							$(span2).attr("id",spanId);
							$(span2).html("<img src=" + myImage + ">");
							$(span2).html(str);
							$(div1).attr("id","div1-"+counter);		
							$(div1).addClass('homeCard');	
							//$(div2).addClass('bottomDiv');	
							$(div2).attr("id","div2");									
								
							homeCard = "<table><tr><td><h1>" + this.city + ",&nbsp;FL</h1></td></tr><tr><td><h3>" + addCommas(this.listprice,true) + "</h3></td></tr><tr><td>" + this.propcode + "</td></tr><tr><td>" + this.beds + "&nbsp;Beds&nbsp;|&nbsp;" + this.baths + "&nbsp;Baths&nbsp;|&nbsp;" + addCommas(this.sqft,false) + "&nbsp;Sq Ft</td></tr><tr><td>On market since " + this.listdate + "</td></tr><tr><td><span class=remarks>" + this.remarks + "</span></td></tr></table>";		
							var span4 = document.createElement('span');
							spanId = "span4-" + counter;
							$(span4).attr('id',spanId);
							$(span4).html(this.subdivision);
							$(span4).addClass('homeSub');
							var span3 = document.createElement('span');
							spanId = "span3-" + counter;
							$(span3).attr('id',spanId);
							$(span3).addClass('large-orange-btn');
							$(span3).html('Details');
							$(div1).append($(span2));
							$(div1).append(homeCard);
							//$(div1).append($(span4));
							
							$(div1).append($(span3));
										
						
						$(thumb).click(function(){
							var index = $(this).attr('id');
							index = index.substr(6,2);
							show(index);
						});
						
						$(span3).click(function(){
							showListing(rec.mlsnum,rec.address1,1);											   
						});	
						$(span2).click(function(){
							$("#pause").trigger('click');													
							showSlides(rec.sysid,rec.photocount,rec.address1);											   
						});	
						
 						$("#gallery").append(div1);
						$("#thumbs").append(thumb);
						$('#loading1').hide();
					}); //each
				} //rec > 0
				show(1);
			});
 		myInt = setInterval(goForward, 8000);
		$("#next").click(function(){goForward()});
		$("#prev").click(function(){goBack()});
		$("#pause").click(function(){clearInterval(myInt);myInt=0});
		
 	}
	
 	function showMiddle(starter){
		var spanId;
		var listCard;
		var str;
		var groupNo, i;
		var active;
//		alert($("#sortOrder").val());
		$("#middles").empty();

		$('#loading').show();
 	//	if(mCode==3)active=0;else active=1;
		if($("#mCode").val()==3)active=0;else active=1;
		var data = $('#homeForm').serialize();
		if(starter<0)starter=0;
		
 		var url = "mids.php?mStarter=" + starter + "&" + data;		
	//	console.log(url);
      	$.getJSON(url, function(json) {
			var counter=0;														  
   			if (json.jrecords.length > 0) {
				$.each(json.jrecords,function() {
					var rec = this;		
					counter++;
					if(counter==json.jrecords.length){
 
						if((starter+5) > this.totalrecs){
 							$("#rview").attr('disabled',true);
							$('#rview').attr('src','images/rightDis.gif');
 							$('#lview').attr('disabled',false);
							$('#lview').attr('src','images/left.gif');
							
							starter = this.totalrecs-5;
						}else if((starter-5) < 0){
  							$('#lview').attr('disabled',true);
							$('#lview').attr('src','images/leftDis.gif');
							$("#rview").attr('disabled',false);
							$('#rview').attr('src','images/right.gif');
							
							starter+=5;
						}else{
							$("#rview").attr('disabled',false);
							$('#rview').attr('src','images/right.gif');
							$("#lview").attr('disabled',false);
							$('#lview').attr('src','images/left.gif');
						}
							
					}
  					if(rec.hasphoto){
						myImage = "http://www.dreamrealty.com/photos/Photo" + this.sysid  + "-1.jpeg";
					}else{	
						myImage = "images/no_image.gif";
					}
					var div1 = document.createElement('div');
					var div2 = document.createElement("div");
					
 						var span1 = document.createElement('span');
						spanId = "span1-" + counter;
						$(span1).attr("id",spanId);
						$(span1).html(rec.address2);
						$(span1).addClass("houseAddress");
						var span2 = document.createElement('span');					
						spanId = "span2-" + counter;
						$(span2).attr("id",spanId);
						$(span2).html("<img src=" + myImage + ">");
						$(span2).html(str);
						$(span2).css("width",160);
						$(span2).css("height",100);
 						var span4 = document.createElement('span');
						spanId = "span4-" + counter;
						$(span4).attr("id",spanId);
						$(span4).html("Follow This Home");
						$(span4).addClass("red-btn");
						
						var span7 = document.createElement('span');										
						spanId = "span7-" + counter;
						$(span7).attr("id",spanId);
						$(div1).addClass('houseCard');	
						$(div1).attr("id","div1");			
						
						$(span7).html("<img src=images/full2.png title='Show complete listing in new page' class='smallPage'>");
	//					$(span7).addClass("smallPage");		
						var houseCard = "<h3>" + addCommas(this.listprice,true) + "</h3><table><tr><td><b>" + this.propcode  + "</b></td></tr><tr><td>MLS#&nbsp;"	+ this.mlsnum + "</td></tr><tr><td>Beds:&nbsp;" + rec.beds + "&nbsp;Baths:&nbsp;" + this.baths + "</td></tr></table>";		
 						$(div2).append($(span1));
						$(div2).append($(span7));
						
						$(div2).css({
								'text-align' : 'center',	
								'margin-left' : 'auto',
								'margin-right': 'auto',
								'display':'inline'
   						}); 				
						
						$(div1).append($(div2));
						
						$(div1).append($(span2));
						$(div1).append(houseCard);		
						$(div1).append($(span4));											
															
 					
   					$(span1).click(function(){
						showListing(rec.mlsnum,rec.address1,active);											   
					});	
   					$(span2).click(function(){
						showSlides(rec.sysid,rec.photocount,rec.address1);											   
					});	
   					$(span4).click(function(){
						saveFav(rec.mlsnum);											   
					});	
    				$(span7).click(function(){
						var str = "http://www.dreamrealty.com/dream-realty-details.php?lid=" + rec.mlsnum + "&active=" + active;
						window.open(str);
 					});	
 
   					$(div1).mouseover(function(){
						$(this).css({
							'background-color' : '#ffa',		
   							'border' : '1px solid orange'
  							});
					});
  					$(div1).mouseout(function(){
 							$(this).css({
								'background-color' : '#fff',		
								'border' : '1px solid #CCBE9F'
								});
 								
					});
					
  					$("#middles").append(div1);
					$('#loading').hide();
 				});
			}else{
				$('#loading').hide();
				//showResults(0,0);				
			}
 			$("#search").css("background-image","url(images/searchBack1.png)");
			$("#search").css("height",80);

		});	
 	}
 		
 
 /*										

 		$("#btn_search").click(function() {  
			$('#extras').hide();	
			$('#searchCollapse').hide();
			$('#searchExpand').show();
			if($('#beby').val()!=saveBeby){
 				getLatLon();
			}
			showMap(0);
  		});  		
*/		
		
		$('#beby').autocomplete({source:'auto.php', minLength:2});			

 
 
 	$("#lview").click(function(){
 		starter-=5;
		showMiddle(starter);
	});

 	$("#rview").click(function(){
		starter+=5;
		showMiddle(starter);
	});
	
	$("#mCode").change(function(){
 		showMiddle(0);
	});

	$("#mCounty").change(function(){
 		showMiddle(0);
	});
	
	$("#mOrder").change(function(){
 		showMiddle(0);
	});
	
	function showNavLeft(totalrecs,starter){
 		if((starter-limit) >= 0){
			starter-=limit;			
			$(but1).click(function(){
				showMap(starter);
			});
		}else{
			$(but1).attr('disabled','disabled');
			$(but1).html("<img src='images/lview_none.png'>");
		}
		$('#searchResults').append(but1);
	}


	function showNavRight(totalrecs,starter){
   		var but1 = document.createElement('button');
		$(but1).css({
			'position' : 'absolute',		
   			'left' : 345,
   			'top' : 0
 		});
		
		$(but1).html("<img src='images/rview.png'>");
		if((starter+limit) < totalrecs){
			starter+=limit;			
			$(but1).click(function(){
				showMap(starter);
			});
		}else{
			$(but1).attr('disabled','disabled');
			$(but1).html("<img src='images/rview_none.png'>");
		}
		
		$('#searchResults').append(but1);
	}

	function showResults(totalRecs,starter){
		var ender, firster;
   		var show = '';
	//	document.getElementById("listNav").style.display = "inline";
		firster = starter + 1;
 		if(starter + perPage > totalRecs) ender = totalRecs; else ender = starter + perPage;
	//	console.log("starter ", starter, "-",ender, "-",totalRecs, "-",firster);
		show = "<span class=sstatus>&nbsp;&nbsp;";
		if(!totalRecs) {
		 	show += "<h1>0 matching listings.</h1>"
//			document.getElementById("statushead").innerHTML = show;		
		}else{
			if (totalRecs===1) show += totalRecs + " matching listing";
			else if (totalRecs > perPage) show += totalRecs + " matchings listings. &nbsp; Showing " + perPage;
			else show += totalRecs + " matching listings.";
			
			if(totalRecs > 1 && totalRecs <= perPage)
				show = "<span id='results'><h2>1-" + totalRecs + "</h2><h3>of " + totalRecs + " total</h3>";
			else if (totalRecs > perPage)
				show = "<span id='results'><h2>" + firster + "-" + ender + " </h2><h3>of " + totalRecs + " total</h3>";
			else 
				show = "<span id-'results'><h2>1-" + totalRecs + "</h2><h3>of " + totalRecs + "total </h3>";
		}
		$('#results').html(show);
	}


	function showSlides(sysid,count,address) {
		var str;
  		if(gPopupContainer==null) initPopUp(); 
		$("#container").css({ opacity: 0.1 });		
		
		str = "slideshow.php?sysid=" + sysid + "&count=" + count;
 		
		showPopWin(str, 535, 420, fixIt);
		
	//	str = '<a href=details.php?lid=' + mlsnum + ' target = "_blank" title="Click to open listing in a new window">' + address + '</a>';
 		document.getElementById('popupTitle').innerHTML = address;
	}
		

	
	function showListing(mlsnum,address,active) {
		var str;
//		address = address.replace(/-/g," ");
		$("#container").css({ opacity: 0.1 });		
		
//		if(!isIE)doOpacity(20);
		if(gPopupContainer==null) initPopUp(); 
		
		if(active){
			str = "md2.php?lid=" + mlsnum + "&active=1";
		} else {
			str = "md2.php?lid=" + mlsnum + "&active=0";
		}
		if($.browser.msie){
			showPopWin(str, 630, 430, fixIt);			
 		}else{
			showPopWin(str, 615, 400, fixIt);			
		}
		
		str = '<a href=/dream-realty-details.php?lid=' + mlsnum + '&active=' + active + ' target = "_blank" title="Click to open listing in a new window">' + address + '</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=/dream-realty-details.php?lid=' + mlsnum + '&active=' + active + ' target = "_blank" title="Click to open listing in a new window" style="color:#ff6">(See full page listing)</a>';
		
	//	str = '<a href=details.php?lid=' + mlsnum + ' target = "_blank" title="Click to open listing in a new window">' + address + '</a>';
		document.getElementById('popupTitle').innerHTML = str;
	}
		
	
 			
	function contactMe(mlsnum,contact,price) {
		var str;
		$("#container").css({ opacity: 0.1 });		
		
		if(gPopupContainer==null) initPopUp(); 
		if(contact){
			str = "contactMe.php?doContact=1&mlsnum=" + mlsnum + "&price=" + price;
		} else {
			str = "contactMe.php?doContact=0&mlsnum=" + mlsnum + "&price=" + price;
		}
		showPopWin(str, 700, 400, fixIt);
		if(contact){
			str = "Contact Me";
		}else{
			str = "Schedule Tour";
		}
		$("popupTitle").innerHTML = str;
	}

	$("#searchExpand").click(function(){
		window.location = "/map-search.php";
	});
		
/* 		
 	$("#searchExpand").click(function() {  
		$('#searchCollapse').show();									  
		$('#searchExpand').hide();									  
 		$("#extras").show();
		$("#search").css("background-image","url(images/searchBack6.jpg)");
		$("#search").css("height",440);
   	});  		
   	
 	$("#searchCollapse").click(function() {  
		$('#searchCollapse').hide();									  
		$('#searchExpand').show();									  										
 		$("#extras").hide();
		$("#search").css("background-image","url(images/searchBack1.png)");
		$("#search").css("height",80);
		
  	});  
*/	
	
  
 
 
 
	function goForward()
	{
		if(myInt=='' || myInt==0){
			myInt = setInterval(goForward, 8000);
		}

		if (index<(limit)){
			index+=1 ; 
 		}else {
			index=1;
		}
		show (index);
	}

	function goBack()
	{
		if(myInt=='' || myInt==0){
			myInt = setInterval(goBack, 8000);
		}

		if (index>1){
			index-=1 ; 
 		}else {
			index=1;
		}
		show (index);
	}
	
	function show(num)
	{
//		console.log(currentDiv,"-",num);
 		$("#div1-"+currentDiv).fadeOut(400);
//		$("#gallery").empty();
 
//		$("#span2-"+num ).stop().fadeIn(400);
		$("#div1-" + num).stop().fadeIn(400);
		$("#div1-"+num).css('opacity',1);
		
	//	$("#div1-"+num).show();
		currentDiv = num--;
		var scrollPos = (num+1)*imgHeight;
 		$("#thumbs").stop().animate({scrollTop: scrollPos}, 400);		
	}
	
   
    initialize();
    
});//end doc ready



