//Flyout Product Navigation
$(document).ready(function() {
    var hide = false;
    $("#products").hover(function(){
        if (hide) clearTimeout(hide);
        $("#product_navigation_wrap").show();
    }, function() {
        hide = setTimeout(function() {$("#product_navigation_wrap").hide();}, 0);
    });
    $("#product_navigation_wrap").hover(function(){
        if (hide) clearTimeout(hide);
    }, function() {
        hide = setTimeout(function() {$("#product_navigation_wrap").hide();}, 0);
    });
});
//Image Preloader
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//Tabs
$(function () {
    var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();
    
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});
// Comma Plugin
$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}
//Build page
$(function() {	
	
	// Prevent enter key submission
	$("input").bind("keydown", function(event) {
	    if (event.which === 13) {
	        event.stopPropagation();
	        event.preventDefault();
	        $(this).closest('*').focus();  
	    }
	});

	// Define variables
	var total = 0;
	var plotter_price = 0;
	var plotter_width_size = 0;
	var plotter_width_price = 0;
	var plotter_width_total = 0;
	var table_length_total = 0;
	var blower_type_price = 0;
	var blower_qty_price = 0;
	var blower_type_total = 0;
	var add_blade_price = 0;
	var computer_price = 0;
	var parts_kit_price = 0;
	var workstation_price = 0;
	var digitizer_price = 0;
	var commission_price = 0;
	
	// Total Function
	function total_it_up() {
		total = parseInt(plotter_price) + 
		parseInt(plotter_width_total) + 
		parseInt(table_length_total) +
		parseInt(blower_type_total) +
		parseInt(add_blade_price) +
		parseInt(computer_price) +
		parseInt(parts_kit_price) +
		parseInt(workstation_price) +
		parseInt(digitizer_price) +
		parseInt(commission_price);
		$('#build_total_amount').text('$' + Number(total).toFixed(2)).digits();
	};
	
	// Hide plotter divs and width selector
	$('.hide').hide();
	
	// On plotter_model change
	$('#plotter_model').change(function() {
		
		// Keep unselected information hidden during change
		$('.hide').hide();
		
		// Define plotter model variable
		var plotter_model = $('option:selected', this).attr('title');
		
		// Show information with matched label & note
		$('.' + plotter_model).show();
		$('.note').show();
		
		// Set price back to 0 for change and assign value
		plotter_price = 0;
		plotter_price = $(this).val();
		
		// Set next two values back if already set
		plotter_width_total = 0;
		$('.width_price').text('$' + Number(plotter_width_total).toFixed(2)).digits();
		table_length_total = 0;
		$('#table_length_price').text('$' + Number(table_length_total).toFixed(2)).digits();
		
		// Display price
		$('.model_price').text('$' + Number(plotter_price).toFixed(2)).digits();
		
		total_it_up();
	});
	
	// On plotter_width change
	$('.plotter_width').change(function() {
		
		// Define selected option's value & title as variables
		plotter_width_price = $('option:selected', this).val();
		plotter_width_size = $('option:selected', this).attr('title');
		
		// Set table back to zero
		table_length_total = 0;
		$('#table_length_price').text('$' + Number(table_length_total).toFixed(2)).digits();
		$("#table_length option:first").attr('selected','selected');
		
		// Find difference between plotter width and plotter price
		plotter_width_total = 0;
		plotter_width_total = parseInt(plotter_width_price) - parseInt(plotter_price);
		
		// Display difference
		$('.width_price').text('$' + Number(plotter_width_total).toFixed(2)).digits();
		
		// Show Table Length Select
		$('#select_table_length').show();
		
		total_it_up();
	});
		// On stx_width change
		$('#stx_width').change(function() {
			
			
			// Define name value
			stx_num = $('option:selected', this).attr('name');
			
			// Hide all then show corresponding image
			$('.icon .hide').hide();
			if (stx_num == 1) { $('#stx_one').show(); };
			if (stx_num == 2) { $('#stx_two').show(); };
			if (stx_num == 3) { $('#stx_three').show(); };
			if (stx_num == 4) { $('#stx_four').show(); };	
		});
		// On ctx_width change
		$('#ctx_width').change(function() {
			
			
			// Define name value
			ctx_num = $('option:selected', this).attr('name');
			
			// Hide all then show corresponding image
			$('.icon .hide').hide();
			if (ctx_num == 1) { $('#ctx_one').show(); };
			if (ctx_num == 2) { $('#ctx_two').show(); };
			if (ctx_num == 3) { $('#ctx_three').show(); };
			if (ctx_num == 4) { $('#ctx_four').show(); };	
		});
		// On pt_width change
		$('#pt_width').change(function() {
			
			// Define name value
			pt_num = $('option:selected', this).attr('name');
			
			// Hide all then show corresponding image
			$('.icon .hide').hide();
			if (pt_num == 1) { $('#pt_one').show(); };
			if (pt_num == 2) { $('#pt_two').show(); };
			if (pt_num == 3) { $('#pt_three').show(); };
			if (pt_num == 4) { $('#pt_four').show(); };
			if (pt_num == 5) { $('#pt_five').show(); };
			if (pt_num == 6) { $('#pt_six').show(); };
			if (pt_num == 7) { $('#pt_seven').show(); };
			if (pt_num == 8) { $('#pt_eight').show(); };
		});
		
	// On table_length change
	$('#table_length').change(function() {
		
		// Define selected option's title as variable
		var section_amount = $('option:selected', this).val();
		
		// Show image		
		$('.table .hide').hide();
		if (section_amount == 1) { $('#tab_one').show(); };
		if (section_amount == 2) { $('#tab_two').show(); };
		if (section_amount == 3) { $('#tab_three').show(); };
		if (section_amount == 4) { $('#tab_four').show(); };
		if (section_amount == 5) { $('#tab_five').show(); };
		if (section_amount == 6) { $('#tab_six').show(); };
		if (section_amount == 7) { $('#tab_seven').show(); };
		if (section_amount == 8) { $('#tab_eight').show(); };
		if (section_amount == 9) { $('#tab_nine').show(); };
		if (section_amount == 10) { $('#tab_ten').show(); };
		
		// Price equation - sections
		table_length_total = 0;
		var x = (parseInt(plotter_width_size) / 12);
		var y = (parseInt(section_amount) * 4);
		var z = ((x * y) * 35);
		if (section_amount > 6) {
			z = ((y - 24) * 50) + z;
		};
		table_length_total = z;
		
		// Display total_length total
		$('#table_length_price').text('$' + Number(table_length_total).toFixed(2)).digits();
		
		total_it_up();
	});
	
	// Table Length Total Price equation - custom
	$('#custom_table_length').change(function() {
		var custom_table_length = $(this).val();
		z = (parseInt(custom_table_length) * 50);
		
		table_length_total = 0;
		table_length_total = z;
		
		// Display total_length total
		$('#table_length_price').text('$' + Number(table_length_total).toFixed(2)).digits();
		
		total_it_up();
	});
	
	// On blower_type change
	$('#blower_type').change(function() {
		
		// Change values back if already set
		$("#blower_qty option:first").attr('selected','selected');
	
		// Get blower price
		blower_type_price = 0;
		blower_type_price = $(this).val();
		
		$('#blower_type_price').text('$' + Number(0).toFixed(2)).digits();
		
		blower_type_total = 0;
		total_it_up();
	});
	// On blower_qty change
	$('#blower_qty').change(function() {
		var blower_qty = $(this).val();
		
		blower_type_total = 0;
		blower_type_total = (parseInt(blower_qty) * parseInt(blower_type_price));
		
		// Display blower_type total
		$('#blower_type_price').text('$' + Number(blower_type_total).toFixed(2)).digits();
		
		total_it_up();
	});
	// On additional_blade change
	$('#additional_blade').change(function() {
		add_blade_price = 0;
		add_blade_price = $(this).val();
		
		// Display additional_blade total
		$('#additional_blade_price').text('$' + Number(add_blade_price).toFixed(2)).digits();
		
		total_it_up();
	});
	// Computer checkbox
	$('#computer').click(function() {
		if ($(this).is(':checked')) {
			computer_price = $(this).val();
			
			// Display computer total
			$('#computer_price').text('$' + Number(computer_price).toFixed(2)).digits();
			
			total_it_up();
		} else {
			computer_price = 0;
			
			// Display computer total
			$('#computer_price').text('$' + Number(computer_price).toFixed(2)).digits();
			
			total_it_up();
		};
	});
	// Parts Kit click
	$('#parts_kit').click(function() {
		if ($(this).is(':checked')) {
			parts_kit_price = $(this).val();
			
			// Display computer total
			$('#parts_kit_price').text('$' + Number(parts_kit_price).toFixed(2)).digits();
			
			total_it_up();
		} else {
			parts_kit_price = 0;
			
			// Display computer total
			$('#parts_kit_price').text('$' + Number(parts_kit_price).toFixed(2)).digits();
			
			total_it_up();
		};
	});
	// Workstation select
	$('#workstation').change(function() {
		workstation_price = $(this).val();
		
		// Display workstation total
		$('#workstation_price').text('$' + Number(workstation_price).toFixed(2)).digits();
		
		total_it_up();
	});
	// Digitizer change
	$('#digitizer').change(function() {
		digitizer_price = $(this).val();
		
		// Display workstation total
		$('#digitizer_price').text('$' + Number(digitizer_price).toFixed(2)).digits();
		
		total_it_up();
	});
	// Commission checkbox
	$('#commission').click(function() {
		if ($(this).is(':checked')) {
			commission_price = $(this).val();
			
			// Display commission total
			$('#commission_price').text('$' + Number(commission_price).toFixed(2)).digits();
			
			total_it_up();
		} else {
			commission_price = 0;
			
			// Display commission total
			$('#commission_price').text('$' + Number(commission_price).toFixed(2)).digits();
			
			total_it_up();
		};
	});
});
