﻿// configurator.js
//
// Paired categories can't have one blank
// Incompatibilities with product or category
// Graphics card duplicated to card #2 and #3
// Get descriptions
// Calculate totals
// Price differences for products
// 

Array.prototype.has=function(v){
	for (i=0;i<this.length;i++){
		if (this[i]==v) return i;
	}
	return -1;
}

function getUrlVars() {
	var map = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		map[key] = value;
	});
	return map;
}

var paired_categories = new Array();
paired_categories[0] = ["ProcessorStock", "AdvancedCooling"];
paired_categories[1] = ["ProcessorOverclocked", "CustomLiquidCooling"];

function addTotals() {
	// Add totals
	var subtotal = 0;
	var total = 0;
	$('select').each(function(index) {
		if ($('option:selected', this).attr('value') !== undefined) {
			var price = $('option:selected', this).attr('value').split(';')[0];
			subtotal = subtotal + parseFloat(price); 	
		}
	});
	total = subtotal + parseFloat($('#subTotal').val());
	subtotal = Math.round(subtotal).toFixed(2);
	total = Math.round(total).toFixed(2);
	$('#subTotalPrice').text('$' + subtotal);
	$('#totalPrice').text('$' + total);
}

function setDescriptions() {
	// Set description
	$('select').each(function(index) {
		var description = $('option:selected', this).attr('title');
		$(this).next('p').html(description);
	});
}

function priceDifference() {
	// Set product price difference
	var currentPrice = parseFloat($(this).attr('value').split(';')[0]);
	$('option', this).each(function(index) {
		var thisPrice = parseFloat($(this).attr('value').split(';')[0]);
		var priceDifference = thisPrice - currentPrice;
		priceDifference = Math.round(priceDifference).toFixed(2);
		
		var position = $(this).html().indexOf('$');
		if (position > -1) {
			var productString = $(this).html().substr(0, position);  
			$(this).html(productString);
		}

		if ( ! $(this).is(':selected')) {
			$(this).append(' $' + priceDifference);
		}
	});
}

function graphicsUpdate() {
	$('select[name=GraphicsCard2] option:not(:first), select[name=GraphicsCard3] option:not(:first)').remove();
	$('select[name=GraphicsCard] option:selected').clone().removeAttr('selected').appendTo($('select[name=GraphicsCard2], select[name=GraphicsCard3]'));
}

$(document).ready(function() {
	$('#priceBox').show();
	
	$('select').change(function() {
		if ($(this).attr("name") === "GraphicsCard") {
			graphicsUpdate();
		}
		addTotals();
		setDescriptions();

		// Set product price difference
		var currentPrice = parseFloat($(this).attr('value').split(';')[0]);
		$('option', this).each(function(index) {
			var thisPrice = parseFloat($(this).attr('value').split(';')[0]);
			var priceDifference = thisPrice - currentPrice;
			priceDifference = Math.round(priceDifference).toFixed(2);
			
			var position = $(this).html().indexOf('$');
			if (position > -1) {
				var productString = $(this).html().substr(0, position);  
				$(this).html(productString);
			}
				
			if ( ! $(this).is(':selected')) {
				$(this).append(' $' + priceDifference);
			}
		});

		// Check for partners
		var a = paired_categories[0].has($(this).attr("name"));
		var b = paired_categories[1].has($(this).attr("name"));
		
		if (a >= 0 || b >= 0) {
			if (a >= 0)  {
				var altCategory = paired_categories[1][a];
			} else {
				var altCategory = paired_categories[0][b];
			}
		
			if ($(this).attr("selectedIndex") == 0 && $('select[name='+altCategory+']').attr('selectedIndex') == 0) {
				alert("You must select at least one of these two options.");
				$(this).attr('selectedIndex', 1).trigger('change');
			} else if ($(this).attr("selectedIndex") > 0 && $('select[name='+altCategory+']').attr('selectedIndex') > 0) {
				$('select[name='+altCategory+']').attr('selectedIndex', 0).trigger('change');
			}
		}	


		// Check for incompatibilities
		$('select:disabled, option:disabled').removeAttr('disabled');
		
		$('select').each(function(index) {
			if ($('option:selected', this).attr('class') !== undefined) {
				var classList = $('option:selected', this).attr('class').split(/\s+/);
				for (i = 0; i < classList.length; i++) {
					if(classList[i].length > 0){
						if (classList[i].indexOf("incompatible-category-") >= 0) {
							var cid = classList[i].replace("incompatible-category-", "");
							$('select[title='+cid+']').attr('disabled', 'disabled').attr('selectedIndex', 0);
							addTotals();
							setDescriptions();
							priceDifference();
						} else if (classList[i].indexOf("incompatible-product-") >= 0) {
							var pid = classList[i].replace("incompatible-product-", "");;
							$('.product-'+pid).attr('disabled', 'disabled');
							if ($('.product-'+pid).is(':selected')) {
								var parentSelect = $('.product-'+pid).parent();
								var availableIndex = parentSelect.children(':not(:disabled):not(.product-0):first)').index();
								parentSelect.attr('selectedIndex', availableIndex);
								addTotals();
								setDescriptions();
								priceDifference();
							}
						}
					}
				}
			}
		});
	});

	$('select:first').trigger("change");
	$('select[name=GraphicsCard]').trigger("change");
	
	if (getUrlVars()["r"] == "xt") {
		// yo
	} else if (getUrlVars()["r"] == "sli") {
		$('select[name=GraphicsCard2]').val($('select[name=GraphicsCard2] option:eq(1)').attr('value'));
	}
	addTotals();
	setDescriptions();
	priceDifference();
});
