$(document).ready(
  function(){
    
    /*
    ** Setup banners
    */    
    var i = (RBD.length - 1);
    $('#prieksapmaksa div.tab-class, #pieslegums div.tab-class').find('a').each(
      function(){
        var tmpRegx = /<url>(.*?)<\/url><img>(.*?)<\/img>/.exec(RBD[i]);
            
        jQuery.data($(this).get(0), 'data', {
          url: tmpRegx[1],
          img: tmpRegx[2]
        });

        // banner preload :>
        $('body').append('<img id="tmp" style="display: none;"/>');
        $('#tmp').attr("src", tmpRegx[2]).remove();

        i--;
      }
    );
    
    /*
    ** Choose default one (first)
    */    
    $('#prieksapmaksa, #pieslegums').each(
      function(){
        $(this).find('div.main-split-background')
            .css('backgroundImage', "url('" + jQuery.data($(this).find('a.main-tab-startof').get(0), 'data').img + "')");
            
        $(this).find('a.main-split-inner-content')
            .attr('href', jQuery.data($(this).find('a.main-tab-startof').get(0), 'data').url);
      }
    );
    
    
    /*
    ** What do when user click on Tab button
    */    
    $('#prieksapmaksa, #pecapmaksa').each(
      function(){
        $('a.main-tab-endof, a.main-tab, a.main-tab-startof').click(
          function(){
            $(this.parentNode.parentNode.parentNode).find('div.main-split-background')
                .css('backgroundImage', "url('" + jQuery.data($(this).get(0), 'data').img + "')");
                
            $(this.parentNode.parentNode.parentNode).find('a.main-split-inner-content')
                .attr('href', jQuery.data($(this).get(0), 'data').url).show("hide").show("slow");
                
            $(this.parentNode).find('a').each(
              function(){
                $(this).css('backgroundImage', "url('../i/main-page/" + $(this).attr('class') + ".png')");
                $(this).hover(
                  function() {
                    $(this).css('backgroundImage', "url('../i/main-page/" + $(this).attr('class') + "-hover.png')");
                  },
                  function() {
                    $(this).css('backgroundImage', "url('../i/main-page/" + $(this).attr('class') + ".png')");
                  }
                );
              }
            );
            
            $(this).css('backgroundImage', "none");
            $(this).hover(
              function() {
                $(this).css('backgroundImage', "none");
              },
              function(){
                $(this).css('backgroundImage', "none");
              }
            );
            
          }
        );
      }
    );
    
    /*
    ** Setup drop down menu
    */    
    $('#prieksapmaksa a.main-split-title, #pieslegums a.main-split-title').each(
      function(){
        $(this).hover(
            
          function(){
            // Clear everything up, before start
            $('#pieslegums, #prieksapmaksa').each(
              function(){
                clearTimeout(jQuery.data($(this).get(0), 'hover-data'));
                $(this).find('div.drop-down-menu').hide();
              }
            );
                
            // Show menu
            $(this.parentNode).find('div.drop-down-menu').show();
                
            // Correct right side div to be same height as all menu
            $(this.parentNode).find('div.drop-down-menu-right-side').height(
              ($(this.parentNode).find('div.drop-down-menu-text').height() + 10) + 'px'
            );
          },
              
          function(){
            jQuery.data($(this.parentNode.parentNode).get(0), 'hover-data', 
              setTimeout(
                function(){
                  // Hide menu after 1 sec
                  $(this.parentNode).find('div.drop-down-menu').hide();
                },
                1000
              )
            );
          }
        );
            
            
        $(this.parentNode.parentNode).find('div.drop-down-menu').hover(
          function(){
            // If user now hovering on drop down menu, don't hide menu
            clearTimeout(jQuery.data($(this.parentNode.parentNode).get(0), 'hover-data'));
          },
                  
          function(){
            jQuery.data($(this.parentNode.parentNode).get(0), 'hover-data', 
            setTimeout(
              function(){
                // Hide menu after 1 sec
                $(this.parentNode).find('div.drop-down-menu').hide();
              },
              1000
            )
            );
          }
        );
      }
    );
    
    $('body').addClass('fp');
    
  }
);