        var markers = new Array(100);
        var centerMap = false;
        var totalMarkerCount = 50;
        function hideAllExtras() {
            for(i=0;i<totalMarkerCount;i++) {
                HideIt("extra"+i);
                theID = "zaphour"+i;
                if(document.getElementById(theID) != null) {
                    document.getElementById(theID).style.background= "";
                    document.getElementById(theID).style.border= "";
                }
            }
        }

        var currentSelectedMarker = null;  // once a marker is selected this holds the number of the selected marker.
        var currentSelectedLatLng = null;
        var selectedClickCount = 0;

        function showExtra(n,trigger) {
            theID = "zaphour"+n;
            hideAllExtras();
            // trigger is true if user clicked on list view...
            if(trigger) {
                centerMap = true;
                GEvent.trigger(markers[n], "click");
            } else {
            }
            ShowIt("extra"+n);
            /*
            if(currentSelectedMarker && currentSelectedMarker != n) {
                alert("bam..");
                selectedClickCount ++;
                if(selectedClickCount>1) {
                    alert("test"+selectedClickCount);
                    if(!trigger) {
                        location.href="#"+theID;  // jump to this div id.. this will show the clicked on zaphour in the list view.
                    }
                }
            }
            */
        }

		
		
        function highlightItem(n) {
            theID = "zaphour"+n;
            if(document.getElementById(theID) != null) {
                document.getElementById(theID).style.background= "#eee";
                document.getElementById(theID).style.border= "2px solid #333";
                //document.getElementById(theID).scrollTop(800);
                //$("#scroller").scrollTop((n*50)-40); // offset for each listing. 57
                theScroller.go(theID);
            }
        }


/*        
    function startupInit() {
      setTimeout('showExtra(0,true)', 2000);
    }
    window.onload =  startupInit; // load the first one...
*/

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}


function populateHomePage(server,location) {
	//var searchFor = urlencode('<?=$m['Captial'];?>,<?=$m['Name'];?>');
	var biscuit = getCookie("ZAPLIVE_");
	//var location = "Portland";
	var search = document.getElementById("businessSearch").value;
	var searchURL = "";
	var resultCount = 0;
	if(search.length > 2) {
		if(search != "Search Here") {
			searchURL = "/&search="+urlencode(search);
		}
	} else {
		alert("You must type more then 2 letters for searching.");
	}
	$.getJSON("http://"+server+"/api/getHomePageZapHours/"+biscuit+"/"+location+searchURL, function(data){
		  $.each(data.payload.Offers, function(index, item){
		  	addBiz(item);
		  	resultCount ++;
		  });
		  if(resultCount==0) {
		  	alert("No results found for your search. Please try something else.");
		  } else {
		  	showExtra(1,null);
		  }
		  return true;
	});
}


        function distance(lat1, lon1, lat2, lon2) {
            var radlat1 = Math.PI * lat1/180;
            var radlat2 = Math.PI * lat2/180;
            var radlon1 = Math.PI * lon1/180;
            var radlon2 = Math.PI * lon2/180;
            var theta = lon1-lon2;
            var radtheta = Math.PI * theta/180;
            var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
            dist = Math.acos(dist);
            dist = dist * 180/Math.PI;
            dist = dist * 60 * 1.1515;  // for miles.
            return dist;
        }

        var nextPointNum = 0;
        function getNextPoint() {
            nextPointNum = nextPointNum + 1;
            if(nextPointNum > 6) nextPointNum = 1;
            switch (nextPointNum) {
                case 1: return new GPoint(1, 1);
                case 2: return new GPoint(30, 1);
                case 3: return new GPoint(30, 30);
                case 4: return new GPoint(1, 30);
                case 5: return new GPoint(-20, 1);
                case 6: return new GPoint(-20, -20);
            }
        }

		var orderCreationCount = 500;
        function orderOfCreation(marker,b) { orderCreationCount--; return orderCreationCount; }
        function orderOfCreationAlwaysOnTop(marker,b) {return 501; }
        
        function createMarkerListener(count,latlng,size,checkCurrentSelected) {
            if(centerMap){ map.setCenter(latlng, 11); centerMap = false; }  // 11 zoomed out.. 12 zoomed in.

            // if there is already a selected marker.. first make it smaller.
            if(checkCurrentSelected==true && currentSelectedMarker !=null) {
                createMarkerListener(currentSelectedMarker,currentSelectedLatLng,30,false);
            }
            showExtra(count,false);
            highlightItem(count);
            var icon = new GIcon();
            icon.image = markers[count].getIcon().image;
            icon.iconSize = new GSize(size, size);  // set new size
            icon.iconAnchor =  markers[count].getIcon().iconAnchor;
            icon.infoWindowAnchor = new GPoint(35, 1);
            icon.infoShadowAnchor = new GPoint(13, 13);
            map.removeOverlay(markers[count]);  // delete old one.
            markers[count] = new GMarker(latlng,{icon:icon, zIndexProcess:orderOfCreationAlwaysOnTop});

            map.addOverlay(markers[count]);
            GEvent.addListener(markers[count], "click", function() {
                     createMarkerListener(count,latlng,40,true);  //recurse and make bigger
            });
            if(checkCurrentSelected==true) {
                currentSelectedMarker = count;  // set the current selected marker.
                currentSelectedLatLng = latlng;
            }
        }

        function RandomNum(low, high) {  return Math.random()*(high-low) + low;  }
        function RandomInt(low, high) {  return Math.floor(Math.random()*(high-low+1)) + low;  }
        
        function addBiz(item) {
        		//item.Address,item.BusinessName,item.Discount,item.Left,item.Num,item.URL,item.Lat,item.Lon
				var zapClass;
				var soldout = false;
               	if(item.Left<1) { zapClass = 'gray'; soldout = true; }
                else zapClass = 'orange';
                
                var disText = item.Discount;
                var motext = '';
                var moTitle = '';

                if(item.MOStatus) {
                    motext = "<span><p style='font-size:17pt;padding-left:50px;'>- OR -</p></span>";
                    motext += "<span id='mo"+item.Num+"'><p><a href='"+item.MOURL+"'><img src='images/button-make-offer.png' width='160'></a></p></span>";
                    zapClass = 'green';
                    if(soldout) {
                    	disText = "<b> - Make An Offer - </b>";
                    } else {
	                    disText += '<br>OR <b>Make An Offer</b>';
                    }
                }

                var txt = "<div id='zaphour"+item.Num+"' class='"+zapClass+"' onclick=\"showExtra('"+item.Num+"',true);\" >"+item.BusinessName+"<span>"+disText+"</span>";
                if(soldout) {
                	var signInForNotices = "<br><span style='font-size:9pt;padding-top:6px;'><a href='join.x'>Join to hear about offers first</a></span>";
                    txt += "<span id='extra"+item.Num+"' style='display:none;'><p>No Offers Left<br>(try tomorrow)"+signInForNotices+"</p>"+motext+"</span>";
                } else
                    txt += "<span id='extra"+item.Num+"' style='display:none;'><p>"+item.Left+" Offers Left</p><p style='font-size:10pt;'>[ <a href='"+item.URL+"'>GET DETAILS</a> ]</p>"+motext+"</span>";
                txt += "</div>\n";
                
                document.getElementById('scroller').innerHTML += txt;
                makeoffer = 0;
                if(item.MOStatus=="Active") {
               		makeoffer = 1;
                	log("make offer for :"+item.BusinessName);
                }
           
	        	showAddress(item.Address,item.Left,item.Num,item.Lat,item.Lon,makeoffer);
        }
        
        function showAddress(address,left,count,lat,lon,makeoffer) {
	        var latlng = new GLatLng(lat,lon);
			if(count==1) map.setCenter(latlng, 10);  // set zoom? 
			var icon = new GIcon();
			if(left<1) {
				icon.image = "http://zaphour.com/images/ZapHour-grey-bolt.png";
				fontColor = "ff9900";
			} else {
				icon.image = "http://zaphour.com/images/ZapHour-gold-bolt.png";
				fontColor = "ff9900";
			}
			if(makeoffer) {
				icon.image = "http://zaphour.com/images/ZapHour-GreenBolt.png";
				fontColor = "ff9900";
				
			}
			
			//icon.shadow = "path to your icon shadow";
			icon.iconSize = new GSize(30, 30);
			//icon.shadowSize = new GSize(22, 20);
			//icon.iconAnchor = new GPoint(RandomInt(1,40), RandomInt(1,40));   //6,20
			icon.iconAnchor = getNextPoint();
			icon.infoWindowAnchor = new GPoint(35, 1);
			icon.infoShadowAnchor = new GPoint(13, 13);

			//markers[count] = new GMarker(latlng,icon);
			markers[count] = new GMarker(latlng,{icon:icon, zIndexProcess:orderOfCreation});
			map.addOverlay(markers[count]);

			GEvent.addListener(markers[count], "click", function() {
				 createMarkerListener(count,latlng,40,true);
			});
	    }


function getTheDay(num) {
	myDays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
	return myDays[num];
}

function getCurrentTime() {
	var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	var txt;
	if(hours>12) hours = hours - 12;
	if (minutes < 10){ minutes = "0" + minutes }
		txt = hours + ":" + minutes + " ";
	if(currentTime.getHours() > 11){
		txt += "PM";
	} else {
		txt += "AM";
	}
	return txt;
}

function getCurrentDayAndTime() {
	var currentTime = new Date();
	var txt = getTheDay(currentTime.getDay());
	txt += " " + getCurrentTime();	
	return txt;
	
}

