// detectBrowser
// Checks the browser and adds classes to the body to reflect it.
function detectBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    // Is this a version of IE?
    if($.browser.msie){
        $('body').addClass('browserIE');
        
        // Add the version number
        $('body').addClass('browserIE' + $.browser.version.substring(0,1));
    }

    // Is this a version of Chrome?
    if($.browser.chrome){
    
        $('body').addClass('browserChrome');
        
        //Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserChrome' + userAgent);
        
        // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
        $.browser.safari = false;
    }
    
    // Is this a version of Safari?
    if($.browser.safari){
        $('body').addClass('browserSafari');
        
        // Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserSafari' + userAgent);
    }
    
    // Is this a version of Mozilla?
    if($.browser.mozilla){
        
        //Is it Firefox?
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
            $('body').addClass('browserFirefox');
            
            // Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,1);
            $('body').addClass('browserFirefox' + userAgent);
        }
        // If not then it must be another Mozilla
        else{
            $('body').addClass('browserMozilla');
        }
    }
    
    // Is this a version of Opera?
    if($.browser.opera){
        $('body').addClass('browserOpera');
    }
}

// onload
$(document).ready(function(){
	detectBrowser();
	
	// init navigation menus
	$('.browserIE6 #nav-top li, .browserIE7 #nav-top li')
		.mouseover(function(){
			$(this).addClass('over');
		})
		.mouseout(function(){
			$(this).removeClass('over');
		});
	
	// set search input behavior
	var prefill = 'Search MAT Site';
	$("#keywords")
		.addClass('prefilled')
		.val(prefill)
		.focus(function(){
			$(this).removeClass('prefilled')
			if ($(this).val() == prefill) {
				$(this).val('');
			}
		})
		.blur(function(){
			if ($(this).val() == '') {
				$(this).addClass('prefilled')
				$(this).val(prefill);
			}
		});
	
	// start slideshow on home page
	if ($('body').attr('id') == 'page-home') {
		
		$('#page-home .tabs').tabs('#slideshow .slide', {
			effect: 'fade', 
			fadeOutSpeed: "slow",
			rotate: true
		}).slideshow({
			autoplay: true,
			interval: 6000,
			clickable: false
		});
		
		var tabs_api = $('#page-home .tabs').tabs();
		
		// stop slideshow on link click
		$('#slideshow .slide a').click(function(){
			tabs_api.stop();
		});
		
	}
	
	// set external links to new window
	$('a.external').attr('target','_blank');
	
	// sidebar contact validation
	$('#form_contact')
		.submit(function(){
			
			$(this).find('.error').remove();
			
			var err = 0;
			
			if ($("input[name=firstname]").val() == '')		err = 1;
			if ($("input[name=lastname]").val() == '')		err = 1;
			if ($("input[name=email]").val() == '')			err = 1;
			if ($("input[name=company]").val() == '')		err = 1;
			if ($("input[name=interest]").val() == '')		err = 1;
			
			if (err == 1) {
				$(this)
					.find('fieldset')
					.prepend('<div class="error">Please complete all fields.</div>');
				return false;
			}
			
			return true;
			
		});
	
	// sidebar 3g validation
	$('#sidebar_3g')
		.submit(function(){
			
			$(this).find('.error').remove();
			
			var err = 0;
			
			if ($("input[name=lastname]").val() == '')		err = 1;
			if ($("input[name=email]").val() == '')			err = 1;
			if ($("input[name=company]").val() == '')		err = 1;
			if ($("input[name=phonenum]").val() == '')		err = 1;
			
			if (err == 1) {
				$(this)
					.find('fieldset')
					.prepend('<div class="error">Please complete all fields.</div>');
				return false;
			}
			
			return true;
			
		});
	
	// contact us form validation
	$('#contact_us_form')
		.submit(function(){
			
			$(this).find('.error').remove();
			
			var err = 0;
			
			if ($("input[name=firstname]").val() == '')		err = 1;
			if ($("input[name=lastname]").val() == '')		err = 1;
			if ($("input[name=email]").val() == '')			err = 1;
			if ($("input[name=company]").val() == '')		err = 1;
			if ($("input[name=jobtitle]").val() == '')		err = 1;
			if ($("input[name=phonenumber]").val() == '')	err = 1;
			if ($("input[name=interest]").val() == '')		err = 1;
			
			if (err == 1) {
				$(this)
					.find('fieldset:first')
					.prepend('<div class="error">Please complete all fields.</div>');
				return false;
			}
			
			return true;
			
		});
	
});

// font replacement
Cufon.replace('#page-title span',{textShadow: '3px 3px rgba(0, 0, 0, 0.175), 2px 2px rgba(0, 0, 0, 0.35)'})
     .replace('h2, #main h3, #supporting h3, table caption, .slide .desc');