$(document).ready(function(){
	
	var ftop = $('div.main ul p a:eq(0)').offset().top;
	
	$('div.lamp a').hide();
	$('div.main ul p a').click(function(){
		if (!$(this).parents('li').hasClass('active')){
			$('div.scroll span').animate({
				top: $(this).offset().top-ftop
			},500);

			$('div.main ul li').removeClass('active');
			$(this).parents('li').addClass('active');

			if ($(this).attr('href')=='#lamp-cold'){
				$('div.lamp a.cold').appendTo('div.lamp');
				$('div.lamp a.cold').css('opacity', 0);
				$('div.lamp a.cold').show().animate({'opacity':1},500);
			}
			if ($(this).attr('href')=='#lamp-warm'){
				$('div.lamp a.warm').appendTo('div.lamp');
				$('div.lamp a.warm').css('opacity', 0);
				$('div.lamp a.warm').show().animate({'opacity':1},500);
			}
			if ($(this).attr('href')=='#lamp-daily'){
				$('div.lamp a.daily').appendTo('div.lamp');
				$('div.lamp a.daily').css('opacity', 0);
				$('div.lamp a.daily').show().animate({'opacity':1},500);
			}
			if ($(this).attr('href')=='#lamp-off'){
				$('div.lamp a').not(':last').hide();
				$('div.lamp a:last').animate({'opacity':0},500);
			}
		}
		return false;
	});
	
	/*
	 * scroll
	 */
	
	$('div.scroll span').draggable({
		axis: 'y',
		containment: 'parent',
		stop: function(event, ui) {
			for (var i=0;i<4;i++){
				if (ui.position.top>=(i*44)-12 && ui.position.top<=((i+1)*44-12)){
					$('div.main ul li:eq('+i+') p a').click();
					return;
				}
			}
		}
	});

	var mainimg = $('<img />');
	mainimg.attr('src','/assets/templates/leek/i/l.jpg');
	mainimg.load(function(){
		$('div.main ul a[href=#lamp-cold]').click();
	});
	
	$('ul.lamps li, ul.ch li, ul.ch li#c1, ul.ch li#c2, ul.ch li#c3, ul.ch li#c4, ul.ch li#c5').hover(function(){		$(this).addClass('hover');	},function(){		$(this).removeClass('hover');	});

	if ($('div.lamps').length){		var cg  = [];
		var cgp = [];
		var cgr = [];

		cgp[0] = [200,60];
		cgr[0] = [115,25,130,0,200,0,100,60];

		cgp[1] = [200,80];
		cgr[1] = [130,80,190,60,200,0,100,80];

		cgp[2] = [160,100];
		cgr[2] = [110,90,120,70,140,40,40,100];

		cgp[3] = [200,200];
		cgr[3] = [110,190,110,140,100,100,200,200];

		for (var i=0;i<4;i++){
			cg[i] = document.createElement('canvas');
			document.getElementById('lp'+(i+1)).appendChild(cg[i]);
			if (window.G_vmlCanvasManager) G_vmlCanvasManager.initElement(cg[i]);

			cg[i].width = cgp[i][0];
			cg[i].height = cgp[i][1];

			ctx = cg[i].getContext('2d');
			ctx.save();
			ctx.lineWidth = 1;

			ctx.beginPath();
			ctx.strokeStyle = "#188991";
			ctx.moveTo(cgr[i][6], cgr[i][7]);
			ctx.bezierCurveTo(cgr[i][0], cgr[i][1], cgr[i][2], cgr[i][3], cgr[i][4], cgr[i][5]);
			ctx.stroke();
			ctx.restore();
		}
	}

	$('form .text').each(function(){
		if (this.title!=''){
			this.value = (this.value=='')? this.title: this.value;
			$(this).blur(function(){
				this.value = (this.value=='')? this.title: this.value;
			});
			$(this).focus(function(){
				this.value = (this.value==this.title)? '': this.value;
				$(this).removeClass('error');
			});
		}
	});

	$('form').each(function(){
		$(this).submit(function(){
			var flag = false;
			$(this).find('.required').each(function(){
				if (this.value=='' || this.value==this.title){
					$(this).addClass('error');
					flag = true;
				}
			});
			if (!flag){
				$(this).find('.text').each(function(){
					if (this.value==this.title) this.value='';
				});
			}

			if (flag) return false;
		});
	});

	if ($('#power').length){
		var tu = 10;
		var cg = document.createElement('canvas');
		document.getElementById('power').appendChild(cg);
		if (window.G_vmlCanvasManager) G_vmlCanvasManager.initElement(cg);
		cg.width = 104;
		cg.height = 55;

		ctx = cg.getContext('2d');
		ctx.lineWidth = 1;

		var imgsp = $('<img />');
		var tm;

		function animatesp(val){
			var ctx = cg.getContext('2d');
			var r =32;
			if (tu<val)	tu++;
			if (tu>val) tu--;
			if (tu == val) return;

			alpha = 180*(tu+10)/120;
			var sh = 1;
			if (alpha>90){
				alpha = 180 - alpha;
				sh = -1;
			}
			var xh = Math.cos(alpha*Math.PI/180)*r;
			var x = parseInt(51 -xh*sh);
			var yh = Math.sin(alpha*Math.PI/180)*r;
			var y = parseInt(52-yh);
			ctx.save();
			ctx.drawImage(imgsp.get(0), 0, 0);
			ctx.beginPath();
			ctx.strokeStyle = "#aef6e7";
			ctx.moveTo(51, 52);
			ctx.lineTo(x,y);
			ctx.stroke();
			ctx.restore();
			tm = setTimeout(function(){
				animatesp(val);
			},10);
		}

		var powerselected = '';
		var socleselected = '';

		function updatecatalog(){
			$('ul.products li').each(function(){
				var tpower = $(this).find('span.power-value').text().split(',');
				var tsocle = $(this).find('span.socle-value').text().split(',');
				if (!((powerselected=='' || $.inArray(powerselected,tpower)!=-1) && (socleselected=='' || $.inArray(socleselected,tsocle)!=-1))){
					$(this).hide(500);
				}
			});
			setTimeout(function(){
				$('ul.products li').each(function(){
					var tpower = $(this).find('span.power-value').text().split(',');
					var tsocle = $(this).find('span.socle-value').text().split(',');
					if ((powerselected=='' || $.inArray(powerselected,tpower)!=-1) && (socleselected=='' || $.inArray(socleselected,tsocle)!=-1)){
						$(this).show(500);
					}
				});
			},500);
		}

		$('p.power span').click(function(){
			if ($(this).hasClass('active')){
				powerselected = '';
				$(this).removeClass('active');
			}else{
				clearTimeout(tm);
				var val = parseInt($(this).attr('rel'));
				animatesp(val);
				$('p.power span').removeClass('active');
				$(this).addClass('active');

				powerselected = $(this).attr('rel');
			}
			updatecatalog();
		});

		imgsp.load(function(){
			animatesp(75);
		});
		imgsp.attr('src','/assets/templates/leek/i/sp.png');

		$('div.socle span').click(function(){
			if ($(this).hasClass('active')){
				socleselected = '';
				$(this).removeClass('active');
			}else{
				$('div.socle span').removeClass('active');
				$(this).addClass('active');

				socleselected = $(this).attr('rel');
			}
			updatecatalog();
		});
	}

	/*
	 *	<- ctrl ->
	 */

	if ($('div.toright a').length || $('div.toleft a').length){
		$(document).keypress(function(e){
			if (e.ctrlKey && e.which==39 && $('div.toright a').length){
				document.location = $('div.toright a').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('div.toleft a').length){
				document.location = $('div.toleft a').get(0).href;
				return false;
			}
		});
		$(document).keydown(function(e){
			if (e.ctrlKey && e.which==39 && $('div.toright a').length){
				document.location = $('div.toright a').get(0).href;
				return false;
			}
			if (e.ctrlKey && e.which==37 && $('div.toleft a').length){
				document.location = $('div.toleft a').get(0).href;
				return false;
			}
		});
	}

	$('.footer a.sl').click(function(){
		$('.footer div.sl').toggle();
		return false;
	});

	if ($('div.econom').length){
		var stime;
		function updateval(eclass,to,i,shag){
			var now = parseInt($('div.'+eclass+' .rub').text())*100 + parseInt($('div.'+eclass+' .cop').text());

			if (to == now) return;
			if (i==0){
				shag = (to-now)/10;
				if ((to<0) && (now<0) && (to>now)) shag = (now-to)/10;
			}

			i++;

			n = now+shag;

			if (i==10) n = to;
			$('div.'+eclass+' .rub').text(parseInt(n/100));

			var cop = Math.round(n % 100);
			if (n<0) cop = cop*(-1);
			if (cop<10) cop = "0"+cop;

			$('div.'+eclass+' .cop').text(cop);

			if (i==10) return;
			stime = setTimeout(function(){
				updateval(eclass,to,i,shag);
			},30);
		}

		function updateprice(){
			var oprice = Math.round(parseFloat($('input.o-price').attr('value'))*100)/100;
			var eprice = Math.round(parseFloat($('input.e-price').attr('value'))*100)/100;
			var olife = parseInt($('span.o-life').text());
			var elife = parseInt($('input.e-life').attr('value'));

			var lmp =  Math.round((elife * oprice / olife - eprice)*100);	//	Экономия на покупке лампы

			var pricew = Math.round(parseFloat($('input.o-price-w').attr('value'))*100)/100;
			var opower = parseInt($('select.o-power').attr('value'));
			var epower = parseInt($('span.e-power').text());

			var l42 = Math.round((opower-epower)*pricew*100);		//	Экономия на Эл. Энергии за 42 дня непрерывной работы
			var life = Math.round(l42 * elife/olife);				//	Экономия на Эл. Энергии за весь срок службы
			var total = life + lmp;

			updateval('l42',l42,0);
			updateval('life',life,0);
			updateval('lmp',lmp,0);
			updateval('total',total,0);
		}

		$('select.o-power').change(function(){			$('span.e-power').text(parseInt($(this).attr('value'))/5);
			updateprice();		});

		$('input.o-price-w').keyup(function(){			var p = parseFloat(this.value);
			var last = this.value.substr(this.value.length-1,1);
			var f = false;
			if (last==',' || last=='.') f = true;
			this.value=(p>0 && p<10)? p+(f?'.':''): (p>0?10:0);
			$('span.e-price-w').text(this.value);
			updateprice();
		});
		$('input.o-price').keyup(function(){			var p = parseFloat(this.value);
			var last = this.value.substr(this.value.length-1,1);
			var f = false;
			if (last==',' || last=='.') f = true;
			this.value=(p>0 && p<20)? p+(f?'.':''): (p>0?20:0);
			updateprice();		});
		$('input.e-price').keyup(function(){
			var p = parseFloat(this.value);
			var last = this.value.substr(this.value.length-1,1);
			var f = false;
			if (last==',' || last=='.') f = true;
			this.value=(p>0 && p<500)? p+(f?'.':''): (p>0?500:0);
			updateprice();
		});
		$('input.e-life').keyup(function(){
			var p = parseFloat(this.value);
			var last = this.value.substr(this.value.length-1,1);
			var f = false;
			if (last==',' || last=='.') f = true;
			this.value=(p>0 && p<50000)? p+(f?'.':''): (p>0?50000:0);
			updateprice();
		});

		$('span.e-power').text(parseInt($('select.o-power').attr('value'))/5);
		$('span.e-price-w').text($('input.o-price-w').attr('value'));
		updateprice();
	}
	
	$('h3.m-more a').click(function(){
		$('div.mcontent div.slide').slideDown(250);
		$(this).parent().hide(150);
		return false;
	});
});

