// shirt data and classes

var SetupCostPerColor = 15.00;
var ShirtTypeList = new Array();
var ShirtSizeList = new Array();
var ShirtQuantityList = new Array();
var PrintQuantityList = new Array();

function ShirtType(shirtName, costWhite, costColor)
{
	this.ShirtName = shirtName;
	this.CostWhite = costWhite;
	this.CostColor = costColor;
}

function ShirtSize(sizeName, costAdjust)
{
	this.SizeName = sizeName;
	this.CostAdjust = costAdjust;
}

function ShirtQuantity(quantity, costAdjust)
{
	this.Quantity = quantity;
	this.CostAdjust = costAdjust;
}

function PrintQuantity(quantity, costAdjustList)
{
	this.Quantity = quantity;
	this.CostAdjustList = costAdjustList;
}







// if error loading XML

function DataLoadError()
{
	//TODO: add XML error handling here
}







// calculate and display shirt cost
function CalculateShirtCost()
{
	//init vars
	var Quantity = 0;
	var ShirtQuantityRange = 0;
	var PrintQuantityRange = 0;
	var TypeChoice = 0;
	var SizeChoice = 0;
	var ShirtColorChoice = 0;
	var BackDesignColorsChoice = 0;
	var FrontDesignColorsChoice = 0;
		
	//get type
	$('#ShirtTypeRadioButtons li input').each(function(){
		if ($(this).attr('checked') == true)
		{
			TypeChoice = parseInt($(this).attr('value'));
		}
	});
	
	//get shirt color
	$('#ShirtColorRadioButtons li input').each(function(){
		if ($(this).attr('checked') == true)
		{
			ShirtColorChoice = parseInt($(this).attr('value'));
		}
	});
	
	//get size
/*
	$('#ShirtSizeRadioButtons li input').each(function(){
		if ($(this).attr('checked') == true)
		{
			SizeChoice = parseInt($(this).attr('value'));
		}
	});
*/		
	//get quantity
	Quantity = parseFloat($('#TextQuantity').attr('value'));
	
	//get shirt range
	for (var n = 0; n <= ShirtQuantityList.length - 1; n++)
	{
		if (Quantity >= ShirtQuantityList[n].Quantity)
		{
			ShirtQuantityRange = n;
		}
	}
	
	//get print range
	for (var n = 0; n <= PrintQuantityList.length - 1; n++)
	{
		if (Quantity >= PrintQuantityList[n].Quantity)
		{
			PrintQuantityRange = n;
		}
	}
	
	//get number of colors in front design
	$('#FrontDesignColorsRadioButtons li input').each(function(){
		if ($(this).attr('checked') == true)
		{
			FrontDesignColorsChoice = parseInt($(this).attr('value'));
		}
	});
	
	//get number of colors in back design
	$('#BackDesignColorsRadioButtons li input').each(function(){
		if ($(this).attr('checked') == true)
		{
			BackDesignColorsChoice = parseInt($(this).attr('value'));
		}
	});
	
	
	
	//use choices to calculate values
	
	if (ShirtColorChoice == 0){
		var CostType = ShirtTypeList[TypeChoice].CostWhite * Quantity;
		//var CalcLog = "Type: " + ShirtTypeList[TypeChoice].ShirtName + " at " + ShirtTypeList[TypeChoice].CostColor + " x " +  Quantity + " = " + CostType;
	} else {
		var CostType = ShirtTypeList[TypeChoice].CostColor * Quantity;
		//var CalcLog = "Type: " + ShirtTypeList[TypeChoice].ShirtName + " at " + ShirtTypeList[TypeChoice].CostColor + " x " +  Quantity + " = " + CostType;
	}
	
	//var CostSize = ShirtSizeList[SizeChoice].CostAdjust * Quantity;
	//CalcLog += "<br/>Size: " +  ShirtSizeList[SizeChoice].SizeName + " at " + ShirtSizeList[SizeChoice].CostAdjust + " x " + Quantity + " = " + CostSize;
	
	var CostQuantity = ShirtQuantityList[ShirtQuantityRange].CostAdjust * Quantity;
	//CalcLog += "<br/> Shirt Quantity: (" +  ShirtQuantityList[ShirtQuantityRange].Quantity + " range) at " + ShirtQuantityList[ShirtQuantityRange].CostAdjust + " x " + Quantity + " = " + CostQuantity;
	
	var CostPrintFront = PrintQuantityList[PrintQuantityRange].CostAdjustList[FrontDesignColorsChoice] * Quantity;
	//CalcLog += "<br/>Print Quantity: (" +  PrintQuantityList[PrintQuantityRange].Quantity + " range) for " + (FrontDesignColorsChoice) + " colors at " + PrintQuantityList[PrintQuantityRange].CostAdjustList[FrontDesignColorsChoice] + " x " + Quantity + " = " + CostPrintFront;
	
	var CostPrintBack = PrintQuantityList[PrintQuantityRange].CostAdjustList[BackDesignColorsChoice] * Quantity;
	//CalcLog += "<br/>Print Quantity: (" +  PrintQuantityList[PrintQuantityRange].Quantity + " range) for " + (BackDesignColorsChoice) + " colors at " + PrintQuantityList[PrintQuantityRange].CostAdjustList[BackDesignColorsChoice] + " x " + Quantity + " = " + CostPrintBack;
	
	var CostSetup = SetupCostPerColor * (FrontDesignColorsChoice + BackDesignColorsChoice);
	//CalcLog += "<br/>Color Setup: " + (BackDesignColorsChoice) + " colors x " + SetupCostPerColor + " = " + CostSetup;
	
	var PriceTotal = CostType + /*CostSize +*/ CostQuantity + CostPrintFront + CostPrintBack + CostSetup;
	//CalcLog += "<br/>Total : $" + PriceTotal;

	var PricePerShirt = PriceTotal / Quantity;
	
	
	// display price
	var TextField = $('p#CostPerShirt strong span');
	if (isNaN(PricePerShirt) || PricePerShirt == Infinity)
	{
		TextField.fadeOut(200);
	}
	else
	{
		var PriceText = PricePerShirt.toFixed(2);
		if (PriceText != TextField.text())
		{
			TextField.fadeOut(200, function(){
				$(this).text(PricePerShirt.toFixed(2));
				$(this).fadeIn(200);
			});
			//alert(CalcLog);
		}
	}
	
	
	
	// send values to hidden form fields
	// add 1 to account for EE's count variable
	$('input#TypeField').attr('value', TypeChoice + 1);
	$('input#ShirtColorField').attr('value', ShirtColorChoice);
//	$('input#SizeField').attr('value', SizeChoice + 1);
	$('input#DesignFrontField').attr('value', FrontDesignColorsChoice);
	$('input#DesignBackField').attr('value', BackDesignColorsChoice);
	$('input#QuantityField').attr('value', Quantity);
}








// after XML is parsed, setup calculator
var LastValidText = "";

function RenderCalculator()
{	
	//shirt type radio buttons
	for (var n = 0; n <= ShirtTypeList.length - 1; n++)
	{
		var sn = ShirtTypeList[n].ShirtName;
		var OptionHTML = '<li><input type="radio" id="OptionType' + n + '" value="' + n + '" name="ShirtTypeOptions"/><label for="OptionType' + n + '">' + sn + '</label></li>';
		$('ul#ShirtTypeRadioButtons').append(OptionHTML);
	}
	
	//shirt size radio buttons
/*
	for (var n = 0; n <= ShirtSizeList.length - 1; n++)
	{
		var sn = ShirtSizeList[n].SizeName;
		var OptionHTML = '<li><input type="radio" id="OptionSize' + n + '" value="' + n + '" name="ShirtSizeOptions"/><label for="OptionSize' + n + '">' + sn + '</label></li>';
		$('ul#ShirtSizeRadioButtons').append(OptionHTML);
	}
*/	
	//design colors radio buttons
	for (var n = 0; n <= 8; n++)
	{
		var OptionHTML = '<li><input type="radio" id="OptionBackDesignColors' + n + '" value="' + n + '" name="BackDesignColorsOptions"/><label for="OptionBackDesignColors' + n + '">' + n + '</label></li>';
		$('ul#BackDesignColorsRadioButtons').append(OptionHTML);
		
		var OptionHTML = '<li><input type="radio" id="OptionFrontDesignColors' + n + '" value="' + n + '" name="FrontDesignColorsOptions"/><label for="OptionFrontDesignColors' + n + '">' + n + '</label></li>';
		$('ul#FrontDesignColorsRadioButtons').append(OptionHTML);
	}
	
	// check every first radio button
	$('#Calculator ul li:first-child input').each(function(){
		$(this).attr('checked','true');											  
	});
	
	// click event on every radio button
	$('#Calculator ul li input[type=radio]').each(function(){
		$(this).bind("click", function(e){								   
			CalculateShirtCost();
		});
	});
	
	// keyup event on textbox, validates text
	LastValidText = $('#TextQuantity').attr('value');
	$('#TextQuantity').bind("keyup", function(e){								  
		if (isNaN($(this).attr('value')))
		{
			$(this).attr('value', LastValidText)
		} 
		else
		{
			LastValidText = $(this).attr('value');	
		}
		CalculateShirtCost();
	});
	
	//setup dropdowns (drowndown.js)
	InitDropdowns();
}









// XML parsing

$(document).ready(function(){
	$.ajax({
		 type: "GET",
		 url: "http://www.goteez.com/index.php/main/calculator-data/",
		 dataType: "xml",
		 error: function (XMLHttpRequest, textStatus, errorThrown) { DataLoadError(); },
		 success: function(xml) {
			 
			 //populate ShirtTypeList
			 $(xml).find('shirt-types type').each(function(){
				 ShirtTypeList.push(new ShirtType( $(this).attr('name') , parseFloat($(this).attr('cost-white')) , parseFloat($(this).attr('cost-color')) ));
			 });
			 
			 //populate ShirtSizeList
		/*	 
			 $(xml).find('shirt-sizes size').each(function(){
				 ShirtSizeList.push(new ShirtSize( $(this).attr('label') , parseFloat($(this).attr('cost-adjust')) ));
			 });
		*/	 
			 //populate ShirtQuantityList
			 $(xml).find('shirt-quantities quantity').each(function(){
				 ShirtQuantityList.push(new ShirtQuantity( parseFloat($(this).attr('amount')) , parseFloat($(this).attr('cost-adjust')) ));
			 });
			 
			 //populate PrintQuantityList
			 $(xml).find('print-quantities quantity').each(function(){
			 	 var CostAdjustList = new Array();
				 CostAdjustList.push(0);
				 $(this).find('color').each(function(){
					CostAdjustList.push(parseFloat($(this).text()));	
				 });
				 PrintQuantityList.push(new PrintQuantity( parseFloat($(this).attr('amount')) , CostAdjustList ));
			 });
			 
			 //get SetupCostPerColor
			 SetupCostPerColor = parseFloat($(xml).find('print-quantities').attr('cost-color'));
			 
			 RenderCalculator();
		 }
	 });	
});