/******************************************************************************
 *
 * Purpose: Measure lines lengths and polygons areas
 * Author:  Jaouad Bennasser, SIRAP
 *
 ******************************************************************************
 *
 * Copyright (c) 2009 SIRAP
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See the COPYING file.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with p.mapper; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ******************************************************************************/


$.extend(PM.Plugin.Measure2,
{

	init: function() {
		this.init_base(); //init parameters of common drawing class  
		
		this.selectColorId = "measure2Color"; //color input element's name for the plugin measure2
		this.tableContentId = "measure2TableContent"; //outline color input element's name for the plugin measure2
		this.emptyButtonId = "measure2Empty"; //empty button name for the plugin measure2
		this.tableId = "measure2Table"; //HTML table header name for the plugin drawing
		
		//load config settings from config_XXXXX.xml file 
		if (typeof(PM.ini.pluginsConfig.sirap_measure2) != 'undefined') {
			if (PM.ini.pluginsConfig.sirap_measure2.dlgType) {
				this.dlgType = PM.ini.pluginsConfig.sirap_measure2.dlgType;
			}
			if (PM.ini.pluginsConfig.sirap_measure2.default_color) {
				this.default_color = PM.ini.pluginsConfig.sirap_measure2.default_color;
			}
			if (PM.ini.pluginsConfig.sirap_measure2.createMeasureInput) {
				this.createMeasureInput = PM.ini.pluginsConfig.sirap_measure2.createMeasureInput;
			}
		}
		
		//redraw polyline while refreshing map, zoom in, zoom out ...
		$("#pmMapRefreshImg").bind("load", function(e){
			if (PM.Map.maction == "measure2") {
				PM.Plugin.Measure2.redrawPoly();
			}	
		}); 
	},
	
	/** Used to add a measure to the HTML table.
	 * @param: nbpoly: number of polygon or polyline ; type: line, polygon 
	 * @return: void
	 */
	addObjToTab_extend: function(nbpoly, type) {
		var measure = this.current_properties["comment"];
		var color = this.current_properties["color"];
		var measureType = this.current_properties["measureType"];
		var upperMeasureType = upperWord(measureType);
		$("#" + this.tableContentId).append("<tr><td>" + (nbpoly + 1) + "</td><td class='measure_" + measureType + "' alt='" + _p(upperMeasureType) + "' title='" + _p(upperMeasureType) + "'></td><td>" + measure + "</td><td title='" + _p('Change') + "'><input type='text' class='measure2Color' value='" + color + "' onchange='javascript:PM.Plugin.Measure2.changeElement(" + (nbpoly) + ", \"color\")'  /></td><td><a href='javascript:PM.Plugin.Measure2.deleteObj(" + (nbpoly) + ")'><img alt='delete' title='" + _p('Delete') + "' width='20' height='20' src='" + PM_PLUGIN_LOCATION + "/measure2/images/delete.jpeg'/></a></td></tr>");
		var thisPlugin = this;
		// be careful: because of js compression bad algorithm, do not use space followed by point in string
		$("#" + this.tableContentId + " tr:not(:first):eq(" + (nbpoly) + ")" + " " + ".measure2Color").each(function() {
			thisPlugin.showElemColor(this);
		});
	},
	
	
	
	/** 
	 * change color of a measure.
	 * @param num: number of object, elmt: property to be changed. 
	 * @return: void  
	 */
	changeElement: function(num, elemt) {
		
		if (elemt == "color") {
			var colorHex = this.tabObjects[num]["properties"]["color"]; 
			var updateColorHex = $("#" + this.tableContentId + " tr:eq(" + (num + 1) + ") > td:eq(3) > input:eq(0)").val(); 
			
			if (colorHex != updateColorHex) {
				this.tabObjects[num]["properties"]["color"] = updateColorHex;
				var type = this.tabObjects[num]["type"]; 
				var data = this.generateJson(type);
		
				if (type == "line") {
					this.sendLayerToServer('distance', data);
				} else if(type == "polygon") {
					this.sendLayerToServer('area', data);
				}
				this.updateTab();
			}
		}
	},
	
	// show color graphically
	showElemColor: function(elem) {
		$(elem).show().SevenColorPicker();
	}
	
});
