var current_tab = 1;
$(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'));
  });

  /*-------------------- MT4 Rotator -------------------*/
  var container = $(".mtTabs-wrapper");
  var sTerval   = setInterval("mqRotate()",5000);
  var Tclass    = "";

  for(var i = 1; i <= $(".mt-content").length ; i++){
    if(i == 1) Tclass = "active";
    else Tclass = "";
    $(".mt-controls").append("<a class='ctrl-button "+Tclass+"' href='javascript:;'><span>"+i+"</span></a>");
  }

  $(".mt-content:first").show();
  $(".mt-title").text($(".mt-content:first").attr("title"));
  $(".ctrl-button").click(function(){
    if(!$(this).hasClass("active")){
      clearInterval(sTerval);
      current_tab = $(this).index();
      mqRotate();
      sTerval = setInterval("mqRotate()",5000);
    }
  });

  var cTerval = setInterval(function(){
    if($(".img-mt").css('display') == "block")
      $(".img-mt").fadeOut("fast");
    else
      $(".img-mt").fadeIn("fast");
  },100);
  /*----------------------------------------------------*/

  $(".websites-wrapper .items div.new").append("<img class='new-website' src='media/images/new-website-ribbon.png' />");

});

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');
      });
    }
  });
}

function mqRotate(){
  var container = $(".mtTabs-wrapper");
  var totale = container.find(".mt-content").size();

  if (current_tab >= totale) current_tab = 0;
  container.find(".mt-content").not(":eq("+current_tab+")").fadeOut("slow").end().filter(":eq("+current_tab+")").fadeIn("slow");
  container.find(".ctrl-button").filter(":eq("+current_tab+")").addClass("active").end().not(":eq("+current_tab+")").removeClass("active");
  $(".mt-title").text($(".mt-content").eq(current_tab).attr("title"));
  current_tab++;
}
