Shadowbox.init({
	language: 'ru',
	adapter: 'mootools',
	skipSetup: true,
	players:  ['img']
});
window.addEvent('domready', function() {

	// включаем shadowbox
  var Shadowbox_options = {
    resizeDuration: 0.1,
    fadeDuration: 0.1,
    marginDuration: 0.03,
    viewportPadding: 10,
    handleOversize: 'resize',
    overlayOpacity: 0.5,
    viewportPadding: 5,
    autoDimensions: true
  };

	list = $$(document.links);
  list = list.filter(function(item){
  	//if (item.hasClass('no')) return false;
    return item.href.test(/\.(jpe?g|png|gif|swf)$/)
  });
  if (list && list.length) {
		Shadowbox.setup(list, Shadowbox_options);
  }

  new SmoothScroll({
  	onStart: function() {
  		if (this.oldanchor) {
  			this.oldanchor.removeClass('anchored');
  		}
  		this.oldanchor = $(this.anchor).addClass('anchored');
  	}
  });

	$$('#nav ul.l2').each(function(ul) {
		var li = ul.getParent();

		if (!ul.childNodes.length) return;

		ul.verticalSize = ul.getSize().y;

		ul.setStyles({
			height: 0,
			visibility: 'hidden',
			opacity: 0
		});
		ul.set('morph', {
			duration: 'short',
			onComplete: function() {
				if (this.element.getStyle('height') == '0px') {
					ul.setStyle('visibility', 'hidden');
				}

			}

		});

		li.addEvent('mouseenter', function() {
			ul.setStyle('visibility', 'visible');
			li.addClass("hover");
			ul.morph({
				height: ul.verticalSize
				,opacity: 1
			});

		}).addEvent('mouseleave', function() {
			li.removeClass("hover");
			ul.morph({
				height: 0
				,opacity: 0
			});

		})

		Element.NativeEvents.touchstart = 2;
		Element.NativeEvents.touchend = 2;
		Element.NativeEvents.touchmove = 2;

		// ipod/android
		if (Browser.Platform.ipod) li.addEvent('click', function(e) {

			if (!ul.touched) {
				e.stop();
				ul.touched = true;
				li.fireEvent('mouseenter');

				// убираем эвент при клике
				var removehandler = function(e) {
					if (e.target.nodeName == 'A') {
						return true;
					}
					li.fireEvent('mouseleave');
					e.stop();
					ul.touched = false;
					$('wrap').removeEvent('touchend', removehandler);
				}
				$('wrap').addEvent('touchend', removehandler);

			}
		});



	});

	new Tips($$('*[title]'), {className: 'atip'});


	if ($('promo')) {
		var curPromo = 1;
		var maxPromo = 5;
		var buttonOpacity = 0.8;

		$$('#promo .promo_text, #promo .pic img').setStyle('opacity', 0);

		$$('#promo_t1, #promo_p1').setStyle('opacity', 1);

		var promoActivate = function(numbah) {

			if (numbah == maxPromo) {
				$('rightb').tween('opacity', 0);
			} else {
				$('rightb').tween('opacity', buttonOpacity);
			}

			if (numbah == 1) {
				$('leftb').tween('opacity', 0);
			} else {
				$('leftb').tween('opacity', buttonOpacity);
			}

			$('promo_t' + curPromo).tween('opacity', 0);
			$('promo_p' + curPromo).tween('opacity', 0);

			curPromo = numbah;

			$('promo_t' + numbah).tween('opacity', 1);
			$('promo_p' + numbah).tween('opacity', 1);

		}

		$('leftb').addEvent('click', function() {
			if (curPromo - 1 >= 1) promoActivate(curPromo - 1);
		});
		$('rightb').addEvent('click', function() {
			if (curPromo + 1 <= maxPromo) promoActivate(curPromo + 1);
		});

	}


	// calc
	if (window.calcdata) {

		$$('table.form input, table.form select').addEvent('change', function() {
			if (this.className == 'zak') return true;
			if (this.type == 'text' && (this.value != parseInt(this.value) || this.value < 1  )) this.value = this.old;
			this.old = this.value;

			recalc();
		}).each(function(i) {
			if (i.type == 'text') i.old = i.value;
		});
		recalc();

		if (!$('opod').checked)
			$('tbodypod').style.display = 'none';

		$('opod').addEvent('change', function() {

			if (this.checked) $('tbodypod').style.display = '';
			else $('tbodypod').style.display = 'none';
		});

	}



});


function recalc() {
	d = window.calcdata;
	price = 0;
	s = $('owidth').value * $('oheight').value / 1000000;

	price += s * d['mat' + $('omat').value].price * d.curtype_mod;

	if ($('opod').checked) {
		price += $('owidth').value * $('opodlength').value / 1000000 * d.podokonnik_mod * d['mat' + $('opodmat').value].price;
	}

	if ($('odostavka').checked) {
		price += d.dostavka_price;
	}
	if ($('osborka').checked) {
		price += d.sborka_price * s;
	}

	price *= $('oamount').value;

	price = parseInt(price);

	$('totalprice').set('html', price + '&nbsp;руб.');
	if (price == 'NaN') $('totalprice').set('html', 'Неверно заполнена форма!');

}

function doZakaz() {
	error = "";

	if (!$('oname').value) error = "Пожалуйста, заполните поле Имя";
	if (!$('ophone').value) error = "Пожалуйста, заполните поле Телефон";
	//if (!$('oaddress').value) error = "Пожалуйста, заполните поле Адрес";

	if (error) {
		alert (error);
		return false;
	}

	return true;
}
