// JavaScript Document

function City () {};
City.prototype = {
	__infoWindowOpened:false,
	id:'',
	name:'',
	title:'',
	office_title:'',
	address:'',
	contact_person:'',
	phone:'',
	lat:0,
	long:0,
	pin:'http://www.clarity-ag.net/fileadmin/template/new_layout2/images/locations/marker-red.png',
	zoom:0,
	map_style:0,
	
	marker:'',
	// City parameters: id, name, title, address, contact_person, phone, lat, long, pin, zoom, map_style
	init: function(data, map) {
		//this.__infoWindowOpened=false;
		this.id = data.id;
		this.name = data.name;
		this.title = data.title;
		this.office_title = data.office_title;
		this.address = data.address;
		this.contact_person = data.contact_person;
		this.phone = data.phone;
		this.lat = data.lat;
		this.long = data.long;
		if (typeof(data.pin) != "undefined") {
			this.pin = data.pin; // this is google marker image
		}
		this.zoom = 1*data.zoom;
		this.map_style = data.map_style;
		
		data.map = typeof(data.map) != "undefined"?data.map:map;
			   
		if (typeof(data.map) != "undefined") {
			var point = new GLatLng(this.lat, this.long);
			//marker = addOffice(point, this.office_title );
			//debugger;
			if (this.pin != '') {
				var icon = new GIcon(G_DEFAULT_ICON, this.pin);
			} else {
				var icon = new GIcon(G_DEFAULT_ICON);
			}
			this.marker = new GMarker(point, {icon:icon, title:this.title});
			this.marker.parentObj = this;
			
			GEvent.addListener(this.marker, "click", function() {
				this.parentObj.showOnMap(map);
				//this.openInfoWindowHtml(this.parentObj.getInfoForBubble());
			});
			GEvent.addListener(this.marker, "mouseover", function() {
				/*if (!this.parentObj.__infoWindowOpened) {*/
					this.openInfoWindowHtml(this.parentObj.getInfoForBubble());
				/*}*/
			});

			GEvent.addListener(this.marker, "infowindowopen", function() {
				this.parentObj.__infoWindowOpened = true;
			});
			GEvent.addListener(this.marker, "infowindowclose", function() {
				this.parentObj.__infoWindowOpened = false;
			});
	
			data.map.addOverlay(this.marker);
		}

//		this. = data[];
	},
	
	addAddress: function (address) {
		this.address = address;
	},

	showOnMap: function (map) {
		// show this city on Google	Map
		// select map style
		// zoom map
		// use custom pin
		//map.setMapType(this.map_style);
		//console.log('Centering map on '+this.office_title+' on point '+new GLatLng(this.lat, this.long)+'with Zoom '+this.zoom);
		map.setCenter(new GLatLng(this.lat, this.long), this.zoom);
		//map.panTo(new GLatLng(this.lat, this.long));
		//map.setZoom(this.zoom);
		/*if (!this.__infoWindowOpened) {*/
			this.marker.openInfoWindowHtml(this.getInfoForBubble());
		/*}*/
		
	},
	
	showOnMapWithBubble: function () {
		// center map on this city
		// show bubble with title
	},
	
	closeInfoWindows: function () {
		this.marker.closeInfoWindow();
	},
	
	getInfoForBubble: function () {
		var info = '';
		
		if (this.office_title != '') info += '<b>'+this.office_title+'</b>';
		if (this.address != '') info += '<br />'+this.address;
//		if (this.contact_person != '') info += '<br />'+this.contact_person;
//		if (this.phone != '') info += '<br />'+this.phone;
		info += '<br />'+'<a href="'+contactFormUrl+this.country.name+'-'+this.name+'">'+contact_text+'</a>';

		return info;
	}
	
};

function Country () {};
Country.prototype = {
	__infoWindowOpened:false,
	id:'',
	name:'',
	title:'',
	office_title:'',
	address:'',
	contact_person:'',
	phone:'',
	lat:0,
	long:0,
	pin:'http://www.clarity-ag.net/fileadmin/template/new_layout2/images/locations/marker-red.png',
	zoom:0,
	map_style:0,
	
	marker:'',
	
	cities:Array(),
	// City parameters: id, name, title, address, contact_person, phone, lat, long, pin, zoom, map_style
	init: function(data, map) {
		this.id = data.id;
		this.name = data.name;
		this.title = data.title;
		this.office_title = data.office_title;
		this.address = data.address;
		this.contact_person = data.contact_person;
		this.phone = data.phone;
		this.lat = data.lat;
		this.long = data.long;
		if (typeof(data.pin) != "undefined") {
			this.pin = data.pin; // this is google marker image
		}
		this.zoom = 1*data.zoom;
		this.map_style = data.map_style;
		this.cities = new Array();
		
//		this. = data[];
	},
	
	initOnMap: function(data) {
		if (typeof(data.map) != "undefined" && this.cities.length < 1) {
			// if there is no cities in this country, then we will show as marker it's capital
			// otherwise skip showing marker on the map
			var point = new GLatLng(this.lat, this.long);
			//marker = addOffice(point, this.office_title );
			
			if (this.pin != '') {
				var icon = new GIcon(G_DEFAULT_ICON, this.pin);
				//icon.image = this.pin;
				//marker = new GMarker(point);
			} else {
				var icon = new GIcon(G_DEFAULT_ICON);
			}
			this.marker = new GMarker(point, {icon:icon});
			this.marker.parentObj = this;
			
			if (this.cities.length < 1) {
				GEvent.addListener(this.marker, "click", function() {
					this.openInfoWindowHtml(this.parentObj.getInfoForBubble());
				});
				GEvent.addListener(this.marker, "mouseover", function() {
					/*if (!this.parentObj.__infoWindowOpened) {*/
						this.openInfoWindowHtml(this.parentObj.getInfoForBubble());
					/*}*/
				});

				GEvent.addListener(this.marker, "infowindowopen", function() {
					this.parentObj.__infoWindowOpened = true;
				});
				GEvent.addListener(this.marker, "infowindowclose", function() {
					this.parentObj.__infoWindowOpened = false;
				});
			}
	
			data.map.addOverlay(this.marker);
		}		
	},
	
	addCity: function (city) {
		//alert(this.cities.length);
		this.cities[this.cities.length] = city;
	},
	
	showOnMap: function (map) {
		// show this country on Google Map
		// select map style
		// zoom map
		// use custom pins for the cities
		this.closeInfoWindows();
		
		//map.setMapType(this.map_style);
		//console.log('Centering map on '+this.title+' on point '+new GLatLng(this.lat, this.long)+' with Zoom '+this.zoom);
		map.setCenter(new GLatLng(this.lat, this.long), this.zoom);
		//map.panTo(new GLatLng(this.lat, this.long));
		//map.setZoom(this.zoom);
		if (this.cities.length < 1 /*&& !this.__infoWindowOpened*/)	this.marker.openInfoWindowHtml(this.getInfoForBubble());	
	},
	
	showCity: function (city) {
		// show this city on Google	Map
		// select map style
		// zoom map
		// use custom pin
		// show bubble with title
		city.show();
	},
	
	closeInfoWindows: function () {
		if (this.marker != '') {
			this.marker.closeInfoWindow();
		}
		for (var i=0; i<this.cities.length; ++i) {
			this.cities[i].closeInfoWindows();
		}		
	},
	
	getInfoForBubble: function () {
		var info = '';
		
		if (this.office_title != '') info += '<b>'+this.office_title+'</b>';
		if (this.address != '') info += '<br />'+this.address;
//		if (this.contact_person != '') info += '<br />'+this.contact_person;
//		if (this.phone != '') info += '<br />'+this.phone;
		if (this.cities.length < 1) {
			info += '<br />'+'<a href="'+contactFormUrl+this.name+'">'+contact_text+'</a>';
		}

		return info;
	}

}

function Locations () {};
Locations.prototype = {
	country_selector_id:'',
	country_selector:0,
	city_selector_id:'',
	city_selector: 0,
	map_id: '',
	map: 0,
	zoom:2,
	map_style:G_NORMAL_MAP,
	lat:50.21273,
	long:8.655027,
	marker:'',
	
	country_index: '',
	city_index: '',
	countries: Array(),
	
	init: function (country, city, map, desc) {
		this.countries = new Array();
		this.country_selector_id = country;
		this.city_selector_id = city;
		this.map_id = map;
		this.desc_id = desc;
		if (typeof(country) != 'string') {
			this.country_selector= country;
		} else {
			this.country_selector= $(this.country_selector_id);
		}
		
	},
	
	addCountry: function (country) {
		//data = arguments;
		this.countries[this.countries.length] = country;
	},
	
	showOnMap: function (map) {
		// show all the Locations on Google Map
		// select map style
		// zoom map
		// use custom pins for the cities
		this.closeInfoWindows();
		//console.log('Centering map on World on point '+new GLatLng(this.lat, this.long)+'with Zoom '+this.zoom);
        map.setCenter(new GLatLng(this.lat, this.long), this.zoom);
		//map.setMapType(this.map_style);

		
	},
	
	closeInfoWindows: function () {
		//this.marker.closeInfoWindow();
		for (var i=0; i<this.countries.length; ++i) {
			this.countries[i].closeInfoWindows();
		}		
	}

};
