$(document).ready(function() {

    // If there is a hash / named anchor in the link, avoid scrolling to it 
    // in the browser like a normal anchor - we only want it's contents to 
    // show. The page should appear at the top as usual.
    setTimeout(function() {
      if (location.hash) {
        window.scrollTo(0, 0);
      }
    }, 1);
    
    /*
    // Browser sniff IE7, change z-index values so that dropdown menu does not sit behind headings
    if($.browser.msie && parseInt($.browser.version, 10) == 7) {
    
        alert('IE7');
    
        $(function() {
            var zIndexNumber = 1000;
            $('div').each(function() {
                $(this).css('zIndex', zIndexNumber);
                zIndexNumber -= 10;
            });
        });
    }   
    */


    //$('.tab-head').css('color','#004f9d');

    
    // IE does not like opacity:0; in css, unless it is dealt with by jQuery. 
    // This causes an annoying flutter whilst this loads and does it's thing.
    // SO - Set the original divs to display:none and only switch back to 
    // display:block; when opacity has been set correctly.
    $('.tab-head').animate({ opacity: 0 }, 1, function() {  
        // done
        
        $('.tab-sub').animate({ opacity: 0 }, 1, function() {  
            // done
            
            $('.tab-head-0').animate({ opacity: 100 }, 1, function() {
            
                $('.tab-head-0').show(1);
            });
            
            $('.tab-sub-0').animate({ opacity: 100 }, 1, function() { 
            
                $('.tab-sub-0').show(1);                
            });
            
            // Set divs back to display block so that they will show after being animated in
            // The fade comes from animating the opacity - further down this file..
            $('.tab-head').css('display','block');
            $('.tab-sub').css('display','block');
            
        });
    });
    
    
    // Init superfish menu
    $('ul.sf-menu').superfish(); 
    
    
    
    

    // Initialise tab navigation
	//$('#main-nav > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
    
    // Bind function to tab click event and change heading/subheading text
    // Increase the first value passed to fadeIn() or fadeOut() to see
    // animation take place
    $('#tabs').bind('tabsselect', function(event, ui) {
        
        //var tab_num = ui.index + 1;
        
        // Makes divs jump about due to using display:none;
        /*
        $('.tab-head').fadeOut(1, function() {
            // Animation complete.
            
            $('.tab-head-' + ui.index).fadeIn(1, function() {
                // Animation complete
            });

        });
        
        $('.tab-sub').fadeOut(1, function() {
            // Animation complete.
            
            $('.tab-sub-' + ui.index).fadeIn(1, function() {
                // Animation complete
            });
        });
        */
        
        $(".tab-head").animate({ opacity: 0 }, 1, function() {

            $('.tab-head-' + ui.index).animate({ opacity: 100 }, 1, function() {  
                // done
            });            
        });
        
        
        $(".tab-sub").animate({ opacity: 0 }, 1, function() {
        
            $('.tab-sub-' + ui.index).animate({ opacity: 100 }, 1, function() {  
                // done
            });
        });
      
    });

    
    // Switch tabs when one of these links are clicked when you are currently on the page in question..
    // E.G:
    // You are on this page: http://www.idaptestelle.gws/room_layout_planner/index.php?page=learn-suppliers
    // Then click a link which has href of:
    // http://www.idaptestelle.gws/room_layout_planner/index.php?page=learn-suppliers#tab2
    // OR:
    // http://www.idaptestelle.gws/room_layout_planner/index.php?page=learn-suppliers#tab4
    // Also detect if the link is to another page, if so reload as normal..
    $('ul.sf-menu li a').click(function() {
      
        var same_page = false;
        var href = $(this).attr('href');
      
        var split = new Array();
        split = href.split("#");
        
        // Need to check if current page is same as link href too...
        var cur_url = document.URL;
        
        var current_url_split = cur_url.split("?page=");
        var target_href_split = href.split("?page=");
        
        // Remove numbers from string.. e.g. tab#1 - as it causes the IF to fail below
        current_url_split[1] = current_url_split[1].replace(/[0-9]/g, '');
        target_href_split[1] = target_href_split[1].replace(/[0-9]/g, '');
        
        // Prints array out.. handy        
        /*
        $.each(current_url_split, function(key, value) { 
          alert('current_url_split: ' + key + ': ' + value); 
        });
        */
        
        //alert(target_href_split[1] + ' compare: ' + current_url_split[1]);
        
        
        if(target_href_split[1].indexOf(current_url_split[1]) != -1)
        {
            //alert('targets are the same, jump tab');
            same_page = true;
        }
        else
        {
            //alert('targets different, load new page');
        }
        
          
        if(split.length == 2 && same_page == true)
        {
          var tab_sel = split[1];
          $('#tabs').tabs('select', tab_sel);
          return false;
        }          
    });
    
    
    // Do the same as above, but for the quick links dropdown at the top.
    $('#footer ul.structure li a').click(function() {
    
      var same_page = false;
      var href = $(this).attr('href');
      
      var split = new Array();
      split = href.split("#");
      
      // Need to check if current page is same as link href too...
      var cur_url = document.URL;
      var current_url_split = cur_url.split("?page=");
      var target_href_split = href.split("?page=");
      
      // Remove numbers from string.. e.g. tab#1 - as it causes the IF to fail below
      // Quick & dirty!
      current_url_split[1] = current_url_split[1].replace(/[0-9]/g, '');
      target_href_split[1] = target_href_split[1].replace(/[0-9]/g, '');
        
      //alert(current_url_split[1]);
      //alert(target_href_split[1]);
        
      
      if(target_href_split[1].indexOf(current_url_split[1]) != -1)
      {
            same_page = true;
      }
      
      if(split.length == 2 && same_page == true)
      {
        var tab_sel = split[1];
        $('#tabs').tabs('select', tab_sel);
        return false;
      }
      
    });
 
    

});




