var map, manager, refreshTime;
//var centerLatitude = 33.321349, centerLongitude = -111.873779, startZoom = 12;
var masterHandler;
var masterMarker;
function createMarkerClickHandler(marker, text, link, image, phone, info, directions, hours, master) {
	
	hand = '<div style="clear:both; width:350px;">'+image+'<h3>' + text + '</h3>';
	if(phone != ""){ hand += '<div style="margin-bottom:0px; font-weight:bold;">phone: '+phone+'</div>';}
	if(hours != ""){ hand += '<div style="margin-bottom:4px; font-weight:bold;">hours: '+hours+'</div>';}
	hand += '<div id="favDiv">'+info+'</div>' +
		'<br><a href="' + link + '" target="_blank">Get Directions from Google</a></div>';
	
	if(master){
		masterHandler = hand
		masterMarker = marker;
	}
	return function() {
			
			hand = '<div style="clear:both; width:350px;">'+image+'<h3>' + text + '</h3>';
			if(phone != ""){ hand += '<div style="margin-bottom:0px; font-weight:bold;">phone: '+phone+'</div>';}
			if(hours != ""){ hand += '<div style="margin-bottom:4px; font-weight:bold;">hours: '+hours+'</div>';}
			hand += '<div id="favDiv">'+info+'</div>' +
				'<br><a href="' + link + '" target="_blank">Get Directions from Google</a></div>';
				
			marker.openInfoWindowHtml( hand);
		
		return false;
	};
}




function createMarker(pointData) {
	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
	icon.image = '/images/mapArrow'+pointData.abbr+'.png';
	icon.iconSize = new GSize(68, 21);
	icon.iconAnchor = new GPoint(34, 12);
	icon.infoWindowAnchor = new GPoint(34, 12);

	//	"labelText": pointData.abbr,
	opts = {
		"icon": icon,
		"clickable": true,
		"labelOffset": new GSize(-16, -16)
	};
	if(pointData.nickname == storeName){
		 master = true;
	}else{
		master = false;
	}
	var marker = new LabeledMarker(latlng, opts);
	var handler = createMarkerClickHandler(marker, pointData.nickname, pointData.wp, pointData.image, pointData.phone, pointData.info, pointData.directions, pointData.hours, master);
	
	
	GEvent.addListener(marker, "click", handler);

	if(pointData.info != '' && pointData.info != '                  ' && pointData.info != null){
		info = '<span style="font-weight:bold;color:black;">'+pointData.info+'</span><br>';	
	}else{
		info = "";	
	}
	var listItem = document.createElement('li');
	//listItem.setAttribute("onClick",pointData.wp);
	bland = '<div class="label" style="color:white">'+pointData.abbr+'</div>'+ pointData.name;
	if(pointData.phone != "") { bland += 'Phone: '+pointData.phone;}
	if(pointData.info == "Coming Soon!") { bland += '<i>'+pointData.info+'</i>';}
	listItem.innerHTML = bland;
	listItem.onclick = handler;

	document.getElementById('sidebar-list').appendChild(listItem);
	
	var listItem2 = document.createElement('li');
	listItem2.innerHTML = '<a href="' + pointData.wp + '" target="_blank">Get Directions from Google</a>';
	document.getElementById('sidebar-list').appendChild(listItem2);

	return marker;
}

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case. 
	return 0;
}

function handleResize() {
	//var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
	//document.getElementById('map').style.height = height + 'px';
	//document.getElementById('sidebar').style.height = height + 'px';
}

function init() {
	eval(storeImages);
	handleResize();
	map = new GMap(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), 3);
	map.addControl(new GMapTypeControl());

	manager = new GMarkerManager(map);
	
	document.getElementById('sidebar-list').innerHTML = "";
	
	// This is a sorting trick, don't worry too much about it.
	//markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
	
	var topItem = document.createElement('li');
	
	if(myTown != ''){
		topItem.innerHTML = '<div>Stores Near:<br><h3>'+myTown+' '+myZip+'</h3></div>';
	}
	document.getElementById('sidebar-list').appendChild(topItem);
	
	//
	
	batch = [];
	for(id in markers) {
		batch.push(createMarker(markers[id]));
	}
	manager.addMarkers(batch, 2);
	manager.refresh();
	//
	refreshTime = setTimeout ( "zoomp()", 3000 );
	teeter();
}


	function zoomp(){
		
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);	
		clearTimeout ( refreshTime );
		if(masterHandler != null){
			setTimeout ("masterMarker.openInfoWindowHtml(masterHandler)", 1000 );
		}
	}

	function jack(cname,cval,what,where){
		
		//alert(task);
		document.getElementById('favDiv').innerHTML = "please wait...";
		
		eval(cname+" = '"+cval+"'");
	
		url  = "/_setCookie.php?what="+what+"&cookieName="+cname+"&cookieValue="+cval+"&where="+where;
		
		//alert(url);
		loadXMLDoc(url);
	  
		
	}
	
	
	
	function loadXMLDoc(url) 
	{
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send(null);
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processReqChange;
				req.open("GET", url, true);
				req.send();
			}
		}
	}
	
	function processReqChange() 
	{
		// only if req shows "complete"
		if (req.readyState == 4) {
			// only if "OK"
			if (req.status == 200) {
				// ...processing statements go here...
				response  = req.responseXML.documentElement;
				result = response.getElementsByTagName('result')[0].firstChild.data;
				
				document.getElementById('favDiv').innerHTML = "This is set as your favorite store";
				
				//alert("result is "+result);
				
			} else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}else{
			//alert("2There was a problem retrieving the XML data:\nreadyState is: " + req.readyState);
		}
	}
	
	

//window.onresize = handleResize;
//window.onload = init;
//window.onunload = GUnload;
