/**
* 	limit map boundaries so the user can't drag the map outside of a certain area
*
*	usage:
*	var defaultSW = new GLatLng(26.163753,-98.314097); // max south west point of our map
*	var defaultNE = new GLatLng(26.172862,-98.296115); // max north east point of our map
*	var allowedBounds = new GLatLngBounds(defaultSW,defaultNE); //our allowed boundaries
*	//the listener
*	GEvent.addListener(map, "move", function() {
*		checkBounds();
*	});
*
*/

// If the map position is out of range, move it back
function checkBounds() {
	// Perform the check and return if OK
	var currentBounds = map.getBounds();
	var cSpan = currentBounds.toSpan(); // width and height of the bounds
	var offsetX = cSpan.lng() / (2+aberration); // we need a little border
	var offsetY = cSpan.lat() / (2+aberration);
	var C = map.getCenter(); // current center coords
	var X = C.lng();
	var Y = C.lat();

	// now check if the current rectangle in the allowed area
	var checkSW = new GLatLng(C.lat()-offsetY,C.lng()-offsetX);
	var checkNE = new GLatLng(C.lat()+offsetY,C.lng()+offsetX);
		
	if (allowedBounds.containsLatLng(checkSW) && allowedBounds.containsLatLng(checkNE)) {
		return; // nothing to do
	}

	var AmaxX = allowedBounds.getNorthEast().lng();
	var AmaxY = allowedBounds.getNorthEast().lat();
	var AminX = allowedBounds.getSouthWest().lng();
	var AminY = allowedBounds.getSouthWest().lat();

	if (X < (AminX+offsetX)) {X = AminX + offsetX;}
	if (X > (AmaxX-offsetX)) {X = AmaxX - offsetX;}
	if (Y < (AminY+offsetY)) {Y = AminY + offsetY;}
	if (Y > (AmaxY-offsetY)) {Y = AmaxY - offsetY;}

	map.setCenter(new GLatLng(Y,X));
	return;
}

/**
* 	get marker icon
*   Takes 2 sizes of icons (small/big) 
*   
*	usage:
*	var iconSmallWidth 		= 12;
*	var iconSmallHeight 	= 12;
*	var iconBigWidth 		= 32;
*	var iconBigHeight 		= 46;
*	var iconUrlStart		= "../img/marker_";
*	var iconUrlEnd			= ".png";
*	getIcon("small", new GLatLng("lat","long");
*   create 4 icons; marker_small.png, marker_big.png, marker_print.gif, marker_mozPrint.jpg
*/

function getIcon(size,color) {
	if(size == 'small'){
    	width 	= iconSmallWidth;
        height 	= iconSmallHeight;
	}else{
    	width 	= iconBigWidth;
    	height 	= iconBigHeight;
    } 
     
    var haveColor = '';  
    if(color == '1'){
    	haveColor = iconColor;
    }
    
	icon = new GIcon();      		  
    icon.image = iconUrlStart + size + haveColor + iconUrlEnd;
    icon.printImage = iconUrlStart+'print.gif'; 
    icon.mozPrintImage = iconUrlStart+'mozPrint.jpg'; 
	icon.iconSize = new GSize(width, height);
	
	if(size == 'small'){
    	icon.infoWindowAnchor = new GPoint(0, 0 ); 
	    icon.iconAnchor = new GPoint((iconSmallWidth/2),(iconSmallHeight/2));
	}else{
		icon.infoWindowAnchor = new GPoint((iconBigWidth/2), 0); 
	    icon.iconAnchor = new GPoint((iconBigWidth/2), iconBigHeight);
	}           
    return icon;
}
			
/**
* 	restrict map zoomlevels
*
*	usage:
*	restrictZoomLevels(map, 5, 17);
*
*/

function restrictZoomLevels(map, minZoom, maxZoom) {
	// Get the list of map types
	var mt = map.getMapTypes();

	// Overwrite the getMinimumResolution() and getMaximumResolution() methods
	for(var i=0; i<mt.length; i++) {
		mt[i].getMinimumResolution = function() {return minZoom;}
		mt[i].getMaximumResolution = function() {return maxZoom;}
	}
}

function setMapType(map,mapType){
	map.setMapType(mapType);
}

/**
* 	loop in order to grab our address details from the google geocoder response
*
*	usage:
*	restrictZoomLevels(map, 5, 17);
*
*/

var geoAddress_GL;
var geoAddress_array
var geoCountryNameCode;
var geoCountryName;
var geoAdministrativeAreaName;
var geoSubAdministrativeAreaName;
var geoLocalityName;
var geoDependentLocalityName;
var geoThoroughfare;
var geoPostalCode;
var geoAccuracy;

// Based on script by: Klaas-Bindert de Haan, Amsterdam, The Netherlands, april 2009
function doAddressLoop(geoDetails){
	for (d in geoDetails) {
		if (geoDetails.hasOwnProperty(d)) {
			if (isString(geoDetails[d])) {
				switch(d) {
					case "CountryNameCode":
						geoCountryNameCode = geoDetails[d];
						break;
					case "CountryName":
						geoCountryName = geoDetails[d];
						break;
					case "AdministrativeAreaName":
						geoAdministrativeAreaName = geoDetails[d];
						break;
					case "SubAdministrativeAreaName":
						geoSubAdministrativeAreaName = geoDetails[d];
						break;
					case "LocalityName":
						geoLocalityName = geoDetails[d];
						break;
					case "DependentLocalityName":
						geoDependentLocalityName = geoDetails[d];
						break;
					case "ThoroughfareName":
						geoThoroughfare = geoDetails[d];
						break;
					case "PostalCodeNumber":
						geoPostalCode = geoDetails[d];
						break;
					case "Accuracy":
						geoAccuracy = geoDetails[d];
						break;
				}
			}
			// it's not a string, but a next nested object: loop deeper
			else {
				var geoNextDetails= geoDetails[d];
				doAddressLoop(geoNextDetails);
			}
		}
	}
}

function isString() {
	if (typeof arguments[0] == 'string') return true;
	if (typeof arguments[0] == 'object') {
		var theCriterion =  arguments[0].constructor.toString().match(/string/i); 
		return (theCriterion != null); 
	}
	return false;
}


function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }else{
   	  return true;
   }
}