$(document).ready(function(){
	// Set menu dropdown
	$('ul.sf-menu')
		.supersubs({
			minWidth:    12,   // minimum width of sub-menus in em units 
			maxWidth:    36,   // maximum width of sub-menus in em units 
			extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
		})
		.superfish({ delay:800, speed:'fast' }); 
	
	// Product cycling
	$('#productfade')
		.before('<div id="productNav">')//.before('<div id="productPause"><a class="play" href="#">Play</a><a class="pause" href="#">Pause</a>')
		.cycle({ fx: 'scrollLeft', timeout: 9000, speed: 1000, pause:'true', pager:'#productNav' }); 
	/*
	$('a.pause').bind('click', function(){
		$('#productfade').cycle('pause');
		return false;
	});
	$('a.play').bind('click', function(){
		$('#productfade').cycle('play');
		return false;
	});
	*/			
	// Solutions cycling
	$('.solutions').cycle({ fx: 'fade', timeout: 5000, speed: 1500, pause:'true', next: '.solutions' });
	
	// Add colons ':' to label elements
	$('label').append(' :');
	
	// Resizeable text areas
	//$('textarea.resizable').autogrow();

	// Validate contact form
	$('#contactform').validate({
		messages: {
			yemail: { required: 'Valid email please.' },
			yname: { required: 'Your name please.' },
			ps: { required: 'Enter your message.'},
			question: { required: 'Break out a calculator!'}
		} 
	});
	$('input.submit').click(function() {
		if ($('#contactform').valid() ) {
			// process when the form is valid					
		}
	});

	// Defuscate email addresses
	$('.email').defuscate();
	
	// Search Autocomplete
	$('#q').autocomplete('/search_autocomplete.php', {
		delay: 50,
		minChars: 1,
		cacheLength: 10,
		onItemSelect: function(){ $('#search').submit(); },
		matchSubset: 1
	});

	// Distributor Page Select Boxes
	$('#distributorsSelect select').change(function(){
		var $item = $(this).val();
		if ( $item == 'all' ) {
			$('#distributors li').fadeIn('fast');
		} else {	
			$('#distributors li').not('.'+$item).fadeOut('slow');
			$('#distributors li.'+$item).fadeIn('fast');
		}
		// reset select boxes
		$('select').find('option:nth(0)').attr('selected','selected');
		$('select option').each(function(){
		  if ( $(this).val() == $item )
		    $(this).attr('selected','selected');
		});
	});

	// Google Analytics tracking
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function(){

        try {
            var pageTracker = _gat._getTracker("UA-100729-22");
            pageTracker._trackPageview();
        } catch(err) {}

        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;

        $('a').each(function(){
            var href = $(this).attr('href');

            if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
                $(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    pageTracker._trackEvent('External', 'Click', extLink);
                });
            }
            else if (href.match(/^mailto\:/i)){
                $(this).click(function() {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    pageTracker._trackEvent('Email', 'Click', mailLink);
                });
            }
            else if (href.match(filetypes)){
                $(this).click(function() {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    var filePath = href.replace(/^https?\:\/\/(www.)taoglas\.com\//i, '');
                    pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
                });
            }
        });
    });
    $('#search').submit(function(){
    	pageTracker._trackEvent('Search', 'Click', $('#q').val());
    });
    /*
	var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
    $('a').each(function(){
        var href = $(this).attr('href');

        if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
            $(this).click(function() {
                var extLink = href.replace(/^https?\:\/\//i, '');
                _gaq.push(['_trackEvent','External', 'Click', extLink]));
            });
        }
        else if (href.match(/^mailto\:/i)){
            $(this).click(function() {
                var mailLink = href.replace(/^mailto\:/i, '');
                _gaq.push(['_trackEvent','Email', 'Click', mailLink]);
            });
        }
        else if (href.match(filetypes)){
            $(this).click(function() {
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
                _gaq.push(['_trackEvent','Download', 'Click - ' + extension, filePath]);
            });
        }
    });
    $('a').click(function () {
		var thisel = $(this);
		try {
			_gaq.push(['_trackEvent', thisel.parents().andSelf().filter('[id!=""]:first').get(0).id, 'clicked', (thisel.text() || thisel.children('img:first').attr('alt'))]);
		} catch (err) {}
	});
	*/
});