//alert("targetMap.js loaded")
function targetMap(md, lh, ld, nb, bb, pd){
	var mapdiv = md
	this.listheading = lh
	this.listdiv = ld
	this.nextbutton = nb
	this.backbutton = bb
	this.printdiv = pd
	//this.printdiv.style.visibility ="hidden"
	this.nextbutton.style.visibility ="hidden"
	this.backbutton.style.visibility ="hidden"
	
	//class variables
	var map;
	var geocoder = new GClientGeocoder();
	var infobox
	var nearAddress
	
	//custom marker icon
	var boonIcon = new GIcon();
	boonIcon.image = "images/target.png";
	//boonIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	boonIcon.iconSize = new GSize(30, 51);
	boonIcon.shadowSize = new GSize(22, 20);
	boonIcon.iconAnchor = new GPoint(25, 50);
	boonIcon.infoWindowAnchor = new GPoint(25, 30);
	markerOptions = { icon:boonIcon };	
	var defaultZoom = 4
	var defaultRadius = 50;
	
	// locations and current locations shown
	var markersXml;
	var currentFrom;
	var currentTo;


	//CREATE THE MAP AND LEAVE IT AT THAT UNTIL WE SEARCH
	initMap()

	//initialize map and center on myAdress
	function initMap() {
		//alert("initMap")
		if (GBrowserIsCompatible()) {
			//alert("GBrowserIsCompatible")
			map = new GMap2(mapdiv);
			map.setCenter(new GLatLng(40, -100), defaultZoom);
			map.addControl(new GSmallMapControl());
		}
		//alert("end initMap")
	}
	
	this.searchLocationsNear = function(nearAddress) {
		//alert("searchLocationsNear called")
		this.nearAddress = nearAddress
		var center;
		var thisobj = this
		geocoder.getLatLng(nearAddress, function(latlng) {
			if (!latlng) {
				alert(nearAddress + ' not found');
				return
			} else {
				center = latlng;
				var radius = defaultRadius
				var s1=center.lat();
				var s2=center.lng();
				var searchUrl = 'map.aspx?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
				GDownloadUrl(searchUrl, function(data) {
				var xml = GXml.parse(data);
				thisobj.markersXml = xml.documentElement.getElementsByTagName('marker');
				thisobj.showMarkers(0,8)
				thisobj.generateprint()
				});
			}
		});
	}
	
	this.generateprint = function(){
		//var sidebar = this.listdiv
		this.printdiv.innerHTML = "";
		var html2;
	if (this.markersXml.length == 0) {
			this.printdiv.innerHTML = 'No results found.';
	 
return;
		}
		else
		{
			html2 = "<span class=blue12>Total addresses found near the location of zipcode " + this.nearAddress + " is " + this.markersXml.length + ".</span><br/>";
			}
			
		for (var j = 0; j < this.markersXml.length; j++) {
			var Store_Id = this.markersXml[j].getAttribute('Store_Id');
			var address = this.markersXml[j].getAttribute('address');
			var city = this.markersXml[j].getAttribute('city')
			var storename= this.markersXml[j].getAttribute('storeName')
			var state = this.markersXml[j].getAttribute('state')
			var Zip_Code = this.markersXml[j].getAttribute('ZipCode')
			var phone = this.markersXml[j].getAttribute('phone')

		html2 = html2 + "<span class='address'> Store No. "+Store_Id+",<br/>";
		if(storename = null)
			{
				alert(storename);
				alert("se");
				html2=html2 + "," + storename + "<br/>";
			}
		html2=html2 + address + "<br/>" +city+", "+state+", "+Zip_Code+"<br/>"+phone+"</span>";
		}

this.printdiv.innerHTML = html2;
			
	}
	
	
	this.showMarkers = function(fromNum,toNum){
		this.currentFrom = Math.max(Math.min(fromNum,this.markersXml.length-1),0);
		this.currentTo = Math.max(Math.min(toNum,this.markersXml.length-1),0);
		//var sidebar = this.listdiv
		this.listdiv.innerHTML = "";
		if (this.markersXml.length == 0) {
			this.listdiv.innerHTML = 'No results found.';
			map.setCenter(new GLatLng(40, -100), this.defaultZoom);
			return;
		}
		else
		{
			this.listdiv.innerHTML = 'Total addresses found near the location ' + this.markersXml.length + '.<br>Click the address to show the directions in the map.';
			}
		map.clearOverlays();
		var bounds = new GLatLngBounds();
		
		for (var i = this.currentFrom; i <= this.currentTo; i++) {
			var Store_Id = this.markersXml[i].getAttribute('Store_Id');
			var address = this.markersXml[i].getAttribute('address');
			var city = this.markersXml[i].getAttribute('city')
			var state = this.markersXml[i].getAttribute('state')
			var Zip_Code = this.markersXml[i].getAttribute('ZipCode')
			var phone = this.markersXml[i].getAttribute('phone')
			var logo = this.markersXml[i].getAttribute('logo')			
			var point = new GLatLng(parseFloat(this.markersXml[i].getAttribute('lat')),parseFloat(this.markersXml[i].getAttribute('lng')));
			var marker = this.createMarker(point, Store_Id, address, city, state, Zip_Code, phone, logo);
			map.addOverlay(marker);
			var sidebarEntry = this.createSidebarEntry(marker, Store_Id, address, city, state, phone);
			this.listdiv.appendChild(sidebarEntry);
			bounds.extend(point);
		}
		if (toNum < this.markersXml.length-1){
			this.nextbutton.style.visibility ="visible"
		}else{
			this.nextbutton.style.visibility ="hidden"
		}
		if (fromNum > 0){
			this.backbutton.style.visibility = "visible"
		}else{
			this.backbutton.style.visibility ="hidden"
		}
		
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		
		
	}
	this.nextLocations = function(){
		this.showMarkers(this.currentFrom+9,this.currentTo+9)
	}
	this.prevLocations = function(){
		this.showMarkers(this.currentFrom-9,this.currentFrom-1)
	}
	
	this.fakeSearch = function(){
		this.nearAddress = "63119"
		var marker = this.createMarker(new GLatLng(40, -100),"Arni's shop", "611 N. Rock Hill Road", "St. Louis", "MO", "63119", "1-800-BUY-STUFF")
		map.addOverlay(marker)
	}
	
	this.createMarker = function(point, Store_Id, address, city, state, zip_Code, phone, logo) {
		var marker = new GMarker(point,markerOptions);
		var adressCombo = address +", " +city +", " +state +", " +zip_Code
		var directionURL = "http://maps.google.com/maps?"+"daddr="+escape(adressCombo)+"&"+"saddr="+escape(this.nearAddress)
		
		var html='<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:arial; float:left; font-size:12px;" > <tr>  <td width="65"><img src="images/' + logo + '"/></td>   <td><b>' + Store_Id + '</b></div><div>' + address +'</div><div>' + phone + '<br /><b><a href="'+directionURL+'" style="color:#ffff00"  target="boonDirections">Get Directions</a></b></td></tr></table>';
		var infoBox = new InfoBox(marker, 260, 135, "mapbox", 55, 50, html) 
		marker.infobox = infoBox
		GEvent.addListener(marker, "click", function() {
			//alert("marker clicked")
			if(map.infobox){
				map.removeOverlay(map.infobox)
			}
			map.panTo(point);
			map.infobox= this.infobox
			map.addOverlay(map.infobox);
		});
		return marker;
	}

	this.createSidebarEntry = function(marker, Store_Id, address, city, state, phone) {
		var div = document.createElement('div');
		var html = "<span class='address'> <b>"+Store_Id+"</b><br/>"+address+"<br/>"+city+", "+state+"<br/>"+phone+"</span>";
		div.innerHTML = html;
		div.style.cursor = 'pointer';
		div.style.marginBottom = '5px'; 
		GEvent.addDomListener(div, 'click', function() {
			GEvent.trigger(marker, 'click');
		});

			GEvent.addDomListener(div, 'mouseover', function() {
			div.style.backgroundColor = '#eee';
		});
			GEvent.addDomListener(div, 'mouseout', function() {
		div.style.backgroundColor = '#fff';
		});
		return div;
	}
}

function Clickheretoprint()
{ 
 var disp_setting="toolbar=yes,location=no,directories=no,menubar=no,"; 
      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
  var content_vlue = document.getElementById("printdiv").innerHTML; 
  var ss=document.getElementById("location_zipcode").value;
  var docprint=window.open("","",disp_setting); 
   docprint.document.open(); 
   docprint.document.write('<html><head><title>Lil Rinser -  Shampoo Shield - Store Addresses near ' + ss + '</title>'); 
   docprint.document.write('<link rel="stylesheet" href="images/print.css" media="screen,projection" type="text/css" /></head><body onLoad="self.print()"><div class="text">'); 
   docprint.document.write(content_vlue);          
   docprint.document.write('</div></body></html>'); 
   docprint.document.close(); 
   docprint.focus(); 
}