$(document).ready(function(){
  /*------------ Menu -------------*/
  
  $('.parents_link a').mouseover(function(){
    showSubMenu($(this).parent().attr('id'));
  });
  
   $('.main_menu_wrapper').mouseleave(function(){
   $id = $('.active_menu').attr('id');
   showSubMenu($id);
  });
  
  $('#page_up').click(function(){
    $('html, body').animate({scrollTop: 0}, 'slow');
  });

  initalizeTabs();
  
  $('.other_products_wrapper .title').click(function(){    
    if($(this).attr('class') == 'title active') return;
    setActiveTab($(this).attr('id'));
  });
  
});

function showSubMenu(id){
      
  id = id.split('_');
  id = id[1];
    
  if($('#child_' + id).css('display') == 'block') return;
   
  $('.childs_links div').hide();
  $('#child_' + id).show();
  
}

function initalizeTabs(){
  
  $('.other_products_wrapper .title').each(function(current){
    $(this).attr('id', 'accor-title_' + current);
  });
  
  $('.other_products_wrapper .content').each(function(current){
    $(this).attr('id', 'accor-content_' + current);
  });
  
  $('#accor-title_0').removeClass('inactive').addClass('active');
  
  $('#accor-content_0').animate({
    width: 'toggle'
  }, 500, function(){
    $('#accor-content_0').removeClass('content-inactive').addClass('content-active');
  });
}

function setActiveTab(id){
  
  id = id.split('_');
  id = id[1];
    
  $('.other_products_wrapper .active').removeClass('active').addClass('inactive');
  $('#accor-title_'+id).removeClass('inactive').addClass('active');
  
  $('.other_products_wrapper .content-active div').fadeOut('fast');
  $('#accor-content_' + id + ' div').fadeOut('fast');
        
  $('.other_products_wrapper .content-active').animate({
    width: 'toggle'
    }, 500, function() {
      $('.other_products_wrapper .content-active').removeClass('content-active').addClass('content-inactive');
      $('#accor-content_' + id).animate({
        width: 'toggle'
        }, 500, function(){
          $('#accor-content_' + id + ' div').fadeIn('fast');
          $('#accor-content_' + id).removeClass('content-inactive').addClass('content-active');
      });       
    });
}

function getLatestPlugins(){
  $.ajax({
    cache: false,
    url: 'modules/plugins_scroller/plugins_scroller.php?action=getlatestplugins',
    success: function(data){
      $('#latest_plugins').fadeOut('slow', function(){
        $('#latest_plugins').html('');
        $('#latest_plugins').html(data);
        $('#latest_plugins').fadeIn('slow');
      });
    },
    error: function(){
      $('#latest_plugins').fadeOut('slow', function(){
        $('#latest_plugins').html('');
        $('#latest_plugins').html('<p align="center" class="planet">Internal error happen while retiving, please try again</p>');
        $('#latest_plugins').fadeIn('slow');
      });
    }
  });
}