var xlp_details_tab = 1;
var xlp_details_tab_over = 0;
var viewing_cu = false;

if (typeof console != 'undefined') 
	log = console.log; 
else 
	log = function () {};

$(document).ready(function() {
	// flow rate conversion
	$('.unit_radio').click(function(e) {
		
		switch($(this).val())
		{
			case 'metric':
				$('.imperial').slideUp();
				$('.metric').slideDown();
				$('input[value=imperial]').attr('checked', '');
				$('input[value=metric]').attr('checked', 'checked');
				
				$.cookie('brahma_units', 'metric', {
					expires : 365,
					path : '/'
				});
				
				break;
				
			case 'imperial':
				$('.imperial').slideDown();
				$('.metric').slideUp();
				$('input[value=imperial]').attr('checked', 'checked');
				$('input[value=metric]').attr('checked', '');
				
				$.cookie('brahma_units', 'imperial', {
					expires : 365,
					path : '/'
				});
				break;
		}
	});
	

	
	if($.cookie('brahma_units') == null) {
		$.cookie('brahma_units', 'metric', {
			expires : 365,
			path : '/'
		});
	}
	else
	{
		switch($.cookie('brahma_units'))
		{
			case 'metric':
				$('.imperial').hide();
				$('.metric').show();
				$('input[value=imperial]').attr('checked', '');
				$('input[value=metric]').attr('checked', 'checked');

				break;
				
			case 'imperial':
				$('.imperial').show();
				$('.metric').hide();
				$('input[value=imperial]').attr('checked', 'checked');
				$('input[value=metric]').attr('checked', '');

				break;
		}
	}
	
	// form submit button
/*	$('a.submit, a.submit_information_request').click(function(e) {
		e.preventDefault();
		$(this).parents().each(function(i,o) {
			if ($(this).is('form'))
				$(this).submit();
		});
		
	});*/
	
	
	// consult form validation
	$('.consult_form a.submit_information_request, .consult_form a.submit').click(function(e) {
		e.preventDefault();
		
		if (($('#first_name').val() == '') || ($('#last_name').val() == '') || ($('#company').val() == '') || ($('#phone').val() == '') || ($('#email').val() == ''))
		{
			alert('Please completely fill out the form before submitting.');	
		}
		else
		{
			
			$(this).parents().each(function(i,o) {
				if ($(this).is('form'))
				//	__submitACForm(document.forms[0], 0);
					__submitACForm($(this)[0], 0);
					//.submit();
			});
		}
	});
	
	// Suplus form validation
	$('#surplus_submit').click(function(e) {
		e.preventDefault();
		if(($('#f_full_name').val() == '') || ($('#f_company').val() == '') || ($('#f_phone_number').val() == '') || ($('#f_email_address').val() == ''))
		{
			alert('Please completely fill out the form before submitting.');
			
		}
		else
		{
			__submitACForm($('#surplus_form')[0], 0);
		}
	});
	
	
	// Suplus form validation
	$('#operator_training_form a.im_interested').click(function(e) {
		
		var validates_ok = true;
		
		var f = $('#operator_training_form input').each(function(i,o) {
			if ($(this).val() == '')
				validates_ok = false;
		});
		
		if (validates_ok)
		{
			__submitACForm($('#operator_training_form')[0], 0);
		//	$().submit();
		}
		else
		{
			e.preventDefault();
			alert('Please complete the form before submitting.');
		}
		
		
	});
	
	// Gets background position in all browser, not just non-ie ones.
	jQuery.fn.backgroundPosition = function() {
		if ($.browser.msie)
		{
			return $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');	
		}
		else
		{
			return $(this).css('background-position');
		}
	};
	
	// Sets background postiion in all browsers.
	jQuery.fn.setBackgroundPosition = function(x,y) {
		if ($.browser.msie)
		{
			$(this.css('background-position-x', x));
			$(this.css('background-position-y', y));
		}
		else
		{
			$(this.css('background-position', x + ' ' + y));
		}
	};
	
	
	var path = location.pathname.split('/');
	
	// set primary nav section
	var o = $('ul#primary-nav a[href=/'+path[1]+'/]');

	if (o.length > 0)
	{
		var pnpos = o.backgroundPosition().split(' ');
		pnpos[1] = '-34px';
		o.setBackgroundPosition(pnpos[0], pnpos[1]);
	}

	// set secondary nav section
	$('ul#secondary-nav a[href=/'+path[1]+'/'+path[2]+'/]').parent().addClass('active');

	// Mouse over animation for primarynav.
	$('ul#primary-nav a').mouseover(function() {
		var pos = $(this).backgroundPosition().split(' ');
		pos[1] = "-34px";
		$(this).setBackgroundPosition(pos[0], pos[1]);
	}).mouseout(function() {
		var pos = $(this).backgroundPosition().split(' ');
		var oldpos = pos;
		pos[1] = "0";
		$(this).setBackgroundPosition(pos[0], pos[1]);
		
		var o = $('ul#primary-nav a[href=/'+path[1]+'/]');
		if (o.length > 0)
		{
			var pnpos = o.backgroundPosition().split(' ');
			pnpos[1] = '-34px';
			o.setBackgroundPosition(pnpos[0], pnpos[1]);

			// set secondary nav section
			$('ul#secondary-nav a[href=/'+path[1]+'/'+path[2]+'/]').parent().addClass('active');
		}
	});
	
	
	// Details panels - tabs
	$('#details-panels .tabs a').mouseover(function() {
		// Remove over class for previously over tab.
		$(this).parent().parent().removeClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// set new tab over var to memorize where we are
		xlp_details_tab_over = $(this).attr('id').replace('t_', '');
		
		// Add over class to current tab
		$(this).parent().parent().addClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);

	}).mouseout(function() {
		// Remove over class for previously over tab.
		$(this).parent().parent().removeClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// set new tab over var to currently visible panel
		xlp_details_tab_over = xlp_details_tab;
		$(this).parent().parent().addClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);

	}).click(function(e) {
		// Prevent click!
		e.preventDefault();
		
		// Special code just for the containerized units
		var sec = $(this).attr('href').replace('#', '');
		
		switch (sec)
		{
			case 'containerized-units':
				if (!viewing_cu)
				{
					$('h1').addClass('oldh1').hide();
					$('h1').after('<h1 class="cu_title replace">Containerized Units</h1>');
					$('.features_panel').hide();
					viewing_cu = true;
				}
				break;
				
			default:
				if (viewing_cu)
				{
					viewing_cu = false;
					
					$('.cu_title').remove();
					$('.oldh1').show();
					$('.features_panel').show();
				}
				break;
		}
		
		// Remove over class for previously over tab.
		$(this).parent().parent().removeClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// Hide current panel
		$('#'+$('#t_'+xlp_details_tab).attr('href').replace('#', '')).hide();
		
		// Select the new tab section and add the class to the tabs.
		xlp_details_tab = xlp_details_tab_over = $(this).attr('id').replace('t_', '');
		$(this).parent().parent().addClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// Show new panel!
		$('#'+$(this).attr('href').replace('#', '')).fadeIn('fast');
	});
	
	
	$('#details-panels .tabs_vcf a').mouseover(function() {
		// Remove over class for previously over tab.
		$(this).parent().parent().removeClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// set new tab over var to memorize where we are
		xlp_details_tab_over = $(this).attr('id').replace('t_', '');
		
		// Add over class to current tab
		$(this).parent().parent().addClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);

	}).mouseout(function() {
		// Remove over class for previously over tab.
		$(this).parent().parent().removeClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// set new tab over var to currently visible panel
		xlp_details_tab_over = xlp_details_tab;
		$(this).parent().parent().addClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);

	});
	
	if($('.tabs_vcf'))
	{
		
		if ($('#details-panels').hasClass('vcf1')) xlp_details_tab = 1;
		if ($('#details-panels').hasClass('vcf2')) xlp_details_tab = 2;
		if ($('#details-panels').hasClass('vcf3')) xlp_details_tab = 3;
	}
	
	// Sub Panels
	$('.sub-tabs a').click(function(e) {
		e.preventDefault();
		$('.sub-panel').hide();
		$('#'+$(this).attr('href').replace('#', '')).fadeIn();
		$('.sub-selected').removeClass('sub-selected');
		$(this).addClass('sub-selected');
	});
	
	// hide sub panels, except the first one
	$('.sub-panel').hide();
	$('.panel .sub-panel:first').show();
	$('.sub-tabs a:first').addClass('sub-selected');
	
	// hide details panel, except the first one
	$('.panel').hide();
	$('.panel:first').show();
	
	/*// Table utility to mark first and last row of every table with a class.
	$('table').each(function(i,o) {
		$(o).find('tr:first').addClass('first').end().find('tr:last').addClass('last');
	});
	
	$('tr').each(function(i,o) {
		$(o).find('td:first').addClass('first').end().find('td:last').addClass('last');
		$(o).find('th:first').addClass('first').end().find('th:last').addClass('last');
	});
	
	$('td[rowspan]').css('border-bottom', '0').css('border-top', '1px solid #FFF');*/
	
	// Sound Ranger link that activates the SR Tab
	
	$('#srlink').click(function() {
		// Remove over class for previously over tab.
		$(this).parent().parent().removeClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// Hide current panel
		$('#'+$('#t_'+xlp_details_tab).attr('href').replace('#', '')).hide();
		
		// Select the new tab section and add the class to the tabs.
		xlp_details_tab = xlp_details_tab_over = 3;
		$('#details-panels .tabs').addClass('t'+xlp_details_tab+'o'+xlp_details_tab_over);
		
		// Show new panel!
		$('#sound-ranger').fadeIn('fast');
	});
	
	
	// tab 4 open - for containerized
	if (location.hash == "#t4")
	{
		$('a#t_4').click();
	}
	
});

calculateCompressor = function() { 
	alert("You need a compressor! \n\n (not implemented yet, thanks for trying!)");
}


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
