/**
* the Googlemaps class
*
* @since june 2011
* @author Mirjam Verloop
* @copyright eFocus
*
* @require jQuery 1.4.2 (http://www.jquery.com)
*
*
*/

function Googlemaps(options) {

	var opt = options || {};

	this.options = {
		'mapContainer': false,
		'mapZoom': 10,
		'mapCenter': false,
		'mapMapType': 'google.maps.MapTypeId.ROADMAP',
		'streetView': true,
		'mapMarkerData': new Array(),
		'customIcon': null,
		'customActiveIcon': null,
		'infoWindowClass': null,
		'infoWindowShow': 'click',
		'ajaxLoader': null,
		'showInfoWindow': true,
		'fullScreenMode': false, 
		'showResultsInList': false,
		'showListContainer': null,
		'selectedCountry': null,
		'markerZIndex': 100,
		'allMarkers': new Array()
	}

	// implement options
	for (i in this.options) {
		if (opt[i] != undefined) {
			this.options[i] = opt[i];
		}
	}

	this.initialize();

};

Googlemaps.prototype = {

	/**
	* initializes carrousel
	*/
	initialize: function () {

		if (this.options.ajaxLoader) this.hideOverlay();

		var latLng = new google.maps.LatLng(this.options.mapCenter.lat, this.options.mapCenter.lng);

		var objMapOptions = {
			zoom: this.options.mapZoom,
			center: latLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false,
			scrollwheel: false
		}

		this.map = new google.maps.Map(this.options.mapContainer, objMapOptions);

		google.maps.event.trigger(this.map, 'resize');
		this.map.setZoom(this.options.mapZoom);

		if (this.options.mapMarkerData.length > 0) {

			// markers
			this.setMarkers();

			// list
			if (this.options.showResultsInList == true) {
				this.showList();
			}
		}

		var cl = this;

		if (this.ajaxLoader) google.maps.event.addListener(this.map, 'zoom_changed', function () {

			cl.showOverlay();

			if (cl.map.getZoom() <= 0 || cl.map.getZoom() >= 15) {
				cl.hideOverlay();
			}

		});

	},

	showOverlay: function () {

		this.options.ajaxLoader.css('display', 'block');

	},

	hideOverlay: function () {

		this.options.ajaxLoader.css('display', 'none');

	},

	positionMapAddress: function () {

		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({ 'address': this.options.selectedCountry }, function (result, status) {
			if (result[0]) {
				this.map.panTo(result[0].geometry.location);
			}
		} .bind(this));

		this.zoomToFit();

	},

	setMarkers: function () {

		var cl = this;

		jQuery.each(this.options.mapMarkerData, function (index, el) {

			if (cl.options.customIcon) {
				// customicon
				var newMarker = cl.getCustomMarker(el, index);
			} else {
				// default icon
				var newMarker = cl.getDefaultMarker(el);
			}

			cl.options.allMarkers.push(newMarker);

		});

		this.zoomToFit();

	},

	zoomToFit: function (el) {

		var bound = new google.maps.LatLngBounds();

		jQuery.each(this.options.allMarkers, function (index, el) {

			bound.extend(el.getPosition());

		});

		this.map.fitBounds(bound);

	},

	getDefaultMarker: function (el) {

		//console.log('getDefaultMarker');

		var latLng = new google.maps.LatLng(el.lat, el.lng);

		var marker = new google.maps.Marker({
			position: latLng,
			map: this.map
		});

		return marker;

	},

	getCustomMarker: function (el, index) {

		var latLng = new google.maps.LatLng(el.lat, el.lng);

		var marker = new google.maps.Marker({
			position: latLng,
			map: this.map,
			icon: this.options.customIcon
		});

		marker.setZIndex(this.options.markerZIndex);
		this.options.markerZIndex += 2;

		this.setLabelOnCustomMarker(marker, (index + 1));

		return marker;

	},

	setLabelOnCustomMarker: function (marker, markertext) {

		//console.log('setLabelOnCustomMarker');

		var customMarkerText = new google.maps.OverlayView();
		customMarkerText.setMap(this.map);

		var markerText;

		customMarkerText.onAdd = function () {

			markerText = document.createElement('div');
			markerText.innerHTML = markertext;
			markerText.className = 'markertext';

			var panes = this.getPanes();
			panes.overlayImage.appendChild(markerText);

		}

		customMarkerText.draw = function () {

			var overlayProjection = this.getProjection();
			var markerPosition = overlayProjection.fromLatLngToDivPixel(marker.getPosition());

			jQuery(markerText).css('z-index', (marker.getZIndex() + 1));
			jQuery(markerText).css('left', markerPosition.x - 12);
			jQuery(markerText).css('top', markerPosition.y - 20);

		}

	},

	showList: function () {

		var cloneItem = jQuery('#clone');

		//empty
		this.options.showListContainer.find('div.list').empty();

		var newList = jQuery('<ul class="search_results clearfix">');
		jQuery.each(this.options.mapMarkerData, function (index, el) {

			var list = jQuery('<li/>').appendTo(newList);
			var thisItem = cloneItem.clone().appendTo(list);

			if (el.name) {
				thisItem.find('h2').text((index + 1) + '. ' + el.name);
			} else {
				thisItem.find('h2').remove();
			}

			var listHTML = '';
			el.street ? listHTML += el.street + '<br/>' : '';
			el.zip ? listHTML += el.zip + ' ' : '';
			el.city ? listHTML += el.city + '<br/>' : '';
			el.country ? listHTML += el.country + '<br/>' : '';
			el.phone ? listHTML += el.phone + '<br/>' : '';
			el.fax ? listHTML += el.fax + '<br/>' : '';

			thisItem.find('p.default_address').html(listHTML);

			if (el.email) {
				thisItem.find('a.default_email').attr('href', 'mailto:' + el.email);
				thisItem.find('a.default_email').attr('title', el.email);
			} else {
				thisItem.find('a.default_email').remove();
			}


			if (el.website) {
				thisItem.find('a.default_website').attr('href', el.website);
				thisItem.find('a.default_website').attr('title', el.website);
			} else {
				thisItem.find('a.default_website').remove();
			}

			thisItem.attr('id', '');
			
		});

		newList.appendTo(this.options.showListContainer.find('div.list'));
		equalizeHeight(newList.find('li'), 3);
		equalizeHeightWithin(newList.find('li'), newList.find('h2'), 3);
		equalizeHeight(jQuery('div.col_sub, div.col_content_large'), 2);

		//this.options.showListContainer.find('div#clone').css('display', 'block');
		//this.options.showListContainer.find('div#clone').css('display', 'none');

	}

}
