var gal, gal_top, left, right, gal_content, sl;
var speed=3;
var go = false;

var my_counter;

window.addEvent('domready', function(){
	if (Cookie.read('design') == 'night')
	{
		InvertDesign(null);
	}

	$$('#footer_block a').each(function(el)
	{
		var myURI = new URI(el.href);
		if (myURI.get('fragment')=='inv')
		{
			el.set({'events': {'click': function(event){InvertDesign(event);}}, 'href':'javascript:void(0)'});
		}
	});

    if ($('portfolio'))
    {
        $('portfolio').getElements('.img_item').each(function(el1)
        {
            el1.addEvent('mouseover', function(event){			
                var el=$(event.target);
                if (el.tagName=='IMG') {el=el.getParent(); el=el.getParent();}
                if (el.tagName=='A') {el=el.getParent();}
                el.addClass('act_item');
                el.removeClass('img_item');
                var el2 = el.getNext('div');
                el2.removeClass('noshow');
            });
            
            el1.addEvent('mouseout', function(event){
                var el=$(event.target);
                if (el.tagName=='IMG') {el=el.getParent(); el=el.getParent();}
                if (el.tagName=='A') {el=el.getParent();}
                el.addClass('img_item');
                el.removeClass('act_item');
                var el2 = el.getNext('div');
                el2.addClass('noshow');
            });		
        });
        
        $('portfolio').getElements('.info_item').each(function(el1)
        {
            el1.addEvent('mouseover', function(event){			
                var el=$(event.target);
                el.removeClass('noshow');
                el=el.getPrevious('div');
                el.addClass('act_item');
                el.removeClass('img_item');
            });
            el1.addEvent('mouseout', function(event){
                var el=$(event.target);
                el.addClass('noshow');
                el=el.getPrevious('div');
                el.addClass('img_item');
                el.removeClass('act_item');	
            });		
        });
    }	

	
	//Предварительная загрузка изображений
	var imagesList = [
		'/images/sleft_a.png',
		'/images/sright_a.png',
		'/images/right_footer_b.jpg',
		'/images/right_footer_w.jpg',
		'/images/footer_b.jpg',
		'/images/footer_w.jpg',
        '/images/logo_w.gif',
        '/images/logo_b.gif',
        '/images/skoro/001w.jpg',
        '/images/skoro/001b.jpg',
        '/images/bw01.jpg',
        '/images/bn01.jpg',
        '/images/bw02.jpg',
        '/images/bn02.jpg',
        '/images/bw04.jpg',
        '/images/bn04.jpg'     
	];
	var aImages = [];
	for (var i = 0, len = imagesList.length; i < len; i++) {
		   aImages[i] = new Image();
		   aImages[i].src = imagesList[i];
	}
	
	//====================================================================================
    if ($('portfolio'))
    {
        gal = $('portfolio');
        gal_top = $('portfolio2');
        left  = gal_top.getElements('.left_s');
        right = gal_top.getElements('.right_s');
        gal_content = gal.getElements('.scontent');
        sl = $('slider_content');
        
        slide();
        left.addEvent('mouseover', function(event){go = 'right'; speed=3; $('sleft_1').setStyle('display', 'none'); $('sleft_2').setStyle('display', 'block');});
        left.addEvent('mouseout', function() {go = false; $('sleft_1').setStyle('display', 'block'); $('sleft_2').setStyle('display', 'none');});
        left.addEvent('mousedown', function() { go = 'right'; speed=6;});
        left.addEvent('mouseup', function() { speed=3; });

        right.addEvent('mouseover', function() {go = 'left'; speed=3; $('sright_1').setStyle('display', 'none'); $('sright_2').setStyle('display', 'block');});
        right.addEvent('mouseout', function() {go = false; $('sright_1').setStyle('display', 'block'); $('sright_2').setStyle('display', 'none');});
        right.addEvent('mousedown', function() {go = 'left'; speed=6;});
        right.addEvent('mouseup', function() {speed=3;});
    }
	//====================================================================================
	
	$('block1').addEvent('click', function(event){			
			var el=$('block1');
			el.removeClass('block_noact');
			el.addClass('block_act');			
			el=el.getChildren();
			el.removeClass('city');
			el.addClass('city_act');
			
			el=$('block2');
			el.removeClass('block_act');
			el.addClass('block_noact');
			el=el.getChildren();
			el.removeClass('city_act');
			el.addClass('city');
			$('idtelefon').innerHTML='+7 (383) 354-12-13';
	});
	
	$('block2').addEvent('click', function(event){			
			var el=$('block2');
			el.removeClass('block_noact');
			el.addClass('block_act');
			el=el.getChildren();
			el.removeClass('city');
			el.addClass('city_act');
			
			el=$('block1');
			el.removeClass('block_act');
			el.addClass('block_noact');
			el=el.getChildren();
			el.removeClass('city_act');
			el.addClass('city');
			$('idtelefon').innerHTML='+7 (495) 364-59-89';
	});	
	
	
	//Кулькулято
	if ($('tablo'))
	{
		my_counter=new Counter();
		my_counter.Init({'height':30, 'dclass':'digit'});
	
		$$('.tcontent input.showtablo').each(function(el){
			el.addEvent('click', function(event){
				Calc($(event.target));
			});
		});
	}
	
});

function InvertDesign(event)
{
	if (event!=null) var ael=$(event.target);
	else  var ael=$('inv_b');
	
	$$('body').each(function(el)
	{
		if ((Cookie.read('design')=='night' && event==null) || el.hasClass('white'))
		{
			el.removeClass('white');
			el.addClass('black');            
            $('logo_dn').set('src', '/images/logo_b.gif');
            if ($('skoro_wb'))
            {
                $('skoro_wb').set('src', '/images/skoro/001b.jpg');
                $('ban_wb1').set('src', '/images/bn01.jpg');
                $('ban_wb2').set('src', '/images/bn02.jpg');
                $('ban_wb4').set('src', '/images/bn04.jpg');
            }
            
			ael.innerHTML='День';
			var myCookie = Cookie.write('design', 'night', {domain:'.web-prospekt.ru', path: '/'});
		} else {
			el.removeClass('black');
			el.addClass('white');
            $('logo_dn').set('src', '/images/logo_w.gif');
            if ($('skoro_wb'))
            {
                $('skoro_wb').set('src', '/images/skoro/001w.jpg');
                $('ban_wb1').set('src', '/images/bw01.jpg');
                $('ban_wb2').set('src', '/images/bw02.jpg');
                $('ban_wb4').set('src', '/images/bw04.jpg');
            }
            
			ael.innerHTML='Ночь';
			var myCookie = Cookie.write('design', 'day', {domain:'.web-prospekt.ru', path: '/'});
		}
	});
}


function slide()
{
	if(go == 'left')
	{
		sl.setStyle('left', sl.getCoordinates(gal).left-speed+'px');
		if(-sl.getCoordinates(gal).left > sl.getSize().x - gal.getSize().x)
		{
			sl.getFirst('.item').inject(sl);
			sl.setStyle('left', sl.getCoordinates(gal).left+sl.getFirst('.item').getSize().x+'px')
		}
	}
	if(go == 'right')
	{
		sl.setStyle('left', sl.getCoordinates(gal).left+speed+'px');
		if(sl.getCoordinates(gal).left > 0)
		{
			sl.getLast('.item').inject(sl, 'top');
			sl.setStyle('left', sl.getCoordinates(gal).left-sl.getFirst('.item').getSize().x+'px')
		}
	}
	setTimeout('slide()', 10);
}

function InitForm(obj_form)
{
	$(obj_form).getElements('input').each(function(el)
	{	
		if (el.get('type')=='submit')
		{
			el.addEvent('click', function(event){
				new Event(event).stop();
				var form = $(event.target).getParent('form');
                form.set('send', {evalResponse: true});
				form.send();
			});
		}
	});
}

function Counter()
{
	var _this = this; 
	this.current_value = 0;
	this.count_digit = 0;
	this.height_digit = 0;
	this.myEffect = new Array();
	this.adigit = new Array();
	this.str_digit='';
	this.isinit=false;
	
	//########################################################
	//	var my_counter=new Counter();
	//	my_counter.Init({'height':30, 'dclass':'digit', 'value':9999999});
	this.Init = function(param)
	{
		if (!param.height && !param.dclass) return;
		_this.height_digit=param.height;

		_this.count_digit=0;
		$$('.'+param.dclass).each(function(O, P){
			_this.myEffect[P]=new Fx.Morph(O, {duration: 'short', transition: Fx.Transitions.Bounce.easeOut});
			_this.count_digit++;			
		});
		if (_this.count_digit>0) _this.isinit=true;
		
		//Устанавливаем значение счетчика
		if (param.value) _this.current_value=param.value;
		 else _this.current_value=0;
		_this.str_digit=_this.DigitToStr(_this.current_value);
		$$('.'+param.dclass).each(function(O, P){
			var digit=0;
			if (_this.str_digit.substr(P,1)=='_') digit=10;
			 else digit=_this.str_digit.substr(P,1).toInt();
			O.setStyle('background-position', '0px -'+(digit*_this.height_digit)+'px');
		});
	}
	
	this.DigitToStr = function (value)
	{
		var str=''+value;
		var kol=_this.count_digit-str.length;
		str='';
		for(i=1; i<=kol; i++) str+='_';	
		str+=''+value;
		return str;
	}
	
	//########################################################
	this.Set = function(value)
	{
		if (!_this.isinit) return;
		
		_this.current_value=value;
		_this.str_digit=_this.DigitToStr(_this.current_value);

		for(var i=_this.count_digit; i>0; i--)
		{
			var digit=0;
			if (_this.str_digit.substr(i-1,1)=='_') digit=10;
			 else digit=_this.str_digit.substr(i-1,1).toInt();		
			_this.adigit[i-1]=digit;
			_this.myEffect[i-1].start({'background-position': '0px, -'+(_this.adigit[i-1]*_this.height_digit)+'px'});
		}
	}	
}


function Calc(sel_el)
{		
	if (sel_el.get('name')=='tip_site')
	{
		var nextEl = sel_el.getNext('div.info');
		nextEl.removeClass('notshow');	
		$$('.tcontent div.info').each(function(el){
			if (el!=nextEl) el.addClass('notshow');
		});
	
		$$('.tcontent input.i_checkbox').each(function(el){
			el.checked=true;
			
			el.addClass('notshow');
			var nextEl = el.getNext('label');
			nextEl.addClass('notshow');
			var nextEl = el.getNext('br');
			nextEl.addClass('notshow');
		});
	} else if (sel_el.get('name')=='tip_design') {
		var nextEl = sel_el.getNext('div.info2');
		nextEl.removeClass('notshow');	
		$$('.tcontent div.info2').each(function(el){
			if (el!=nextEl) el.addClass('notshow');
		});	
	}	
	
	var sel=new Array();
	sel['tip_site']=-1;
	sel['tip_design']=-1;

	$$('.tcontent input').each(function(el){
		if (el.checked && el.get('rel'))
		{
			sel[el.get('name')]=el.get('rel').toInt();
		}
	});
	var summa=((sel['tip_site']<0)?0:sel['tip_site'])+((sel['tip_design']<0)?0:sel['tip_design']);
	
	if (sel['tip_site']!=-1 && sel['tip_design']!=-1)
	{
		$('forma_zakaza').removeClass('notshow');
	
		if (sel['tip_site']!=0)
		{
			$('result').setStyle('display','block');
			$$('.tcontent input.i_checkbox').each(function(el){
				var name = el.get('name');
				var id_opt=name.substr(4, name.length-4).toInt();
				if ($('tip_site_1').checked)
				{
					if (id_opt>=5 && id_opt<=8)
					{
						el.removeClass('notshow');
						var nextEl = el.getNext('label');
						nextEl.removeClass('notshow');
						var nextEl = el.getNext('br');
						nextEl.removeClass('notshow');
					}
				} else if ($('tip_site_2').checked) {
					if ((id_opt==6) ||
						(id_opt>=9 && id_opt<=14))
					{
						el.removeClass('notshow');
						var nextEl = el.getNext('label');
						nextEl.removeClass('notshow');
						var nextEl = el.getNext('br');
						nextEl.removeClass('notshow');
					}
				} else if ($('tip_site_3').checked) {
					if ((id_opt==6) ||
						(id_opt>=9 && id_opt<=10) ||
						(id_opt>=12 && id_opt<=19))
					{
						el.removeClass('notshow');
						var nextEl = el.getNext('label');
						nextEl.removeClass('notshow');
						var nextEl = el.getNext('br');
						nextEl.removeClass('notshow');
					}			
				}
			});
			$('tablo').removeClass('notshow');
			$('tablo2').addClass('notshow');
			$('sel_design').removeClass('notshow');
		} else {
			$('result').setStyle('display','none');
			$('tablo').addClass('notshow');
			$('tablo2').removeClass('notshow');
			$('sel_design').addClass('notshow');
		}
	} else {
		if (sel['tip_site']==0)
		{
			$('result').setStyle('display','none');
			$('tablo').addClass('notshow');
			$('tablo2').removeClass('notshow');	
			$('sel_design').addClass('notshow');
			$('forma_zakaza').removeClass('notshow');
		} else {
			$('tablo').removeClass('notshow');
			$('tablo2').addClass('notshow');		
			$('forma_zakaza').addClass('notshow');
			$('sel_design').removeClass('notshow');
		}
	}
	
	$$('.tcontent input.i_checkbox').each(function(el){
		if (!el.hasClass('notshow') && !el.checked)
		{
			summa-=el.get('rel').toInt();
		}
	});

	var pos=sel_el.getPosition();
	$('tablo').setStyle('top',(pos.y-15)+'px');	
	$('tablo2').setStyle('top',(pos.y-15)+'px');	
	my_counter.Set(summa);	
}
