$(document).ready(function(){

    $('#content #content-inner #thumbs .image img').click(function(){
        $('#content #content-inner #thumbs img').removeClass('selected');
        $(this).addClass('selected');

        var item = $(this).next('input');
        var position = '2px -7px';
        if(item.attr('name') == 'matrix_hero_thumbnail'){
          //  item='matrix_hero';
            position='0px 0px';
        }
        var image = item.val();

        productGallery(image, position);
    });
    
    function productGallery(image, position){
        $('#content #content-inner #video').hide();
        $('#content #content-body').css('background', 'none');
        $('#content #content-body').css('background', 'url(/' + image + ') ' + position + ' no-repeat');
    }
    
    $('#content #content-inner #thumbs img:eq(0)').addClass('selected');
    
    $('#content #content-inner #thumbs .image:eq(0), #content #content-inner #thumbs .image:eq(1), #content #content-inner .image:eq(2)').css('margin', '0px 0px 7px 0px');
    $('#content #content-inner #thumbs .image:eq(1), #content #content-inner #thumbs .image:eq(4)').css('margin', '0px 8px');
    
    $('#content #content-inner #thumbs .image img').mouseenter(function(){
        $(this).addClass('hovered');
    });
    
    $('#content #content-inner #thumbs .image img').mouseleave(function(){
        if ($(this).attr('class')!='selected'){
            $(this).removeClass('hovered');
        }
    });
    
    $('#content #content-inner #features .feature:eq(0)').css('margin-right', '26px');
    $('#content #content-inner #features .feature:eq(1)').css('margin-right', '26px');
    $('#content #content-inner .video a').click(function(e){ 
        var link = $(this).prev().text(); 
        if(link.search(/youtube/i)>-1){
            var mov = '<object width="640" height="385"><param name="movie" '
                + 'value="' + link + '&autoplay=1"> '
                + '</param><param name="allowFullScreen" value="true"></param>'
                + '<param name="wmode" value="opaque" /><param '
                + 'name="allowscriptaccess" value="always"></param><embed '
                + 'src="' + link + '&autoplay=1" '
                + 'type="application/x-shockwave-flash" allowscriptaccess="always" '
                + 'wmode="opaque"'
                + 'allowfullscreen="true" width="640" height="385"></embed></object>';
            $('#content #content-inner #video').show();
            $('#content #content-body').css('background', 'none');
            $('#content #content-inner #content-body #video').html(mov);
        }
        else {
            var video_width = 926;
            var video_height = 703;
            if($('.video-width').length > 0){
                video_width = Number($('.video-width').text());
            }
            if($('.video-height').length > 0){
                video_height = Number($('.video-height').text());
            }
            e.preventDefault();
            $.nyroModalManual({
              url: link,
              width: video_width,
              height: video_height,
              closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close"></a>',
              wrap: {
                iframe: '<div class="functionaldemo"></div>'
              }
            });
            $('#nyroModalWrapper').css({'background' : 'none', 'padding' : '0'});
            if(navigator.userAgent.toLowerCase().indexOf('chrome')>-1){
                //The button is behind the flash in Chrome.  This style doesn't get
                //applied even without the conditional.  Came up empty handed with
                //google.
                $('#nyroModalWrapper #closeBut').css({'position' : 'absolute', 'top' : '0px', 'left' : '0px'});
            }
            return false;
        }
    });
    
    
    //Specs block
    $('#specs #specs-inner ul.treadmills li a').mouseenter(function(){
        //$('#specs #specs-inner ul.treadmills').css('background', 'url(/images/images_specsNav/T7xe.jpg) no-repeat 65px 20px')
    });
    $('#specs #specs-inner ul.treadmills li a').mouseleave(function(){
        //$('#specs #specs-inner ul.treadmills').css('background', 'none')
    });
    
    //Specifications page
    $('#content #content-inner .specifications table tbody tr:odd').css('background', '#b4bac1'); //light old: #a3a6ae
    $('#content #content-inner .specifications table tbody tr:even').css('background', '#91969b'); //dark old: #8e9096
    $('#content #content-inner .specifications table:last-child tbody tr:odd').css('background', '#a3a6ae');
    $('#content #content-inner .specifications table:last-child tbody tr:even').css('background', '#8e9096')
    $('#content #content-inner .specifications table tbody td:even').css({'text-align' : 'right', 'width' : '215px', 'color' : '#303539'});
    $('#content #content-inner .specifications table tbody td:odd').css({'width' : '475px', 'color' : '#42484d'});
    $('#content #content-inner .specifications table tfoot tr').css('background-color', 'transparent');
    $('#content #content-inner .specifications #left_col').corner('14px');
    //$('#content #content-inner table:last-child').corner('bottom 14px');
    
    //Features page
    $('#content #content-inner #content-body #key .feature:eq(1), #content #content-inner #content-body #key .feature:eq(4)').css('margin', '0px 25px');
    $('#content #content-inner #content-body #key .feature:eq(3), #content #content-inner #content-body #key .feature:eq(4), #content #content-inner #content-body #key .feature:eq(5)').css('margin-top', '35px');
    $('#content #content-inner .features').corner('14px');
    
    var url = $('#content #content-inner #content-body.product #features').attr('class');
    $('#content #content-inner #content-body.product #features').click(function(){
        window.location.replace('/product/features/' + url); 
    });
    
    $('#content #content-inner #content-body.product #features').mouseenter(function(){
        $(this).css('cursor', 'pointer'); 
    });
    
    $('#content #content-inner #content-body.product #features').mouseleave(function(){
        $(this).css('cursor', 'default'); 
    });
});

