/**
/**
 * Face constructor representation data type
 */
FaceConstructor = function ( div_id, width )
{
	this.myMainDivId = div_id;
	this.myWidth = width - 150;
	
	this.model_a = SettingsFaceMetrics.default_a;
	this.model_b = SettingsFaceMetrics.default_b;
	
	this.loadParams();
	this.start();
}

// Adding methods
mixin(FaceConstructor.prototype, {
	/**
	 * Loading params function
	 */
	loadParams: function ()
	{
		this.faceChoices = SettingsFaceChoices;
		this.faceParamsChoices = SettingsFaceParamsChoices;
	},
	
	/**
	 * Starting application function
	 */
	start: function ()
	{
		cache_images = [
			"Doors/frames/11.jpg",
			"Doors/frames/125.jpg",
			"Doors/frames/126.jpg",
			"Doors/frames/12.jpg",
			"Doors/frames/14.jpg",
			"Doors/frames/13.jpg",
			"Doors/frames/18.jpg",
			"Doors/frames/15.jpg",
		];
		
		for (var i in cache_images)
		{
			var img = new Image();
			img.src = cache_images[i];
		}
		
		this.selectChoiceFullTree(0);
	},
	
	/**
	 * Generating containers for data function
	 */
	generateContainers: function ( select_wood_txt, select_glass_txt, description )
	{
		$("#" + this.myMainDivId).html("");

		this.generatePath(this, this.myMainDivId);

		$("#" + this.myMainDivId).append("<table><tr><td valign='top' style='min-width: 124px;'><div id='face_img_div'><img id='face_img' src='" + rootPath + "Images/Doors/loading.jpg' /></div></td><td id='face_controls' valign='top' style='padding-left: 15px;'></td></tr></table>");
		this.myFaceImgId = "face_img";
		this.myFaceImgDivId = "face_img_div";
		
		$("#face_controls").append("<table cellpadding='0' cellspacing='0'><tr><td style='font-size: 12px; padding-right: 10px;' id='model_info'></td><td style='width: 1px; background: #AAA;'></td><td style='font-size: 12px; padding-left: 10px; padding-right: 10px;' id='model_size'></td><td style='width: 1px; background: #AAA;'></td><td style='font-size: 12px; padding-left: 10px; height: 43px;' id='model_price'></td></tr></table><br>");
		$("#model_size").append("<input id='model_a' style='width: 50px;'> X ");
		$("#model_size").append("<input id='model_b' style='width: 50px;'><br>");
		$("#model_size").append("(В х Ш, мм)");
		$("#model_a").attr("value", this.model_a);
		$("#model_b").attr("value", this.model_b);

		$("#face_controls").append("<div style='margin-bottom: 10px; font-size: 12px; width: " + this.myWidth + "px;'>" + description + "</div>");
		$("#face_controls").append("<span class='right_span'></span>");

		if (this.faceParams.models.length > 1)
		{
			$("#face_controls").append("<li style='padding-top: 8px;'><a href='javascript:void(0)' id='select_face_link' class='constructor_link_select'>Выбрать модель</a></li>");
			$("#face_controls").append("<div id='select_face_choices' style='margin-bottom: 10px; width: " + this.myWidth + "px;'></div>");
		}

		$("#face_controls").append("<span class='right_span'></span>");
		
		if (this.faceParams.wood_types.length > 1)
		{
			$("#face_controls").append("<li style='padding-top: 8px;'><a href='javascript:void(0)' id='select_wood_link' class='constructor_link_select'>" + select_wood_txt + "</a></li>");
			$("#face_controls").append("<div id='select_wood_choices' style='margin-bottom: 5px; width: " + this.myWidth + "px;'></div>");
		}
		
		$("#face_controls").append("<li style='padding-top: 8px;' id='select_glass_span'><span><a href='javascript:void(0)' id='select_glass_link' class='constructor_link_select'>" + select_glass_txt + "</a></span></li>"); // (или <input type='checkbox' id='face_closed'> глухой фасад)</span>");
		$("#face_controls").append("<div id='select_glass_choices' style='width: " + this.myWidth + "px;'></div>");
	},
	
	/**
	 * Generating path function
	 */
	generatePath: function ( constructor, container )
	{
		$("#" + container).append("<a id='pl_start' class='constructor_link_path' href='javascript:void(0)'>Другой тип фасада</a><br><br>");
		var constructor = this;
		$("#pl_start").click(function () {
			unblockElement(constructor.myFaceImgDivId);
			constructor.selectChoiceFullTree(0);
		});
	},
	
	/**
	 * Selecting choice function
	 */
	selectChoice: function ( path )
	{
		var choices = this.faceChoices;
		for (var i in path)
			choices = choices[path[i]].choices;
		
		var id = 0;
		
		$("#" + this.myMainDivId).html("");
		
		this.generatePath(path, this, this.myMainDivId);
		
		for (var i in choices)
		{
			var choice = choices[i];
			$("#" + this.myMainDivId).append("<li style='color: #CE161E;'><a class='constructor_link' href='javascript:void(0)' id='cl_" + id + "'>" + choice.title + "</a></li><div style='padding-left: 15px; line-height: 1.4; padding-top: 8px;'>" + choice.description + "</div><br>");
			var constructor = this;
			new function ( id ) {
				$("#cl_" + id).click(function () {
					path[path.length] = id;
					if (choices[id].choices)
						constructor.selectChoice(path);
					else
						constructor.modelling(path);
				});
			}(id);
			id++;
		}
	},
	
	/**
	 * Select choice full tree function
	 */
	selectChoiceFullTree: function ( shift, choices )
	{
		if (typeof(choices) == "undefined")
		{
			choices = this.faceChoices;
			$("#" + this.myMainDivId).html("<table width='100%'><tr><td id='menu_tree' valign='top'></td><td id='menu_img' valign='top'></td></tr></table>");
			link_id = 0;
		}

		var shift_str = "";
		for (var i = 0; i < shift * 4; i++)
			shift_str += "&nbsp;";
		
		for (var i in choices)
		{
			choice = choices[i];
			
			if (typeof(choice.choices) != "undefined")
			{
				$("#menu_tree").append(shift_str + "<span class='constructor_link'>" + choice.title + "</span><br>");
				this.selectChoiceFullTree(shift + 1, choice.choices);
			}
			else
			{
				$("#menu_tree").append(shift_str + "<a class='constructor_link' href='javascript:void(0)' id='cl_" + link_id + "'>" + choice.title + "</a><br>");

				var constructor = this;
				new function ( id, choice ) {
					$("#cl_" + id).click(function () {
						constructor.modelling(choice);
					});
				}(link_id, choice);
				new function ( id, choice ) {
					$("#cl_" + id).mouseover(function () {
						if (choice.image)
							$("#menu_img").html("<img src='" + rootPath + "Images/Doors/" + choice.image + "'>");
					});
				}(link_id, choice);
				new function ( id, choice ) {
					$("#cl_" + id).mouseout(function () {
						if (choice.image)
							$("#menu_img").html("");
					});
				}(link_id, choice);
			}

			link_id++;
		}
		
		if (shift == 0 && $.browser.msie) {
			$("#menu_tree").append("<div style='height: 10px'></div>");
		}
	},
	
	/**
	 * Generating dace params function
	 */
	generateFaceParams: function ( model_description )
	{
		this.faceParams = [];
		
		this.faceParams.price = model_description.price;
		this.faceParams.min_price = model_description.min_price;

		this.faceParams.models = [];
		for (var i in model_description.models)
		{
			model_ind = model_description.models[i];
			this.faceParams.models[this.faceParams.models.length] = this.faceParamsChoices.models[model_ind];
		}
		
		this.faceParams.wood_types = [];
		for (var i in model_description.wood_types)
		{
			wood_type = model_description.wood_types[i];
			this.faceParams.wood_types = this.faceParams.wood_types.concat(this.faceParamsChoices.wood_types[wood_type]);
		}
		
		this.faceParams.glass_types = [];
		if (typeof(model_description.glass_types) == "undefined")
			model_description.glass_types = ["simple"];
		for (var i in model_description.glass_types)
		{
			glass_type = model_description.glass_types[i];
			this.faceParams.glass_types = this.faceParams.glass_types.concat(this.faceParamsChoices.glass_types[glass_type]);
		}
		
		this.faceParamsChoices;
	},
	
	/**
	 * Starting modelling function
	 */
	modelling: function ( model_description )
	{
		this.generateFaceParams(model_description);

		var model = new FaceModel(this.faceParams);
		model.myBorderType = model_description.border_type;
		
		this.generateContainers(model_description.select_wood_txt, model_description.select_glass_txt, model_description.description);
		if (this.faceParams.wood_types.length > 1)
			this.generateSelectWood(model);
		this.generateSelectGlass(model);
		if (this.faceParams.models.length > 1)
			this.generateSelectFace(model);
			
		var constructor = this;
		$("#model_a").keydown(function () {
			$("#price_calc").show();
		});
		$("#model_b").keydown(function () {
			$("#price_calc").show();
		});
		$("#model_a").blur(function () {
			constructor.model_a = parseInt($("#model_a").attr("value"));
			if (constructor.model_a != $("#model_a").attr("value") || constructor.model_a < SettingsFaceMetrics.min_a)
			{
				constructor.model_a = SettingsFaceMetrics.min_a;
				$("#model_a").attr("value", SettingsFaceMetrics.min_a);
			}
			else if (constructor.model_a > SettingsFaceMetrics.max_a)
			{
				constructor.model_a = SettingsFaceMetrics.max_a;
				$("#model_a").attr("value", SettingsFaceMetrics.max_a);
			}
			constructor.setPrice(model);
		});
		$("#model_b").blur(function () {
			constructor.model_b = parseInt($("#model_b").attr("value"));
			if (constructor.model_b != $("#model_b").attr("value") || constructor.model_b < SettingsFaceMetrics.min_b)
			{
				constructor.model_b = SettingsFaceMetrics.min_b;
				$("#model_b").attr("value", SettingsFaceMetrics.min_b);
			}
			else if (constructor.model_b > SettingsFaceMetrics.max_b)
			{
				constructor.model_b = SettingsFaceMetrics.max_b;
				$("#model_b").attr("value", SettingsFaceMetrics.max_b);
			}
			constructor.setPrice(model);
		});

		this.correctClosed(model);
		
		var constructor = this;
		setTimeout(function () { constructor.reloadImg(model); }, 30);
	},
	
	/**
	 * Reloading model image function
	 */
	reloadImg: function ( model )
	{
		blockElement(this.myFaceImgDivId);
//		$("#" + this.myFaceImgId).attr("src", "../Images/Doors/loading.jpg");

		$("#model_info").html("");

//		$("#model_info").append("Модель: " + this.faceParams.models[model.myModel].description + "<br>");

		$("#model_info").append("Рамка: " + model.myBorderType + "<br>");

		var closed = this.faceParams.models[model.myModel].closed;
		if (!closed)
			$("#model_info").append("Наполнение: " + this.faceParams.glass_types[model.myGlassType].description);
		
		this.setPrice(model);

		var my_face_id = this.myFaceImgDivId;
		$("#" + this.myFaceImgId).attr("src", model.imageSrc()).load(function () {
			unblockElement(my_face_id);
		});
	},
	
	/**
	 * Selecting model function
	 */
	generateSelectFace: function ( model )
	{
		// Toggle container with images
//		$("#select_face_link").click(function () {
//			$("#select_face_choices").toggle(250);
//			$("#select_wood_choices").hide(250);
//			$("#select_glass_choices").hide(250);
//		});
		
		// Adding images into container
		for (var i in this.faceParams.models)
		{
			var id = "model_" + i;
			var height = $.browser.msie ? 118 : 115;
			
			// Adding images
			$("#select_face_choices").append("<div id='face_div_" + i + "' style='float: left; font-size: 12px; margin-left: 10px; margin-top: 10px; height: " + height + "px;'><img style='height: 100px; width: 50px; cursor: pointer;' src='" + rootPath + "Images/Doors/frames/" + this.faceParams.models[i].light + "' id='" + id + "'><br>" + this.faceParams.models[i].name + "</div>");
			// Adding functionnality
			constructor = this;
			new function ( id ) {
				$("#model_" + id).mouseover(function () {
					$(this).css("opacity", 0.5);
				});
				$("#model_" + id).mouseout(function () {
					$(this).css("opacity", 1);
				});
				$("#model_" + id).click(function () {
					if (id != model.myModel)
					{
						$("#model_" + model.myModel).css("border", "0px");
						model.myModel = id;
						$("#model_" + model.myModel).css("border", "2px solid red");
//						$("#select_face_choices").hide(250);
						constructor.correctClosed(model, true);
					}
				});
			}(i)
		}

		$("#model_" + model.myModel).css("border", "2px solid red");
		this.correctClosed(model);
	},
	
	/**
	 * Selecting glass function
	 */
	generateSelectGlass: function ( model )
	{
		// Toggle container with images
//		$("#select_glass_link").click(function () {
//			$("#select_glass_choices").toggle(250);
//			$("#select_wood_choices").hide(250);
//			$("#select_face_choices").hide(250);
//		});
		
		// Adding images into container
		for (var i in this.faceParams.glass_types)
		{
			var id = "glass_" + i;
			var height = $.browser.msie ? 118 : 115;
			
			// Adding images
			$("#select_glass_choices").append("<div id='glass_div_" + i + "' style='float: left; font-size: 12px; margin-left: 10px; margin-top: 10px; height: " + height + "px;'><img style='height: 100px; width: 50px; cursor: pointer; border: 1px solid black;' src='" + rootPath + "Images/Doors/glass/" + this.faceParams.glass_types[i].file_name + "' id='" + id + "'><br>" + this.faceParams.glass_types[i].name + "</div>");
			// Adding functionnality
			constructor = this;
			new function ( id ) {
				$("#glass_" + id).mouseover(function () {
					$(this).css("opacity", 0.5);
				});
				$("#glass_" + id).mouseout(function () {
					$(this).css("opacity", 1);
				});
				$("#glass_" + id).click(function () {
					if (id != model.myGlassType)
					{
						$("#glass_" + model.myGlassType).css("border", "1px solid black");
						model.myGlassType = id;
						$("#glass_" + model.myGlassType).css("border", "2px solid red");
//						$("#select_glass_choices").hide(250);
						$("#face_closed").attr("checked", false);
						model.myClosed = false;
						constructor.reloadImg(model);
					}
				});
			}(i)
		}

		$("#glass_" + model.myGlassType).css("border", "2px solid red");
		
		// Closed face checkbox
		constructor = this;
		$("#face_closed").click(function () {
			model.myClosed = $("#face_closed").attr("checked");
			constructor.reloadImg(model);
		});
	},
	
	/**
	 * Selecting wood function
	 */
	generateSelectWood: function ( model )
	{
		// Toggle container with images
//		$("#select_wood_link").click(function () {
//			$("#select_wood_choices").toggle(250);
//			$("#select_face_choices").hide(250);
//			$("#select_glass_choices").hide(250);
//		});
		
		// Adding images into container
		for (var i in this.faceParams.wood_types)
		{
			var id = "wood_" + i;
			var height = $.browser.msie ? 118 : 115;
			
			// Adding images
			$("#select_wood_choices").append("<div id='wood_div_" + i + "' style='float: left; font-size: 12px; margin-left: 10px; margin-top: 10px; height: " + height + "px;'><img style='height: 100px; width: 50px; cursor: pointer; border: 1px solid black;' src='" + rootPath + "Images/Doors/wood/" + this.faceParams.wood_types[i].file_name + "' id='" + id + "'><br>" + this.faceParams.wood_types[i].name + "</div>");
			// Adding functionnality
			constructor = this;
			new function ( id ) {
				$("#wood_" + id).mouseover(function () {
					$(this).css("opacity", 0.5);
				});
				$("#wood_" + id).mouseout(function () {
					$(this).css("opacity", 1);
				});
				$("#wood_" + id).click(function () {
					if (id != model.myWoodType)
					{
						$("#wood_" + model.myWoodType).css("border", "1px solid black");
						model.myWoodType = id;
						$("#wood_" + model.myWoodType).css("border", "2px solid red");
//						$("#select_wood_choices").hide(250);
						constructor.reloadImg(model);
					}
				});
			}(i)
		}

		$("#wood_" + model.myWoodType).css("border", "2px solid red");

		$("#select_wood_choices").append("<span class='right_span'></span>");
	},
	
	/**
	 * Correcting controls for closed faces function
	 */
	correctClosed: function ( model )
	{
		var closed = this.faceParams.models[model.myModel].closed;
		
		// Show/hide glass selection
		if (closed)
		{
			$("#select_glass_choices").hide();
			$("#select_glass_span").hide(250);
		}
		else
			$("#select_glass_span").show(250);
		
		// Showing additional textures
		for (var i in this.faceParams.wood_types)
		{
			var id = "wood_div_" + i;
			
			if (this.faceParams.wood_types[i].closed)
			{
				if (this.faceParams.models[model.myModel].featured)
					$("#" + id).show();
				else
					$("#" + id).hide();
			}
		}
		
		// Correcting selected wood type
		
		if (!closed && this.faceParams.wood_types[model.myWoodType].closed)
		{
			model.myWoodType = 0;
			$("#wood_1").css("border", "2px solid red");
		}
		
		// Reloading image
		if (arguments[1])
			this.reloadImg(model);
	},
	
	setPrice: function ( model )
	{
		coef = this.faceParams.glass_types[model.myGlassType].price_coef || 1;
		price = this.faceParams.price(this.model_a / 1000, this.model_b / 1000, coef);
		if (price < this.faceParams.min_price)
			price = this.faceParams.min_price;
		$("#model_price").html("Стоимость:<br><span style='font-size: 16px; font-weight: bold;'>" + Math.round(price) + " р.</span><div id='price_calc' style='font-size: 10px;'><a href='javascript:void(0)' style='color: #444;'>обновить</a></div>");
		$("#price_calc").hide("");
	}
});
