$(function() {
	//$('select').combobox();
	
	var form = $("form");
	if (form.length) {
		form.cssCheckbox();
	}
});

function show_popup(id, link_id) {
	var el = $('#'+id);
	if (typeof(link_id) != 'undefined') {
		var el_link = $('#'+link_id);
		el.css('top', el_link.position().top);
	}

	el.parents('.fon').show();

	var h = el.outerHeight() + parseInt(el.css('top'));
	if ($('body').outerHeight() < h) {
		$('body').height(h + 10);
	}
	
	$(window).scrollTop(parseInt(el.css('top')));
}
function hide_popup(id) {
	$('body').height('auto');
	$('#'+id).parents('.fon').hide();
}

function on(id, img, img_comman) {
	var div = document.getElementById(id);
	//div.style.fontSize="19px";
	div.style.fontWeight="bold";

	var image = document.getElementById('preview_img');
	image.setAttribute('src',img);

	var image_show = document.getElementById('preview_img_show');
	
	image_show.style.display = 'inline';
	div.onmouseout = function() {
		image_show.style.display = 'none';
		//div.style.fontSize="14px";
		div.style.fontWeight="normal";
		image.setAttribute('src',img_comman);
	}

	image_show.style.left = div.offsetWidth - image_show.offsetWidth/2 + 'px';
	image_show.style.top = div.offsetTop - (image_show.offsetHeight - div.offsetHeight)/2 + 'px';
}
function parse_color(color) {
	if (color) {
		var size = color.length;
		var rgb = new Array(3);
		switch (size) {
			case 3:
				for (var i=0; i<3; ++i) {
					rgb[i] = parseInt(color.charAt(i)+color.charAt(i),16);
				}
				return rgb;
			case 6:
				for (var i=0; i<3; ++i) {
					rgb[i] = parseInt(color.charAt(i*2)+color.charAt(i*2+1),16);
				}
				return rgb;
			default:
				return 0;
		}
	}
	else {
		return 0;
	}
}

function rebild_graph(val01,val02,min,max,norm) {
	var graph_height = 110;

	if (!norm) norm = 0;
	var norm_img = document.getElementById('norm');
	norm_img.style.top = graph_height - graph_height/(max-min)*norm + 'px';

	var fac01 = document.getElementById('factory01');
	var fac02 = document.getElementById('factory02');
	fac01.style.top = graph_height - graph_height/(max-min)*val01 + 'px';
	fac02.style.top = graph_height - graph_height/(max-min)*val02 + 'px';

	fac01.firstChild.nodeValue = val01+' ';
	fac02.firstChild.nodeValue = val02+' ';
	
	var img01 = fac01.childNodes[1];
	var img02 = fac02.childNodes[1];

	var color01 = parse_color('00ff00');
	var color02 = parse_color('ff0000');
	var res01 = new Array(3);
	var res02 = new Array(3);
	var a01 = (val01-min)/(max-min);
	var a02 = (val02-min)/(max-min);
	
	var color_new01 = '#';
	var color_new02 = '#';
	for (var i=0; i<3; ++i) {
		res01[i] = Math.round(a01*color02[i]+(1.0-a01)*color01[i]).toString(16);
		res02[i] = Math.round(a02*color02[i]+(1.0-a02)*color01[i]).toString(16);
		if(res01[i].length == 1) {
			res01[i] = '0'+res01[i];
		}
		color_new01 += res01[i];
		if(res02[i].length == 1) {
			res02[i] = '0'+res02[i];
		}
		color_new02 += res02[i];
	}
	fac01.style.color = color_new01;
	fac02.style.color = color_new02;
	
	img01.style.background = color_new01;
	img02.style.background = color_new02;
}

function submit_fcalc(el) {
	form = $(el).parents('form');
	$('#fcalc #name').val(form.find('#mpname').val());
	$('#fcalc #mobile').val(form.find('#mpmobile').val());
	submit_form('fcalc');
	hide_popup('calc');
	$('#fcalc #name').val('');
	$('#fcalc #mobile').val('');
}

function calc(formula) {

	var price = formula().toFixed(2);
	if (price > 0) {
		$('#price').html(price);
		$('#sprice').val(price);
		show_popup('calc');
	}
	else {
		alert("Поверьте правильность заполнения всех полей");
	}
}

