window.onload = function(){

	if ($('gsearch-text')) {
		var searchbox = $('gsearch-text');
		searchbox.value = 'Search';
		searchbox.onfocus = function(){
			if (this._cleared) return
			this.clear();
			this._cleared = true;
		}
		searchbox.onblur = function(){
			if (this.getValue() == '') {
				this.value = 'Search';
				this._cleared = false;
			}
		}
	}
	if ($('lead-link')) {
		$$('#lead-link>a')[0].observe('click', function(){
			$('lead-form').setStyle({top:'-1em'});
		});
		$$('#lead-form>a')[0].observe('click', function(){
			$('lead-form').setStyle({top:'-100em'});
		});
	}
	themestore.init();
};

var themestore = {
	pricing: false,
	options: false,
	setvalue: 0,
	newvalue: 0,
	subtotal: 0,
	grandtotal: 0,
	optiontotals: [],
	init: function(){
		if ($$('dl.item-detail').length <= 0) {
			return;
		}
		var dl = $$('dl.item-detail')[0];
		this.pricing = pricing;
		this.options = options;
		this.setvalue = $F('quantity');
		$$('#item-options select').each(function(sel){
			alert($F(sel.up('td').down('input["type="hidden"]')));
/* 			alert(this.options[].editable); */
/*
			if (this.options[$F(sel.up('td').down('input["type="hidden"]'))].editable == 1) {
				alert();
			}
*/
			sel.disable();
		});
		new Form.Element.Observer($('quantity'),1,themestore.updateCost);
	},
	updateCost: function(inp) {
		newvalue = $F(inp);
		if (this.setvalue == newvalue) {
			return;
		} else {
			this.setvalue = newvalue;
		}
		if (newvalue.length < 1) {
			return false;
			inp.up('div').down('span.subtotal').update('$' + subtotal);
		} else if (isNaN(parseFloat(newvalue))) {
			alert('Please only enter numbers for quantity.');
			inp.focus();
			$$('#order-form input[type="submit"]')[0].disable();
			return false;
		}
		var blockindex = -1;
		var selects = $$('#order-form select');
		themestore.pricing.blocks.each(function(block,index){
			if (newvalue >= parseFloat(block.start) && newvalue <= parseFloat(block.end)) {
				blockindex = index;
			}
		});
		if (newvalue > 0 && blockindex == -1) {
			alert('Please add a quantity less than ' + themestore.pricing.blocks.last().end);
			inp.value = themestore.pricing.blocks.last().end;
			inp.focus();
			$$('#order-form input[type="submit"]')[0].disable();
		}
		if (blockindex >= 0) {
			// update subtotal
			this.subtotal = parseFloat(themestore.pricing.blocks[blockindex].cost).toFixed(2);
			var defaults = themestore.pricing.blocks[blockindex].defaults;
			selects.each(function(sel,selindex){
				var optionkey = $F(sel.up('td').down('input["type="hidden"]'));
				alert(optionkey);
				this.options.each(function(opt,optindex){
					if (sel.name == 'data[options][' + opt.id + ']') {
						defaults.each(function(d){
							if (d.option == opt.id && $F(sel) != d.value) {
								FORMUtil.select.setSelected(sel,d.value);
								new Effect.Highlight(sel.up('tr'),{duration: 1.5});
							}
						});
					}
				});
			});
			this.grandtotal = this.subtotal;
			inp.up('div').down('span.subtotal').update('$' + this.subtotal);
			inp.up('div',2).down('span.total').update('$' + this.grandtotal);
			$$('#order-form input[type="submit"]')[0].enable();
		} else {
			inp.up('div').down('span.subtotal').update('$0.00');
			selects.each(function(sel,selindex){
				FORMUtil.select.setSelected(sel,'');
				new Effect.Highlight(sel,{duration: 1.5});
			});
		}
	}
};
