var menuSlide = new Class({
    
    initialize: function(menu){
        if (!menu) return false;
        this.menu = $(menu);
        if(!this.menu) return false;

        this.secMenuTitle = this.menu.getElements('.doublelist');
        
        this.bound = {
            close: this.close.bind(this),
            stop: this.stop.bind(this)
        };
        this.secMenuTitle.addEvent('mouseenter', this.open.bind(this));
    },
    
    open: function(e){
        curSecMenu = $(e.target).getParent('li').getElement('.secnav');
				if (!curSecMenu)
					return false;
        
        this.curSecMenu = curSecMenu;
        
        this.curTitle = $(e.target);
        if(this.clTimer){
            this.stop();
            this.hideAll();
        }
        this.curTitle.addClass('nav-active');
        this.curTitle.addEvent('mouseleave', this.bound.close);
        this.show();
        this.menuAddEvents();
    },
    
    close: function(){
      if (!this.curSecMenu)
          return false;
          
        this.timer = (function(){
            this.hide();
            this.clTimer = false;
        }).delay(1000, this);
        this.clTimer = true;
    },
    
    show: function(){
        this.curSecMenu.fade('show');
    },
    
    hide: function(){
        this.curTitle.removeClass('nav-active');
        this.curSecMenu.fade('hide');
        this.menuRemoveEvents();
    },
    
    hideAll: function(){
        this.menu.getElements('.secnav').fade('hide');
        this.secMenuTitle.removeClass('nav-active');
        this.menuRemoveEvents();
    },
    
    stop: function(){
        $clear(this.timer);
        this.clTimer = true;
    },
    
    menuAddEvents: function(){
        this.curSecMenu.addEvents({
            'mouseenter': this.bound.stop,
            'mouseleave': this.bound.close
        });
    },
    
    menuRemoveEvents: function(){
        this.curSecMenu.removeEvents({
            'mouseenter': this.bound.stop,
            'mouseleave': this.bound.close
        });
    }

});

var changeSolution = new Class({
    
    initialize: function(main){
        this.main = $(main);
        if(!this.main) return false;
        
        this.List = this.main.getElements('li a');
        this.Content = this.main.getElements('.solution-content');
        this.activeLink = this.main.getElement('.active-solution .solution-content');
        this.cur = this.main.getElement('ul').getElement('.active-solution');
        
        this.Content.fade('hide');
        this.activeLink.fade('show');
        this.start();
        
        this.List.each(function(element){
            if(element.get('class') != 'full-list') element.addEvent('mouseenter', this.change.bind(this));
        }.bind(this));
        this.main.addEvents({
            'mouseenter': this.pause.bind(this),
            'mouseleave': this.start.bind(this)
        });
    },
    
    startTimer: function(){
        if(this.interval) return false;
        this.interval = this.step.periodical(5000, this);
        return true;
    },
    
    stopTimer: function(){
        if(!this.interval) return false;
        this.interval = $clear(this.interval);
        return true;
    },
    
    start: function(){
        this.startTimer();
        return this;
    },
    
    pause: function(){
        this.stopTimer();
        return this;
    },
    
    next: function(){
        this.cur = this.cur.getNext();
        if(!this.cur || this.cur.getElement('a').get('class') == 'full-list') this.cur = this.main.getElement('ul').getFirst();
    },
    
    step: function(){
        this.next();
        this.activeLink = this.cur.getElement('a');
        this.changeSolution();
    },
    
    change: function(e){
        this.activeLink = $(e.target);
        this.cur = this.activeLink.getParent('li');
        this.changeSolution();
    },
    
    changeSolution: function(){
        this.Content.fade('hide');
        this.activeLink.getNext().fade('show');
        this.main.getElements('li').removeClass('active-solution');
        this.activeLink.getParent('li').addClass('active-solution');
        return false;
    }
    
});

var Carousel = new Class({

	menuWidth: 0,
	
	initialize: function(main, id){
        this.main = $(main);
        this.id = id;
        if(!this.main) return false;
        
        this.menu = this.main.getElement('.carousel');
        if (this.id) this.menu = $(this.id);
       
        this.firstSlide = this.menu.getFirst(); 
        this.lastSlide = this.menu.getLast();
        
        this.cur = this.lastSlide;
        this.arrows = this.main.getElements('.arrows');
        this.leftArrow = this.main.getElement('.prev-arrow');
        this.rightArrow = this.main.getElement('.forw-arrow');
        this.arrowWidth = this.leftArrow.getStyle('width').toInt() + 2;
        if(this.leftArrow.get('class').test('nowidth')) this.arrowWidth = 0;
        this.curPosition = this.arrowWidth;
        this.wrapWidth = this.main.getWidth();
        this.fx = new Fx.Tween(this.menu, {duration: 500, transition: Fx.Transitions.Sine.easeInOut});
        
        this.setMenuWidth();
        if(this.menuWidth <= this.main.getWidth()) return false
        else this.setStartPosition();
        
        this.arrows.each(function(arrow){
            arrow.addEvents({
                'mousedown': this.start.bind(this),
                'mouseup': this.pause.bind(this),
        		'mouseleave': this.pause.bind(this)
            });
        }.bind(this));
        this.menu.addEvent('mousewheel', this.start.bind(this));
	},
    
    startTimer: function(){
        if(this.interval) return false;
        this.interval = this.step.periodical(500, this);
        this.step();
        return true;
    },
    
    stopTimer: function(){
        if(!this.interval) return false;
        this.interval = $clear(this.interval);
        return true;
    },
    
    start: function(e){
    	if(!this.menu.get('class').test('active')) return false;
        if(e.wheel){
            if(e.wheel < 0){
                if(this.curPosition != this.wrapWidth - this.menuWidth - this.arrowWidth) this.slideMenu('forw');
            } else if(e.wheel > 0){
                if(this.curPosition != this.arrowWidth) this.slideMenu('prev');
            }
            return false; 
        }
        this.curArrow = $(e.target);
        if(((this.curPosition == this.arrowWidth) && (this.curArrow.get('text') == '<')) || ((this.curPosition == this.wrapWidth - this.menuWidth - this.arrowWidth) && (this.curArrow.get('text') == '>'))) return false;
        
        this.startTimer();
        return this;
    },
    
    pause: function(){
        this.stopTimer();
        return this;
    },
    
    step: function(){
        if(this.curArrow.get('text') == '>') this.slideMenu('forw');
        else if(this.curArrow.get('text') == '<') this.slideMenu('prev');
    },
    
    setMenuWidth: function(){
        this.menu.getElements('li').each(function(element){
            this.menuWidth = this.menuWidth + element.getWidth();
        }.bind(this));
        this.menuWidth += 1;
        this.menu.setStyle('width', this.menuWidth);
    },
    
    setStartPosition: function(){
        this.arrows.setStyle('visibility', 'visible');
        this.fx.set('left', this.arrowWidth);
        if(this.main.get('id') == 'works-title-wrapper') $('works-title').setStyle('float', 'none');
    },
    
    slideMenu: function(how){
        this.fx.pause();
        if(how == 'prev'){
            if(!this.cur.getNext() && this.curPosition == this.arrowWidth){
                this.stopTimer();
                return false;
            } else this.curWidth = this.cur.getWidth();
            this.curPosition += this.curWidth;
            if(this.curPosition >= this.arrowWidth){
                this.curPosition = this.arrowWidth;
                this.cur = this.lastSlide;
            } else this.cur = this.cur.getNext();
        }
        else if(how == 'forw'){
            if(!this.cur.getPrevious() && this.curPosition == this.wrapWidth - this.menuWidth - this.arrowWidth){
                this.stopTimer();
                return false;
            } else this.curWidth = this.cur.getWidth();
            this.curPosition -= this.curWidth;
            if(this.curPosition <= this.wrapWidth - this.menuWidth - this.arrowWidth){
                this.curPosition = this.wrapWidth - this.menuWidth - this.arrowWidth;
                this.cur = this.firstSlide;
            } else this.cur = this.cur.getPrevious();
        }
        
        this.fx.start('left', this.curPosition);
        if (this.cur.get('id') && this.cur.get('id').match(/work_(\d+)/))
        {
        	this.loadVisibleWorks();
        }
    },
    loadVisibleWorks: function() 
    {
		  var current_el = this.cur;
		  var i = 0;
		  do
		  {
		  	i++;
		  	
				if (current_el.get('is_loaded'))
				{
					current_el = current_el.getPrevious();
					continue;
				}
				
				current_el.get('id').match(/work_(\d+)/);
				var workId = RegExp.$1;
				if (!workId)
				{
					current_el = current_el.getPrevious();
					continue;
				}
					
				var workImg = new Element('img', {
				  'src': works[workId].pic,
				  'alt': ''
				});
				
				$(workImg).inject($('work_link_' + workId));	
			
				current_el.set('is_loaded', 1);
				current_el = current_el.getPrevious();
		  } 
		  while (i <= 3 && current_el);
    }
	
});

var changeWork = new Class({

	initialize: function(title, main){
		this.title = $(title);
		this.main = $(main);
		if(!this.title || !this.main) return false;
		
		this.titles = this.title.getElements('li');
		this.previews = this.main.getElements('ul');
		this.arrows = this.main.getParent().getElements('.arrows');

		this.titles.addEvent('click', this.start.bind(this));
	},
	
	start: function(e){
		if($(e.target).getParent('li') == this.curTitle) return false;
		this.curTitle =  $(e.target).getParent('li') || $(e.target);
		this.curWork = $('p-'+this.curTitle.get('id'));
		this.changeTitle();
		this.changeWork();
		this.testArrow();
	},
	
	changeTitle: function(){
		this.titles.removeClass('active-wlink');
		this.curTitle.addClass('active-wlink');
	},
	
	changeWork: function(){
		this.previews.removeClass('active-work');
		this.curWork.addClass('active-work');
		
		this.curWork.carousel.loadVisibleWorks();
	},
	
	testArrow: function(){
		if(this.curWork.getWidth() <= this.main.getWidth()) this.arrows.setStyle('display', 'none')
		else this.arrows.setStyle('display', 'block');
	}
	
});

createCarouselPreview = function(name) {
	var worksPreviews = $(name).getElements('.carousel');
	var activeWork    = $(name).getElement('.active-work');
	
	worksPreviews.each(function(element, index){
		var preview = new Carousel(name, element.get('id'));

		if (activeWork == element)
		{
			preview.loadVisibleWorks();
		}
		element.carousel = preview;
	});
	
	if(activeWork.getWidth() <= $(name).getWidth()) $(name).getElements('.arrows').setStyle('display', 'none');
};

window.addEvent('domready', function(){
    
  var secnav = new menuSlide('nav');
  var solut = new changeSolution('solution');
  
  if($('instroy-preview')) {
    var instroy = new Carousel('instroy-preview');
    
		$('instroy-preview').setStyle('overflow', 'hidden');
	}

  if($('works-title-wrapper')) {
    var worksTitle = new Carousel('works-title-wrapper');
    
    $('works-title-wrapper').setStyle('visibility', 'visible');
    createCarouselPreview('works-preview-wrapper');
	}
    
  if($('works-title')) var works = new changeWork('works-title', 'works-preview');
    
});

function smartFocus(el, value)
{
  if (el.value == value)
    el.value = '';
}

function smartBlur(el, value)
{
  if (el.value == '')
    el.value = value;
}

function openPopup(url,w,h){
	var win=open(url,
		"popup","toolbar=no,location=no,directories=no,"+
		"status=no,scrollbars=no,resizable=yes,"+
		"copyhistory=no,left="+(window.screen.width-w)/2+",top="+
		(window.screen.height-h)/2+",width="+w+",height="+h);
	win.focus();
	return false;
}
function smartInput(id)
{
 var el = jQuery('#' + id);
 if (!el[0])
 return;

 var def_value = el.attr('default_value') ? el.attr('default_value') : el[0].defaultValue;  
 
 el.focus(function()
 {
	var item = jQuery(this); 
	 

 if (def_value == item.val())
 item.val('');
 item.css('color', '#000');
 });
 el.blur(function()
 {   
   var item = jQuery(this);                                                                      
   
   
 if (item.val().trim() == '')
 {
   item.val(def_value);
   item.css('color', '');
  }
 });
}

jQuery(document).ready(function(){
jQuery('.price div').each(function(){
		var div_height = jQuery(this).parent('.price').innerHeight();
		jQuery(this).css('height',div_height)
	});
	var normal_heihgt = jQuery('.see_also').innerHeight();
	jQuery('.more_sinonim').click(function(){
		jQuery('.more_sinonim').remove();	
		jQuery('.see_also').css('height','auto');
		return false
	});
	jQuery('.close_popup').live('click',function(){
		jQuery('.popup_payment, .to_hide').remove();
		return false
	});
	
	
	jQuery('.price a.dashed_underline').live('click',function(){
		jQuery('.popup_payment, .to_hide').remove();
		jQuery(this).after("<br class='to_hide'/><div class='popup_payment'>Выберите удобный Вам способ оплаты:<form><input name='payment' type='radio' id='payment_1' value='http://www.yandex.ru'/><label for='payment_1'> Наличными в офисе</label><br/><br/><input name='payment' value='http://www.ru' type='radio' id='payment_2'/><label for='payment_2'> Квитанция для оплаты через Сбербанк</label><br/><br/><input name='payment' value='http://www.ru' type='radio' id='payment_2'/><label for='payment_2'> Моментальная оплата: Яндекс.Деньги, WebMoney (сервис Robokassa)</label><br/><br/><input name='payment' value='http://www.ru' type='radio' id='payment_2'/><label for='payment_2'> Моментальная оплата: Visa, MasterCard (сервис Robokassa)</label><br/><br/><input name='payment' value='http://www.ru' type='radio' id='payment_2'/><label for='payment_2'> Моментальная оплата: Терминалы оплаты (сервис Robokassa)</label><br/><br/><input name='payment' value='http://www.ru' type='radio' id='payment_2'/><label for='payment_2'> Счет для организации</label><br/><br/><a class='button' onClick='return href_input()' >Перейти</button><br/><a class='close_popup'>Закрыть</a></form></div>");
		return false;
	});
});
function href_input()
	{
		var ll = jQuery(".popup_payment input[type=radio]:checked");
		if (ll.length > 0){
			document.location = ll.val();
		}
		return false
	}