function writeEmail(name) {
	document.write(unescape('%3C'+'a class="eml" hr'+'ef="&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+
	name+'&#64;vguard&#46;ru"%3E'+name+'&#64;vguard&#46;ru%3C/'+'a%3E'));
};
function writeEmail0(name) {
	document.write(unescape('%3C'+'a hr'+'ef="&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+name+
	'&#64;vguard&#46;ru"%3E'+name+'&#64;vguard&#46;ru%3C/'+'a%3E'));
};
function writeEmail2(name,host,host2) {
	document.write(unescape('%3C'+'a hr'+'ef="&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+name+
	'&#64;'+host+'&#46;'+host2+'"%3E'+name+'&#64;'+host+'&#46;'+host2+'%3C/'+'a%3E'));
};

function formatMoney(value, zeroCent, groupDigits) {
	
	var zeroCentPattern = /^\d+\.00$/;
    var groupDigitsPattern = /(\d)((\d\d\d)+\b)/g; 

    var neg = value < 0;

    value = Math.abs(value).toFixed(2);
    if (zeroCent) {
        if (zeroCentPattern.test(value)) {
            value = value.substr(0, value.length - 3);
        }
    }
    if (groupDigits) {
        while (groupDigitsPattern.test(value)) {
            value = value.replace(groupDigitsPattern, '$1&nbsp;$2');
        }
    }
    return (neg ? '-&nbsp;' : '') + value;
};


var VID = {
	cname: 'имя',
	cemail: 'e-mail',
	ctxt: 'текст сообщения',
	priceCamExt: [1200, 1700],
	priceCamInt: [800, 1500],
	priceServ: [
		0,		// 0
		10000,	// 1
		11000,	// 2
		12000,	// 3
		14000,	// 4
		15000,	// 5
		17000,	// 6
		19000,	// 7
		20000,	// 8
		23000,	// 9
		25000	// 10
	],
	priceServEcon: [
		0,		// 0
		8000,	// 1
		9000,	// 2
		10000,	// 3
		11000,	// 4
		12000,	// 5
		13000,	// 6
		13500,	// 7
		14000,	// 8
		16000,	// 9
		16500,	// 10
		17000,	// 11
		17500,	// 12
		18000,	// 13
		18500,	// 14
		19000,	// 15
		19500,	// 16
		22000,	// 17
		23000,	// 18
		24000,	// 19
		25000	// 20
	],
	priceServMax: 10,
	priceServMaxEcon: 20,
	priceLicense: {
		year: {
			p1: 5000,
			p2: 4000,
			p4: 3200,
			p8: 2600,
			p16: 2100,
			p32: 1700,
			p64: 1400,
			p100: 1100
		}
	},
	pcalc: {
		cams: 1,
		type: 0,
		cat: 0,
		serv: 0,
		res: false,
		done: false
	},
	calc: function() {
		
		VID.pcalc.cams = $('#pccams').val();
		
		var cams = VID.pcalc.cams;
		var ret = {};
		
		ret.cams = cams * ((VID.pcalc.type == 1) ? VID.priceCamExt[VID.pcalc.cat] : VID.priceCamInt[VID.pcalc.cat]);
		
		ret.serv = 0;
		
		var sleft = cams;
		while(sleft > ((VID.pcalc.serv == 1) ? VID.priceServMax : VID.priceServMaxEcon)) {
			ret.serv += (VID.pcalc.serv == 1) ? VID.priceServ[VID.priceServMax] : VID.priceServEcon[VID.priceServMax];
			sleft -= (VID.pcalc.serv == 1) ? VID.priceServMax : VID.priceServMaxEcon;
		}
		ret.serv += (VID.pcalc.serv == 1) ? VID.priceServ[sleft] : VID.priceServEcon[sleft];
		
		ret.lic = -1;
		if(cams >= 100) {
			ret.lic = cams * VID.priceLicense.year.p100;
		} else if(cams >= 64) {
			ret.lic = cams * VID.priceLicense.year.p64;
		} else if(cams >= 32) {
			ret.lic = cams * VID.priceLicense.year.p32;
		} else if(cams >= 16) {
			ret.lic = cams * VID.priceLicense.year.p16;
		} else if(cams >= 8) {
			ret.lic = cams * VID.priceLicense.year.p8;
		} else if(cams >= 4) {
			ret.lic = cams * VID.priceLicense.year.p4;
		} else if(cams >= 2) {
			ret.lic = cams * VID.priceLicense.year.p2;
		} else if(cams > 0) {
			ret.lic = cams * VID.priceLicense.year.p1;
		}
		
		ret.total = (ret.lic == -1) ? -1 : ret.lic + ret.serv + ret.cams; 
		
		VID.pcalc.done = true;
		
		VID.pcalc.res = ret;
		VID.calcshow();
	},
	calcshow: function() {
		
		var res = VID.pcalc.res;
		
		$('#pcc').html(formatMoney(res.cams, true, true));
		$('#pcs').html(formatMoney(res.serv, true, true));
		$('#pcl').html(formatMoney(res.lic, true, true));
		$('#pct').html(formatMoney(res.total, true, true));
		
		$('#tcalc .hid').fadeIn();
		$('.acalc a').html('Пересчитать').blur();
	}
};

$(document).ready(function() {
	
	setupZoom();
	//$('a.fz').fancyZoom({scaleImg: true, closeOnClick: true, directory: '/s/fzoom/i/'}); 
	//$('a.fz').fancyZoom({directory: '/s/fzoom/i'});
	
	/*
	$('#form_name,#form_email,#form_txt').click(function() {
		if($(this).val() == VID.cname || $(this).val() == VID.cemail || $(this).val() == VID.ctxt) $(this).val('');
	});
	
	$('#form_name').blur(function() {
		if($(this).val() == '') $(this).val(VID.cname);
	});
	$('#form_email').blur(function() {
		if($(this).val() == '') $(this).val(VID.cemail);
	});
	$('#form_txt').blur(function() {
		if($(this).val() == '') $(this).val(VID.ctxt);
	});
	
	
	$('#form_sub').click(function() {
		
		if(typeof pageTracker != "undefined") {
			pageTracker._trackPageview('/ajax/sendmessage')
		};
		
		$.post("/controller?cmd=msg", { 
			name: ($('#form_name').val() == VID.cname) ? '' : $('#form_name').val(),
			email: $('#form_email').val(),
			txt: $('#form_txt').val()
			
		}, function(i) {
			if(i && i.res == 'ok') {
				alert('Ваше сообщение отправлено!');
			} else {
				alert('При отправке сообщения произошла ошибка. Попробуйте повторить попытку позднее.');
			}
		}, 'json');
		return false; 
	});
	
	*/
	
	$('#umenu li,#urmenu li')
		.mouseover(function() {$(this).addClass('cur')})
		.mouseout(function() {$(this).removeClass('cur')})
	;	
	
	$('tr.even,tr.odd')
		.mouseover(function() {$(this).addClass('cur')})
		.mouseout(function() {$(this).removeClass('cur')})
	;
	
	/*
	$('.pcalcasd').click(function(){
		
		$('<div id="dcalc">'+
			'<a name="pcalctop"></a>'+
			'<h1>Калькулятор стоимости</h1>'+
			'<table id="tcalc" cellspacing="0">'+
			'<tr><td class="tl">Количество видеокамер:</td><td class="ti"><input type="text" value="" id="pccams" /></td></tr>'+
			'<tr><td colspan="2" class="acalc"><a href="#">Рассчитать</a></td></tr>'+
			'<tr class="hid"><td>Стоимость видеокамер:</td><td class="tv" id="pcc">14 200</td></tr>'+
			'<tr class="hid"><td>Стоимость видеосерверов:</td><td class="tv" id="pcs">26 400</td></tr>'+
			'<tr class="hid"><td>Стоимость лицензии на 1 год:</td><td class="tv" id="pcl">16 700</td></tr>'+
			'<tr class="hid"><td colspan="2" id="totalsp"> </td></tr>'+
			'<tr class="trttl hid"><td>Итого:</td><td class="tv" id="pct">124 200</td></tr>'+
			'</table>'+
			'</div>')
			.appendTo('body');
	*/
			
		$('.acalc a').click(function() {
			VID.calc();			
			return false;
		});
		
		$('#tcalc .ti a').click(function() {			
			$('#tcalc .ti input').val($(this).html());			
			$(this).blur(); if(VID.pcalc.done) VID.calc();
			return false;
		});
		
		$('#typeext').click(function() {
			$('#typeint').html('<a href="#">внутренние</a>');
			$('#typeext').html('уличные');
			VID.pcalc.type = 1; if(VID.pcalc.done) VID.calc();
			return false;
		});		
		$('#typeint').click(function() {
			$('#typeint').html('внутренние');
			$('#typeext').html('<a href="#">уличные</a>');
			VID.pcalc.type = 0; if(VID.pcalc.done) VID.calc();
			return false;
		});
		$('#typeec').click(function() {
			$('#typeec').html('эконом');
			$('#typehigh').html('<a href="#">высокого разрешения</a>');
			VID.pcalc.cat = 0; if(VID.pcalc.done) VID.calc();
			return false;
		});
		$('#typehigh').click(function() {
			$('#typeec').html('<a href="#">эконом</a>');
			$('#typehigh').html('высокого разрешения');
			VID.pcalc.cat = 1; if(VID.pcalc.done) VID.calc();
			return false;
		});
		$('#servfull').click(function() {
			$('#servfull').html('полное');
			$('#servecon').html('<a href="#">экономичное</a>');
			VID.pcalc.serv = 1; if(VID.pcalc.done) VID.calc();
			return false;
		});
		$('#servecon').click(function() {
			$('#servfull').html('<a href="#">полное</a>');
			$('#servecon').html('экономичное');
			VID.pcalc.serv = 0; if(VID.pcalc.done) VID.calc();
			return false;
		});
		
		//$('#dcalc').css('left',$('#dhead').offset().left+40)
			//.fadeIn();
			
		//document.location.href='#pcalctop';
		
		$('#pccams').focus();
		
		//return false;
	//});
	
	//$('.pcalc').click();
});

