var curLat = null;
var curLon = null;
var curAddress = null;
var curSqft = null;
var curPoly = null;
var curStation = null;
var curZip = null;
var lastRec = null;
var curMarker = null;
var slideInProgress = false;
var polygon = null;


var cityCode = function(data) {
	//jQuery('#address').html(data.value);
	//jQuery('#lat').html(data.lat);
	//jQuery('#lon').html(data.lon);
	//jQuery('#sqft').html(data.sqft);
	//jQuery('#sqft').html(data.poly);
	curLat = data.lat;
	curLon = data.lon;	
	curAddress = data.value;
	curSqft = data.sqft;
	curPoly = data.poly;
	curStation = data.sta;
	curZip = data.zip;
};

var fadeInSuggestion = function(suggestionBox, suggestionIframe){
	jQuery(suggestionBox).fadeTo(300,0.95);
};
var fadeOutSuggestion = function(suggestionBox, suggestionIframe){
	jQuery(suggestionBox).fadeTo(300,0);
};



var defaultText = 'Enter your address';


		function hideRecommender(){
 			if(jQuery('#recommendHelper').css("display") != 'none'){
				if(!slideInProgress){
	 				slideInProgress = true;
	 				jQuery('#recommendHelper').slideUp("slow",function(){slideInProgress = false;});
				}
			}				
		}

var cpeLoaded = false;


function setMarker(lat, lon, address, sqft, pct){
	var point = new GLatLng(lat,lon);
	//GLog.write(map.getCurrentMapType().getMaximumResolution(point));
	map.setCenter(point, (map.getCurrentMapType().getMaximumResolution(point)-1));
	if(!curMarker){
		curMarker = new GMarker(point, iconSun);
		map.addOverlay(curMarker);
	}else{
		curMarker.setPoint(point);
	}

	// estimate the mean roof kW
	// formulas actually reside in function getMarkerHtml
	//		var kwL = Math.round((rfsq/200)/4);
	//		var kwH = Math.round((rfsq/100)/4);
	var maxKw = Math.round((sqft/100)/4);
	


	// clean power estimator max
	//var maxContentDiv = document.createElement('div');
	//	maxContentDiv.innerHTML = '<iframe src="cpe/cpe.php?address='+address+'&maxKw='+maxKw+'&zip='+zip+'" width="100%" height="800px" id="cpe-frame"></iframe>';
	//maxContentDiv.innerHTML = 'Loading';
	//var maxTitle = "Clean Power Estimate";
	//map.openInfoWindowHtml(point, markerHtml, {maxWidth:450, maxContent: maxContentDiv, maxTitle: maxTitle});

	//markerText = getMarkerHtml(curSqft);
	markerText = '<div id="table-address">'+address+'</div>'+getMarkerHtml(sqft, pct); //+'<div id="table-address"><span id=\'assumptions\'>assumptions</span><span id=\'close\'>close window and zoom out</span></div>';

	// add the polygon to the map
	//GLog.write('polyTxt= '+polyTxt);
	//if(polyTxt.length >0){
	//	var polyVerts = new Array();
	//	var polyVertsString = new Array();
	//	polyVertsString =  polyTxt.split(';');
	//	for(k = 0; k< polyVertsString.length; k++){
	//		coordString = polyVertsString[k];
	//		// get index of comma
	//		commaLoc = coordString.indexOf(',');
	//		vertX = coordString.substring(0, commaLoc);
	//		vertY = coordString.substring(commaLoc+1);
	//		polyVerts.push(new GLatLng(vertY, vertX));
	//	}
	//	if(polygon) map.removeOverlay(polygon);
	//	polygon = new GPolygon(polyVerts, "#ff0000", 1, .5, "#ff0000", 0.2);
	//	  map.addOverlay(polygon);
	//}


	//curMarker.openInfoWindowHtml(markerText, {maxWidth:450,maxContent: maxContentDiv, maxTitle: maxTitle});
	curMarker.openInfoWindowHtml(markerText, {maxWidth:450});
	//var iw = map.getInfoWindow();
	//GEvent.addListener(iw, "maximizeclick", function() {
	//	maxContentDiv.innerHTML = '<iframe src="cpe/cpe.php?address='+address+'&maxKw='+maxKw+'&zip='+zip+'" width="100%" height="800px" id="cpe-frame"></iframe>';
	//});



	GEvent.addListener(curMarker, 'click', function(){
		//curMarker.openInfoWindowHtml(markerText, {maxWidth:450, maxContent: maxContentDiv, maxTitle: maxTitle});
		curMarker.openInfoWindowHtml(markerText, {maxWidth:450});
	});
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function openVideo(){
	tb_show('Welcome to SF.SolarMap.org', 'welcome.html?TB_iframe=true&amp;height=455&amp;width=615', null);
}

