var popups = new Array();

function onPageLoad(){
	formatMagicNumberAreas();

	var divMap = document.getElementById("divMap");
	mqTileMap = new MQTileMap(divMap);
					
	//Add zoom controls to Map
	mqZoomControl =  new MQLargeZoomControl();
	mqTileMap.addControl(mqZoomControl, new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(0, 0)))
	
	//Add view controls to map
	mqViewControl = new MQViewControl();
	mqTileMap.addControl(mqViewControl, new MQMapCornerPlacement(MQMapCorner.TOP_RIGHT, new MQSize(-6, 0)))

	mqTileMap.setLogoPlacement(MQMapLogo.MAPQUEST, new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(44, 0)));

	mqTileMap.setRolloversEnabled(false);

	processResize();				
	
		
	mqTileMap.replacePois(mqLowPoiCollection);
	mqTileMap.bestFit();
	
	showPopups(divMap);
	
	MQEventManager.addListener(mqTileMap, "zoomend", adjustPopups);
	MQEventManager.addListener(mqTileMap, "move", adjustPopups);
	
}

function changePrice(isLow){
	if(isLow != bLowPrice){
		bLowPrice = isLow;
		
		if(bLowPrice){
			mqTileMap.replacePois(mqLowPoiCollection);		
		}
		else {
			mqTileMap.replacePois(mqHighPoiCollection);		
		}
		mqTileMap.bestFit();
		
		var divMap = document.getElementById("divMap");
		showPopups(divMap);

	}
}

function closePopupDiv(id){
	var div = document.getElementById(id);
	DOMFunctions.changeElementVisibility(div, "hidden");
}

function showPopupDiv(e){
	var pDiv = document.getElementById(this.getKey());
	
	var pnt =  mqTileMap.llToPix(this.getLatLng());
	pDiv.style.left = (pnt.getX() - 22) + "px";
	pDiv.style.top = (pnt.getY() - 70 - 2) + "px";
	
	DOMFunctions.changeElementVisibility(pDiv, "visible");
	mqTileMap.getInfoWindow().hide();
}


function getSmallPopupDiv(id, textDiv){
	var div = document.createElement("div");
	div.className = "smallPopup";
	div.id = id;
	
	var topDiv = document.createElement("div");
	topDiv.className = "smallPopup_hdr";
	div.appendChild(topDiv);
	
	var midDiv = document.createElement("div");
	midDiv.className = "smallPopup_cnt";
	midDiv.appendChild(textDiv);
	div.appendChild(midDiv);

	var btmDiv = document.createElement("div");
	btmDiv.className = "smallPopup_ftr";
	div.appendChild(btmDiv);
	
	var clsDiv = document.createElement("div");
	clsDiv.style.width = "14px";
	clsDiv.style.height = "14px";
	clsDiv.style.position = "absolute";
	clsDiv.style.left = "156px";
	clsDiv.style.top = "5px";
	var a = document.createElement("a");
	a.href = "javascript:closePopupDiv(" + id + ");";
	var img = document.createElement("img");
	img.border = 0;
	img.src = "images/smallpopup/close_button.gif";
	a.appendChild(img);
	clsDiv.appendChild(a);
	div.appendChild(clsDiv);
	
	
	return div;
	
}

function adjustPopups(){
	var divMap = document.getElementById("divMap");
	var mqPoiCollection = mqTileMap.getPois();
	for(var i=0; i < mqPoiCollection.getSize(); i++){
		station = mqPoiCollection.getAt(i);
		var pDiv = document.getElementById(station.getKey());
		if(pDiv){
			pnt =  mqTileMap.llToPix(station.getLatLng());
			pDiv.style.left = (pnt.getX() - 22) + "px";
			pDiv.style.top = (pnt.getY() - 70 - 2) + "px";

		}
	}
}

function showPopups(divMap){
	var tDiv, span, pDiv, a;
	var station;
	var pnt;
	
	for(var i=0; i < popups.length; i++){
		divMap.removeChild(popups[i]);
	}
	popups = new Array();
	
	var mqPoiCollection = mqTileMap.getPois();

	for(var i=0; i < mqPoiCollection.getSize(); i++){
		station = mqPoiCollection.getAt(i);
		
		MQEventManager.addListener(station, "infowindowopen", showPopupDiv);

		
		var tDiv = document.createElement("div");
		tDiv.className = "popupsm";
		tDiv.style.marginLeft = "10px";
		span = document.createElement("span");
		if(bLowPrice){
			span.className = "lowest";
			span.appendChild(document.createTextNode("#" + (i+1) + " Lowest Price " + StringFunctions.formatCurrency(station.getLowPrice())));
		}
		else {
			span.className = "highest";
			span.appendChild(document.createTextNode("#" + (i+1) + " Highest Price " + StringFunctions.formatCurrency(station.getLowPrice())));
		}
		
		tDiv.appendChild(span);
		tDiv.appendChild(document.createElement("br"));
		
		a = document.createElement("a");
		a.href = "javascript:postSearch('3,4,5', '', \"" + station.getCity() + "\",\"" + station.getStateProvince() + "\",'', '');"
		a.className = "popupLink";
		a.appendChild(document.createTextNode(station.getCity() + ", " + station.getStateProvince()));
		tDiv.appendChild(a);
		
		
		pDiv = getSmallPopupDiv(station.getKey(), tDiv);
		
		pnt =  mqTileMap.llToPix(station.getLatLng());
		
		
		pDiv.style.left = (pnt.getX() - 22) + "px";
		pDiv.style.top = (pnt.getY() - 70 - 2) + "px";
		
		divMap.appendChild(pDiv);
		popups.push(pDiv);
	}
}


function onPageResize(){
	processResize();
}

function setMapHeightWidth(){
	h = DOMFunctions.getBrowserWindowHeight();
	w = DOMFunctions.getBrowserWindowWidth();
	
		
	if(document.getElementById("rightrailad").style.display=="")
		w -= AD_AREA_WIDTH
	//document.getElementById("tblPrices").style.width = w - 25;
	
	w -= SEARCH_AREA_WIDTH
	
	h = 500
	
	if(w<510)
		w = 510;
	
	if(h<300)
		h = 300;
		
	return new MQSize(w, h);
	
}

function processResize(){
	var mqSize = setMapHeightWidth();
	var w = mqSize.getWidth();
	if(w != mqTileMap.getSize().getWidth()){
		var divMap = document.getElementById("divMap");
		divMap.style.width = mqSize.getWidth() + "px";
		divMap.style.height = mqSize.getHeight() + "px";
		mqTileMap.setSize(mqSize);
		
		//Add zoom controls to Map
		try{
			mqTileMap.removeControl(mqZoomControl);
		}catch(exc){}
		mqTileMap.addControl(mqZoomControl, new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(0, 0)))

		//Add view controls to map
		try{
			mqTileMap.removeControl(mqViewControl);
		}catch(exc){}
		mqTileMap.addControl(mqViewControl, new MQMapCornerPlacement(MQMapCorner.TOP_RIGHT, new MQSize(-6, 0)))
	
		adjustPopups();
	}
}

function resetRefine(){
	document.getElementById("hdRadius").value = getDefaultRadius();
	document.getElementById("hdBrand").value = "";
}

function getDefaultRadius(){
	return "0";
//	var sel = document.getElementById("ddGasPriceType");
//	var gaspricetypeid = sel.options[sel.selectedIndex].value;
//	for(var i = 0 ;i < gastype.length; i++){
//		if(gastype[i][0] == gaspricetypeid){
//			return gastype[i][2];
//		}
//	}
//	return "25";
}

function postSearch(gaspriceTypeids, address, city, stateProvince, postalCode, country){
	document.getElementById("hdSearch").value = "true";
	document.getElementById("hdLatitude").value = "";
	document.getElementById("hdLongitude").value = "";
	document.getElementById("hdGasPriceType").value = gaspriceTypeids;
	document.getElementById("hdAddress").value = address;
	document.getElementById("hdCity").value = city;
	document.getElementById("hdStateProvince").value = stateProvince;
	document.getElementById("hdPostalCode").value = postalCode;
	document.getElementById("hdBrand").value = "";
	document.getElementById("frmSearchHd").submit();
}

