$(document).ready(function(){
         
    // set the current nav item to selected based on page
    var head = $('#content-head').attr('rel');
    
    // required form field indicator
    $('.required').append('<span class="red"> * </span>');

    if($('#content-body').attr('rel')!=null){ //Check this because #content-body may not have a rel tag.
        var body = $('#content-body').attr('rel').replace(/(\D*)/,"").split("-");
        var pageId = $('#content-head').attr('rel');
        var nav  = $('#content-head ul').attr('rel');
        
        // set subnav
        /*$('#content-head ul li').each(function () {
             $(this).find('a').each(function() {
                 //if ($(this).attr('rel') == body[1]) {
                 //   $(this).addClass('selected');
                 //}
                 if ($(this).attr('rel') == nav) {
                    $(this).addClass('selected');
                 }
              });
        });*/
        
        // set submenu
        /*$('#menu_block div ul li').each(function () {
          $(this).find('a').each(function () {
            if ($(this).attr('rel') == pageId) {
              $(this).addClass('highlightLink');
              $(this).find('span').addClass('showArrow');
            }
          });
        });*/

    }
    
    // round corners on subNav
    var $len = $('#content-head ul li').length;
    $('#content-head ul li:eq(0) a').corner('left 15px');
    $('#content-head ul li:eq(' + ($len-1) + ') a').corner('right 15px');
    
    //Set space between content-head and content-body for static pages.
    //Don't apply for about or contact pages because those have a different style and are handled in the stylesheet.
    if(!$('#content-body').hasClass('product')
       && !$('#content-body').hasClass('features')
       && !$('#content-body').hasClass('comparison')
       && !$('#content-body').hasClass('specifications')
       && !$('#content-body').hasClass('about')
       && !$('#content-body').hasClass('contact')
       && !$('#content-body').hasClass('distributors')
       && !$('#content-body').hasClass('sitemap')
       && $('#content-body').hasClass('products')){
        $('#content-body').css('margin-top', '40px');
    }
    
    //Remove top padding on content-inner for Comparison page only.
    $('#content-body.comparison').parent().css('padding-top', '0px');
    
    //Product image pop-up in product block
    var isRequested = false;
    var container = $('<div class="tooltip"></div>');
    $('#specs #specs-inner ul li a').mouseenter(function(){
        var that = $(this);
        var ulWidth = that.parents('ul').width();
        var leftPosition = that.parents('ul').position().left;
        var rightPosition = leftPosition + ulWidth;
        var tooltipLeft = rightPosition - 160;
        var ulTop = that.parents('ul').position().top; 
        var top = ulTop + that.position().top - 40;
        tooltip(that, top, tooltipLeft);
    });
    $('#sitemap #sitemap-inner .category-block .col ul li a').mouseenter(function(){
        var that = $(this);
        var ulWidth = that.parents('ul').width();
        var leftPosition = that.parents('ul').position().left;
        var rightPosition = leftPosition + ulWidth;
        var tooltipLeft = rightPosition - 140;
        tooltip(that, that.position().top-40, tooltipLeft);
    });
    
    //Lang select
    $('#header #header_top_right .menu li a.worldwide').click(function(){ 
        $('#header #lang_select').slideToggle();
        return false;
    });
    
    //UK Facility Design
    $('#content #content-inner .facility-design .gallery').jCarouselLite({
        btnNext: '#next',
        btnPrev: '#prev',
        visible: 1 
    });
    
    $('#content .news-media .archive').click(function(){
        $('#content .news-media .archive-news').slideToggle();
    });
    
    //IT homepage form
    if($('form[name=myForm]').length > 0){
        $('form[name=myForm] input[type=text]').focus(function(){
            $(this).val('').blur(function(){
                if($(this).val() == ''){
                    $(this).val('Indirizzo email');
                }
            });   
        });
    }

    function tooltip(that, top, tooltipLeft) { 
        that.addClass('hovered');
        if(that.attr('id')!=''){ 
            if(isRequested==false){ 
                setTimeout(function(){
                    if ((that.attr('class') == 'hovered')){ 
                        var url = that.attr('id'); 

                        $.ajax({  
                            type: 'GET',  
                            url: '/product/product_block_image/' + url,   
                            success: function(data){ 
                                isRequested = true;
                                if(data){
                                    $('body').append(container); 
                                    $('.tooltip').css('top', top);
                                    $('.tooltip').css('left', tooltipLeft );
                                    $('.tooltip').html('<img src="/' + data + '" />');
                                    container.css('display', 'block'); 
                                }
                                else{ 
                                    $('.tooltip').remove();
                                }
                                isRequested = false;
                            },
                            dataType: 'text'
                        });  
                        
                    }
                }, 500)
            }
        }
    }
    
    $('#specs #specs-inner ul li a, #sitemap #sitemap-inner .category-block .col ul li a').mouseleave(function(){
        $(this).removeClass('hovered');
        container.css('display', 'none');
    });
    
    // check for cookie support
    var TEST_COOKIE = 'test_cookie';
    jQuery.cookie( TEST_COOKIE, true );
    if ( jQuery.cookie ( TEST_COOKIE ) ) {
      jQuery.cookie( TEST_COOKIE, null );
    }
    else {
      $('#ns').replaceWith('<div id="noscript-warning"><img src="/images/noscript-warning.png" alt="johnson health tech fitness treadmill elliptical" /></div>');
    }
  
});

