// Global Variables
var mapID = "map";											// Map element ID
var mapObject;													// Map object
var homePoint;													// Contains the map's home position
var point;														// Contains a specific point
var marker;														// Contains a specific marker
var listener;													// Contains a map listener for dealer selection
var geocoder;													// Contains the dealer geocoder

var selectedDealer = null;									// Specifies the selected delaer hash
var dealerPoints = new Array();							// Contains all dealer store positions
var dealerMarkers = new Array();							// Contains all dealer store markers
var dealerWindows = new Array();							// Contains all dealer information windows
var dealerMsgs = new Array();								// Contains all dealer information window content

var globalPoints = new Array();							// Contains all dealer store positions with numerical IDs




/**********************/
/*                    */
/*  Global Functions  */
/*                    */
/**********************/
// Retrieve an object
function object(elem) {
	// Set default return value
	var r = false;
	
	// Retrieve object
	var obj = document.getElementById(elem);
	
	// If object exists, set return value to object
	if(obj) { r = obj; }
	
	// Send return value
	return r;
}


// Retrieve input value	
function input(elem) { 
	// Set default return value
	var r = false;
	
	// Retrieve object
	var obj = object(elem);
	
	// If object exists, set return value to object value
	if(obj) { r = obj.value; }
	
	// Send return value
	return r;
}


// Show an element
function show(elem) { 
	// Retrieve object
	var obj = object(elem);
	
	// If object exists, hide object
	if(obj) { obj.style.display = 'block'; }
	
	// Return true
	return true;
}


// Hide an element
function hide(elem) { 
	// Retrieve object
	var obj = object(elem);
	
	// If object exists, hide object
	if(obj) { obj.style.display = 'none'; }
	
	// Return true
	return true;
}


// Set focus to a field
function setFocus(elem) {
	// Retrieve object
	var obj = document.getElementById(elem);
	
	// If object exists...
	if(obj) {
		// Try to set focus
		try { obj.focus(); obj.select(); }
		
		// Catch failed attempts
		catch(err) { }
	}

	// Return true
	return true;
}


// Change content of object6
function content(elem, content) {
	// Retrieve object
	var obj = object(elem);
	
	// If object exists, set content to specified content
	if(obj) { obj.innerHTML = content; }
	
	// Return true
	return true;
}


// Create AJAX server
function ajaxServer() {
	// Set default connection value
	var conn = null;
	
	// Try to create an XML HTTP request
	try { conn = new XMLHttpRequest(); }
	
	// Catch failed attempt...
	catch (e) {
		// Try to create an ActiveX XML HTTP request via Msxml2
		try { conn = new ActiveXObject("Msxml2.XMLHTTP"); }
		
		// Catch failed attempt...
		catch (e) { 
			// Try to create an ActiveX XML HTTP request via Microsoft
			try { conn = new ActiveXObject("Microsoft.XMLHTTP"); }
			
			// Catch failed attempt
			catch (e) { alert("An error has occurred while trying to establish asynchronous interaction!"); }
		}
	}
	
	// Return connection value
	return conn;
}




/***************************/
/*                         */
/*  Dealer Tile Functions  */
/*                         */
/***************************/
// Set dealer to hover
function hoverDealer(hash) {
	// Set class to hover
	object('dealer_'+hash).className = 'hover';

	// Return true;
	return true;
}


// Set dealer to idle
function idleDealer(hash) {
	// Set default class
	var newClass = '';
	
	// If this dealer is already selected, set class back to selected
	if(hash == selectedDealer) { newClass = 'selected'; }
	
	// Set class name
	object('dealer_'+hash).className = newClass;
	
	// Return true
	return true;
}


// Select a dealer
function selectDealer(hash, runMap) {
	// Retrieve list object
	var obj = object('dealer_'+hash);
	
	// If object exists...
	if(obj) {
		// If this dealer is already selected...
		if(hash == selectedDealer) {
			// Clear selected dealer
			selectedDealer = null;
			
			// Clear selected class
			obj.className = '';
			
			// If selection should affect the map...
			if(runMap == true) {
				// Close info window
				mapObject.closeInfoWindow();
				
				// Return to original position
				mapObject.returnToSavedPosition();
				
				// Stop waiting for the information window to close
				GEvent.removeListener(listener);
			}
			
			// Return true
			return true;
		}
		
		// If this dealer isn't selected...
		else {
			// If a dealer is already selected...
			if(selectedDealer) {
				// Try to clear selected dealer
				try { 
					object("dealer_"+selectedDealer).className = ''; 
					
					// If selection should affect the map...
					if(runMap == true) {
						// Close info window
						mapObject.closeInfoWindow();
						
						// Return to original position
						mapObject.returnToSavedPosition();
						
						// Stop waiting for the information window to close
						GEvent.removeListener(listener);
					}
				}
				
				// Catch failed attempts
				catch(err) { }
			}

			// Set current dealer to selected
			selectedDealer = hash;
			
			// Set class to selected
			obj.className = 'selected';
			
			// If selection should affect the map...
			if(runMap == true) {
				// Pan to store marker
				mapObject.panTo(dealerPoints[hash]);
				
				// Open information window
				dealerWindows[hash] = mapObject.openInfoWindowHtml(dealerPoints[hash], dealerMsgs[hash]);
				
				// Wait for information window to close
				listener = GEvent.addListener(mapObject, "infowindowclose", function() { 
					// Return to original position
					mapObject.returnToSavedPosition();
					
					// If a dealer is selected, deselect dealer from dealer list
					if(selectedDealer) { selectDealer(hash, false); }
					
					// Stop waiting for the information window to close
					GEvent.removeListener(listener);
				});
			}
			
			// Return true
			return true;
		}		
	}
	
	// If object doesn't exist, return false
	else { return false; }
}




/*******************/
/*                 */
/*  Map Functions  */
/*                 */
/*******************/
// Add a dealer to the map
function addDealer(hash, lat, long, msg) {
	// Set default return value
	var r = false;
	
	// If browser is compatible with the Google Maps API...
	if(GBrowserIsCompatible()) {
		// Set return value to true
		r = true;
		
		// Retrieve dealer point
		point = new GLatLng(lat, long);
		
		// Add dealer point to points
		globalPoints[globalPoints.length] = point;
		dealerPoints[hash] = point;
		
		// Create marker
		dealerMarkers[hash] = new GMarker(point, spotIconOptions);
		
		// Add marker to the map
		mapObject.addOverlay(dealerMarkers[hash]);
		
		// Set dealer message
		dealerMsgs[hash] = '<div class="dealer_info">'+msg+'</div>';
		
		// Wait for mouse clicks on the marker
		GEvent.addListener(dealerMarkers[hash], "click", function() {
			// Pan to store marker
			mapObject.panTo(dealerPoints[hash]);
			
			// Open information window
			dealerWindows[hash] = mapObject.openInfoWindowHtml(dealerPoints[hash], dealerMsgs[hash]);
			
			// Select dealer from dealer list
			selectDealer(hash, false);
			
			// Wait for information window to close
			listener = GEvent.addListener(mapObject, "infowindowclose", function() { 
				// Return to original position
				mapObject.returnToSavedPosition();
				
				// If a dealer is selected, deselect dealer from dealer list
				if(selectedDealer) { selectDealer(hash, false); }
				
				// Stop waiting for the information window to close
				GEvent.removeListener(listener);
			});
		});
	}
	
	// Send return value
	return r;
}


// Get map bounds
function mapBounds() {
	// Create bounds data
	var bounds = new GLatLngBounds();
	
	// For every dealer point, extend bounds data
	for (var i=0; i< globalPoints.length; i++) { bounds.extend(globalPoints[i]); }
	
	// Set zoom to display all dealers
	mapObject.setZoom(mapObject.getBoundsZoomLevel(bounds));
	
	// Set map to the center of all dealers
	mapObject.setCenter(bounds.getCenter());
	
	// Save centered position
	mapObject.savePosition();
}


// Googleİ Maps Creater
function createMap(lat, long) {
	// Set default return value
	var r = false;
	
	// If browser is compatible with the Google Maps API...
	if(GBrowserIsCompatible()) {
		// Set return to true
		r = true;
		
		// Create a new map
		mapObject = new GMap2(document.getElementById(mapID));

		// Add map controls
		mapObject.addControl(new GLargeMapControl());			// Position control
		mapObject.addControl(new GScaleControl());				// Distance scale display
		mapObject.addControl(new GMapTypeControl());				// Map type control
	
		// Create map home
		homePoint = new GLatLng(lat, long);
		
		// Set map to map home
		mapObject.setCenter(homePoint);
		
		// Create marker icon
		var spotIcon = new GIcon();
		spotIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		spotIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		spotIcon.iconSize = new GSize(12, 20);
		spotIcon.shadowSize = new GSize(22, 20);
		spotIcon.iconAnchor = new GPoint(6, 20);
		spotIcon.infoWindowAnchor = new GPoint(5, 1);
		spotIconOptions = { icon:spotIcon };
		
		// Create a geocoder
		geocoder = new GClientGeocoder();
	}
	
	// Send return value
	return r;
}