//Плагин ротации объявлений и новостей
var Rotator = new Class({
    	Implements: [Events, Options],
    	options: {      		
          cur: 0,          
         timeout: 3000
    	},
    
     initialize: function(list, options,events){
        		this.setOptions(options);
        	 	this.list=list;
        		 this.length=list.length;
			 	
        		 this.list[0].setStyle('opacity', 1);
			 	 this.f.delay(this.options.timeout, this);   	
  	},
  	start: function() {
              this.active=true;

    },
    stop: function() {
              this.active=false;
    },
    setpos: function(pos) {
            this.options.cur = pos;
            this.slide();
            
    },
   getpos: function(pos) {             
           return   this.options.cur;            
    },
    f: function() {
        if (this.active) {
            this.options.cur++;
    	      this.slide();	
        }           
	      this.f.delay(this.options.timeout, this);
               
    },
     //События
  
    slide: function() {         
        	for(i=0;i<this.length;i++) {
			    this.list[i].morph({					  									
							'opacity': 0
							
				 });  	 	
		    	}  			         
	        if (this.options.cur >=this.length  ) this.options.cur = 0;
	        if (this.options.cur < 0) this.options.cur = this.length-1;
			//установим бэкграунд 
			//$('fon_adbox').setStyle('background-image','url('+this.list[this.options.cur].get('rel')+')');	
			 $('fon_adbox').morph({					  									
							'background-image': 'url('+this.list[this.options.cur].get('rel')+')'
						
							
				 }); 
				 	
	        this.list[this.options.cur].morph({					  									
							'opacity': 1
							
				 });  	 	
          this.public.slideAfter();         
    },
    
 public: { slideAfter: function() { 
    
    }   
  } 

})
  



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

window.addEvent('domready', function() {

	// включаем shadowbox
  var Shadowbox_options = {
    resizeDuration: 0.2,
    fadeDuration: 0.2,
    viewportPadding: 10,
    handleOversize: 'drag',
    overlayOpacity: 0.5,
    viewportPadding: 5,
    autoDimensions: true,
		animate: false
  };

	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');
  	}
  });
  
  //Ротатор
if ($chk($('impinfo'))) {
  	var  newslist = $$('#impinfo > div ').setStyle('opacity', 0).set('tween', {duration: 200});
	newslist.Rotator = new Rotator(newslist, {timeout: 5000});	 
	newslist.Rotator.start();		

}		
  //Раздвигалка контента на главной
  
  if ($chk('.js-ukazatel')) {
  	   $$('.js-ukazatel').addEvent('click', function(e) {
  	          
	  	  	if (!this.hasClass('active')) {				
					$$('.skriti_box').tween('top', '514px'); // 	  
					this.addClass('active');
					newslist.Rotator.start(); 
			  
	   		}
	   		else {			     
					$$('.skriti_box').tween('top', '395px');
					this.removeClass('active');	
					newslist.Rotator.stop();
							
			}
			
  	   });
  }
  //Клик по иконкам слайда
  if ($chk('.js-adboxItem')) {
  	   $$('.js-adboxItem').addEvent('click', function(e) {
		  e.preventDefault();  	        
	  	  var position = this.get('rel')-1;
	  	  
	  	  newslist.Rotator.setpos(position);
  	   });
  }
 //Раздвигалка о нас
 if ($chk('.js-about_pos1')) {
		$$('.js-about_pos1').addEvent('click', function(event) {
			
				var width =  this.getStyle('width').toInt();
			
		          if (width!=170) { 
					  $(this).morph({							
							width: 170
					  });
					  
					  this.getElements('ul').morph({							
								opacity: 1,
								width: '170px'
					  });
					  this.set('id','about_pos');
					  
		          }
		          if (width==170) {
				  		this.set('id','about_pos1');
					  this.getElements('ul').morph({					  									
							opacity: 0,
							width: '0px'
					  }); 	 
					  $(this).morph({							
							width: '37px'
					  });
					
		          }
					
								
     })
}
if ($chk('.js-main-about')) {
		$$('.js-main-about').addEvent('mouseenter', function(event) {
	 
		      var width =$$('.js-about_pos1').getStyle('width');
		        
		 if (width!='170px') {
		        this.set('id','about');
		         $$('.js-about_pos1').set('id','about_pos1');
		         $$('.js-about_pos1').addClass('about_str_akt') ; 
		}   
		 //if (!this.hasClass('current'))	 this.addClass ('Ho');
     	}).addEvent('mouseleave', function(event) {	
     		var width =$$('.js-about_pos1').getStyle('width');
		 	if (width!='170px') {	    
		        if ($$('js-about_pos1').hasClass('current')) {
				this.set('id','');
				 $$('.js-about_pos1').set('id','');		       
					 $$('.js-about_pos1').removeClass('about_str_akt');		        
				}
		 	}				
     	});
}
if ($chk('.js-about_pos1')) {
		$$('.js-about_pos1').addEvent('mouseenter', function(event) {
		   var width =  this.getStyle('width').toInt();
		   
		 if (width!=170) { 
			$$('.js-main-about').set('id','about');
		        this.set('id','about');
		         this.set('id','about_pos1');
		         this.addClass('about_str_akt') ; 
			}   
		 //if (!this.hasClass('current'))	 this.addClass ('Ho');
     	}).addEvent('mouseleave', function(event) {		    
		       	     
					 	var width =  this.getStyle('width').toInt();
						 if (width!=170) { 
							  this.set('id','');
							  this.removeClass('about_str_akt');				        
					          $$('.js-main-about').set('id','');
					    }
						
     	});
}
 
//Развертывание портфолио
var razvportfolio = function() {
if ($chk('.js-ramka_foto')) {
		$$('.js-ramka_foto').addEvent('click', function(event) {
			
			
			 this.getParent('.portfolio_row').getElement('.fl-portfolio').morph({					  									
							opacity: 1
							
			 });
			 this.morph({					  									
							opacity: 0
							
			 });
			 var f1= function() {
				 
				 this.getParent().getElement('.js-image-other').morph({					  									
								opacity: 1
								
				 });
				 var f4 = function() {
				 this.getParent().getElement('.img1').morph({					  									
								top: '-70px',
								left: '100px'
								
				 });
				 this.getParent().getElement('.img2').morph({					  									
								top: '-65px',
								left: '300px'
								
				 });
				 this.getParent().getElement('.img3').morph({					  									
								top: '90px',
								left: '100px'
								
				 });
				  this.getParent().getElement('.img4').morph({					  									
								top: '100px',
								left: '300px'
								
				 });
				 };
				 f4.delay(100,this);
				 /*
.portfolio_img .img1 { top:-70px; left:100px}
.portfolio_img .img2 { top:-65px; left:300px}
.portfolio_img .img3 { top:90px; left:100px}
.portfolio_img .img4 { top:100px; left:300px}   */
			 } ;
			  var f2 = function() {	
			 
				this.getParent('.portfolio_row').getElement('.fl-portfolio').morph({					  									
								opacity: 0
								
				 			}); 
			} ;		
			  f1.delay(1500,this);
			  f2.delay(1500,this);
				
								
        })
}
}
razvportfolio();
//Авторизация в callцентр
if ($chk($('avt_but'))) {

	   $('avt_but').addEvent('click', function(e) {
	   		 e.preventDefault();
			var	node = this;
			var log = $('login').get('value');
			var pwd =$('password').get('value');
			$$('.avt_kc').setStyle('visibility','hidden');
			$$('.red_r_login').setStyle('visibility','hidden');
			$$('.red_r_pas').setStyle('visibility','hidden');
	         //Ставим статус
	         node.setStyle('display','none');
	         $$('.fl-wait').setStyle('display','block');
		        new Request({
				method: 'post',
				url: '/',
				'link': 'ignore',
				data: {
					login: log,
					password: pwd,
					clientlogin: '1',
					ajax: '1'
				},
				onSuccess: function(response) {
				    node.setStyle('display','block');
				    $$('.fl-wait').setStyle('display','none');
				    var str = response;
				  
					if (str.indexOf('0') + 1) {
			            // alert("Не правильное имя пользователя или пароль!");
			            $$('.avt_kc').morph({					  									
							opacity: 1
							
						 });
						$$('.red_r_login').morph({					  									
							opacity: 1
							
						 });
						$$('.red_r_pas').morph({					  									
							opacity: 1
							
						 });
					}
					else {
				
						new Request({
							method: 'post',
							url: '/support/',
							'link': 'ignore',
							data: {
								login: log,
								password: pwd
								
							},
							onSuccess: function(response) {
							   
							    
						  }
						  }).send();
					
					
					    document.location.href = '/';
					}
				    
			  }
			  }).send();
	   });
	   
	   $('login').addEvent('click', function(e) {
	            this.set('value','');
	            
	   });
	   $('password').addEvent('click', function(e) {
	            
	            this.set('value','');
	   })
}
//Клик по иконкам в портфолио на главной
if ($chk('.js-port_cells_img')) {
	$$('.js-port_cells_img').addEvent('click', function(e) {
			 this.getElement('img').morph({					  									
								'margin-right': ['0','-200px'],
								'margin-top': ['0','-10px']
								
				 			}); 
			var f =  function () {
				 this.getElement('img').morph({					  									
									'margin-right': ['-200px','0'],
									'margin-top': ['-10px','0']
									
				}); 
			}
			f.delay(2000,this);
			
		    	e.preventDefault();
	})
}
//Ползунок в портфолио и новостях
//двигаем  левую стрелку
if ($chk('.js-vibor_news_l')) {
       var maxwidth = $('list_news').getStyle('width').toInt();
      var firstpos = 0;
	  $$('.js-vibor_news_l').addEvent('mousedown', function(e) {
	  	firstpos =  e.page.x; 
		  	
		this.addClass('Dropping');
		e.preventDefault();
	  }).addEvent('mousemove',  function(e) {
	  		if (this.hasClass('Dropping')) {	  		      
	  		        var pos =  e.page.x;	  		  
	  		        var padding = this.getParent('.vibor_news').getStyle('padding-left').toInt();
	  		        var width = this.getParent('.vibor_news').getStyle('width').toInt();
	  		        var newpos = padding+pos-firstpos;
	  		        var newwidth = width-(pos-firstpos);
	  		        if (newpos<28) {
					  newpos= 28;
					  newwidth = width; 
					} 
					if (newwidth>(maxwidth-20)) {				        
					    newwidth= maxwidth-20;					  					  
					} 		  
							        	  		        
	  		        firstpos =pos;
	  		        this.getParent('.vibor_news').setStyle('padding-left',newpos+'px');
	  		        this.getParent('.vibor_news_bg').setStyle('width',newwidth+'px');   
	                this.getParent('.vibor_news_bg').getElement('.visibl_line').setStyle('left',newpos+'px');
	                this.getParent('.vibor_news_bg').getElement('.visibl_line').setStyle('width',newwidth+'px');
	                 
		     }
		 	e.preventDefault();	
	  }).addEvent('mouseup', function(e) {          
			if (this.hasClass('Dropping')) {
				this.removeClass('Dropping');
			//	showNews();
			}
			e.preventDefault();
	  }).addEvent('mouseleave', function(e) {          
			this.removeClass('Dropping');
			
			e.preventDefault();	
	  });
}
//двигаем правую стрелку 
if ($chk('.js-vibor_news_r')) {
      var maxwidth = $('list_news').getStyle('width').toInt();
      var firstpos = 0;
	  $$('.js-vibor_news_r').addEvent('mousedown', function(e) {
	  	firstpos =  e.page.x; 
		   	
		this.addClass('Dropping');
		e.preventDefault();
	  }).addEvent('mousemove',  function(e) {
	  		if (this.hasClass('Dropping')) {	  		      
	  		        var pos =  e.page.x;	  		   
	  		        var width = this.getParent('.vibor_news_bg').getStyle('width').toInt();	  		     
	  		        var newwidth = width+(pos-firstpos);	  		       
										 
					if (newwidth>(maxwidth-20)) {				        
					    newwidth= maxwidth-20;					  					  
					} 		  	  		        
	  		        firstpos =pos;	  		      
	  		        this.getParent('.vibor_news_bg').setStyle('width',newwidth+'px'); 
					this.getParent('.vibor_news_bg').getElement('.visibl_line').setStyle('width',newwidth+'px');
				   
		    	
		     }
		    
		 	e.preventDefault();	
	  }).addEvent('mouseup', function(e) {          
		if (this.hasClass('Dropping')) {
			this.removeClass('Dropping');
			
		//	showNews();
		}
			e.preventDefault();
	  }).addEvent('mouseleave', function(e) {          
			this.removeClass('Dropping');
		    
			e.preventDefault();	
	  });
}
//Двигаем общий ползунок

if ($chk('.vibor_news')) {
		
      var firstpos = 0;
	  $$('.vibor_news').addEvent('mousedown', function(e) {
	  	firstpos =  e.page.x; 
		  	
		this.addClass('Dropping');
		e.preventDefault();
	  }).addEvent('mousemove',  function(e) {
	  		if (this.hasClass('Dropping')) {	  		      
	  		        var pos =  e.page.x;	  		  
	  		        var padding = this.getStyle('padding-left').toInt();
	  		        var width = this.getStyle('width'); 
	  		        var newpos = padding+pos-firstpos;
                 var newwidth = width+(pos-firstpos);	  	  		       
	  		   if (newpos<28) {
					  newpos= 28;					  
					} 
	  		    if (newpos>(maxwidth-20)) {				        
					    newpos = maxwidth;					  					  
					} 		  	  		   	        
	  		       firstpos =pos;
	  		       this.setStyle('padding-left',newpos+'px');
	  		          
	               this.getElement('.visibl_line').setStyle('left',newpos+'px');
	              //  this.getParent('.vibor_news_bg').getElement('.visibl_line').setStyle('width',newwidth+'px');
	                 
		     }
		 	e.preventDefault();	
	  }).addEvent('mouseup', function(e) {          
		if (this.hasClass('Dropping')) {
			this.removeClass('Dropping');
			
			showNews();
		}
			e.preventDefault();
	  }).addEvent('mouseleave', function(e) {          
			this.removeClass('Dropping');
			 
			e.preventDefault();	
	  });
}

function showNews() {
		var posthref=$('ajax_link').get('value');
		
     	 //показываем новости какие надо
		 var tmppos1 = $('js-vibor_news_l').getOffsets();
		  tmppos1.x=tmppos1.x+23;
		 var tmppos2 = $('js-vibor_news_r').getOffsets();
		 var countnews = 0;
		 var idnews = '0';
		
		 $$('.list_news a ').each(function(item) {							
				var rel1 =  item.get('rel1');
				var id =  item.get('rel');				
				if (rel1<=tmppos2.x && rel1>=tmppos1.x) {				
					idnews = idnews+','+id;				    
					countnews=countnews+1;
					$('countnews').set('html',countnews);
				}
				 
	     });
	     $('ajax-load').set('html','<div style="width: 100%; height: 500px; text-align: center"><img src="/images/ajax-loader.gif"> Идет загрузка...</div>');
	      new Request({
						method: 'post',
						url: posthref,
						'link': 'ignore',
						data: {
							ajaxport: 1,
							idn: idnews
						},
						onSuccess: function(response) {
						$('ajax-load').set('html',response);
					   
						var tmp = navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') < 1 ? 1 : 0;
							if(tmp) var isIE = document.namespaces ? 1 : 0;
						 if(isIE){addIEReflex(); }else {addReflex()};
						 	
					   }
					}).send();
	     
}

//Расставим для ячеек положения
$$('.list_news a ').each(function(item) {
	var tmp = item.getOffsets();	
	 item.set('rel1',tmp.x);
});

});
