var Comps = { 
	c : "",
	hostName : "http://" + window.location.hostname,
	pURL : document.location.href,
	pTitle : document.title,
	eMail : 'thorfot' + '@' + 'yahoo' + '.' + 'com'
}

Comps.initialize = function(){
		this.aboutUs();
		this.advertise();
		this.missing();
}

Comps.initializeComp = function(){
		this.addAfterComps();
		this.initialize();
}

Comps.convert = function(unit, value){
	if (unit == "m") return value * 39.37;
	if (unit == "dm") return value * 3.937;
	if (unit == "cm") return value * 0.3937;
	if (unit == "mm") return value * 0.03937;
	if (unit == "ft") return value * 12;
	if (unit == "in") return value * 1;
	return value;
}
  
Comps.insertSKP = function(i){
	alert("insertSKP must be overridden in the script for the actual type");
} 

Comps.preComp = function(compName) {
	var o_unit = $F('o_unit');
	var o_red = this.convert(o_unit, $F('o_red'));
	var o_green = this.convert(o_unit, $F('o_green'));
	var o_blue = this.convert(o_unit, $F('o_blue'));
	var comp_col = $F('comp_col');
	var msg = ""
+ "compName = \"" + compName + "\";"
+ "componentdefinition = nil;"
+ "o_red = " + o_red + ";"
+ "o_green = " + o_green + ";"
+ "o_blue = " + o_blue + ";"
+ "comp_col = \"" + comp_col + "\";"
+ "o_point = Geom::Point3d.new o_red,o_green,o_blue;"
+ "o_transform = Geom::Transformation.new o_point;"
+ "";
	return msg;
}

Comps.suffComp = function(){
	var msg = ""
+ "group.move! o_transform;"
+ "if model.selection[0];"
+ "	s = model.selection[0];"
+ " t = s.transformation;"
+ " group.transform! t;"
+ "end;"
+ "";

	return msg;
}

Comps.addAfterComps = function(i){
		alert("addAfterCompList must be overridden in the script for the actual type");
}

Comps.lengthSelect = function(){
	var msg = "<tr>"
      + "<td>Length</td>"
      + "<td><input type=\"text\" id=\"compL\" value=\"6\" size=\"4\" class=\"center\">"
			+ "<select id=\"compLUnit\">"
      + "<option value=\"m\" selected=\"selected\">m</option>"
      + "<option value=\"dm\">dm</option>"
      + "<option value=\"cm\">cm</option>"
      + "<option value=\"mm\">mm</option>"
      + "<option value=\"in\">in</option>"
      + "<option value=\"ft\">ft</option>"
      + "</select></td>"      
      + "</tr>";
	return msg;		
}

Comps.instanceSelect = function(instance){
	var msg = "";

/**	if (instance == "Group")
		msg += "<tr>"
      + "<td>Insert as </td>"
      + "<td><select id=\"comp_instance\">"
      +"<option value=\"Component\"> Component" 
      +"<option value=\"Group\" selected> Group" 
      +"</select></td>"
      + "</tr>";

	if (instance == "Component")
		msg += "<tr>"
      + "<td>Insert as </td>"
      + "<td><select id=\"comp_instance\">"
      +"<option value=\"Component\" selected> Component" 
      +"<option value=\"Group\"> Group" 
      +"</select></td>"
      + "</tr>";
**/
	return msg;
}

Comps.colorSelect = function(){
	var msg = "<tr>"
      + "<td>Color</td>"
      + "<td><select id=\"comp_col\">"
      + "<option value=\"\" selected=\"selected\">no color</option>"
      + "<option value=\"aqua\">aqua</option>"
      + "<option value=\"black\">black</option>"
      + "<option value=\"blue\">blue</option>"
      + "<option value=\"fuchsia\">fuchsia</option>"
      + "<option value=\"green\">green</option>"
      + "<option value=\"gray\">gray</option>"
      + "<option value=\"lime\">lime</option>"
      + "<option value=\"maroon\">maroon</option>"
      + "<option value=\"navy\">navy</option>"
      + "<option value=\"olive\">olive</option>"
      + "<option value=\"purple\">purple</option>"
      + "<option value=\"red\">red</option>"
      + "<option value=\"silver\">silver</option>"
      + "<option value=\"teal\">teal</option>"
      + "<option value=\"white\">white</option>"
      + "<option value=\"yellow\">yellow</option>"
      + "</select></td>"      
      + "</tr>";
	return msg;
}

Comps.offsetSelect = function(){
	var msg = "<tr class=\"even\">"
      + "<td>Offset</td>"
      + "<td>"
      + "<input id=\"o_red\" value=\"0\" size=\"2\" style=\"text-align: center\"> <font color=\"red\">red</font> " 
      + "<input id=\"o_green\" value=\"0\" size=\"2\" style=\"text-align: center\"> <font color=\"green\">green</font> " 
      + "<input id=\"o_blue\" value=\"0\" size=\"2\" style=\"text-align: center\"> <font color=\"blue\">blue</font> "
			+ "<select id=\"o_unit\">"
      + "<option value=\"m\" selected=\"selected\">m</option>"
      + "<option value=\"dm\">dm</option>"
      + "<option value=\"cm\">cm</option>"
      + "<option value=\"mm\">mm</option>"
      + "<option value=\"in\">in</option>"
      + "<option value=\"ft\">ft</option>"
      + "</select>"
      + "</td>"
      + "</tr>";
	return msg;
}

Comps.rotate = function(axis){
		var degrees = $('degrees').value;
		var ax = "[1,0,0]"; 
		if (axis == "green") ax = "[0,1,0]"; 
		if (axis == "blue") ax = "[0,0,1]"; 
		var msg = ""
+ "model=Sketchup.active_model;"
+ "return unless model.selection[0];"
+	"point=model.selection[0].bounds.center;"
+	"model.start_operation(\"Rotate\");"
+ "model.selection.each{|selected| next unless selected.respond_to?(\"transformation\");"
+	"	point=selected.transformation.origin;"
+ "	tr=Geom::Transformation.rotation(point, "+ ax +"," + degrees + ".degrees);"
+ "	selected.transform! tr;};"
+	"model.commit_operation;";

		window.location='skp:insert@' + msg;
}

Comps.moveCopy = function(){
	var m_unit = $F('m_unit');
	var m_red = this.convert(m_unit, $F('m_red'));
	var m_green = this.convert(m_unit, $F('m_green'));
	var m_blue = this.convert(m_unit, $F('m_blue'));
	var m_copies = $F('m_copies');
	var msg = ""
+ "m_red = " + m_red + ";"
+ "m_green = " + m_green + ";"
+ "m_blue = " + m_blue + ";"
+ "m_copies = " + m_copies + ";"
+ "model=Sketchup.active_model;"
+ "return unless model.selection[0];"
+	"model.start_operation(\"Move\");"
+ "m_point = Geom::Point3d.new m_red,m_green,m_blue;"
+ "m_transform = Geom::Transformation.new m_point;"
+ "model.selection.each{|selected| next unless selected.respond_to?(\"transformation\");"
+ "	if m_copies > 0 ;"
+ "		if selected.typename == \"ComponentInstance\" ;"
+ "			n_transform = selected.transformation;"
+ "			compNew = model.active_entities.add_instance selected.definition, n_transform;"
+ "			compNew.transform! m_transform;"
+ "			n_transform = compNew.transformation;"
+ "			i = 0;"
+ "			while i < m_copies -1 do "
+ "				compNew = model.active_entities.add_instance selected.definition, n_transform;"
+ "				compNew.transform! m_transform;"
+ "				n_transform = compNew.transformation;"
+ "				i += 1;"
+ "			end;"
+ "		else;"
+ "			i = 0;"
+ "			selectedCopy = selected.copy;"
+ "			selectedCopy.name = selected.name;"
+ "			selectedCopy.transform! m_transform;"
+ "			while i < m_copies - 1 do "
+ "				selectedCopy = selectedCopy.copy;"
+ "				selectedCopy.name = selected.name;"
+ "				selectedCopy.transform! m_transform;"
+ "				i += 1;"
+ "			end;"
+ "		end;"
+ " else;"
+ "		selected.transform! m_transform;"
+ "	end;"
+ "};"
+	"model.commit_operation;"
+ "";
	window.location='skp:insert@' + msg;
}

Comps.moveToPosition = function(){
		var moved = 0;
		var target = 1;
		var msg = ""
						+ "model=Sketchup.active_model;"
						+ "if model.selection[1];"
						+ "	moved = model.selection[" + moved + "];"
						+ " transMoved = moved.transformation;"
						+ " moved.transform! transMoved.invert!;"
						+ "	target = model.selection[" + target + "];"
						+ " transTarget = target.transformation;"
						+ " moved.transform! transTarget;"
						+ " ok = UI.messagebox \"Correct object moved?\", MB_YESNO, \"\";"
						+ " if (ok == 7);"
						+ "  moved.transform! transTarget.invert!;"
						+ "  moved.transform! transMoved.invert!;"
						+ "	 moved = model.selection[" + target + "];"
						+ "  transMoved = moved.transformation;"
						+ "  moved.transform! transMoved.invert!;"
						+ "	 target = model.selection[" + moved + "];"
						+ "  transTarget = target.transformation;"
						+ "  moved.transform! transTarget;"
						+ " end;"
						+ "end;"
						+ "";
		window.location='skp:insert@' + msg;
}


Comps.helpLine = function(){
		var unit = $('helpline_unit').value;
		var red_from = this.convert(unit, $('helpline_red_from').value);
		var green_from = this.convert(unit, $('helpline_green_from').value);
		var blue_from = this.convert(unit, $('helpline_blue_from').value);
		var red_to = this.convert(unit, $('helpline_red_to').value);
		var green_to = this.convert(unit, $('helpline_green_to').value);
		var blue_to = this.convert(unit, $('helpline_blue_to').value);
		var msg = ""
						+ "p_from=Geom::Point3d.new(" + red_from + "," + green_from + "," + blue_from + ");"
						+ "p_to=Geom::Point3d.new(" + red_to + "," + green_to + "," + blue_to + ");"
						+ "model=Sketchup.active_model;"
						+ "entities = model.active_entities;"
						+ "line = entities.add_group;"
						+ "line.name = \"Line\";"
						+	"line.entities.add_line(p_from,p_to);"
						+ "if model.selection[0];"
						+ "	selected = model.selection[0];"
						+ " trans = selected.transformation;"
						+ " line.transform! trans;"
						+ "end;"
						+ "";

		window.location='skp:insert@' + msg;
}

Comps.helpHPlane = function(){
		var unit = $('hPlane_unit').value;
		var red = this.convert(unit, $('hPlane_red').value);
		var green = this.convert(unit, $('hPlane_green').value);
		var blue = this.convert(unit, $('hPlane_blue').value);
		var msg = ""
						+ "p1=Geom::Point3d.new(0,0," + blue + ");"
						+ "p2=Geom::Point3d.new(0," + green + "," + blue + ");"
						+ "p3=Geom::Point3d.new(" + red + "," + green + "," + blue + ");"
						+ "p4=Geom::Point3d.new(" + red + ",0," + blue + ");"
						+ "model=Sketchup.active_model;"
						+ "entities = model.active_entities;"
						+ "hplane = entities.add_group;"
						+ "hplane.name = \"Horizontal Plane\";"
						+ "hplane.entities.add_face [p1,p2,p3,p4];"
						+ "if model.selection[0];"
						+ "	selected = model.selection[0];"
						+ " trans = selected.transformation;"
						+ " hplane.transform! trans;"
						+ "end;"
						+ "";

		window.location='skp:insert@' + msg;
}

Comps.helpVPlane = function(pos){
	var msg = "";
	if (pos == "red") {
		var unit = $('vPlane_unit').value;
		var red = this.convert(unit, $('vPlane_red').value);
		var green = this.convert(unit, $('vPlane_green').value);
		var blue = this.convert(unit, $('vPlane_blue').value);
		msg = ""
					+ "p1=Geom::Point3d.new(" + red + ",0," + blue + ");"
					+ "p2=Geom::Point3d.new(" + red + "," + green + "," + blue + ");"
					+ "p3=Geom::Point3d.new(" + red + "," + green + ",0);"
					+ "p4=Geom::Point3d.new(" + red + ",0,0);"
					+ "model=Sketchup.active_model;"
					+ "entities = model.active_entities;"
					+ "vplane = entities.add_group;"
					+ "vplane.name = \"Vertical Plane\";"
					+ "vplane.entities.add_face [p1,p2,p3,p4];"
					+ "if model.selection[0];"
					+ "	selected = model.selection[0];"
					+ " trans = selected.transformation;"
					+ " vplane.transform! trans;"
					+ "end;"
					+ "";
	} else if (pos == "green") {
		var unit = $('vPlanegreen_unit').value;
		var red = this.convert(unit, $('vPlanegreen_red').value);
		var green = this.convert(unit, $('vPlanegreen_green').value);
		var blue = this.convert(unit, $('vPlanegreen_blue').value);
		msg = ""
					+ "p1=Geom::Point3d.new(0," + green + ",0);"
					+ "p2=Geom::Point3d.new("+ red + "," + green + ",0);"
					+ "p3=Geom::Point3d.new("+ red +","+ green +"," + blue + " );"
					+ "p4=Geom::Point3d.new(0," + green + "," + blue + ");"
					+ "model=Sketchup.active_model;"
					+ "entities = model.active_entities;"
					+ "vplane = entities.add_group;"
					+ "vplane.name = \"Vertical Plane\";"
					+ "vplane.entities.add_face [p1,p2,p3,p4];"
					+ "if model.selection[0];"
					+ "	selected = model.selection[0];"
					+ " trans = selected.transformation;"
					+ " vplane.transform! trans;"
					+ "end;"
					+ "";
	}
	window.location='skp:insert@' + msg;
}

Comps.addBox = function(){
		var unit = $('addBox_unit').value;
		var red = this.convert(unit, $('addBox_red').value);
		var green = this.convert(unit, $('addBox_green').value);
		var blue = this.convert(unit, $('addBox_blue').value);
		var msg = ""
						+ "p1=Geom::Point3d.new(0,0," + blue + ");"
						+ "p2=Geom::Point3d.new(0," + green + "," + blue + ");"
						+ "p3=Geom::Point3d.new(" + red + "," + green + "," + blue + ");"
						+ "p4=Geom::Point3d.new(" + red + ",0," + blue + ");"
						+ "model=Sketchup.active_model;"
						+ "entities = model.active_entities;"
						+ "box = entities.add_group;"
						+ "box.name = \"Box\";"
						+ "face = box.entities.add_face [p1,p2,p3,p4];"
						+ "face.pushpull " + blue + ", true;"
						+ "if model.selection[0];"
						+ "	selected = model.selection[0];"
						+ " trans = selected.transformation;"
						+ " box.transform! trans;"
						+ "end;"
						+ "";

		window.location='skp:insert@' + msg;
}


Comps.setColor = function(){
	var col = $F('col');
	if(col == "") {
		alert("No color selcted!");
		return;
	}
	var msg = ""
+ "model=Sketchup.active_model;"
+ "return unless model.selection[0];"
+ "color = Sketchup::Color.new \"" + col + "\";"
+ "model.selection.each { |selected|"
+ "	if selected.typename == \"Group\" then "
+ "		selected.entities.each { |entity| "
+ "			if entity.typename == \"Face\" then "
+ "   		entity.material = color;"
+ "   		entity.material.alpha = 0.99;"
+ " 		end;"
+	"  	};"
+ " elsif selected.typename == \"ComponentInstance\" then "
+ "		componentdefinition = selected.definition;"
+	"		entities = componentdefinition.entities;"
+ "		entities.each { |entity| "
+ "			if entity.typename == \"Face\" then "
+ "				entity.material = color; "
+ "				entity.material.alpha = 0.99;"
+ "			end;"
+ "		};"
+ " end;"
+ "};"
+ "";	
	window.location='skp:insert@' + msg;
}

Comps.makeComponent = function(){
	var msg = ""
+ "model=Sketchup.active_model;"
+ "return unless model.selection[0];"
+ "group = model.selection[0];"
+ "groupName = group.name;"
+ "comp = group.to_component;"
+ "comp.definition.name = groupName;"
+ "";
		window.location='skp:insert@' + msg;
}

Comps.addCompUrl = function(){
var compName =  $F('addCompUrl_compName');
var url = $F('addCompUrl_url');
var msg = ""
+ "class LoadHandler;"
+ " def initialize(name);"
+ "  @name = name;"
+ " end;"

+ " def onPercentChange(p);"
+ "  Sketchup::set_status_text(\"LOADING:    \" + p.to_i.to_s + \"%\");"
+ " end;"

+ " def onSuccess;"
+ "  p = Geom::Point3d.new(0,0,0);"
+ "  ptrans = Geom::Transformation.new p;"
+ "  definitions = Sketchup.active_model.definitions;"
+ "  if definitions[\"Model\"];"
+ "   loaded_definition = definitions[\"Model\"];"
+ "   loaded_definition.name= @name;"
+ "  else;" 
+ "   loaded_definition = definitions[@name];"
+ "  end;"
+ "  compNew = Sketchup.active_model.active_entities.add_instance(loaded_definition, ptrans);"
+ " end;"

+ " def onFailure(error_message);"
+ "  $last_error = error_message;"
+ "  Sketchup::set_status_text(\"Failed\");"
+ "  UI.messagebox \"Failed loading component!\";"
+ " end;"

+ "end;"

+ "url = \"" + url + "\";"
+ "compName = \"" + compName + "\";"
+ "load_handler = LoadHandler.new compName;"
+ "Sketchup.active_model.definitions.load_from_url url, load_handler;"
+ "";

    window.location='skp:insert@' + msg;
}

Comps.addAfterComps = function(){
  // overide by type or component
}






Comps.undo = function() {
		var msg = "Sketchup.undo";
		window.location='skp:insert@' + msg;
}

Comps.send_action = function(action) {
		var msg = "Sketchup.send_action(\"" + action + "\")";
		window.location='skp:insert@' + msg;
}
	
Comps.leaving = function(){
		alert("Note! You can return to this page by pressing Alt + left arrow keys.");
}

Comps.aboutUs = function(){
		if ($('aboutUs')) $('aboutUs').innerHTML = "<br />"
			+ "<h2>About the ToolBox</h2>" 
			+ "<p>We appreciate any comments and tips on how to make The Engineering ToolBox a better information source. "
			+ "Please contact us by email</p>" 
			+ "<p><ul><li> <a href=mailto:" + this.eMail + "?subject=" + escape(this.pURL + " : " + document.title) + ">" 
			+ this.eMail + "</a> </li></ul></p>"
			+ "<p>if You find any faults, inaccurate, or otherwise unacceptable information.</p>"
			+ "<p>The content in The Engineering ToolBox is <a href=\"javascript:Comps.noWarranty()\">copyrighted</a> "
			+ "but can be used with <a href=\"javascript:Comps.noWarranty()\">NO WARRANTY or LIABILITY</a>. "
			+ "Important information should always be double checked with alternative sources. All applicable national "
			+ "and local regulations and practices concerning this aspects must be strictly followed and adhered to.</p>"
			+ "<h2>Adding new Components?</h2>"
			+ "<p>If you want new components added to the SketchUp ToolBox - more flanges, construction structures - you name it, "
			+ "email us spreadsheets with component data. Use formats (rows and columns) more or less similar to existing components (if they exists) - "
			+ " and we will do our best to incorporate!<p>";
}

Comps.noWarranty = function(){
		var txt = "THIS CONTENT OF THIS WEBSITE CAN BE USED FREE OF CHARGE, "
			+ "BUT BE AWARE THAT THE CONTENT IS COPYRIGHTED "
			+ "AND ANY REDISTRIBUTION OR COPYING OF THE CONTENT IN ANY MEDIA IS PROHIBITED."
			+ "SINCE THE CONTENT IS FREE OF CHARGE THERE IS NO WARRANTY OR LIABILITY OF ANY KIND. "
			+ "THE ENTIRE RISK OF USING THE CONTENT IS WITHIN YOU. SHOULD THE CONTENT PROVE FAULTY, "
			+ "INACCURATE, OR OTHERWISE UNACCEPTABLE, YOU ASSUME THE COST OF ALL NECESSARY REPAIR OR CORRECTIONS.";
		alert(txt); 
} 

Comps.advertise = function(){
		if ($('advertise')) $('advertise').innerHTML = "<br />"
			+ "<h2>Advertise in the ToolBox</h2>"  
			+ "<p>If you want to promote your products or services in this site - please follow " 
			+ "<a href=\"https://adwords.google.com/\">this link</a>.</p>";
		if ($('adLink')) $('adLink').innerHTML = "<p><a href=\"#advertise\"><b>Advertise</b> on this site</a></p>";
}

Comps.missing = function(){
		if ($('missing')) $('missing').innerHTML = "<br />"
			+ "<h2>Something Missing?</h2>"  
			+ "<p>If you miss something in the ToolBox - or want some components added - please contact us by email. " 
			+ "If you can add documentation like property tables or similar for the components - it would be very helpful.</p>";
}

Comps.rounding = function(n){
		var t = 1;
			if (n > 0.000001){
			if (n < 100) t = 10;
			if (n < 10) t = 100;
			if (n < 0.1) t = 10000;
			if (n < 0.01) t = 100000;
			if (n < 0.001) t = 1000000;
			if (n < 0.0001) t = 10000000;
			if (n < 0.00001) t = 100000000;
			if (n < 0.000001) t = 1000000000;
			if (n < 0.0000001) t = 10000000000;
			if (n < 0.00000001) t = 100000000000;
			if (n < 0.000000001) t = 1000000000000;
			if (n < 0.0000000001) t = 10000000000000;
			if (n < 0.00000000001) t = 100000000000000;
			if (n < 0.000000000001) t = 1000000000000000;
			return Math.round(n * t) / t;
		} else if (n < - 0.000001){
			if (n < - 0.00001) t = 100000000;
			if (n < - 0.0001) t = 10000000;
			if (n < - 0.001) t = 1000000;
			if (n < - 0.01) t = 100000;
			if (n < - 0.1) t = 10000;
			if (n < - 10) t = 100;
			if (n < - 100) t = 10;
			return Math.round(n * t) / t;
		} else {
			return n;
	}
}






