var map;
var centerLatitude = 35.01308817645;
var centerLongitude = -95.00266826152802;
var startZoom = 4;

    var iconBlue = new GIcon(); 
    iconBlue.image = '/resources/images/default/mm_20_blue.png';//moving map.html into other hierarchical structre maybe result in failed getting images
    iconBlue.shadow = '/resources/images/default/mm_20_shadow.png';
    //iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    //iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = '/resources/images/default/mm_20_red.png';
    iconRed.shadow = '/resources/images/default/mm_20_shadow.png';
    //iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
    //iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconRed.iconSize = new GSize(12, 20);
    iconRed.shadowSize = new GSize(22, 20);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

	var iconGreen = new GIcon(); 
	iconGreen.image = '/resources/images/default/mm_20_green.png';
	iconGreen.shadow = '/resources/images/default/mm_20_shadow.png';
	//iconGreen.image = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
	//iconGreen.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';	
	iconGreen.iconSize = new GSize(12, 20);
	iconGreen.shadowSize = new GSize(22, 20);
	iconGreen.iconAnchor = new GPoint(6, 20);
	iconGreen.infoWindowAnchor = new GPoint(5, 1);
	
//	var iconYellow = new GIcon(); 
//	iconYellow.image = 'http://labs.google.com/ridefinder/images/mm_20_yellow.png';
//	iconYellow.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
//	iconYellow.iconSize = new GSize(12, 20);
//	iconYellow.shadowSize = new GSize(22, 20);
//	iconYellow.iconAnchor = new GPoint(6, 20);
//	iconYellow.infoWindowAnchor = new GPoint(5, 1);
//	
//	var iconPurple = new GIcon(); 
//	iconPurple.image = 'http://labs.google.com/ridefinder/images/mm_20_purple.png';
//	iconPurple.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
//	iconPurple.iconSize = new GSize(12, 20);
//	iconPurple.shadowSize = new GSize(22, 20);
//	iconPurple.iconAnchor = new GPoint(6, 20);
//	iconPurple.infoWindowAnchor = new GPoint(5, 1);
//	
//	var iconOrange = new GIcon(); 
//	iconOrange.image = 'http://labs.google.com/ridefinder/images/mm_20_orange.png';
//	iconOrange.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
//	iconOrange.iconSize = new GSize(12, 20);
//	iconOrange.shadowSize = new GSize(22, 20);
//	iconOrange.iconAnchor = new GPoint(6, 20);
//	iconOrange.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["Warehouse"] = iconBlue;
    customIcons["Sales Office"] = iconGreen;
    customIcons["Headquarters"] = iconRed;


function addMarker(pointData, latitude, longitude, description, type, contact) {
	var marker = new GMarker(new GLatLng(latitude, longitude), customIcons[type]);
	/*toggle multi-tabs setting and single tab*/
	//var infoTabs = [
		//new GInfoWindowTab("Info", description),
		//new GInfoWindowTab("Contact", contact)
	//];
	var visible = false;
	
	GEvent.addListener(marker, 'click',
		function() {
			marker.openInfoWindowHtml(description)
			/*toggle multi-tabs setting and single tab*/
// 			marker.openInfoWindowTabsHtml(infoTabs, {
//				maxWidth: 310
//			});
		}
	);
	
	/*GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml("Marker #<b>" + number + "</b>");
  	  });
    */
	
	pointData.show = function() {
		if(!visible) {
			map.addOverlay(marker);
			visible = true;
		}
	}
	
	pointData.hide = function() {
		if(visible) {
			map.removeOverlay(marker);
			visible = false;
		}
	}
	
	pointData.show();
	
	
}

function initialiseSortTab(type) {
	var listItem = document.createElement('li');
	if (type == "View All") {
		listItem.className = "all_markers";
	}
	var listItemLink = listItem.appendChild(document.createElement('a'));
    var listLinkClasses = [];
    listLinkClasses["View All"] = "all";
    listLinkClasses["Warehouse"] = "warehouse";
    listLinkClasses["Sales Office"] = "sales-office";
    listLinkClasses["Headquarters"] = "headquarters";
/*set filter classification here*/
	
	listItemLink.href = "#";
	listItemLink.innerHTML = type;
	listItemLink.className = listLinkClasses[type];
/*set background image on filter*/
//  listItemLink.backgroundImage ="";
	
	$("#filters li a.all").addClass('selected');
	
	listItemLink.onclick = function() {
		
		for(id in markers) {
			if (markers[id].type == type || 'View All' == type) {markers[id].show();} else {markers[id].hide();}
		}
		
		$("#filters li a").removeClass('selected');
		$(this).addClass('selected');
		return false;
	}
	document.getElementById('filters').appendChild(listItem);
}

function init()
{
	var type;
	var allTypes = { 'View All':[] };
	
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

		for(id in markers) {
			addMarker(markers[id], markers[id].latitude, markers[id].longitude, markers[id].name, markers[id].type, markers[id].contact);	
			allTypes[markers[id].type] = true;	
		}
		
		for(type in allTypes)
		{initialiseSortTab(type);}
	}
}

window.onload = init;
window.unload = GUnload;
