
var existingMarkers = new Array();
var currentGGLMarkers = new Array();
cancelHide = false;
function hideElement(elementId){
	if(document.getElementById(elementId)){
		document.getElementById(elementId).style.display = 'none';
	}
}

function showElement(elementId){
	cancelHide = true;
	if(document.getElementById(elementId)){
		document.getElementById(elementId).style.display = '';
	}
}

function hideMenuElement(elementId){
	cancelHide = false;
	if(!cancelHide){
		setTimeout("hideElement('"+elementId+"')", 1);
	}
}

function showMenuElement(elementId){
	if(cancelHide){
		cancelHide = true;
		setTimeout("showElement('"+elementId+"')", 1);
	}
}

function fadeIn(id, millisec, startOpacity, endOpacity){
	if(!startOpacity) startOpacity = 0;
	if(!endOpacity) endOpacity = 1;
	dojo.fadeIn({
		node: id,
		start:startOpacity,
		end:endOpacity,
		duration: millisec,
		beforeBegin: function() {
			var node = dojo.byId(id);
			dojo.style(node, "opacity", startOpacity);
			dojo.style(node, "display", "");
		}
	}).play();
	return true;
}

function fadeOut(id, millisec, endOpacity, startOpacity){
	if(!endOpacity) endOpacity = 0;
	if(!startOpacity) startOpacity = 1;
	dojo.fadeOut({
		start:startOpacity,
		end:endOpacity,
		node: id,
		duration: millisec
	}).play();
	if(endOpacity==0) setTimeout("hideElement('"+id+"')", millisec);
	return true;
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height){
  if(popUpWin) popUpWin.focus();
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}



var popUpWin=0;
function popUpWindow1(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}



var currentZipCode = '';
function submitZipCodeStoreSearch(zipCode){
	if(zipCode != ''){
		currentZipCode = zipCode;
		var geocoder = new GClientGeocoder();
		point = geocoder.getLatLng(zipCode,zipCodeSearchRedirect);
	}
	else{
		point = geocoder.getLatLng('ZIP CODE',zipCodeSearchRedirect);
	}
	return false;
}

function zipCodeSearchRedirect(point){
	radius = city = state = mode = '';
	if(document.getElementById('radius')) radius = '&radius='+document.getElementById('radius').value;
	if(document.getElementById('city')) city = '&city='+document.getElementById('city').value;
	if(document.getElementById('state')) state = '&state='+document.getElementById('state').value;
	if(document.getElementById('mode')) mode = '&mode='+document.getElementById('mode').value;
	
	if(!point) alert('Invalid Zip Code');
	else window.location = 'https://secure.kinneydrugs.com/locations/?latitude='+point.y+'&longitude='+point.x+'&zipCode='+currentZipCode+''+radius+''+city+''+state+''+mode;
	return false;
}


function createMarker(theID, latitude, longitude, name, contactInfo, fullDetailsURL){
	
	var iconArray = new Array();
	
	var icon = new GIcon(baseIcon);
	icon.image = 'http://www.kinneydrugs.com/i/google-map-icon.gif';
    icon.infoWindowAnchor = new GPoint(29,45);
	var point = new GLatLng(latitude,longitude);
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
	existingMarkers[existingMarkers.length] = theID; // add to existing markers array
	currentGGLMarkers[currentGGLMarkers.length] = new GLatLng(latitude,longitude); // store map point object so we can recenter the map
	GEvent.addListener(marker, "click", function(){
					 openListingWindow(theID, latitude, longitude, name, '', contactInfo, fullDetailsURL);});//end of event handler

}


function openListingWindow(theID, latitude, longitude, item_name, image, contactHTML, fullDetailsURL){

	var point = new GLatLng(latitude,longitude);
	
	//trim lat/lon
	latitude = new String(latitude).slice(0,7);
	longitude = new String(longitude).slice(0,8); //catch the "-"

	if(image != ''){
		var htmlStr = '<div class="bubbleText"><img src="'+image+'" hspace="2" style="margin-bottom:2px;" align="left"><strong>'+item_name+'</strong><br />('+latitude+','+longitude+')<br />'+contactHTML+'<br clear="all" />';
	}
	else{
		var htmlStr = '<div class="bubbleText"><strong>'+item_name+'</strong><br />('+latitude+','+longitude+')<br />'+contactHTML;
	}
	  
	  
		//add the directions form
	//htmlStr = htmlStr+'<br /><form action="http://maps.google.com/maps" method="get" target="_blank">For a map &amp; driving directions to this site,<br /> enter your starting address here:<br /><input name="saddr" type="text" id="saddr" style="width:160px; height:15px;"> <input type="submit" value="GO" style="height:20px; width:25px; font-size:10px;"><input type="hidden" name="daddr" value="'+latitude+' '+longitude+'"><input type="hidden" name="hl" value="en"></form>';
	
		//add full details link
	if(fullDetailsURL){
		htmlStr = htmlStr+'<p><a href="javascript:void(0);" onclick="window.location=\''+fullDetailsURL+'\';" style="color:#333333;">full details</a>';
	}

	htmlStr = htmlStr+"</div>";

	map.openInfoWindowHtml(point, htmlStr);
	map.setCenter(point);

}

function centerMapOnPoint(latitude,longitude){
	var point = new GLatLng(latitude,longitude);
	map.setCenter(point);
}


function zoomToBestView(){
	if(currentGGLMarkers.length){
		var minLat = 90;
		var maxLat = -90;
		var minLon = 180;
		var maxLon = -180;
		
		for(var k=0; k < currentGGLMarkers.length;k++)
		{
			if(parseFloat(currentGGLMarkers[k].lat()) < parseFloat(minLat)){minLat = currentGGLMarkers[k].lat();}
			if(parseFloat(currentGGLMarkers[k].lat()) > parseFloat(maxLat)){maxLat = currentGGLMarkers[k].lat();}
			if(parseFloat(currentGGLMarkers[k].lng()) < parseFloat(minLon)){minLon = currentGGLMarkers[k].lng();}
			if(parseFloat(currentGGLMarkers[k].lng()) > parseFloat(maxLon)){maxLon = currentGGLMarkers[k].lng();}
		}
		//alert(minLat + ' ' + minLon + ' ' + maxLat + ' ' + maxLon);
		var GLLBounds = new GLatLngBounds(new GLatLng(minLat,minLon),new GLatLng(maxLat,maxLon));
		//alert(GLLBounds);
		zoomLevel = parseInt(map.getBoundsZoomLevel(GLLBounds));
		if(zoomLevel<1) zoomLevel = 1;
		map.setCenter(GLLBounds.getCenter(),zoomLevel);

	}
	
}


function saveTempLocation(point){
	map.setCenter(point) ;
	if(saveLocationIcon != ''){
		map.removeOverlay(saveLocationIcon);
	}
	
	var marker = new GMarker(point, icon);
 	
	GEvent.addListener(marker, "click", saveLocation);
	map.addOverlay(marker);
	saveLocationIcon = marker;
 }

function saveLocation()
{	
	var center = map.getCenter();
	
	//test for fields, catch errors
	try{
		opener.document.getElementById("flott-latitude").value =  center.lat();
		opener.document.getElementById("flott-longitude").value =  center.lng();
	}
	catch(e)
	{
		opener.document.getElementById("flott-lati").value =  center.lat();
		opener.document.getElementById("flott-longi").value =  center.lng();
	}
	//opener.document.forms[0].flott-longitude.value = center.lng();
	close();
}
if(isdefined('GMap2')){
	//-------------------------------------------------------------------------------------------	
	 var map = new GMap2(document.getElementById("map")); 
	 map.addControl(new GLargeMapControl());
	 var point = new GLatLng(43.06387139555526,-76.14761352539062);
	 map.setCenter(point,10);
	//-------------------------------------------------------------------------------------------
	// Create our "click" marker icon
	var icon = new GIcon();
	icon.image = "http://www.kinneydrugs.com/i/map-save-this-location.gif";
	icon.iconSize = new GSize(129, 32);
	icon.iconAnchor = new GPoint(8, 32);
	
	var saveLocationIcon = "";

	GEvent.addListener(map, 'click', function(overlay, point){
		if(typeof overlay == "undefined" || overlay== null){ //only check if the save marker was NOT clicked
			saveTempLocation(point);
		}
 	});
}
var popUpWin = 0;
function popLocationSelectPage(){
	var width = 800;
	var height = 600;
	var left = 10;
	var top = 10;
	if(popUpWin){
		if(!popUpWin.closed) popUpWin.close();
	}
	if(document.getElementById("flott-lati")){
		currentLatitude = document.getElementById("flott-lati").value;
		currentLongitude = document.getElementById("flott-longi").value;
	}
	else{
		currentLatitude = document.getElementById("flott-latitude").value;
		currentLongitude = document.getElementById("flott-longitude").value;
	}
		// should be in the admin directory
	popUpWin = open('http://www.kinneydrugs.com/secure/chooseLocationGoogle.php?latitude='+currentLatitude+'&longitude='+currentLongitude, 'popUpWin', 'toolbar=no,location=no,directories=no,status=1,menubar=no,scrollbars=1,resizable=1,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function isdefined(variable){
    return (typeof(window[variable]) == "undefined")?  false: true;
}

