// CLASS Order //////////////////////////////////////////////////////////////////////////////////////////////
function Order() {
	// handle
	var order = this;
	// properties
	this.categoryButtons = [];
	this.itemButtons = [];
	this.optionButtons = [];
	this.category = '';
	this.item = '';
	this.option = '';
	this.quantity = 1;
	this.price = '0';
	this.anispeed = 500;
	// methods
	this.setCategory = function(category) {
		order.category = category;
		order.updateSKU();
	};
	this.setItem = function(item) {
		order.item = item;
		order.updateSKU();
	};
	this.setOption = function(option) {
		order.option = option;
		order.updateSKU();
	};
	this.setQuantity = function(quantity) {
		order.quantity = parseInt(quantity);
		order.updateTotal();
	};
	this.updateSKU = function() { $('#id_sku').attr('value', this.category + this.item + this.option) };
	this.updateTotal = function() {
		$('#order_total').html('$' + (order.quantity * order.price).toFixed(2));
	};
	this.setActive = function(el) { el.addClass('active'); };
	this.setInactive = function(el) { el.removeClass('active'); };
	this.chooseCategory = function(cb) {
		order.setCategory(cb.category);
		jQuery.each(order.categoryButtons, function(){
			if (this == cb)
				this.activate();
			else
				this.deactivate();
		});
		for (var i = 0; i < order.itemButtons.length ; ++i) {
			var ib = order.itemButtons[i]
			if (ib.category == order.category) {
				order.chooseItem(ib);
				break;
			}
		};
		if (!cb.noOption) {
			order.showOptions();
			order.chooseOption(order.optionButtons[0]);
		}
		else {
			order.hideOptions();
			order.setOption('');
		}
		order.updateTotal();
	};
	this.chooseItem = function(ib) {
		order.setItem(ib.item);
		order.price = ib.price;
		jQuery.each(order.itemButtons, function(){
			if (this == ib)
				this.activate();
			else
				this.deactivate();
		});
		order.updateTotal();
	};
	this.chooseOption = function(ob) {
		order.setOption(ob.option);
		jQuery.each(order.optionButtons, function(){
			if (this == ob)
				this.activate();
			else
				this.deactivate();
		});
	};
	this.showOptions = function() {
		//$('#order_edges').show();
		$('#order_edges').slideDown({ queue:true, duration:order.anispeed }).animate({ opacity:1 }, order.anispeed);
	}
	this.hideOptions = function() {
		//$('#order_edges').hide();
		$('#order_edges').animate({ opacity:0 }, { queue:true, duration:order.anispeed }).slideUp(order.anispeed);
	}
	// init
	this.init = function(){
		//first hide all items and options
		$('#order_lengths .selectContainer').hide();
		$('#order_edges').hide();
		// then continue
		var firstCategory;
		$("#order_categories li").each(function(i) {
			var newBtn = new CategoryButton(order, $(this));
			order.categoryButtons.push(newBtn);
			if (i == 0)
				firstCategory = newBtn;
		});
		$("#order_lengths li").each(function(i) {
			var newBtn = new ItemButton(order, $(this));
			order.itemButtons.push(newBtn);
		});
		$("#order_edges li").each(function(i) {
			var newBtn = new OptionButton(order, $(this));
			order.optionButtons.push(newBtn);
		});
		order.chooseCategory(firstCategory);
		$('#id_quantity').change(function(){
			order.setQuantity(this.value);
		});
	}();
};

// CLASS CategoryButton /////////////////////////////////////////////////////////////////////////////////////
function CategoryButton(order, el) {
	// handle
	var cb = this;
	// properties
	this.order = order;
	this.element = el;
	this.category = el.attr('id').substring(1);
	this.noOption = el.hasClass('no_edge_selection');
	// methods
	this.activate = function(){
		order.setActive(cb.element.children('a'));
		$('#l' + cb.category).fadeIn(order.anispeed);
		//$('#l' + cb.category).slideDown(order.anispeed);
		//$('#l' + cb.category).animate({ opacity:1 }, { queue:false, duration:order.anispeed }).slideDown(order.anispeed);
	};
	this.deactivate = function(){
		order.setInactive(cb.element.children('a'));
		$('#l' + cb.category).fadeOut(order.anispeed);
		//$('#l' + cb.category).slideUp(order.anispeed);
		//$('#l' + cb.category).animate({ opacity:0 }, { queue:false, duration:order.anispeed }).slideUp(order.anispeed);
	};
	// init
	this.init = function(){
		cb.element.click(function(){ order.chooseCategory(cb); return false; });
	}();
};

// CLASS ItemButton /////////////////////////////////////////////////////////////////////////////////////////
function ItemButton(order, el) {
	// handle
	var ib = this;
	// properties
	this.order = order;
	this.element = el;
	this.category = el.parent().parent().attr('id').substring(1);
	this.item = el.attr('id').substring(1);
	this.price = parseFloat(el.children('a').children('.price').html().substring(1));
	// methods
	this.activate = function(){ order.setActive(ib.element.children('a')); };
	this.deactivate = function(){ order.setInactive(ib.element.children('a')); };
	// init
	this.init = function(){
		ib.element.click(function(){ order.chooseItem(ib); return false; });
	}();
};

// CLASS ItemButton /////////////////////////////////////////////////////////////////////////////////////////
function OptionButton(order, el) {
	// handle
	var ob = this;
	// properties
	this.order = order;
	this.element = el;
	this.option = el.attr('id').substring(1);
	// methods
	this.activate = function(){ order.setActive(ob.element.children('a')); };
	this.deactivate = function(){ order.setInactive(ob.element.children('a')); };
	// init
	this.init = function(){
		ob.element.click(function(){ order.chooseOption(ob); return false; });
	}();
};


/////////////////////////////////////////////////////////////////////////////////////////////////////////////

var theOrder;

$(document).ready(function() {
	theOrder = new Order();
});