﻿$(document).ready(function(){
    
    $(document).pngFix();

    var currUrl = location.href;
    
    // Highlight link in main nav bars
    if (currUrl.indexOf('.aspx') == -1){
        $('#header .nav li:first').addClass('on');
    }
    else{
        var currPage = currUrl.split('/')[currUrl.split('/').length -1];
        $('li a[href*=' + currPage + ']').parent().addClass('on');
        
        var currSection = currUrl.split('/')[currUrl.split('/').length -2];
        if (currSection.toLowerCase() != 'category' && currSection.toLowerCase() != 'video'){
            $('#categories li a[href*=' + currSection + ']').parent().addClass('on');
        }
    }
    
    // Make parts table scrollable if there is more than 5 rows
    
    $('.parts').each(function(){
        if ($(this).find('tbody tr').length > 5){
            $(this).addClass('scrollable');
        }
    });
    
    // Product images large version
    $('.product .right img').mouseover(function(e){
        $('body').append('<div id="detail"><img src="_images/large/' + $(this).attr('id') + '.jpg" /></div>');
        //$('#detail').css('left',e.pageX - $('#detail').width() - 10 + 'px').css('top',e.pageY - $('#detail').height() - 10 + 'px').show();
        $(this).mousemove(function(e){
            $('#detail').css('left',e.pageX - $('#detail').width() - 20 + 'px').css('top',e.pageY + 10 + 'px').show();
        });
        $(this).mouseout(function(){
            $('#detail').remove();    
        });
    });
    
    // Contact form validation and storing data
    $('.submit').click(function(){
		if ($(this).parents('form').find('.mandatory').length > 0){
		    $('.mandatory').each(function(){
			    if (this.value == ''){
				    $(this).addClass('error');
			    }
		    });
		    if ($('.error').length > 0){
			    alert('Please complete all fields marked with an asterisk.');
			    return false;	
		    }
		}
	});
	
	$('.mandatory').blur(function(){
		if (this.value != ''){
			$(this).removeClass('error');
		}		
	});
    
});