// JavaScript Document
// Author: Ben Barnett
// Website: http://www.brushfiredesign.net



/**********************************************************************
*
*	Google Maps Functions
*		- for google maps
*
*
**********************************************************************/
var map;
var geocoder;

function CreateOverlay(){}
CreateOverlay.prototype = new GControl();
CreateOverlay.prototype.initialize = function(map) 
{
	var container = document.createElement("div");
		
	// controls
	var viewLabel = document.createElement("div");
	viewLabel.appendChild(document.createTextNode("View:"));
	this.setElementStyle(viewLabel);
	
	viewLabel.style.marginLeft = "350px";
	
	container.appendChild(viewLabel);
	
	var streetLabel = document.createElement("div");
	streetLabel.appendChild(document.createTextNode("Street"));
	this.setElementStyle(streetLabel);
	this.setActiveStyle(streetLabel);
	streetLabel.isActive = true;
	
	streetLabel.style.marginLeft = "380px";
	streetLabel.style.cursor = "pointer";
	
	GEvent.addDomListener(streetLabel, "click", function() {
		// clear all other element styles
		CreateOverlay.prototype.setElementStyle(satLabel);
		CreateOverlay.prototype.setElementStyle(streetLabel);
		CreateOverlay.prototype.setElementStyle(hybridLabel);
		streetLabel.isActive = false;
		hybridLabel.isActive = false;
		satLabel.isActive = false;
		
		// apply our style
		CreateOverlay.prototype.setActiveStyle(streetLabel);
		this.isActive = true;
		map.setMapType(G_NORMAL_MAP);
	});

	GEvent.addDomListener(streetLabel, "mouseover", function() {
		if (!this.isActive)
		{
			// apply our style
			CreateOverlay.prototype.setRolloverStyle(streetLabel);
		}
	});
	
	GEvent.addDomListener(streetLabel, "mouseout", function() {
		if (!this.isActive)
		{
			// apply our style
			CreateOverlay.prototype.setElementStyle(streetLabel);
		}
	});

	container.appendChild(streetLabel);
	
	
	
	
	var satLabel = document.createElement("div");
	satLabel.appendChild(document.createTextNode("Satellite"));
	this.setElementStyle(satLabel);
	
	satLabel.style.marginLeft = "420px";
	satLabel.style.cursor = "pointer";
	
	GEvent.addDomListener(satLabel, "click", function() {
		// clear all other element styles
		CreateOverlay.prototype.setElementStyle(satLabel);
		CreateOverlay.prototype.setElementStyle(streetLabel);
		CreateOverlay.prototype.setElementStyle(hybridLabel);
		streetLabel.isActive = false;
		hybridLabel.isActive = false;
		satLabel.isActive = false;
		
		// apply our style
		CreateOverlay.prototype.setActiveStyle(satLabel);
		this.isActive = true;
		map.setMapType(G_SATELLITE_MAP);
	});

	GEvent.addDomListener(satLabel, "mouseover", function() {
		if (!this.isActive)
		{
			// apply our style
			CreateOverlay.prototype.setRolloverStyle(satLabel);
		}
	});
	
	GEvent.addDomListener(satLabel, "mouseout", function() {
		if (!this.isActive)
		{
			// apply our style
			CreateOverlay.prototype.setElementStyle(satLabel);
		}
	});

	container.appendChild(satLabel);
	
	
	
	
	
	var hybridLabel = document.createElement("div");
	hybridLabel.appendChild(document.createTextNode("Hybrid"));
	this.setElementStyle(hybridLabel);
	
	hybridLabel.style.marginLeft = "472px";
	hybridLabel.style.cursor = "pointer";
	
	GEvent.addDomListener(hybridLabel, "click", function() {
		// clear all other element styles
		CreateOverlay.prototype.setElementStyle(satLabel);
		CreateOverlay.prototype.setElementStyle(streetLabel);
		CreateOverlay.prototype.setElementStyle(hybridLabel);
		streetLabel.isActive = false;
		hybridLabel.isActive = false;
		satLabel.isActive = false;
		
		// apply our style
		CreateOverlay.prototype.setActiveStyle(hybridLabel);
		this.isActive = true;
		map.setMapType(G_HYBRID_MAP);
	});

	GEvent.addDomListener(hybridLabel, "mouseover", function() {
		if (!this.isActive)
		{
			// apply our style
			CreateOverlay.prototype.setRolloverStyle(hybridLabel);
		}
	});
	
	GEvent.addDomListener(hybridLabel, "mouseout", function() {
		if (!this.isActive)
		{
			// apply our style
			CreateOverlay.prototype.setElementStyle(hybridLabel);
		}
	});

	container.appendChild(hybridLabel);
	

	
	var sepLabel1 = document.createElement("div");
	sepLabel1.appendChild(document.createTextNode("|"));
	this.setElementStyle(sepLabel1);
	
	sepLabel1.style.marginLeft = "413px";
	container.appendChild(sepLabel1);
	
	var sepLabel2 = document.createElement("div");
	sepLabel2.appendChild(document.createTextNode("|"));
	this.setElementStyle(sepLabel2);
	
	sepLabel2.style.marginLeft = "464px";
	container.appendChild(sepLabel2);

		
	
	
	var zoomLabel = document.createElement("div");
	zoomLabel.appendChild(document.createTextNode("Zoom:"));
	this.setElementStyle(zoomLabel);
	
	zoomLabel.style.marginLeft = "10px";
	container.appendChild(zoomLabel);
	
	
	
	
	var zoomOutButton = document.createElement("img");
	zoomOutButton.src = "../../images/ZoomBarMinus.gif";
	this.setElementStyle(zoomOutButton);
	
	GEvent.addDomListener(zoomOutButton, "click", function() {
		if (map.getZoom() > 1)
		{
			map.zoomOut();
			
			var nextX = parseInt(map.getZoom() * 9);
			if (nextX <= 12) { nextX = 0; }
			
			document.getElementById('zoomSlider').style.left = nextX;
		}
	});
	
	zoomOutButton.style.cursor = "pointer"
	zoomOutButton.style.marginLeft = "50px"
	container.appendChild(zoomOutButton);
	
	
	var zoomOutButton = document.createElement("img");
	zoomOutButton.src = "../../images/ZoomBarPlus.gif";
	this.setElementStyle(zoomOutButton);
	
	GEvent.addDomListener(zoomOutButton, "click", function() {
		if (map.getZoom() < 17)
		{
			map.zoomIn();
			
			var nextX = parseInt(map.getZoom() * 9);
			if (nextX >= 226) { nextX = 226; }
			
			document.getElementById('zoomSlider').style.left = nextX;
		}
	});
	
	zoomOutButton.style.cursor = "pointer"
	zoomOutButton.style.marginLeft = "247px"
	container.appendChild(zoomOutButton);
	
	
	
	
	var zoomBG = document.createElement("img");
	this.setElementStyle(zoomBG);
	zoomBG.style.width = "170px";
	zoomBG.style.marginLeft = "70px";
	zoomBG.style.marginTop = "14px";
	zoomBG.src = "../../images/ZoomBarBG.gif";
	zoomBG.id = "zoomBG";
	
	container.appendChild(zoomBG);
	
	var zoomSlider = document.createElement("img");
	this.setElementStyle(zoomSlider);
	zoomSlider.src = "../../images/ZoomBarSqSLider.gif";
	zoomSlider.id = "zoomSlider";
	zoomSlider.style.marginLeft = "70px";
	zoomSlider.style.cursor = "move";
		
	container.appendChild(zoomSlider);
	
	Drag.init(zoomSlider, null, 0, 156, 0, 0);
	zoomSlider.onDragEnd = function(x, y) {
		if (x > 11)
		{
			var zoomValue = parseInt(x / 9);
			map.setZoom(zoomValue);
		}
		else
		{
			map.setZoom(1);
		}
	};

	
	
	container.style.backgroundColor = "black";
	container.style.width = "550px";
	container.style.height = "40px";
	
	
	map.getContainer().appendChild(container);
	return container;
}

CreateOverlay.prototype.setElementStyle = function(button) 
{
	button.style.font = "11px Franklin Gothic Medium";
	button.style.color = "white";	
	button.style.position = "absolute";
	button.style.marginTop = "11px";
	button.style.textDecoration = "none";
}

CreateOverlay.prototype.setActiveStyle = function(button) 
{
	button.style.color = "#f00";
}

CreateOverlay.prototype.setRolloverStyle = function(button) 
{
	button.style.color = "#f00";
	button.style.textDecoration = "underline";
}


CreateOverlay.prototype.getDefaultPosition = function() 
{
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}




function createMarker(point, name, telephone, mobile, fax, email, website, locationID, locality, country, address, postcode, isImporter) 
{
	if (isImporter == 0)
	{
		var icon = new GIcon(G_DEFAULT_ICON, "../../images/MapPinpoint.gif");
	}
	else
	{
		var icon = new GIcon(G_DEFAULT_ICON, "../../images/pin_crossed_aks.png");
	}
	
	
	var marker = new GMarker(point, icon);
	var addressline = address;
	if (addressline != "")
	{
		addressline += ",<br/>";
	}
	
	var curHTML = "<h1>" + name + "</h1>";
	if (locality && country) { curHTML += "<h3>" + addressline + locality + ", " + postcode + ".<br/>" + country + "</h3>"; }
	curHTML += "<p>";
	if (telephone) { curHTML += "<b>tel:</b> " + telephone + "<br/>"; }
	if (mobile) { curHTML += "<b>mob:</b> " + mobile + "<br/>"; }
	if (fax) { curHTML += "<b>fax:</b> " + fax + "<br/>"; }
	if (email) { curHTML += "<br/><a href=\"mailto:" + email + "\">" + email + "</a><br/>"; }
	if (website) { curHTML += "<a href=\"" + website + "\">" + website + "</a><br/>"; }
	curHTML += "<b>Type:</b> " + ((isImporter == 1) ? "Importer" : "Distributor") + "<br />";
	
	curHTML += "</p>";
		
	
	GEvent.addListener(marker, "click", function() 
	{
		marker.openInfoWindowHtml(curHTML);
	});
	return marker;
}




//<![CDATA[

function load() 
{
  if (GBrowserIsCompatible()) 
  {	 
 
	  
	map = new GMap2(document.getElementById("map"));
	map.addControl(new CreateOverlay());
	map.setCenter(new GLatLng(33.1376, -80.0684), 2);


	GDownloadUrl("../../xml/locations.xml.php", function(data, responseCode) 
	{
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) 
		{
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),	parseFloat(markers[i].getAttribute("lng")));

			map.addOverlay(createMarker(point, markers[i].getAttribute("name"), markers[i].getAttribute("telephone"), markers[i].getAttribute("mobile"), markers[i].getAttribute("fax"), markers[i].getAttribute("email"), markers[i].getAttribute("website"), markers[i].getAttribute("locationID"), markers[i].getAttribute("locality"), markers[i].getAttribute("country"), markers[i].getAttribute("address"), markers[i].getAttribute("postcode"), markers[i].getAttribute("isImporter")));
		}
	});
	
	//document.getElementById('country').selectedIndex = 0;
	document.getElementById('zoomSlider').style.left = '18px';
	//setupAutoComplete('');
	
	var t = setTimeout("map.setZoom(3)", 200);
  }
}

function setupAutoComplete(countryID)
{
	document.getElementById('locality').value = "";
	filterMap(countryID);
	if (countryID == "")
	{
		
	}
	else
	{
		var options = {
			script: "php/getLocalities.php?country="+countryID+"&",
			varname: "curValue",
			delay: 100,
			json: true,
			callback: function(obj) { filterMap(countryID, obj.value); }
		 };
		var as = new AutoSuggest('locality', options);
	}
}

function filterMap(country, locality)
{
	var url = "xml/locations.xml.php?";
	if (country) { url += "country="+country; }
	if (locality) { url += "&locality="+locality; }
	
	map.clearOverlays();
	GDownloadUrl(url, function(data, responseCode) 
	{
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		
		if (country && !locality)
		{
			map.setCenter(new GLatLng(parseFloat(markers[0].getAttribute("lat")),	parseFloat(markers[0].getAttribute("lng"))));
			map.setZoom(6);
			document.getElementById('zoomSlider').style.left = 6*9;
		}
		if (country && locality)
		{
			map.setCenter(new GLatLng(parseFloat(markers[0].getAttribute("lat")),	parseFloat(markers[0].getAttribute("lng"))));
			map.setZoom(13);
			document.getElementById('zoomSlider').style.left = 13*9;
		}
		
		
		for (var i = 0; i < markers.length; i++) 
		{
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),	parseFloat(markers[i].getAttribute("lng")));

			map.addOverlay(createMarker(point, markers[i].getAttribute("name"), markers[i].getAttribute("telephone"), markers[i].getAttribute("mobile"), markers[i].getAttribute("fax"), markers[i].getAttribute("email"), markers[i].getAttribute("website"), markers[i].getAttribute("locationID"), markers[i].getAttribute("locality"), markers[i].getAttribute("country"), markers[i].getAttribute("address"), markers[i].getAttribute("postcode")));
		}
	});
}

//]]>



/**********************************************************************
*
*	Drag Functionality
*		- for zoom slider
*
*
**********************************************************************/
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
/**********************************************************************
*
*	Mouse Position
*		- for zoom slider
*
*
**********************************************************************/
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}


