/****
** Find a Retailer
****/

function changeResultsPage(num) {
	
	if (num > total_pages)
		return;
	
	num--;
	
	var newmargin = ((5*90)*num);
	newmargin = newmargin*-1;
	
	$('#find-a-retailer-results').animate({
		top: newmargin+'px'
	}, 500);
		
	var showing_of = ((num+1)*5);
	if (showing_of > total_results) 
		showing_of = total_results;
		
	far_current_page = (num+1);
	
	updateMarkers();
	
	$('#find-a-retailer-pages .displaying').html('Displaying '+((num*5)+1)+'-'+showing_of+' of '+total_results);
	
	return false;
}

var far_current_page = 1;
function nextResultsPage() {
	changeResultsPage(far_current_page+1);
	return false;
}

var map;
var icons;
function initMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2($('#map')[0]);
		resetMap();
		map.setUIToDefault();

		icons = new Array();
		icons[1] = new GIcon();
		icons[1].image = BASE_HREF+"_images/wheretobuy-icon_a.png";
		icons[1].shadow = BASE_HREF+"_images/wheretobuy-icon_shadow.png";
		icons[1].iconSize = new GSize(20.0, 29.0);
		icons[1].shadowSize = new GSize(35.0, 29.0);
		icons[1].iconAnchor = new GPoint(10.0, 14.0);
		icons[1].infoWindowAnchor = new GPoint(10.0, 14.0);
		icons[2] = new GIcon();
		icons[2].image = BASE_HREF+"_images/wheretobuy-icon_b.png";
		icons[2].shadow = BASE_HREF+"_images/wheretobuy-icon_shadow.png";
		icons[2].iconSize = new GSize(20.0, 29.0);
		icons[2].shadowSize = new GSize(35.0, 29.0);
		icons[2].iconAnchor = new GPoint(10.0, 14.0);
		icons[2].infoWindowAnchor = new GPoint(10.0, 14.0);
		icons[3] = new GIcon();
		icons[3].image = BASE_HREF+"_images/wheretobuy-icon_c.png";
		icons[3].shadow = BASE_HREF+"_images/wheretobuy-icon_shadow.png";
		icons[3].iconSize = new GSize(20.0, 29.0);
		icons[3].shadowSize = new GSize(35.0, 29.0);
		icons[3].iconAnchor = new GPoint(10.0, 14.0);
		icons[3].infoWindowAnchor = new GPoint(10.0, 14.0);
		icons[4] = new GIcon();
		icons[4].image = BASE_HREF+"_images/wheretobuy-icon_d.png";
		icons[4].shadow = BASE_HREF+"_images/wheretobuy-icon_shadow.png";
		icons[4].iconSize = new GSize(20.0, 29.0);
		icons[4].shadowSize = new GSize(35.0, 29.0);
		icons[4].iconAnchor = new GPoint(10.0, 14.0);
		icons[4].infoWindowAnchor = new GPoint(10.0, 14.0);
		icons[5] = new GIcon();
		icons[5].image = BASE_HREF+"_images/wheretobuy-icon_e.png";
		icons[5].shadow = BASE_HREF+"_images/wheretobuy-icon_shadow.png";
		icons[5].iconSize = new GSize(20.0, 29.0);
		icons[5].shadowSize = new GSize(35.0, 29.0);
		icons[5].iconAnchor = new GPoint(10.0, 14.0);
		icons[5].infoWindowAnchor = new GPoint(10.0, 14.0);
	}
}

var gmarkers = new Array();
var markers = new Array();
function createMarker(marker_point, marker_sid, marker_html, ii) {
	var marker = new GMarker(marker_point, {icon:window.icons[ii]});
	/*marker_html = 'test';
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(marker_html);
	});
	marker.window_html = marker_html;*/
	window.gmarkers[marker_sid] = marker;
	return marker;
}

function resetMap() {
	map.clearOverlays();
	map.setCenter(new GLatLng(parseInt(38.8226), parseInt(-94.8555)), parseInt(3));
}

function updateMarkers() {

	resetMap();
	window.gmarkers = new Array();
	var bounds = new GLatLngBounds();
	var ii = 1;
	
	var marker_latitude, marker_longitude, marker_sid, marker_html;
	
	if (typeof window.markers == 'undefined' || !window.markers) return;
	
	for(var i=((far_current_page*5)-5);i<(far_current_page*5);i++) {
		if ((i+1) > window.markers.length) break;
		
		if (window.markers[i].latitude != '') {
		
			marker_latitude = parseFloat(window.markers[i].latitude);
			marker_longitude = parseFloat(window.markers[i].longitude);
			marker_sid = parseInt(window.markers[i].sid);
			marker_html = window.markers[i].window_html;
			
			
			if (marker_latitude != '') {
			
				var marker_point = new GLatLng(marker_latitude,marker_longitude);
				bounds.extend(marker_point);
				
				var marker = createMarker(marker_point, marker_sid, marker_html, ii);
				window.map.addOverlay(marker);
				
			}

		}
				
		ii++;
		if (ii==6) ii = 1;
	}
		
	//this.map.setCenter(new GLatLng(0,0), 0);
	map.setZoom(this.map.getBoundsZoomLevel(bounds)-1);
	map.setCenter(bounds.getCenter());
	//this.map.checkResize();
}

function getSearch() {
	return $('#far-search').attr('value');
}

function searchStores() {
	if (getSearch() == '' || getSearch() == 'Enter City, State or Zip') return;
	
	$('#find-a-retailer-results-container').show().addClass('loading');
	$('#find-a-retailer-results').html('').css({ top: '0px' });
	$('#find-a-retailer-pages').hide();
	
	$.post(BASE_HREF+"ajax.php", { method: "get_stores", search: getSearch() },
	  function(data){
		try {
			
			$('#find-a-retailer-results-container').removeClass('loading');
			
			window.markers = data.markers;
			$('#find-a-retailer-results').html(data.results);
			window.total_results = parseInt(data.total_results);
			window.total_pages = parseInt(Math.ceil(total_results/5));
			$('#find-a-retailer-pages div.pagination').html(data.pagination);
			$('#find-a-retailer-pages').show();
			updateMarkers();
			changeResultsPage(1);
			
		} catch(e) {
		}
		
	  }, "json");

}

$(document).ready(function() {
	initMap();
});

////
// Set the Unload for the map...
// This is currently the best way to do it
window.onbeforeunload = function() {
	GUnload();
}
