/* Функция для выравнивания новостей на главной странице */
function newsonmain() {
    
      // Есть ли  елемент с id=content  и есть ли элементы в .b-narod-news 
      if(false == $chk($('content')) || $$('.b-narod-news table td').length == 0) {
            
            // если нет - выйти из функции
            return;
      }
      
      cwidth = parseInt($('content').getStyle('width'));   
      width = 0;  
        
      $$('.b-narod-news').each(function(div) {
          
        width = 0;   
        div.getElements('table td').each(function(el) {
          
          w1 = parseInt(el.getStyle('width'));
          t = width + w1 + 80;
          
         // alert('w: ' + w1+ ', t ' + t + ', cwidth ' + cwidth);
          if(t  > cwidth) {
            //$$('.b-narod-news').setStyle('width', t);
            el.dispose(); 
          }
          else 
            width += w1;
         
         }); 
      });
      
}

/* Функция плавного передвижения меню */
function menutween() {
    
    // Есть ли  меню с классом tween ?
    if($$('.tween').length == 0) {
        
        // если нет - выйти из функции
        return;
    }
    
        $$('.tween').addEvent('click', function() {
        //now select the element you want to tween
        //then tack on .tween
        //finally declare the style property and value you want to tween to
           // alert(this.getCoordinates().top);
            if(Browser.Engine.trident == 1 && (Browser.Engine.version < 5)) {    
                $('current').get('tween', {property: 'top', 'duration': 'long'}).start(this.getCoordinates().top);
                
            }
            else {                                                                     
                $('current').get('tween', {property: 'top', 'duration': 'long'}).start(this.getCoordinates().top-267);
            }
            
            $$('.tween').removeClass('current');
            this.addClass('current');
            //this.get('tween', {property: 'opacity', duration: 'long'}).start(0);
            return false;

        });
}

/* Функция для логина\пароля  */
function loginblock() {
    
    // Есть ли  елемент с id=login
    if(false == $chk($('login'))) {
        
        // если нет - выйти из функции
        return;
    }
    
       $('login').addEvent('focus', function() {
                
            if(this.value == 'Логин') {
                this.value = '';
                this.setStyle('color', '#000');
            }
            
        });
        
        $('login').addEvent('blur', function() {
            if(this.value == '')  {
                this.value = 'Логин';
                this.setStyle('color', '#606060');  
            }
        });
        
        $('password').addEvent('focus', function() {
            if(this.value == 'Пароль') {
                this.value = '';
                this.setStyle('color', '#000');
            }
        });
        
        $('password').addEvent('blur', function() {
            if(this.value == '') {
                this.value = 'Пароль';
                this.setStyle('color', '#606060');
            }
        });
}

function iebugfix() {
    
    if(Browser.Engine.trident == 1 && Browser.Engine.version <= 5) { // ie7 
        if(parseInt(($$('body').getStyle('width'))) == 1260) {
            $$('body').setStyle('overflow-x', 'hidden');
            $$('html').setStyle('overflow-x', 'hidden');
        }
            
    }
}

function sync(url, data, div) {

              if(div == undefined || div == 'none')
                 div = 'content';

               $(div).setStyle('opacity', 0);
               var req = new Request.HTML({  
                   method: 'post',  
                   url: url,
                   data: data,
                   update: div,
                   onRequest: function() { 
                        $(div).set('html', '<div align="center"><img src="css/img/ajax-loader.gif" /></div>'); 
                   },  
                   onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) { 
                       $(div).fade('in');   
                       //alert(responseHTML);
                   }
               }).send();  


         return false;
}

function ajaxlinks() {
    
    $$('.sync').each( function(el) {
        
        el.addEvent('click', function(e) {
            sync(el.getProperty('href'), 'ajax=1', 'content' );
            return false;
        });
    });
}


window.addEvent('domready', function() { 
      

    
          iebugfix();
          newsonmain();
          menutween();
          loginblock();
          ajaxlinks();
          

});
