
var icons = new Object();
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
icons[0] = 'Red';
icons[1] = 'Green';
icons[2] = 'Blue';
icons[3] = 'LightBlue';
icons[4] = 'Orange';
icons[5] = 'Yellow';
icons[6] = 'Purple';
icons[7] = 'Grey';
icons[8] = 'LightGreen';
var iconPrefix = '/gmaps-portlet/img/mapDot';
var iconSuffix = '.png';
var checkedSuffix = 'Checked';


var infoWindowWidth = 25;

var nl2br = function(text) {
	var newText = escape(text);
	var re_nlchar;
	if(newText.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(newText.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(newText.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	} else {
		return text;
	}
	return unescape( newText.replace(re_nlchar,'<br />') );
};

var linyfy = function(text) {
	var newText = escape(text);
	var re_nlchar;
	if(newText.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(newText.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(newText.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	} else {
		var lines = new Array();
		lines[0] = text;
		return lines;
	}
	var lines = newText.split(re_nlchar);
	for (var i in lines) {
		lines[i] = unescape(lines[i]);
	}
	return lines;
};

var formatLines = function(lines,link) {
	var result = '';
	for (var i in lines) {
		if (i == 0) {
			result = '<strong>' + lines[i] + '</strong>';
			if (link && link.length > 0) {
				result = '<a href="' + link + '">' + result + '</a>';
			}
		} else {
			result += lines[i]; 
		}
		if (i < lines.length -1) {
			result += '<br/>';
		}
	}
	return result;
};

var maxHeights = [10, 10, 12 , 18 , 65 , 80];

var createWindowContent = function(t,link) {
	var container = document.createElement('div');
	var lines = linyfy(t);
	//console.log('lines: ' + lines.length);
	var maxHeight = (16 * lines.length) - 15;
	//console.log('calculated max-height: ' + maxHeight);
	if (lines.length <= maxHeights.length) {
		maxHeight = maxHeights[lines.length - 1];
	}
	//console.log('max-height: ' + maxHeight);
	container.style.maxHeight = maxHeight + 'px';
	//container.style.width = "10px";
	//container.style.height = "10px";
	container.innerHTML = formatLines(lines,link);
	return container;
};

var getIconByIndex = function(color) {
	return (color > -1 ? icons[color] : null);
};

var getIconByName = function(color) {
	return icons[color];
};

var getIcon = function(t,checked) {
	if (t == null) {
		return null;
	}
	return iconPrefix + icons[t] + (checked ? checkedSuffix : '') + iconSuffix;
	/*if (typeof t == 'string') {
		return getIconByName(t);
	}
	if (typeof t == 'number') {
		return getIconByIndex(t);
	} */
};

var openInfoWindows = new Array();

var createMarker = function(lat,lng,map,descr,options) {
	return createMarkerBacker({
		lat : lat,
		lng : lng,
		map : map,
		title : descr,
		description : options.iwContent,
		drag : (options.drag ? options.drag : false),
		icon : getIcon(options.icon),
		link : options.link
	});
};

var createMarkerBacker = function(options) {
	var iwText = (options.iwContent ? options.iwContent : createWindowContent(options.title,options.link));
	var infowindow = new google.maps.InfoWindow({
		content: iwText,
		//content: 'c',
		maxWidth: infoWindowWidth
	});
	var icon = (options.icon ? options.icon : null);
	var point = new google.maps.LatLng(options.lat,options.lng);	
	var marker = new google.maps.Marker( {
		position : point,
		map : options.map,
		title : options.title,
		draggable: (options.drag ? options.drag : false),
		icon: options.icon
	});
	marker.iw = infowindow;
	marker.toggleWindow = function() {
		while (openInfoWindows.length > 0) {
			openInfoWindows.pop().close();
		}
		infowindow.open(options.map,marker);
		openInfoWindows.push(infowindow);
	};
	google.maps.event.addListener(marker, 'click', function() {
		marker.toggleWindow();
	});
	
	if (options.drag) {
		google.maps.event.addListener(marker,'dragend',function(event) {
			jQuery('[name=model.localPlace.latitude]').val(this.position.lat());
			jQuery('[name=model.localPlace.longitude]').val(this.position.lng());
		});
	}
	marker.toggle = function() {
		if (marker.getMap()) {
			marker.setMap(null);
			return false;
		} else {
			marker.setMap(options.map);
			return true;
		}
	};
	return marker;
};


