// numb only for input
function MakeButton(_obj) {
	_obj.addEvents({
					'mouseover': function(_e) {
									if (this.disabled)
										return;

								    this.setStyle('cursor','pointer');
									this.setStyle('background', 'url("/img/button_over.gif")');
								},
					'mouseout': function(_e) {
									if (this.disabled)
										return;

									this.setStyle('cursor','');
									this.setStyle('background', 'url("/img/button_bg.gif")');
								}
					});
}

function DisableButton(_obj) {
	_obj.disabled = true;
	_obj.setStyle("color", "#999");
	_obj.setStyle('background', 'url("/img/button_bg.gif")');
}

function EnableButton(_obj) {
	_obj.disabled = false;
	_obj.setStyle("color", "#444");
}

function NumberOnly(_obj) {
	_obj.addEvents({'focus': function(_e) {
									_e.stop();
									this.oldValue = this.value;
								},
						'blur':	function(_e) {
									_e.stop();
									if (this.oldValue == this.value)
										return;
										
									if (this.OnBlur)
										this.OnBlur()
									else
										alert("setup this.onBlur()");
								},
						
						'keydown': function(_e) {
									if (_e.key != 'backspace' && 
										_e.key != 'tab' && 
										_e.key != 'enter' && 
										_e.key != 'up' && _e.key != 'down' &&
										_e.key != 'left' && _e.key != 'right' &&
										(_e.key < '0' || _e.key > '9')) {
										_e.stop();
										return;
									}
	
									if (_e.key == 'up') {
										_e.stop();
										if (this.value == "")
											this.value = 0;
											
										if (parseInt(this.value) == 999)
											return;
	
										this.value = parseInt(this.value) + 1;
										this.select();
									}
									else if (_e.key == 'down') {
										_e.stop();
										if (this.value == "")
											this.value = 0;
	
										if (parseInt(this.value) == 0)
											return;
											
										this.value = parseInt(this.value) - 1;
										this.select();
									}
									else if (_e.key == 'enter') {
										this.blur();
									}
									
								}
					});
}

// Tip class start
var Tip = new Class({
	initialize: function(_obj) {
		this.obj	= _obj;
		this.target	= null;

		_obj.set('class', 'tip');
	},
	
	Show:	function(_target) {
		if (!_target && !this.target) {
			alert("need to know where to show tip!");
			return;
		}

		if (_target)
			this.target = _target

		this.obj.setStyle("z-index", 100);
		this.obj.setStyle("display", "block");
		pos = this.target.getPosition();
		pos.y += this.target.getSize().y + 2;
		pos.x -= (this.obj.getSize().x - this.target.getSize().x)/2;
		this.obj.setPosition(pos);
		
		if (this.fx)
			this.fx.cancel();

		this.fx = Fade(this.obj, .9);
	},
	
	Hide:	function() {
		if (this.fx)
			this.fx.cancel();
		this.fx = Fade(this.obj, 0);
		this.fx.obj = this.obj
		this.fx.addEvent('complete', function() {
							this.obj.setStyle("z-index", -100);		
						});
	},
	
	Follow:	function(_target) {
		this.target = _target;
		_target.tip = this;
		_target.addEvents({
							'click'		: function(_e) {	_e.stop();	},
							'mouseover'	: function(_e) {	this.tip.Show();	},
							'mouseout'	: function(_e) {	this.tip.Hide();	}
						});
	}
	

});
// Tip class end

// submenu class start
var Submenu = new Class({
	initialize: function(_prefix, _items, _targetId) {	
		this.targetId	= _targetId;
		this.prefix		= _prefix;
		this.items		= _items;
		this.numItems	= _items.length;
		this.curr		= null;

		// define submenu buttons
		for (i=0; i<this.numItems; i++) {
			var item;
			item = $(this.prefix + i);
			item.sm_parent = this;
			item.sm_idx = i;
			item.addEvents({'click':
								function(_e){
									if (this.sm_parent.curr == this)
										return;
									_e.stop();
									
									if (this.fx)
										this.fx.cancel();
									this.sm_parent.Select(this.sm_idx);
								},
								
								'mouseover':
								function(_e){
									if (this.sm_parent.curr == this)
										return;
			
									if (this.fx)
										this.fx.cancel();
									this.fx = new Fx.Tween(this, {duration: 'short', property:'background-color'}).start('#fff', '#e3dccd');
								},
								
								'mouseout':
								function(_e){
									if (this.sm_parent.curr == this)
										return;
			
									if (this.fx)
										this.fx.cancel();
									this.fx = new Fx.Tween(this, {duration: '100', property:'background-color'}).start('#e3dccd', '#fff');
								}
							});
		}

	},
	
	Select: function(_idx) {
		if (this.curr)
			this.curr.set('class', 'sm');
		
		var item;
		item = $(this.prefix + _idx);
		item.setStyle('background-color', '#fff');
		item.set('class', 'sm_selected');
		this.curr = item;
		
		this.OnSelect(_idx);
	},
	
	OnSelect: function(_idx) {
		// refreh content
		req = new Request.HTML({
						'url':	this.items[_idx],
						'evalScripts' : true,
						'onRequest': function(){
							ele = $(this.targetId);
							ele.setStyle('opacity', 0);
						},
		
						'update':this.targetId,
						'onSuccess': function(_tree, _eles, _html, _script) {
							ele = $(this.targetId);
							Fade(ele, 1);
						}
		
			});
		req.targetId = this.targetId;
		req.send();
	}
});
// submenu class end

function Fade(_ele, _value) {
	fx = new Fx.Morph(_ele, {duration: 'short'});
	fx.start({'opacity': _value});
	return fx;
}

function UpdateCartSummary(_numItems, _total) {
	$('cart_summary').set("html", _numItems + ", nt$" + _total);
	Fade($('cart_summary'), [0,1]);
}
