jQuery(document).ready(function(){
	
  /*  Show / Hide submenu   */
  jQuery('.with-submenu').slightSubmenu({
    buttonActivateEvents : 'click',
    handlerButtonIn : function($submenu) {
      $submenu.show(200);
    },
    handlerForceClose : function($submenu) {
      $submenu.hide(200);
    }
  });

  /*  Multiselect   */
  jQuery(".dropdowns select").multiselect({
    header: false,
    height: 175,
    minWidth: 225,
    classes: '',
    checkAllText: 'Check all',
    uncheckAllText: 'Uncheck all',
    noneSelectedText: 'Валюта',
    selectedText: false,
    //selectedText: '# selected',
    selectedList: 0,
    show: null,
    hide: null,
    autoOpen: false,
    multiple: true,
    position: {}
  });
  
  /*  Init scrollbar   */
  var scrollbar = jQuery(".scrollbar-inner");
  scrollbar.scrollbar();

  var report = jQuery('.report.scrollbar-inner');
  report.scrollbar();
  
  /*  Show / Hide configuration block  */
  jQuery('.conf-wrapper').hide();
  jQuery('.conf-toggle').click(function(e){
    e.preventDefault();
    jQuery(this).toggleClass('active');
    jQuery('.conf-wrapper').slideToggle(200).promise().done(function() {
    	scrollToSelectedReport();
    });
    
    if (jQuery(this).hasClass('active')) {
      jQuery('span', this).html(hideConfig);
    }
    else {
      jQuery('span', this).html(showConfig);
    }
   
  });
  /*  Show / Hide configuration block  */
  jQuery('.conf-wrapper-modif').hide();
  jQuery('.conf-toggle-modif').click(function(e){
    e.preventDefault();
    jQuery(this).toggleClass('active');
    jQuery('.conf-wrapper-modif').slideToggle(200);
    if (jQuery(this).hasClass('active')) {
      jQuery('span', this).html(hideModif);
    }
    else {
      jQuery('span', this).html(showModif);
    }
  });
 

  /*  Datepicker  */
  jQuery( "#from" ).datepicker({
    showOn: "focus",
    defaultDate: "+1w",
    changeMonth: false,
    numberOfMonths: 1,
    onClose: function( selectedDate ) {
      jQuery( "#to" ).datepicker( "option", "minDate", selectedDate );
    }
  });
  jQuery( "#to" ).datepicker({
    showOn: "focus",
    defaultDate: "+1w",
    changeMonth: false,
    numberOfMonths: 1,
    onClose: function( selectedDate ) {
      jQuery( "#from" ).datepicker( "option", "maxDate", selectedDate );
    }
  });


  /*   Select / Unselect Nested checkboxes   */
  jQuery('input[type="checkbox"]').change(function(e) {
    var checked = jQuery(this).prop("checked"),
      container = jQuery(this).parent().parent(),
      siblings = container.siblings();

    container.find('input[type="checkbox"]').prop({
      indeterminate: false,
      checked: checked
    });

    function checkSiblings(el) {
      var parent = el.parent().parent(),
        all = true;

      el.siblings().each(function() {
        return all = (jQuery(this).children().children('input[type="checkbox"]').prop("checked") === checked);
      });

      if (all && checked) {
        parent.children().children('input[type="checkbox"]').prop({
          indeterminate: false,
          checked: checked
        });
        checkSiblings(parent);
      } else if (all && !checked) {
        parent.children().children('input[type="checkbox"]').prop("checked", checked);
        parent.children().children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
        checkSiblings(parent);
      } else {
        el.parents("li").children().children('input[type="checkbox"]').prop({
          indeterminate: true,
          checked: false
        });
      }
    }

    checkSiblings(container);

  });

  /*  Font-size change   */
  jQuery(".change-font .size-small").click(function(event){
    event.preventDefault();
    jQuery(this).parent().find('a').removeClass('active-size');
    jQuery(this).addClass('active-size');
    jQuery("html").css({"font-size":"100%"});
  });

  jQuery(".change-font .size-medium").click(function(event){
    event.preventDefault();
    jQuery(this).parent().find('a').removeClass('active-size');
    jQuery(this).addClass('active-size');
    jQuery("html").css({"font-size":"115%"});

  });

  jQuery(".change-font .size-big").click(function(event){
    event.preventDefault();
    jQuery(this).parent().find('a').removeClass('active-size');
    jQuery(this).addClass('active-size');
    jQuery("html").css({"font-size":"130%"});

  });
  /*begin. change locale added block*/
  jQuery(".block-locale .ro").click(function(event){
	    event.preventDefault();
	    
	    jQuery(this).parent().find('li').removeClass('active-lg');
	    jQuery(this).addClass('active-lg');
	    
	    var h = location.href;
	    if(h.indexOf("DON1")>0){
	    	jQuery(".block-locale  .ru").addClass('active-lg1');
	    	jQuery(".block-locale  .en-gb").addClass('active-lg1');
	    }
  });

  jQuery(".block-locale  .ru").click(function(event){
	    event.preventDefault();
	    jQuery(this).parent().find('li').removeClass('active-lg');
	    jQuery(this).addClass('active-lg');
  });

  jQuery(".block-locale .en-gb").click(function(event){
	    event.preventDefault();
	    jQuery(this).parent().find('li').removeClass('active-lg');
	    jQuery(this).addClass('active-lg');
  });
  /*end. change locale added block*/
  /*  hidden block   */
  jQuery('.contacts-block').hide();

  jQuery('.phone-button').click(function(e) {

      e.stopPropagation();
    jQuery('.region-header-right>.block>.content>a').removeClass('active');
      jQuery(this).toggleClass('active');
      jQuery('.contacts-block').toggle();
      jQuery('#block-interface-ext-help-block').hide();
      e.preventDefault();

  });
  var $contacts = jQuery('.contacts-text>div>div');
  $contacts.hide();
  jQuery('.contacts-text div div:first').show();
  jQuery('.contact-titles li:first a').addClass('active-tab');

  jQuery('.contact-titles li a').click(function(){
    jQuery('.contact-titles li a').removeClass('active-tab');
    jQuery(this).addClass('active-tab');
    var currentTab = jQuery(this).attr('href');
    $contacts.hide();
    jQuery(currentTab).show();
    return false;
  });
  jQuery('#block-interface-ext-help-block').hide();

  jQuery('.help-center-button').click( function(e) {

      e.stopPropagation();
      jQuery('.region-header-right>.block>.content>a').removeClass('active');
      jQuery(this).toggleClass('active');
      jQuery('#block-interface-ext-help-block').toggle();
      jQuery('.contacts-block').hide();
      e.preventDefault();
  });
  /*begin. change locale added block*/
  var active_locale = jQuery('#locale-hidden').text();
  if(active_locale=="ro"){
	  jQuery(".block-locale .ro").click();
  }
  if(active_locale=="ru"){
	  jQuery(".block-locale .ru").click();
  }
  if(active_locale=="en"){
	  jQuery(".block-locale .en-gb").click();
  }

  /*end. change locale added block*/
  jQuery('.block-locale ul').css('position', 'absolute').css('width', '100%').hide();
  
  var active_txt = jQuery('.block-locale li.active-lg').text();

  jQuery('.block-locale .content').prepend('<div class = "active-lang">'+active_txt+'</div>');

  jQuery('.block-locale .active-lang').unbind('click');
  jQuery('.block-locale .active-lang').click(function(e){
    e.stopPropagation();
    jQuery('.region-header-right>.block>.content>a').removeClass('active');
    jQuery('#block-interface-ext-help-block').hide();
    jQuery('.contacts-block').hide();
    jQuery('.block-locale ul').toggle();
    e.preventDefault();
  });


  jQuery(document).click(function() {
    jQuery('.block-locale ul').hide();
    jQuery('#block-interface-ext-help-block').hide();
    jQuery('.contacts-block').hide();

  });
  /*  Tooltips */

  jQuery('.header [title!=""]').qtip({
    position: {
      my: 'top center'
    },
    style: {
      classes: 'qtip-bnm',
      width: '240px'
    }
  });
  jQuery('.header-small [title!=""]').qtip({
	    position: {
	      my: 'top center'
	    },
	    style: {
	      classes: 'qtip-bnm',
	      width: '60px'
	    }
	  });  
  
  jQuery('[data-tooltip!=""]').qtip({ // Grab all elements with a non-blank data-tooltip attr.
    content: {
      attr: 'data-tooltip' // Tell qTip2 to look inside this attr for its content
    },
    position: {
      my: 'top center'
    },
    style: {
      classes: 'qtip-bnm',
      width: '240px'
    }
  })  
});

/**
 * Poziționează bara de derulare verticală în cadrul componentei de selecție a rapoartelor, astfel încât să fie vizibil raportul curent selectat 
 */
function scrollToSelectedReport() {
	var selectedReport = jQuery('#treeMenu .ui-treenode-selected');

	if(selectedReport.length !== 0) {
		var scrollContainer = selectedReport.closest('.options.scroll-content');
		var topPos = selectedReport.offset().top - scrollContainer.offset().top;
		scrollContainer.scrollTop(topPos);
	}
}