﻿/*
ALL THE FUNCTIONS USED ALL OVER THE SITE SHOULD BE DEFINED HERE
*/

var EntityType = { not_set: -1, country: 0, capital: 1, county: 2, main_city: 3, city: 4, town: 5, locality: 6, zone: 7, street: 8}
var DiacriticsMap = {
	"ă": "a", "Ă": "A",
	"â": "a", "Â": "A", 
	"î": "i", "Î": "I",
	"ş": "s", "Ş": "S",
	"ţ": "t", "Ţ": "T"
}

// this will be popultated in other .js files
var MYAPP = {
	normalizer: new Normalizer()
};

String.prototype.removeDiacritics = function() {
	var str = this;
	for (var i in DiacriticsMap) {
		str = str.replace(new RegExp(i, 'g'), DiacriticsMap[i]);
	}
	return str;
}

String.prototype.removeDashes = function() { 
	return this.replace(new RegExp('-', 'g'), ' ');
}

function Normalizer() {
	return {
		invalid_search_strings_: [" st$", "^st ", "^st\\.", "street", "^str ", "str\\.", "strada", "b-dul ", "b-dul\\.", "^bdul ", "bdul\\.","bulevardul", "blvd", "nr ", "nr\\.", "pta ", "pta\\.", "p-ta ", "p-ta\\.", "piata", "^sos ", "sos\\."],
		// removes diacritics, trims the string and transforms to lower case
		normalize: function(str) {
			str = str.toLowerCase();
			str = str.removeDiacritics();
			return jQuery.trim(str.replace(new RegExp('^\\.|\\s\\.', 'g'), ' '));
		},
		// removes 'romania' from string
		removeCountry: function(str) {
			str = str.replace(/,romania/i, '').replace(/romania,/i, '').replace(/romania/i, '').replace(/, romania/i, '').replace(/romania ,/i, '');
			return str;
		},
		// checks if one of the strings from the list is contained in the given string
		compareStrings: function(str1, str2) {
			var strList = str1.replace(',', ' ').split(' ');
			for (var i = 0; i < strList.length; i++) {
				if (str2.indexOf(strList[i]) != -1)
					return true;
			}
			return false;
		},
		// remove unwanted search strings
		removeUnwSearchStrings: function(str) {
			for (var i = 0; i < this.invalid_search_strings_.length; i++) {
				var lRegExp = new RegExp(this.invalid_search_strings_[i], 'i');
				str = str.replace(lRegExp, '');
			}
			return jQuery.trim(str);
		}
	}
}

// executes right after all the elements are loaded and solves the z-index IE problem
//$(function() {
//	if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 7) {

//		var zIndexNumber = 1000;
//		$('div').not('#map_canvas *').each(function() {
//			if ($(this).is('#map_shadow *')) {
//				$(this).css('z-index', 1000);
//				return;
//			}

//			$(this).css('z-index', zIndexNumber);
//			zIndexNumber -= 10;
//		});
//	}
//});

